@@ -114,18 +114,35 @@ Syntax.convertTabsToSpaces = function (text, tabSize) {
114
114
return { text : text , offsets : offsets } ;
115
115
} ;
116
116
117
+ // This function converts from a compressed set of offsets of the form:
118
+ // [
119
+ // [offset, width, totalOffset],
120
+ // ...
121
+ // ]
122
+ // This means that at a $offset, a tab (single character) was expanded to $width
123
+ // single space characters.
124
+ // This function produces a lookup table of offsets, where a given character offset
125
+ // is mapped to how far the character has been offset.
117
126
Syntax . convertToLinearOffsets = function ( offsets , length ) {
118
127
var current = 0 , changes = [ ] ;
119
128
120
129
// Anything with offset after offset[current][0] but smaller than offset[current+1][0]
121
130
// has been shifted right by offset[current][2]
122
131
for ( var i = 0 ; i < length ; i ++ ) {
123
132
if ( offsets [ current ] && i > offsets [ current ] [ 0 ] ) {
124
- if ( offsets [ current + 1 ] && i <= offsets [ current + 1 ] [ 0 ] ) {
125
- changes . push ( offsets [ current ] [ 2 ] ) ;
133
+ // Is there a next offset?
134
+ if ( offsets [ current + 1 ] ) {
135
+ // Is the index less than the start of the next offset?
136
+ if ( i <= offsets [ current + 1 ] [ 0 ] ) {
137
+ changes . push ( offsets [ current ] [ 2 ] ) ;
138
+ } else {
139
+ // If so, move to the next offset.
140
+ current += 1 ;
141
+ i -= 1 ;
142
+ }
126
143
} else {
127
- current += 1 ;
128
- i -= 1 ;
144
+ // If there is no next offset we assume this one to the end.
145
+ changes . push ( offsets [ current ] [ 2 ] ) ;
129
146
}
130
147
} else {
131
148
changes . push ( changes [ changes . length - 1 ] || 0 ) ;
0 commit comments