@@ -12,7 +12,7 @@ import { TextDocument, CompletionList, CompletionItemKind, CompletionItem, TextE
1212import { WorkspaceFolder } from 'vscode-languageserver' ;
1313import { ICompletionParticipant } from 'vscode-css-languageservice' ;
1414
15- import { startsWith } from './utils/strings' ;
15+ import { startsWith , endsWith } from './utils/strings' ;
1616
1717export function getPathCompletionParticipant (
1818 document : TextDocument ,
@@ -21,32 +21,73 @@ export function getPathCompletionParticipant(
2121) : ICompletionParticipant {
2222 return {
2323 onCssURILiteralValue : ( { position, range, uriValue } ) => {
24- const isValueQuoted = startsWith ( uriValue , `'` ) || startsWith ( uriValue , `"` ) ;
2524 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+ }
3229 return ;
3330 }
3431
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+ }
3641 return ;
3742 }
38- const workspaceRoot = resolveWorkspaceRoot ( document , workspaceFolders ) ;
39- const paths = providePaths ( valueBeforeCursor , URI . parse ( document . uri ) . fsPath , workspaceRoot ) ;
4043
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+ }
4457 result . items = [ ...suggestions , ...result . items ] ;
4558 }
46-
4759 } ;
4860}
4961
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+
5091function stripQuotes ( fullValue : string ) {
5192 if ( startsWith ( fullValue , `'` ) || startsWith ( fullValue , `"` ) ) {
5293 return fullValue . slice ( 1 , - 1 ) ;
0 commit comments