@@ -6,12 +6,7 @@ type PluginOptions = {
6
6
} ;
7
7
8
8
export function getCompletionEntries ( info : ts . server . PluginCreateInfo ) : ts . CompletionEntry [ ] {
9
- const options = info . config . options as PluginOptions ;
10
-
11
- const currentDir = info . project . getCurrentDirectory ( ) ;
12
- const filePaths = options . paths . flatMap ( ( dirPath ) => {
13
- return info . project . readDirectory ( path . resolve ( currentDir , dirPath ) , [ '.ts' , '.js' ] ) ;
14
- } ) ;
9
+ const filePaths = getPathsToImport ( info . config . options , info . project ) ;
15
10
16
11
return filePaths . map ( ( filePath ) => {
17
12
const name = getFileNameWithoutExt ( filePath ) ;
@@ -36,27 +31,7 @@ export function getCompletionEntryDetails(
36
31
modulePath : string ,
37
32
project : ts . server . Project ,
38
33
) : ts . CompletionEntryDetails {
39
- const importPath = transformModulePath ( selfPath , modulePath , project ) ;
40
- const text = `import * as ${ name } from "${ getFilePathWithoutExt ( importPath ) } ";\n` ;
41
- const action : CodeFixAction = {
42
- fixName : 'namespace-import' ,
43
- description : text ,
44
- changes : [
45
- {
46
- fileName : selfPath ,
47
- textChanges : [
48
- {
49
- span : {
50
- start : 0 ,
51
- length : 0 ,
52
- } ,
53
- newText : text ,
54
- } ,
55
- ] ,
56
- } ,
57
- ] ,
58
- commands : [ ] ,
59
- } ;
34
+ const action : CodeFixAction = getCodeFixActionFromPath ( name , selfPath , modulePath , project ) ;
60
35
return {
61
36
name : name ,
62
37
kind : ScriptElementKind . alias ,
@@ -66,6 +41,34 @@ export function getCompletionEntryDetails(
66
41
} ;
67
42
}
68
43
44
+ export function getCodeFixActionByName (
45
+ selfPath : string ,
46
+ start : number ,
47
+ end : number ,
48
+ info : ts . server . PluginCreateInfo ,
49
+ ) : CodeFixAction | null {
50
+ const name = info . languageService . getProgram ( ) ?. getSourceFile ( selfPath ) ?. text . slice ( start , end ) ;
51
+ if ( ! name ) {
52
+ return null ;
53
+ }
54
+
55
+ const filePaths = getPathsToImport ( info . config . options , info . project ) ;
56
+ const modulePath = filePaths . find ( ( filePath ) => getFileNameWithoutExt ( filePath ) === name ) ;
57
+ if ( modulePath ) {
58
+ return getCodeFixActionFromPath ( name , selfPath , modulePath , info . project ) ;
59
+ } else {
60
+ return null ;
61
+ }
62
+ }
63
+
64
+ function getPathsToImport ( options : PluginOptions , project : ts . server . Project ) : string [ ] {
65
+ const currentDir = project . getCurrentDirectory ( ) ;
66
+
67
+ return options . paths . flatMap ( ( dirPath ) => {
68
+ return project . readDirectory ( path . resolve ( currentDir , dirPath ) , [ '.ts' , '.js' ] ) ;
69
+ } ) ;
70
+ }
71
+
69
72
function getFileNameWithoutExt ( filePath : string ) : string {
70
73
const ext = path . extname ( filePath ) ;
71
74
return path . basename ( filePath , ext ) ;
@@ -84,3 +87,32 @@ function transformModulePath(selfPath: string, filePath: string, project: ts.ser
84
87
return './' + path . relative ( path . dirname ( selfPath ) , filePath ) ;
85
88
}
86
89
}
90
+
91
+ export function getCodeFixActionFromPath (
92
+ name : string ,
93
+ selfPath : string ,
94
+ modulePath : string ,
95
+ project : ts . server . Project ,
96
+ ) : CodeFixAction {
97
+ const importPath = transformModulePath ( selfPath , modulePath , project ) ;
98
+ const text = `import * as ${ name } from "${ getFilePathWithoutExt ( importPath ) } ";\n` ;
99
+ return {
100
+ fixName : 'namespace-import' ,
101
+ description : text ,
102
+ changes : [
103
+ {
104
+ fileName : selfPath ,
105
+ textChanges : [
106
+ {
107
+ span : {
108
+ start : 0 ,
109
+ length : 0 ,
110
+ } ,
111
+ newText : text ,
112
+ } ,
113
+ ] ,
114
+ } ,
115
+ ] ,
116
+ commands : [ ] ,
117
+ } ;
118
+ }
0 commit comments