Skip to content

Commit 646d275

Browse files
Merge pull request #828 from am11/css-selector-tooltip
Tooltip: Show full selector path. (#371)
2 parents 21f7492 + 4e790ea commit 646d275

File tree

2 files changed

+68
-7
lines changed

2 files changed

+68
-7
lines changed

EditorExtensions/CSS/QuickInfo/Declaration/DeclarationQuickInfo.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Text;
45
using System.Windows;
56
using System.Windows.Controls;
67
using System.Windows.Media;
78
using System.Windows.Media.Imaging;
8-
using MadsKristensen.EditorExtensions.BrowserLink.UnusedCss;
99
using MadsKristensen.EditorExtensions.Settings;
1010
using Microsoft.CSS.Core;
1111
using Microsoft.CSS.Editor;
@@ -60,7 +60,7 @@ public void AugmentQuickInfoSession(IQuickInfoSession session, IList<object> qiC
6060
//If the selector's full name would require computation (it's nested), compute it and add it to the output
6161
if (ruleSet != null && ruleSet.Parent.FindType<RuleSet>() != null)
6262
{
63-
qiContent.Add(LessDocument.GetLessSelectorName(ruleSet));
63+
qiContent.Add(GetSelectorText(session, point.Value, ruleSet));
6464
}
6565

6666
applicableToSpan = _buffer.CurrentSnapshot.CreateTrackingSpan(tipItem.Start, tipItem.Length, SpanTrackingMode.EdgeNegative);
@@ -85,6 +85,37 @@ public void AugmentQuickInfoSession(IQuickInfoSession session, IList<object> qiC
8585
qiContent.Add(CreateBrowserList(browsers));
8686
}
8787

88+
private string GetSelectorText(IQuickInfoSession session, SnapshotPoint point, RuleSet rules)
89+
{
90+
if (rules == null || rules.Selectors.Count == 0)
91+
return null;
92+
93+
var compilerResult = session.TextView.Properties.GetProperty("CssCompilerResult") as CssCompilerResult;
94+
95+
if (compilerResult == null)
96+
return null;
97+
98+
StringBuilder result = new StringBuilder();
99+
100+
foreach (Selector item in rules.Selectors)
101+
{
102+
var line = point.GetContainingLine().LineNumber - 1;
103+
var column = item.Start - point.Snapshot.GetLineFromPosition(item.Start).Start;
104+
105+
var node = compilerResult.SourceMap.MapNodes.FirstOrDefault(s =>
106+
s.SourceFilePath == _buffer.GetFileName() &&
107+
s.OriginalLine == line &&
108+
s.OriginalColumn == column);
109+
110+
if (node == null)
111+
return null;
112+
113+
result.Append(node.GeneratedSelector.Text).Append(" ");
114+
}
115+
116+
return result.ToString();
117+
}
118+
88119
private static Tuple<ParseItem, ICssCompletionListEntry> GetEntriesAndPoint(ParseItem item, SnapshotPoint point, ICssSchemaInstance schema)
89120
{
90121
// Declaration

EditorExtensions/Shared/Helpers/CssSourceMap.cs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Web.Helpers;
77
using Microsoft.CSS.Core;
88
using Microsoft.CSS.Editor;
9+
using Microsoft.Scss.Core;
910
using Microsoft.VisualStudio.Utilities;
1011

1112
namespace MadsKristensen.EditorExtensions
@@ -102,9 +103,10 @@ private IEnumerable<CssSourceMapNode> CorrectionsForScss(string cssFileContents)
102103

103104
ParseItem item = null;
104105
Selector selector = null;
106+
SimpleSelector simple = null;
105107
StyleSheet styleSheet = null, cssStyleSheet = null;
106108
int start = 0, indexInCollection, targetDepth;
107-
string fileContents = null;
109+
string fileContents = null, simpleText = "";
108110
var result = new List<CssSourceMapNode>();
109111
var contentCollection = new Dictionary<string, string>(); // So we don't have to read file for each map item.
110112
var parser = new CssParser();
@@ -131,13 +133,16 @@ private IEnumerable<CssSourceMapNode> CorrectionsForScss(string cssFileContents)
131133

132134
item = cssStyleSheet.ItemAfterPosition(start);
133135
selector = item.FindType<Selector>();
136+
simple = item.FindType<SimpleSelector>();
134137

135-
if (selector == null)
138+
if (selector == null || simple == null)
136139
continue;
137140

141+
simpleText = simple.Text;
142+
138143
indexInCollection = sortedForGenerated.FindIndex(e => e.Equals(node));//sortedForGenerated.IndexOf(node);
139144

140-
targetDepth = 1;
145+
targetDepth = 0;
141146

142147
for (int i = indexInCollection;
143148
i >= 0 && node.GeneratedLine == sortedForGenerated[i].GeneratedLine;
@@ -153,10 +158,35 @@ private IEnumerable<CssSourceMapNode> CorrectionsForScss(string cssFileContents)
153158
item = item.Parent;
154159
}
155160

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;
157177

158178
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+
}
160190

161191
node.OriginalLine = fileContents.Substring(0, selector.Start).Count(s => s == '\n');
162192
node.OriginalColumn = fileContents.GetLineColumn(selector.Start, node.OriginalLine);

0 commit comments

Comments
 (0)