2828define ( function ( require , exports , module ) {
2929 "use strict" ;
3030
31+ var TokenUtils = require ( "utils/TokenUtils" ) ;
32+
3133 //constants
3234 var TAG_NAME = "tagName" ,
3335 ATTR_NAME = "attr.name" ,
3436 ATTR_VALUE = "attr.value" ;
3537
36- /**
37- * @private
38- * moves the current context backwards by one token
39- * @param {editor:{CodeMirror}, pos:{ch:{string}, line:{number}}, token:{object} } ctx
40- * @return {boolean } whether the context changed
41- */
42- function _movePrevToken ( ctx ) {
43- if ( ctx . pos . ch <= 0 || ctx . token . start <= 0 ) {
44- //move up a line
45- if ( ctx . pos . line <= 0 ) {
46- return false ; //at the top already
47- }
48- ctx . pos . line -- ;
49- ctx . pos . ch = ctx . editor . getLine ( ctx . pos . line ) . length ;
50- } else {
51- ctx . pos . ch = ctx . token . start ;
52- }
53- ctx . token = ctx . editor . getTokenAt ( ctx . pos ) ;
54- return true ;
55- }
56-
57- /**
58- * @private
59- * moves the current context forward by one token
60- * @param {editor:{CodeMirror}, pos:{ch:{string}, line:{number}}, token:{object} } ctx
61- * @return {boolean } whether the context changed
62- */
63- function _moveNextToken ( ctx ) {
64- var eol = ctx . editor . getLine ( ctx . pos . line ) . length ;
65- if ( ctx . pos . ch >= eol || ctx . token . end >= eol ) {
66- //move down a line
67- if ( ctx . pos . line >= ctx . editor . lineCount ( ) - 1 ) {
68- return false ; //at the bottom
69- }
70- ctx . pos . line ++ ;
71- ctx . pos . ch = 0 ;
72- } else {
73- ctx . pos . ch = ctx . token . end + 1 ;
74- }
75- ctx . token = ctx . editor . getTokenAt ( ctx . pos ) ;
76- return true ;
77- }
78-
79- /**
80- * @private
81- * moves the current context in the given direction, skipping any whitespace it hits
82- * @param {function } moveFxn the funciton to move the context
83- * @param {editor:{CodeMirror}, pos:{ch:{string}, line:{number}}, token:{object} } ctx
84- * @return {boolean } whether the context changed
85- */
86- function _moveSkippingWhitespace ( moveFxn , ctx ) {
87- if ( ! moveFxn ( ctx ) ) {
88- return false ;
89- }
90- while ( ! ctx . token . className && ctx . token . string . trim ( ) . length === 0 ) {
91- if ( ! moveFxn ( ctx ) ) {
92- return false ;
93- }
94- }
95- return true ;
96- }
97-
98- /**
99- * @private
100- * creates a context object
101- * @param {CodeMirror } editor
102- * @param {{ch:{string}, line:{number} } pos
103- * @return {editor:{CodeMirror}, pos:{ch:{string}, line:{number}}, token:{object} }
104- */
105- function _getInitialContext ( editor , pos ) {
106- return {
107- "editor" : editor ,
108- "pos" : pos ,
109- "token" : editor . getTokenAt ( pos )
110- } ;
111- }
112-
113- /**
114- * @private
115- * in the given context, get the character offset of pos from the start of the token
116- * @param {editor:{CodeMirror}, pos:{ch:{string}, line:{number}}, token:{object} } context
117- * @return {number }
118- */
119- function _offsetInToken ( ctx ) {
120- var offset = ctx . pos . ch - ctx . token . start ;
121- if ( offset < 0 ) {
122- console . log ( "CodeHintUtils: _offsetInToken - Invalid context: the pos what not in the current token!" ) ;
123- }
124- return offset ;
125- }
126-
12738 /**
12839 * @private
12940 * Sometimes as attr values are getting typed, if the quotes aren't balanced yet
@@ -136,7 +47,7 @@ define(function (require, exports, module) {
13647 var attrValue = ctx . token . string ,
13748 startChar = attrValue . charAt ( 0 ) ,
13849 endChar = attrValue . charAt ( attrValue . length - 1 ) ,
139- offset = _offsetInToken ( ctx ) ,
50+ offset = TokenUtils . offsetInToken ( ctx ) ,
14051 foundEqualSign = false ;
14152
14253 //If this is a fully quoted value, return the whole
@@ -251,12 +162,12 @@ define(function (require, exports, module) {
251162 }
252163
253164 //Move to the prev token, and check if it's "="
254- if ( ! _moveSkippingWhitespace ( _movePrevToken , ctx ) || ctx . token . string !== "=" ) {
165+ if ( ! TokenUtils . moveSkippingWhitespace ( TokenUtils . movePrevToken , ctx ) || ctx . token . string !== "=" ) {
255166 return createTagInfo ( ) ;
256167 }
257168
258169 //Move to the prev token, and check if it's an attribute
259- if ( ! _moveSkippingWhitespace ( _movePrevToken , ctx ) || ctx . token . className !== "attribute" ) {
170+ if ( ! TokenUtils . moveSkippingWhitespace ( TokenUtils . movePrevToken , ctx ) || ctx . token . className !== "attribute" ) {
260171 return createTagInfo ( ) ;
261172 }
262173
@@ -283,13 +194,13 @@ define(function (require, exports, module) {
283194
284195 var tagName = _extractTagName ( ctx ) ;
285196 var attrName = ctx . token . string ;
286- var offset = _offsetInToken ( ctx ) ;
197+ var offset = TokenUtils . offsetInToken ( ctx ) ;
287198
288- if ( ! _moveSkippingWhitespace ( _moveNextToken , ctx ) || ctx . token . string !== "=" ) {
199+ if ( ! TokenUtils . moveSkippingWhitespace ( TokenUtils . moveNextToken , ctx ) || ctx . token . string !== "=" ) {
289200 return createTagInfo ( ATTR_NAME , offset , tagName , attrName ) ;
290201 }
291202
292- if ( ! _moveSkippingWhitespace ( _moveNextToken , ctx ) ) {
203+ if ( ! TokenUtils . moveSkippingWhitespace ( TokenUtils . moveNextToken , ctx ) ) {
293204 return createTagInfo ( ATTR_NAME , offset , tagName , attrName ) ;
294205 }
295206 //this should be the attrvalue
@@ -320,9 +231,9 @@ define(function (require, exports, module) {
320231 // the pos the caller passed in so we use extend to make a safe copy of it.
321232 // This is what pass by value in c++ would do.
322233 var pos = $ . extend ( { } , constPos ) ,
323- ctx = _getInitialContext ( editor . _codeMirror , pos ) ,
234+ ctx = TokenUtils . getInitialContext ( editor . _codeMirror , pos ) ,
324235 tempCtx = null ,
325- offset = _offsetInToken ( ctx ) ,
236+ offset = TokenUtils . offsetInToken ( ctx ) ,
326237 tagInfo ,
327238 tokenType ;
328239
@@ -366,15 +277,15 @@ define(function (require, exports, module) {
366277 // use it to scan backwards if we don't find an equal sign here.
367278 // Comment out this block to fix issue #1510.
368279// if (testToken.string.length > 0 && testToken.string.charAt(0) !== ">") {
369- // tempCtx = _getInitialContext (editor._codeMirror, pos);
370- // if (_moveSkippingWhitespace(_moveNextToken , tempCtx) && tempCtx.token.string === "=") {
280+ // tempCtx = TokenUtils.getInitialContext (editor._codeMirror, pos);
281+ // if (TokenUtils.moveSkippingWhitespace(TokenUtils.moveNextToken , tempCtx) && tempCtx.token.string === "=") {
371282// // Return an empty tag info since we're between an atribute name and the equal sign.
372283// return createTagInfo();
373284// }
374285// }
375286
376287 // next, see what's before pos
377- if ( ! _movePrevToken ( ctx ) ) {
288+ if ( ! TokenUtils . movePrevToken ( ctx ) ) {
378289 return createTagInfo ( ) ;
379290 }
380291
@@ -438,7 +349,7 @@ define(function (require, exports, module) {
438349 if ( ctx . token . string === "=" ) {
439350 // We could be between the attr and the value
440351 // Step back and check
441- if ( ! _moveSkippingWhitespace ( _movePrevToken , ctx ) || ctx . token . className !== "attribute" ) {
352+ if ( ! TokenUtils . moveSkippingWhitespace ( TokenUtils . movePrevToken , ctx ) || ctx . token . className !== "attribute" ) {
442353 return createTagInfo ( ) ;
443354 }
444355
@@ -484,13 +395,13 @@ define(function (require, exports, module) {
484395 */
485396 function findStyleBlocks ( editor ) {
486397 // Start scanning from beginning of file
487- var ctx = _getInitialContext ( editor . _codeMirror , { line : 0 , ch : 0 } ) ;
398+ var ctx = TokenUtils . getInitialContext ( editor . _codeMirror , { line : 0 , ch : 0 } ) ;
488399
489400 var styleBlocks = [ ] ;
490401 var currentStyleBlock = null ;
491402 var inStyleBlock = false ;
492403
493- while ( _moveNextToken ( ctx ) ) {
404+ while ( TokenUtils . moveNextToken ( ctx ) ) {
494405 if ( inStyleBlock ) {
495406 // Check for end of this <style> block
496407 if ( ctx . token . state . mode !== "css" ) {
0 commit comments