@@ -6,6 +6,7 @@ import { projects } from './lib/utils';
66import * as vue from '@vue/language-core' ;
77import { startNamedPipeServer } from './lib/server' ;
88import { _getComponentNames } from './lib/requests/componentInfos' ;
9+ import { capitalize } from '@vue/shared' ;
910
1011const windowsPathReg = / \\ / g;
1112const externalFiles = new WeakMap < ts . server . Project , string [ ] > ( ) ;
@@ -63,15 +64,70 @@ function createLanguageServicePlugin(): ts.server.PluginModuleFactory {
6364 startNamedPipeServer ( info . project . projectKind ) ;
6465
6566 const getCompletionsAtPosition = info . languageService . getCompletionsAtPosition ;
67+ const getCompletionEntryDetails = info . languageService . getCompletionEntryDetails ;
68+ const getCodeFixesAtPosition = info . languageService . getCodeFixesAtPosition ;
6669 const getEncodedSemanticClassifications = info . languageService . getEncodedSemanticClassifications ;
6770
6871 info . languageService . getCompletionsAtPosition = ( fileName , position , options ) => {
6972 const result = getCompletionsAtPosition ( fileName , position , options ) ;
7073 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+ }
72105 }
73106 return result ;
74107 } ;
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+ } ;
75131 info . languageService . getEncodedSemanticClassifications = ( fileName , span , format ) => {
76132 const result = getEncodedSemanticClassifications ( fileName , span , format ) ;
77133 const file = files . get ( fileName ) ;
0 commit comments