6
6
using System . Web . Helpers ;
7
7
using Microsoft . CSS . Core ;
8
8
using Microsoft . CSS . Editor ;
9
+ using Microsoft . Scss . Core ;
9
10
using Microsoft . VisualStudio . Utilities ;
10
11
11
12
namespace MadsKristensen . EditorExtensions
@@ -102,9 +103,10 @@ private IEnumerable<CssSourceMapNode> CorrectionsForScss(string cssFileContents)
102
103
103
104
ParseItem item = null ;
104
105
Selector selector = null ;
106
+ SimpleSelector simple = null ;
105
107
StyleSheet styleSheet = null , cssStyleSheet = null ;
106
108
int start = 0 , indexInCollection , targetDepth ;
107
- string fileContents = null ;
109
+ string fileContents = null , simpleText = "" ;
108
110
var result = new List < CssSourceMapNode > ( ) ;
109
111
var contentCollection = new Dictionary < string , string > ( ) ; // So we don't have to read file for each map item.
110
112
var parser = new CssParser ( ) ;
@@ -131,13 +133,16 @@ private IEnumerable<CssSourceMapNode> CorrectionsForScss(string cssFileContents)
131
133
132
134
item = cssStyleSheet . ItemAfterPosition ( start ) ;
133
135
selector = item . FindType < Selector > ( ) ;
136
+ simple = item . FindType < SimpleSelector > ( ) ;
134
137
135
- if ( selector == null )
138
+ if ( selector == null || simple == null )
136
139
continue ;
137
140
141
+ simpleText = simple . Text ;
142
+
138
143
indexInCollection = sortedForGenerated . FindIndex ( e => e . Equals ( node ) ) ; //sortedForGenerated.IndexOf(node);
139
144
140
- targetDepth = 1 ;
145
+ targetDepth = 0 ;
141
146
142
147
for ( int i = indexInCollection ;
143
148
i >= 0 && node . GeneratedLine == sortedForGenerated [ i ] . GeneratedLine ;
@@ -153,10 +158,35 @@ private IEnumerable<CssSourceMapNode> CorrectionsForScss(string cssFileContents)
153
158
item = item . Parent ;
154
159
}
155
160
156
- selector = item . FindType < RuleSet > ( ) . Selectors . First ( ) ;
161
+ // selector = item.FindType<RuleSet>().Selectors.First();
162
+
163
+ RuleSet rule ;
164
+ ScssRuleBlock scssRuleBlock = item as ScssRuleBlock ;
165
+
166
+ if ( scssRuleBlock == null )
167
+ rule = item as RuleSet ;
168
+ else
169
+ rule = scssRuleBlock . RuleSets . First ( ) ;
170
+
171
+ // Because even on the same TreeDepth, there may be mulitple ruleblocks
172
+ // and the selector names may include & or other symbols which are diff
173
+ // fromt he generated counterpart, here is the guess work
174
+ item = rule . Children . FirstOrDefault ( r => r is Selector && r . Text . Trim ( ) == simpleText ) as Selector ;
175
+
176
+ selector = item == null ? null : item as Selector ;
157
177
158
178
if ( selector == null )
159
- continue ;
179
+ {
180
+ // One more try; dive deep and sniff then skip.
181
+ var items = rule . Children . Where ( r => r is RuleBlock )
182
+ . SelectMany ( r => ( r as RuleBlock ) . Children
183
+ . Where ( s => s is RuleSet )
184
+ . Select ( s => ( s as RuleSet ) . Selectors . FirstOrDefault ( sel => sel . Text . Trim ( ) == simpleText ) ) ) ;
185
+ selector = items . Any ( ) ? items . First ( ) : null ;
186
+
187
+ if ( selector == null )
188
+ continue ;
189
+ }
160
190
161
191
node . OriginalLine = fileContents . Substring ( 0 , selector . Start ) . Count ( s => s == '\n ' ) ;
162
192
node . OriginalColumn = fileContents . GetLineColumn ( selector . Start , node . OriginalLine ) ;
0 commit comments