-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhover_feature.dart
420 lines (366 loc) · 12.9 KB
/
hover_feature.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import 'package:lsp_server/lsp_server.dart' as lsp;
import 'package:sass_api/sass_api.dart' as sass;
import 'package:sass_language_services/sass_language_services.dart';
import 'package:sass_language_services/src/utils/sass_lsp_utils.dart';
import 'package:sass_language_services/src/utils/string_utils.dart';
import '../../css/css_data.dart';
import '../../sass/sass_data.dart';
import '../go_to_definition/go_to_definition_feature.dart';
import '../node_at_offset_visitor.dart';
class HoverFeature extends GoToDefinitionFeature {
final _cssData = CssData();
final _sassData = SassData();
HoverFeature({required super.ls});
bool _supportsMarkdown() =>
ls.clientCapabilities.textDocument?.hover?.contentFormat
?.any((f) => f == lsp.MarkupKind.Markdown) ==
true ||
ls.clientCapabilities.general?.markdown != null;
Future<lsp.Hover?> doHover(
TextDocument document, lsp.Position position) async {
var stylesheet = ls.parseStylesheet(document);
var offset = document.offsetAt(position);
var path = getNodePathAtOffset(stylesheet, offset);
lsp.Hover? hover;
for (var i = 0; i < path.length; i++) {
var node = path.elementAt(i);
if (node is sass.SimpleSelector) {
return _selectorHover(path, i);
} else if (node is sass.Declaration) {
hover = _declarationHover(node);
} else if (node is sass.VariableExpression) {
hover = await _variableHover(node, document, position);
} else if (node is sass.FunctionExpression) {
hover = await _functionHover(node, document, position);
} else if (node is sass.IncludeRule) {
hover = await _mixinHover(node, document, position);
}
}
return hover;
}
lsp.Hover _selectorHover(List<sass.AstNode> path, int index) {
var (selector, specificity) = _getSelectorHoverValue(path, index);
if (_supportsMarkdown()) {
var contents = _asMarkdown('''```scss
$selector
```
[Specificity](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity): ${readableSpecificity(specificity)}
''');
return lsp.Hover(contents: contents);
} else {
var contents = _asPlaintext('''
$selector
Specificity: ${readableSpecificity(specificity)}
''');
return lsp.Hover(contents: contents);
}
}
/// Go back up the path and calculate a full selector string and specificity.
(String, int) _getSelectorHoverValue(List<sass.AstNode> path, int index) {
var pre = "";
var selector = "";
var specificity = 0;
var pastImmediateStyleRule = false;
var lastWasParentSelector = false;
for (var i = index; i >= 0; i--) {
var node = path.elementAt(i);
if (node is sass.ComplexSelector) {
var sel = node.span.text;
var parentSelectorIndex = sel.indexOf('&');
if (parentSelectorIndex != -1) {
lastWasParentSelector = true;
pre = sel.substring(0, parentSelectorIndex);
var post = sel.substring(parentSelectorIndex + 1);
selector = "$post $selector";
specificity += node.specificity;
} else {
if (lastWasParentSelector) {
lastWasParentSelector = false;
selector = "$pre$sel$selector";
pre = "";
} else {
selector = "$sel $selector";
}
specificity += node.specificity;
}
} else if (node is sass.StyleRule) {
// Don't add the direct parent StyleRule,
// otherwise we'll end up with the same selector twice.
if (!pastImmediateStyleRule) {
pastImmediateStyleRule = true;
continue;
}
try {
if (node.selector.isPlain) {
var selectorList = sass.SelectorList.parse(node.selector.asPlain!);
// Just pick the first one in case of a list.
var ruleSelector = selectorList.components.first;
var sel = ruleSelector.toString();
if (lastWasParentSelector) {
lastWasParentSelector = false;
var parentSelectorIndex = sel.indexOf('&');
if (parentSelectorIndex != -1) {
lastWasParentSelector = true;
pre = "$pre ${sel.substring(0, parentSelectorIndex)}".trim();
var post = sel.substring(parentSelectorIndex + 1);
selector = "$post$selector";
} else {
selector = "$pre $sel$selector".trim();
pre = "";
}
// subtract one class worth that would otherwise be duplicated
specificity -= 1000;
} else {
var parentSelectorIndex = sel.indexOf('&');
if (parentSelectorIndex != -1) {
lastWasParentSelector = true;
pre = sel.substring(0, parentSelectorIndex);
var post = sel.substring(parentSelectorIndex + 1);
selector = "$post $selector";
} else {
selector = "$pre $sel $selector".trim();
}
}
specificity += ruleSelector.specificity;
}
} on sass.SassFormatException catch (_) {
// Do nothing.
}
}
}
return (selector.trim(), specificity);
}
lsp.Either2<lsp.MarkupContent, String> _asMarkdown(String content) {
return lsp.Either2.t1(
lsp.MarkupContent(
kind: lsp.MarkupKind.Markdown,
value: content,
),
);
}
lsp.Either2<lsp.MarkupContent, String> _asPlaintext(String content) {
return lsp.Either2.t1(
lsp.MarkupContent(
kind: lsp.MarkupKind.PlainText,
value: content,
),
);
}
lsp.Hover? _declarationHover(sass.Declaration node) {
var data = _cssData.getProperty(node.name.toString());
if (data == null) return null;
var description = data.description;
var syntax = data.syntax;
final re = RegExp(r'([A-Z]+)(\d+)?');
const browserNames = {
"E": "Edge",
"FF": "Firefox",
"S": "Safari",
"C": "Chrome",
"IE": "IE",
"O": "Opera",
};
if (_supportsMarkdown()) {
var browsers = data.browsers?.map<String>((b) {
var matches = re.firstMatch(b);
if (matches != null) {
var browser = matches.group(1);
var version = matches.group(2);
return "${browserNames[browser]} $version";
}
return b;
}).join(', ');
var references = data.references
?.map<String>((r) => '[${r.name}](${r.uri.toString()})')
.join('\n');
var contents = _asMarkdown('''
$description
Syntax: $syntax
$references
$browsers
'''
.trim());
return lsp.Hover(contents: contents);
} else {
var browsers = data.browsers?.map<String>((b) {
var matches = re.firstMatch(b);
if (matches != null) {
var browser = matches.group(1);
var version = matches.group(2);
return "${browserNames[browser]} $version";
}
return b;
}).join(', ');
var contents = _asPlaintext('''
$description
Syntax: $syntax
$browsers
''');
return lsp.Hover(contents: contents);
}
}
Future<lsp.Hover?> _variableHover(sass.VariableExpression node,
TextDocument document, lsp.Position position) async {
var name = node.nameSpan.text;
var range = toRange(node.nameSpan);
var definition = await internalGoToDefinition(document, range.start);
if (definition == null || definition.location == null) {
// If we don't have a location we are likely dealing with a built-in.
for (var module in _sassData.modules) {
for (var variable in module.variables) {
if ('\$${variable.name}' == name) {
if (_supportsMarkdown()) {
var contents = _asMarkdown('''
${variable.description}
[Sass reference](${module.reference}#${variable.name})
''');
return lsp.Hover(contents: contents, range: range);
} else {
var contents = _asPlaintext(variable.description);
return lsp.Hover(contents: contents, range: range);
}
}
}
}
return null;
}
var definitionDocument = ls.cache.getDocument(definition.location!.uri);
if (definitionDocument == null) {
return null;
}
var definitionStylesheet = ls.parseStylesheet(definitionDocument);
var path = getNodePathAtOffset(
definitionStylesheet,
definitionDocument.offsetAt(definition.location!.range.start),
);
String? docComment;
String? rawValue;
for (var i = 0; i < path.length; i++) {
var node = path.elementAt(i);
if (node is sass.VariableDeclaration) {
docComment = node.comment?.docComment;
rawValue = node.expression.toString();
break;
}
}
String? resolvedValue;
if (rawValue != null && rawValue.contains(r'$')) {
resolvedValue = await findVariableValue(
definitionDocument,
definition.location!.range.start,
);
}
if (_supportsMarkdown()) {
var contents = _asMarkdown('''
```${document.languageId}
$name: ${resolvedValue ?? rawValue}${document.languageId != 'sass' ? ';' : ''}
```${docComment != null ? '\n____\n${docComment.replaceAll('\n', '\n\n')}\n\n' : ''}
''');
return lsp.Hover(contents: contents, range: range);
} else {
var contents = _asPlaintext('''
$name: ${resolvedValue ?? rawValue}${document.languageId != 'sass' ? ';' : ''}${docComment != null ? '\n\n$docComment' : ''}
''');
return lsp.Hover(contents: contents, range: range);
}
}
Future<lsp.Hover?> _functionHover(sass.FunctionExpression node,
TextDocument document, lsp.Position position) async {
var name = node.nameSpan.text;
var range = toRange(node.nameSpan);
var definition = await internalGoToDefinition(document, range.start);
if (definition == null || definition.location == null) {
// If we don't have a location we may be dealing with a built-in.
for (var module in _sassData.modules) {
for (var function in module.functions) {
if (function.name == name) {
if (_supportsMarkdown()) {
var contents = _asMarkdown('''
${function.description}
[Sass reference](${module.reference}#${function.name})
''');
return lsp.Hover(contents: contents, range: range);
} else {
var contents = _asPlaintext(function.description);
return lsp.Hover(contents: contents, range: range);
}
}
}
}
return null;
}
var definitionDocument = ls.cache.getDocument(definition.location!.uri);
if (definitionDocument == null) {
return null;
}
var definitionStylesheet = ls.parseStylesheet(definitionDocument);
var path = getNodePathAtOffset(
definitionStylesheet,
definitionDocument.offsetAt(definition.location!.range.start),
);
String? docComment;
String arguments = '()';
for (var i = 0; i < path.length; i++) {
var node = path.elementAt(i);
if (node is sass.FunctionRule) {
docComment = node.comment?.docComment;
arguments = '(${node.arguments.toString()})';
break;
}
}
if (_supportsMarkdown()) {
var contents = _asMarkdown('''
```${document.languageId}
@function $name$arguments
```${docComment != null ? '\n____\n${docComment.replaceAll('\n', '\n\n')}\n\n' : ''}
''');
return lsp.Hover(contents: contents, range: range);
} else {
var contents = _asPlaintext('''
@function $name$arguments${docComment != null ? '\n\n$docComment' : ''}
''');
return lsp.Hover(contents: contents, range: range);
}
}
Future<lsp.Hover?> _mixinHover(sass.IncludeRule node, TextDocument document,
lsp.Position position) async {
var name = node.nameSpan.text;
var range = toRange(node.nameSpan);
var definition = await goToDefinition(document, range.start);
if (definition == null) {
return null;
}
var definitionDocument = ls.cache.getDocument(definition.uri);
if (definitionDocument == null) {
return null;
}
var definitionStylesheet = ls.parseStylesheet(definitionDocument);
var path = getNodePathAtOffset(
definitionStylesheet,
definitionDocument.offsetAt(definition.range.start),
);
String? docComment;
String arguments = '';
for (var i = 0; i < path.length; i++) {
var node = path.elementAt(i);
if (node is sass.MixinRule) {
docComment = node.comment?.docComment;
arguments = '(${node.arguments.toString()})';
break;
}
}
if (_supportsMarkdown()) {
var contents = _asMarkdown('''
```${document.languageId}
@mixin $name$arguments
```${docComment != null ? '\n____\n${docComment.replaceAll('\n', '\n\n')}\n\n' : ''}
''');
return lsp.Hover(contents: contents, range: range);
} else {
var contents = _asPlaintext('''
@mixin $name$arguments${docComment != null ? '\n\n$docComment' : ''}
''');
return lsp.Hover(contents: contents, range: range);
}
}
}