feature #2558 added functions to get all attributes at the current or an abritrary position

pull/2565/head
Thomas Muehlichen 2015-03-25 12:04:10 +01:00
parent a67664055d
commit e8d85c1173
1 changed files with 51 additions and 1 deletions

View File

@ -98,7 +98,7 @@ AttributeManager.prototype = _(AttributeManager.prototype).extend({
/*
Gets all attributes on a line
@param lineNum: the number of the line to set the attribute for
@param lineNum: the number of the line to get the attribute for
*/
getAttributesOnLine: function(lineNum){
// get attributes of first char of line
@ -122,6 +122,56 @@ AttributeManager.prototype = _(AttributeManager.prototype).extend({
return [];
},
/*
Gets all attributes at a position containing line number and column
@param lineNumber starting with zero
@param column starting with zero
returns a list of attributes in the format
[ ["key","value"], ["key","value"], ... ]
*/
getAttributesOnPosition: function(lineNumber, column){
// get all attributes of the line
var aline = this.rep.alines[lineNumber];
if (!aline) {
return [];
}
// iterate through all operations of a line
var opIter = Changeset.opIterator(aline);
// we need to sum up how much characters each operations take until the wanted position
var currentPointer = 0;
var attributes = [];
var currentOperation;
while (opIter.hasNext()) {
currentOperation = opIter.next();
currentPointer = currentPointer + currentOperation.chars;
if (currentPointer > column) {
// we got the operation of the wanted position, now collect all its attributes
Changeset.eachAttribNumber(currentOperation.attribs, function (n) {
attributes.push([
this.rep.apool.getAttribKey(n),
this.rep.apool.getAttribValue(n)
]);
}.bind(this));
// skip the loop
return attributes;
}
}
return attributes;
},
/*
Gets all attributes at caret position
*/
getAttributesOnCaret: function(){
return this.getAttributesOnPosition(this.rep.selStart[0], this.rep.selStart[1]);
},
/*
Sets a specified attribute on a line
@param lineNum: the number of the line to set the attribute for