@@ -12,7 +12,7 @@ import { TextDocument, CompletionList, CompletionItemKind, CompletionItem, TextE
12
12
import { WorkspaceFolder } from 'vscode-languageserver' ;
13
13
import { ICompletionParticipant } from 'vscode-css-languageservice' ;
14
14
15
- import { startsWith } from './utils/strings' ;
15
+ import { startsWith , endsWith } from './utils/strings' ;
16
16
17
17
export function getPathCompletionParticipant (
18
18
document : TextDocument ,
@@ -21,32 +21,73 @@ export function getPathCompletionParticipant(
21
21
) : ICompletionParticipant {
22
22
return {
23
23
onCssURILiteralValue : ( { position, range, uriValue } ) => {
24
- const isValueQuoted = startsWith ( uriValue , `'` ) || startsWith ( uriValue , `"` ) ;
25
24
const fullValue = stripQuotes ( uriValue ) ;
26
- const valueBeforeCursor = isValueQuoted
27
- ? fullValue . slice ( 0 , position . character - ( range . start . character + 1 ) )
28
- : fullValue . slice ( 0 , position . character - range . start . character ) ;
29
-
30
- if ( fullValue === '.' || fullValue === '..' ) {
31
- result . isIncomplete = true ;
25
+ if ( ! shouldDoPathCompletion ( uriValue , workspaceFolders ) ) {
26
+ if ( fullValue === '.' || fullValue === '..' ) {
27
+ result . isIncomplete = true ;
28
+ }
32
29
return ;
33
30
}
34
31
35
- if ( ! workspaceFolders || workspaceFolders . length === 0 ) {
32
+ let suggestions = providePathSuggestions ( uriValue , position , range , document , workspaceFolders ) ;
33
+ result . items = [ ...suggestions , ...result . items ] ;
34
+ } ,
35
+ onCssImportPath : ( { position, range, pathValue } ) => {
36
+ const fullValue = stripQuotes ( pathValue ) ;
37
+ if ( ! shouldDoPathCompletion ( pathValue , workspaceFolders ) ) {
38
+ if ( fullValue === '.' || fullValue === '..' ) {
39
+ result . isIncomplete = true ;
40
+ }
36
41
return ;
37
42
}
38
- const workspaceRoot = resolveWorkspaceRoot ( document , workspaceFolders ) ;
39
- const paths = providePaths ( valueBeforeCursor , URI . parse ( document . uri ) . fsPath , workspaceRoot ) ;
40
43
41
- const fullValueRange = isValueQuoted ? shiftRange ( range , 1 , - 1 ) : range ;
42
- const replaceRange = pathToReplaceRange ( valueBeforeCursor , fullValue , fullValueRange ) ;
43
- const suggestions = paths . map ( p => pathToSuggestion ( p , replaceRange ) ) ;
44
+ let suggestions = providePathSuggestions ( pathValue , position , range , document , workspaceFolders ) ;
45
+
46
+ if ( document . languageId === 'scss' ) {
47
+ suggestions . forEach ( s => {
48
+ if ( startsWith ( s . label , '_' ) && endsWith ( s . label , '.scss' ) ) {
49
+ if ( s . textEdit ) {
50
+ s . textEdit . newText = s . label . slice ( 1 , - 5 ) ;
51
+ } else {
52
+ s . label = s . label . slice ( 1 , - 5 ) ;
53
+ }
54
+ }
55
+ } ) ;
56
+ }
44
57
result . items = [ ...suggestions , ...result . items ] ;
45
58
}
46
-
47
59
} ;
48
60
}
49
61
62
+ function providePathSuggestions ( pathValue : string , position : Position , range : Range , document : TextDocument , workspaceFolders : WorkspaceFolder [ ] ) {
63
+ const fullValue = stripQuotes ( pathValue ) ;
64
+ const isValueQuoted = startsWith ( pathValue , `'` ) || startsWith ( pathValue , `"` ) ;
65
+ const valueBeforeCursor = isValueQuoted
66
+ ? fullValue . slice ( 0 , position . character - ( range . start . character + 1 ) )
67
+ : fullValue . slice ( 0 , position . character - range . start . character ) ;
68
+ const workspaceRoot = resolveWorkspaceRoot ( document , workspaceFolders ) ;
69
+
70
+ const paths = providePaths ( valueBeforeCursor , URI . parse ( document . uri ) . fsPath , workspaceRoot ) ;
71
+ const fullValueRange = isValueQuoted ? shiftRange ( range , 1 , - 1 ) : range ;
72
+ const replaceRange = pathToReplaceRange ( valueBeforeCursor , fullValue , fullValueRange ) ;
73
+
74
+ const suggestions = paths . map ( p => pathToSuggestion ( p , replaceRange ) ) ;
75
+ return suggestions ;
76
+ }
77
+
78
+ function shouldDoPathCompletion ( pathValue : string , workspaceFolders : WorkspaceFolder [ ] ) : boolean {
79
+ const fullValue = stripQuotes ( pathValue ) ;
80
+ if ( fullValue === '.' || fullValue === '..' ) {
81
+ return false ;
82
+ }
83
+
84
+ if ( ! workspaceFolders || workspaceFolders . length === 0 ) {
85
+ return false ;
86
+ }
87
+
88
+ return true ;
89
+ }
90
+
50
91
function stripQuotes ( fullValue : string ) {
51
92
if ( startsWith ( fullValue , `'` ) || startsWith ( fullValue , `"` ) ) {
52
93
return fullValue . slice ( 1 , - 1 ) ;
0 commit comments