@@ -6,12 +6,7 @@ type PluginOptions = {
66} ;
77
88export 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 ) ;
1510
1611 return filePaths . map ( ( filePath ) => {
1712 const name = getFileNameWithoutExt ( filePath ) ;
@@ -36,27 +31,7 @@ export function getCompletionEntryDetails(
3631 modulePath : string ,
3732 project : ts . server . Project ,
3833) : 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 ) ;
6035 return {
6136 name : name ,
6237 kind : ScriptElementKind . alias ,
@@ -66,6 +41,34 @@ export function getCompletionEntryDetails(
6641 } ;
6742}
6843
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+
6972function getFileNameWithoutExt ( filePath : string ) : string {
7073 const ext = path . extname ( filePath ) ;
7174 return path . basename ( filePath , ext ) ;
@@ -84,3 +87,32 @@ function transformModulePath(selfPath: string, filePath: string, project: ts.ser
8487 return './' + path . relative ( path . dirname ( selfPath ) , filePath ) ;
8588 }
8689}
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