Skip to content

Commit 4e9f967

Browse files
committed
collect completion entries
1 parent 74c8ae7 commit 4e9f967

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

src/index.ts

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

45
declare global {
56
namespace ts {
@@ -14,7 +15,7 @@ function stripExt(filePath: string) {
1415
}
1516

1617
function init({ typescript: ts }: { typescript: typeof TsServerLibrary }) {
17-
function create(info: TsServerLibrary.server.PluginCreateInfo) {
18+
function create(info: ts.server.PluginCreateInfo) {
1819
const log = (...params: unknown[]) => {
1920
const text = params.map((p) => (p ? JSON.stringify(p) : p)).join(' ');
2021
info.project.projectService.logger.info(`[namespace-import] ${text}`);
@@ -30,27 +31,15 @@ function init({ typescript: ts }: { typescript: typeof TsServerLibrary }) {
3031
return original;
3132
}
3233

33-
const entry: ts.CompletionEntry = {
34-
name: 'TestService',
35-
kind: ts.ScriptElementKind.alias,
36-
source: '/Users/yukukotani/ghq/github.com/ubie-inc/yukustory/packages/client/src/TestService.ts',
37-
sortText: 'TestService',
38-
hasAction: true,
39-
isImportStatementCompletion: true,
40-
data: {
41-
exportName: 'TestService',
42-
modulePath: '/Users/yukukotani/ghq/github.com/ubie-inc/yukustory/packages/client/src/TestService.ts',
43-
},
44-
};
45-
original.entries.push(entry);
34+
original.entries = [...original.entries, ...getCompletionEntries(info)];
4635

4736
return original;
4837
};
4938

5039
const getCompletionEntryDetails = info.languageService.getCompletionEntryDetails;
5140
info.languageService.getCompletionEntryDetails = (fileName, position, name, options, source, preferences, data) => {
5241
log('getCompletionEntryDetails', { fileName, position, name, options, source });
53-
if (name === 'TestService' && data?.modulePath) {
42+
if (data?.modulePath) {
5443
const importPath = path.relative(path.dirname(fileName), data.modulePath);
5544
const text = `import * as ${name} from "${stripExt(importPath)}";\n`;
5645
const action: CodeFixAction = {

src/lib/import.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import ts from 'typescript/lib/tsserverlibrary';
2+
import * as path from 'path';
3+
4+
export function getCompletionEntries(info: ts.server.PluginCreateInfo): ts.CompletionEntry[] {
5+
const currentDir = info.project.getCurrentDirectory();
6+
const filePaths = info.project.readDirectory(path.resolve(currentDir, 'src/services'));
7+
8+
return filePaths.map((filePath) => {
9+
const ext = path.extname(filePath);
10+
const name = path.basename(filePath, ext);
11+
return {
12+
name: name,
13+
kind: ts.ScriptElementKind.alias,
14+
source: filePath,
15+
sortText: name,
16+
hasAction: true,
17+
isImportStatementCompletion: true,
18+
data: {
19+
exportName: name,
20+
modulePath: filePath,
21+
},
22+
};
23+
});
24+
}

0 commit comments

Comments
 (0)