Skip to content

Commit aef8c98

Browse files
committed
refactor
1 parent 4e9f967 commit aef8c98

File tree

2 files changed

+51
-38
lines changed

2 files changed

+51
-38
lines changed

src/index.ts

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import TsServerLibrary, { CodeFixAction, ScriptElementKind } from 'typescript/lib/tsserverlibrary';
22
import * as path from 'path';
3-
import { getCompletionEntries } from './lib/import';
3+
import * as namespaceImportPlugin from './lib/import';
44

55
declare global {
66
namespace ts {
@@ -10,10 +10,6 @@ declare global {
1010
}
1111
}
1212

13-
function stripExt(filePath: string) {
14-
return filePath.slice(0, filePath.lastIndexOf('.'));
15-
}
16-
1713
function init({ typescript: ts }: { typescript: typeof TsServerLibrary }) {
1814
function create(info: ts.server.PluginCreateInfo) {
1915
const log = (...params: unknown[]) => {
@@ -31,7 +27,7 @@ function init({ typescript: ts }: { typescript: typeof TsServerLibrary }) {
3127
return original;
3228
}
3329

34-
original.entries = [...original.entries, ...getCompletionEntries(info)];
30+
original.entries = [...original.entries, ...namespaceImportPlugin.getCompletionEntries(info)];
3531

3632
return original;
3733
};
@@ -40,34 +36,7 @@ function init({ typescript: ts }: { typescript: typeof TsServerLibrary }) {
4036
info.languageService.getCompletionEntryDetails = (fileName, position, name, options, source, preferences, data) => {
4137
log('getCompletionEntryDetails', { fileName, position, name, options, source });
4238
if (data?.modulePath) {
43-
const importPath = path.relative(path.dirname(fileName), data.modulePath);
44-
const text = `import * as ${name} from "${stripExt(importPath)}";\n`;
45-
const action: CodeFixAction = {
46-
fixName: 'namespace-import',
47-
description: text,
48-
changes: [
49-
{
50-
fileName: fileName,
51-
textChanges: [
52-
{
53-
span: {
54-
start: 0,
55-
length: 0,
56-
},
57-
newText: text,
58-
},
59-
],
60-
},
61-
],
62-
commands: [],
63-
};
64-
return {
65-
name: name,
66-
kind: ScriptElementKind.alias,
67-
kindModifiers: '',
68-
displayParts: [],
69-
codeActions: [action],
70-
};
39+
return namespaceImportPlugin.getCompletionEntryDetails(name, fileName, data.modulePath);
7140
}
7241

7342
return getCompletionEntryDetails(fileName, position, name, options, source, preferences, data);

src/lib/import.ts

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import ts from 'typescript/lib/tsserverlibrary';
1+
import ts, { CodeFixAction, ScriptElementKind } from 'typescript/lib/tsserverlibrary';
22
import * as path from 'path';
33

44
export function getCompletionEntries(info: ts.server.PluginCreateInfo): ts.CompletionEntry[] {
55
const currentDir = info.project.getCurrentDirectory();
6-
const filePaths = info.project.readDirectory(path.resolve(currentDir, 'src/services'));
6+
const filePaths = info.project.readDirectory(path.resolve(currentDir, 'src/services'), ['.ts', '.js']);
77

88
return filePaths.map((filePath) => {
9-
const ext = path.extname(filePath);
10-
const name = path.basename(filePath, ext);
9+
const name = getFileNameWithoutExt(filePath);
1110
return {
1211
name: name,
1312
kind: ts.ScriptElementKind.alias,
@@ -22,3 +21,48 @@ export function getCompletionEntries(info: ts.server.PluginCreateInfo): ts.Compl
2221
};
2322
});
2423
}
24+
25+
export function getCompletionEntryDetails(
26+
name: string,
27+
selfPath: string,
28+
modulePath: string,
29+
): ts.CompletionEntryDetails {
30+
const importPath = path.relative(path.dirname(selfPath), modulePath);
31+
const text = `import * as ${name} from "${getFilePathWithoutExt(importPath)}";\n`;
32+
const action: CodeFixAction = {
33+
fixName: 'namespace-import',
34+
description: text,
35+
changes: [
36+
{
37+
fileName: selfPath,
38+
textChanges: [
39+
{
40+
span: {
41+
start: 0,
42+
length: 0,
43+
},
44+
newText: text,
45+
},
46+
],
47+
},
48+
],
49+
commands: [],
50+
};
51+
return {
52+
name: name,
53+
kind: ScriptElementKind.alias,
54+
kindModifiers: '',
55+
displayParts: [],
56+
codeActions: [action],
57+
};
58+
}
59+
60+
function getFileNameWithoutExt(filePath: string): string {
61+
const ext = path.extname(filePath);
62+
return path.basename(filePath, ext);
63+
}
64+
65+
function getFilePathWithoutExt(filePath: string): string {
66+
const ext = path.extname(filePath);
67+
return filePath.slice(0, filePath.length - ext.length);
68+
}

0 commit comments

Comments
 (0)