1
+ import 'dart:math' ;
2
+
1
3
import 'package:lsp_server/lsp_server.dart' as lsp;
2
4
import 'package:sass_api/sass_api.dart' as sass;
3
5
import 'package:sass_language_services/sass_language_services.dart' ;
6
+ import 'package:sass_language_services/src/css/entry_status.dart' ;
4
7
8
+ import '../../utils/sass_lsp_utils.dart' ;
5
9
import '../language_feature.dart' ;
6
10
import '../node_at_offset_visitor.dart' ;
7
11
import './completion_context.dart' ;
8
12
import './completion_list.dart' ;
9
13
14
+ final triggerSuggestCommand = lsp.Command (
15
+ title: 'Suggest' ,
16
+ command: 'editor.action.triggerSuggest' ,
17
+ );
18
+
19
+ // Sort string prefixes
20
+ const enums = ' ' ;
21
+ const normal = 'e' ;
22
+ const vendorPrefix = 'o' ;
23
+ const term = 'p' ;
24
+ const variable = 'q' ;
25
+
10
26
class CompletionFeature extends LanguageFeature {
11
27
CompletionFeature ({required super .ls});
12
28
@@ -47,7 +63,7 @@ class CompletionFeature extends LanguageFeature {
47
63
lineBeforePosition: lineBeforePosition,
48
64
);
49
65
50
- for (var i = path.length; i >= 0 ; i-- ) {
66
+ for (var i = path.length - 1 ; i >= 0 ; i-- ) {
51
67
var node = path[i];
52
68
if (node is sass.Declaration ) {
53
69
_declarationCompletion (node, context, result);
@@ -130,12 +146,9 @@ class CompletionFeature extends LanguageFeature {
130
146
131
147
// From offset, go back until hitting a newline
132
148
var i = offset - 1 ;
133
- var codeUnit = text.codeUnitAt (i);
134
- const lineFeed = 10 ; // \n
135
- const carriageReturn = 13 ; // \r
136
- while (codeUnit != lineFeed && codeUnit != carriageReturn) {
149
+ var linebreaks = '\n\r ' .codeUnits;
150
+ while (i >= 0 && ! linebreaks.contains (text.codeUnitAt (i))) {
137
151
i-- ;
138
- codeUnit = text.codeUnitAt (i);
139
152
}
140
153
var lineBeforePosition = text.substring (i + 1 , offset);
141
154
@@ -150,8 +163,63 @@ class CompletionFeature extends LanguageFeature {
150
163
return (lineBeforePosition, currentWord);
151
164
}
152
165
153
- void _declarationCompletion (sass.Declaration node, CompletionContext context,
154
- CompletionList result) {}
166
+ void _declarationCompletion (
167
+ sass.AstNode node, CompletionContext context, CompletionList result) {
168
+ for (var property in cssData.properties) {
169
+ var range = context.defaultReplaceRange;
170
+ var insertText = property.name;
171
+ var triggerSuggest = false ;
172
+
173
+ if (node is sass.Declaration ) {
174
+ range = toRange (node.name.span);
175
+ if (! node.span.text.contains (':' )) {
176
+ insertText += ': ' ;
177
+ triggerSuggest = true ;
178
+ }
179
+ } else {
180
+ insertText += ': ' ;
181
+ triggerSuggest = true ;
182
+ }
183
+
184
+ var isDeprecated = property.status == EntryStatus .nonstandard ||
185
+ property.status == EntryStatus .obsolete;
186
+
187
+ if (property.restrictions == null ) {
188
+ triggerSuggest = false ;
189
+ }
190
+
191
+ lsp.Command ? command;
192
+ if (context.configuration.triggerPropertyValueCompletion &&
193
+ triggerSuggest) {
194
+ command = triggerSuggestCommand;
195
+ }
196
+
197
+ var relevance = 50 ;
198
+ if (property.relevance case var rel? ) {
199
+ relevance = min (max (rel, 0 ), 99 );
200
+ }
201
+
202
+ var suffix = (255 - relevance).toRadixString (16 );
203
+ var prefix = insertText.startsWith ('-' ) ? vendorPrefix : normal;
204
+ var sortText = '${prefix }_$suffix ' ;
205
+
206
+ var item = lsp.CompletionItem (
207
+ label: property.name,
208
+ documentation: supportsMarkdown ()
209
+ ? property.getMarkdownDescription ()
210
+ : property.getPlaintextDescription (),
211
+ tags: isDeprecated ? [lsp.CompletionItemTag .Deprecated ] : [],
212
+ textEdit: lsp.Either2 .t2 (
213
+ lsp.TextEdit (range: range, newText: insertText),
214
+ ),
215
+ insertTextFormat: lsp.InsertTextFormat .Snippet ,
216
+ sortText: sortText,
217
+ kind: lsp.CompletionItemKind .Property ,
218
+ command: command,
219
+ );
220
+ result.items.add (item);
221
+ }
222
+ }
155
223
156
224
void _interpolationCompletion (sass.Interpolation node,
157
225
CompletionContext context, CompletionList result) {}
@@ -166,7 +234,9 @@ class CompletionFeature extends LanguageFeature {
166
234
CompletionContext context, CompletionList result) {}
167
235
168
236
void _styleRuleCompletion (
169
- sass.StyleRule node, CompletionContext context, CompletionList result) {}
237
+ sass.StyleRule node, CompletionContext context, CompletionList result) {
238
+ _declarationCompletion (node, context, result);
239
+ }
170
240
171
241
void _variableDeclarationCompletion (sass.VariableDeclaration node,
172
242
CompletionContext context, CompletionList result) {}
0 commit comments