@@ -6,6 +6,7 @@ import { projects } from './lib/utils';
6
6
import * as vue from '@vue/language-core' ;
7
7
import { startNamedPipeServer } from './lib/server' ;
8
8
import { _getComponentNames } from './lib/requests/componentInfos' ;
9
+ import { capitalize } from '@vue/shared' ;
9
10
10
11
const windowsPathReg = / \\ / g;
11
12
const externalFiles = new WeakMap < ts . server . Project , string [ ] > ( ) ;
@@ -63,15 +64,70 @@ function createLanguageServicePlugin(): ts.server.PluginModuleFactory {
63
64
startNamedPipeServer ( info . project . projectKind ) ;
64
65
65
66
const getCompletionsAtPosition = info . languageService . getCompletionsAtPosition ;
67
+ const getCompletionEntryDetails = info . languageService . getCompletionEntryDetails ;
68
+ const getCodeFixesAtPosition = info . languageService . getCodeFixesAtPosition ;
66
69
const getEncodedSemanticClassifications = info . languageService . getEncodedSemanticClassifications ;
67
70
68
71
info . languageService . getCompletionsAtPosition = ( fileName , position , options ) => {
69
72
const result = getCompletionsAtPosition ( fileName , position , options ) ;
70
73
if ( result ) {
71
- result . entries = result . entries . filter ( entry => entry . name . indexOf ( '__VLS_' ) === - 1 ) ;
74
+ // filter __VLS_
75
+ result . entries = result . entries . filter (
76
+ entry => entry . name . indexOf ( '__VLS_' ) === - 1
77
+ && ( ! entry . labelDetails ?. description || entry . labelDetails . description . indexOf ( '__VLS_' ) === - 1 )
78
+ ) ;
79
+ // modify label
80
+ for ( const item of result . entries ) {
81
+ if ( item . source ) {
82
+ const originalName = item . name ;
83
+ for ( const ext of vueOptions . extensions ) {
84
+ const suffix = capitalize ( ext . substring ( '.' . length ) ) ; // .vue -> Vue
85
+ if ( item . source . endsWith ( ext ) && item . name . endsWith ( suffix ) ) {
86
+ item . name = item . name . slice ( 0 , - suffix . length ) ;
87
+ if ( item . insertText ) {
88
+ // #2286
89
+ item . insertText = item . insertText . replace ( `${ suffix } $1` , '$1' ) ;
90
+ }
91
+ if ( item . data ) {
92
+ // @ts -expect-error
93
+ item . data . __isComponentAutoImport = {
94
+ ext,
95
+ suffix,
96
+ originalName,
97
+ newName : item . insertText ,
98
+ } ;
99
+ }
100
+ break ;
101
+ }
102
+ }
103
+ }
104
+ }
72
105
}
73
106
return result ;
74
107
} ;
108
+ info . languageService . getCompletionEntryDetails = ( ...args ) => {
109
+ const details = getCompletionEntryDetails ( ...args ) ;
110
+ // modify import statement
111
+ // @ts -expect-error
112
+ if ( args [ 6 ] ?. __isComponentAutoImport ) {
113
+ // @ts -expect-error
114
+ const { ext, suffix, originalName, newName } = args [ 6 ] ?. __isComponentAutoImport ;
115
+ for ( const codeAction of details ?. codeActions ?? [ ] ) {
116
+ for ( const change of codeAction . changes ) {
117
+ for ( const textChange of change . textChanges ) {
118
+ textChange . newText = textChange . newText . replace ( 'import ' + originalName + ' from ' , 'import ' + newName + ' from ' ) ;
119
+ }
120
+ }
121
+ }
122
+ }
123
+ return details ;
124
+ } ;
125
+ info . languageService . getCodeFixesAtPosition = ( ...args ) => {
126
+ let result = getCodeFixesAtPosition ( ...args ) ;
127
+ // filter __VLS_
128
+ result = result . filter ( entry => entry . description . indexOf ( '__VLS_' ) === - 1 ) ;
129
+ return result ;
130
+ } ;
75
131
info . languageService . getEncodedSemanticClassifications = ( fileName , span , format ) => {
76
132
const result = getEncodedSemanticClassifications ( fileName , span , format ) ;
77
133
const file = files . get ( fileName ) ;
0 commit comments