@@ -69,9 +69,9 @@ function set(prop, value, opts) {
6969 var cursorPosition = editor . getCursorBufferPosition ( ) ;
7070
7171 var css = editor . getText ( ) ;
72- postcss ( [ ] ) . process ( css ) . then ( function ( res ) {
72+ postcss ( [ ] ) . process ( css , { from : undefined } ) . then ( function ( res ) {
7373
74- var currentNode = 0 ;
74+ var currentNode = null ;
7575
7676 var propPos = [ 0 , 0 ] ;
7777 for ( var h = 0 ; h < order . length ; h ++ ) {
@@ -86,157 +86,164 @@ function set(prop, value, opts) {
8686 for ( var i = 0 ; i < res . root . nodes . length ; i ++ ) {
8787
8888 var node = res . root . nodes [ i ] ;
89- node . selectorHeight = node . selector . split ( "\n" ) . length ;
89+ if ( typeof node . selector === 'string' ) {
9090
91- if ( node . nodes [ 0 ] ) {
92- node . startWhiteLine = node . nodes [ 0 ] . source . start . line - ( node . source . start . line + node . selectorHeight ) ;
93- }
94- else {
95- node . startWhiteLine = 0 ;
96- }
97- if ( node . nodes [ node . nodes . length - 1 ] ) {
98- node . endWhiteLine = ( node . source . end . line - 1 ) - node . nodes [ node . nodes . length - 1 ] . source . end . line ;
99- }
100- else {
101- node . endWhiteLine = 0 ;
102- }
91+ node . selectorHeight = node . selector . split ( "\n" ) . length ;
92+ if ( node . nodes [ 0 ] ) {
93+ node . startWhiteLine = node . nodes [ 0 ] . source . start . line - ( node . source . start . line + node . selectorHeight ) ;
94+ }
95+ else {
96+ node . startWhiteLine = 0 ;
97+ }
98+ if ( node . nodes [ node . nodes . length - 1 ] ) {
99+ node . endWhiteLine = ( node . source . end . line - 1 ) - node . nodes [ node . nodes . length - 1 ] . source . end . line ;
100+ }
101+ else {
102+ node . endWhiteLine = 0 ;
103+ }
104+
105+ node . properties = { } ;
103106
104- node . properties = { } ;
107+ for ( var j = 0 ; j < node . nodes . length ; j ++ ) {
108+ if ( node . nodes [ j ] . type === 'decl' ) {
109+ node . properties [ node . nodes [ j ] . prop ] = j ;
110+ }
111+ }
105112
106- for ( var j = 0 ; j < node . nodes . length ; j ++ ) {
107- if ( node . nodes [ j ] . type === 'decl' ) {
108- node . properties [ node . nodes [ j ] . prop ] = j ;
113+ if ( cursorPosition . row >= node . source . start . line && cursorPosition . row <= node . source . end . line ) {
114+ currentNode = i ;
109115 }
110- }
111116
112- if ( cursorPosition . row >= node . source . start . line && cursorPosition . row <= node . source . end . line ) {
113- currentNode = i ;
114117 }
115118
116119 }
117120
118- currentNode = res . root . nodes [ currentNode ] ;
121+ if ( currentNode !== null ) {
119122
120- var dist = null ;
121- var dest = null ;
122- var toLine = 1 ;
123- if ( typeof currentNode . properties [ prop ] !== 'undefined' ) {
123+ currentNode = res . root . nodes [ currentNode ] ;
124124
125- var node = currentNode . nodes [ currentNode . properties [ prop ] ] ;
125+ var dist = null ;
126+ var dest = null ;
127+ var toLine = 1 ;
128+ if ( typeof currentNode . properties [ prop ] !== 'undefined' ) {
126129
127- var newValue = value ( node ) ;
130+ var node = currentNode . nodes [ currentNode . properties [ prop ] ] ;
128131
129- if ( node . value == newValue && ! options . focus ) {
130- timer [ prop ] . time = 0 ;
131- if ( options . cb ) { options . cb ( ) ; }
132- return ;
133- }
132+ var newValue = value ( node ) ;
134133
135- var diff = ( node . source . end . line - 1 ) - cursorPosition . row ;
136- if ( diff > 0 ) {
137- cursor . moveDown ( diff ) ;
138- }
139- else if ( diff < 0 ) {
140- cursor . moveUp ( Math . abs ( diff ) ) ;
141- }
142- cursor . moveToEndOfLine ( ) ;
143- editor . deleteToBeginningOfLine ( ) ;
134+ if ( node . value == newValue && ! options . focus ) {
135+ timer [ prop ] . time = 0 ;
136+ if ( options . cb ) { options . cb ( ) ; }
137+ return ;
138+ }
144139
145- var newCursorPosition = editor . getCursorBufferPosition ( ) ;
146- var tab = newCursorPosition . column === 0 ? "\t" : '' ;
147- editor . insertText ( tab + prop + ': ' + newValue + ';' ) ;
148- editor . moveLeft ( ) ;
140+ var diff = ( node . source . end . line - 1 ) - cursorPosition . row ;
141+ if ( diff > 0 ) {
142+ cursor . moveDown ( diff ) ;
143+ }
144+ else if ( diff < 0 ) {
145+ cursor . moveUp ( Math . abs ( diff ) ) ;
146+ }
147+ cursor . moveToEndOfLine ( ) ;
148+ editor . deleteToBeginningOfLine ( ) ;
149149
150- if ( options . cb ) { options . cb ( ) ; }
150+ var newCursorPosition = editor . getCursorBufferPosition ( ) ;
151+ var tab = newCursorPosition . column === 0 ? "\t" : '' ;
152+ editor . insertText ( tab + prop + ': ' + newValue + ';' ) ;
153+ editor . moveLeft ( ) ;
151154
152- }
153- else {
155+ if ( options . cb ) { options . cb ( ) ; }
154156
155- for ( var a = 0 ; a < order . length ; a ++ ) {
156- for ( var b = 0 ; b < order [ a ] . length ; b ++ ) {
157- var item = order [ a ] [ b ] ;
158- if ( typeof currentNode . properties [ item ] !== 'undefined' ) {
157+ }
158+ else {
159159
160- for ( var h = 0 ; h < order . length ; h ++ ) {
161- var index = order [ h ] . indexOf ( item ) ;
162- if ( index > - 1 ) {
163- var newDist = [ h , index ] ;
164- break ;
160+ for ( var a = 0 ; a < order . length ; a ++ ) {
161+ for ( var b = 0 ; b < order [ a ] . length ; b ++ ) {
162+ var item = order [ a ] [ b ] ;
163+ if ( typeof currentNode . properties [ item ] !== 'undefined' ) {
164+
165+ for ( var h = 0 ; h < order . length ; h ++ ) {
166+ var index = order [ h ] . indexOf ( item ) ;
167+ if ( index > - 1 ) {
168+ var newDist = [ h , index ] ;
169+ break ;
170+ }
165171 }
166- }
167172
168- if ( dist === null || Math . abs ( propDist - ( dist [ 0 ] * 100 + dist [ 1 ] ) ) > Math . abs ( propDist - ( newDist [ 0 ] * 100 + newDist [ 1 ] ) ) ) {
169- dist = [ newDist [ 0 ] , newDist [ 1 ] ] ;
170- dest = item ;
171- }
173+ if ( dist === null || Math . abs ( propDist - ( dist [ 0 ] * 100 + dist [ 1 ] ) ) > Math . abs ( propDist - ( newDist [ 0 ] * 100 + newDist [ 1 ] ) ) ) {
174+ dist = [ newDist [ 0 ] , newDist [ 1 ] ] ;
175+ dest = item ;
176+ }
172177
178+ }
173179 }
174180 }
175- }
176181
177- if ( dist ) {
178- if ( ( dist [ 0 ] * 100 + dist [ 1 ] ) < propDist ) {
179- toLine = 1 ;
182+ if ( dist ) {
183+ if ( ( dist [ 0 ] * 100 + dist [ 1 ] ) < propDist ) {
184+ toLine = 1 ;
185+ }
186+ else {
187+ toLine = - 1 ;
188+ }
189+ var insertLine = propPos [ 0 ] === dist [ 0 ] ? false : true ;
180190 }
181191 else {
182- toLine = - 1 ;
192+ toLine = 0 ;
183193 }
184- var insertLine = propPos [ 0 ] === dist [ 0 ] ? false : true ;
185- }
186- else {
187- toLine = 0 ;
188- }
189194
190- var ref ;
191- if ( dest ) {
192- var ref = currentNode . nodes [ currentNode . properties [ dest ] ] ;
193- }
194- else {
195- ref = {
196- source : {
197- start : {
198- line : currentNode . source . start . line - 1
199- } ,
200- end : {
201- line : currentNode . source . start . line + 1
195+ var ref ;
196+ if ( dest ) {
197+ var ref = currentNode . nodes [ currentNode . properties [ dest ] ] ;
198+ }
199+ else {
200+ ref = {
201+ source : {
202+ start : {
203+ line : currentNode . source . start . line - 1
204+ } ,
205+ end : {
206+ line : currentNode . source . start . line + 1
207+ }
202208 }
203- }
204- } ;
205- }
209+ } ;
210+ }
206211
207- var diff ;
208- if ( toLine < 0 ) {
209- diff = ( ref . source . start . line - 2 ) - cursorPosition . row ;
210- }
211- else {
212- diff = ( ref . source . end . line - 1 ) - cursorPosition . row ;
213- }
212+ var diff ;
213+ if ( toLine < 0 ) {
214+ diff = ( ref . source . start . line - 2 ) - cursorPosition . row ;
215+ }
216+ else {
217+ diff = ( ref . source . end . line - 1 ) - cursorPosition . row ;
218+ }
214219
215- if ( diff > 0 ) {
216- cursor . moveDown ( diff ) ;
217- }
218- else if ( diff < 0 ) {
219- cursor . moveUp ( Math . abs ( diff ) ) ;
220- }
221- cursor . moveToEndOfLine ( ) ;
222- if ( currentNode . nodes . length || emptyLine ) {
223- editor . insertNewlineBelow ( ) ;
224- }
225- if ( insertLine && toLine < 0 && emptyLine ) {
226- editor . insertNewlineBelow ( ) ;
227- cursor . moveUp ( ) ;
228- }
229- if ( insertLine && toLine > 0 && emptyLine ) {
230- editor . insertNewlineBelow ( ) ;
231- }
220+ if ( diff > 0 ) {
221+ cursor . moveDown ( diff ) ;
222+ }
223+ else if ( diff < 0 ) {
224+ cursor . moveUp ( Math . abs ( diff ) ) ;
225+ }
226+ cursor . moveToEndOfLine ( ) ;
227+ if ( currentNode . nodes . length || emptyLine ) {
228+ editor . insertNewlineBelow ( ) ;
229+ }
230+ if ( insertLine && toLine < 0 && emptyLine ) {
231+ editor . insertNewlineBelow ( ) ;
232+ cursor . moveUp ( ) ;
233+ }
234+ if ( insertLine && toLine > 0 && emptyLine ) {
235+ editor . insertNewlineBelow ( ) ;
236+ }
237+
238+ var newCursorPosition = editor . getCursorBufferPosition ( ) ;
239+ var tab = newCursorPosition . column === 0 ? "\t" : '' ;
232240
233- var newCursorPosition = editor . getCursorBufferPosition ( ) ;
234- var tab = newCursorPosition . column === 0 ? "\t" : '' ;
241+ editor . insertText ( tab + prop + ': ' + value ( null ) + ';' ) ;
242+ editor . moveLeft ( ) ;
235243
236- editor . insertText ( tab + prop + ': ' + value ( null ) + ';' ) ;
237- editor . moveLeft ( ) ;
244+ if ( options . cb ) { options . cb ( ) ; }
238245
239- if ( options . cb ) { options . cb ( ) ; }
246+ }
240247
241248 }
242249
0 commit comments