Skip to content

Commit 74c8ae7

Browse files
committed
implement import action
1 parent 323644c commit 74c8ae7

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"typescript": "^4.3.5"
2222
},
2323
"devDependencies": {
24+
"@types/node": "^16.7.10",
2425
"@ubie/prettier-config": "^0.1.0",
2526
"prettier": "^2.3.2"
2627
}

src/index.ts

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1-
import TsServerLibrary from 'typescript/lib/tsserverlibrary';
1+
import TsServerLibrary, { CodeFixAction, ScriptElementKind } from 'typescript/lib/tsserverlibrary';
2+
import * as path from 'path';
3+
4+
declare global {
5+
namespace ts {
6+
interface CompletionEntryData {
7+
modulePath?: string;
8+
}
9+
}
10+
}
11+
12+
function stripExt(filePath: string) {
13+
return filePath.slice(0, filePath.lastIndexOf('.'));
14+
}
215

316
function init({ typescript: ts }: { typescript: typeof TsServerLibrary }) {
4-
function create(info: ts.server.PluginCreateInfo) {
17+
function create(info: TsServerLibrary.server.PluginCreateInfo) {
518
const log = (...params: unknown[]) => {
619
const text = params.map((p) => (p ? JSON.stringify(p) : p)).join(' ');
720
info.project.projectService.logger.info(`[namespace-import] ${text}`);
@@ -23,11 +36,53 @@ function init({ typescript: ts }: { typescript: typeof TsServerLibrary }) {
2336
source: '/Users/yukukotani/ghq/github.com/ubie-inc/yukustory/packages/client/src/TestService.ts',
2437
sortText: 'TestService',
2538
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+
},
2644
};
2745
original.entries.push(entry);
2846

2947
return original;
3048
};
49+
50+
const getCompletionEntryDetails = info.languageService.getCompletionEntryDetails;
51+
info.languageService.getCompletionEntryDetails = (fileName, position, name, options, source, preferences, data) => {
52+
log('getCompletionEntryDetails', { fileName, position, name, options, source });
53+
if (name === 'TestService' && data?.modulePath) {
54+
const importPath = path.relative(path.dirname(fileName), data.modulePath);
55+
const text = `import * as ${name} from "${stripExt(importPath)}";\n`;
56+
const action: CodeFixAction = {
57+
fixName: 'namespace-import',
58+
description: text,
59+
changes: [
60+
{
61+
fileName: fileName,
62+
textChanges: [
63+
{
64+
span: {
65+
start: 0,
66+
length: 0,
67+
},
68+
newText: text,
69+
},
70+
],
71+
},
72+
],
73+
commands: [],
74+
};
75+
return {
76+
name: name,
77+
kind: ScriptElementKind.alias,
78+
kindModifiers: '',
79+
displayParts: [],
80+
codeActions: [action],
81+
};
82+
}
83+
84+
return getCompletionEntryDetails(fileName, position, name, options, source, preferences, data);
85+
};
3186
}
3287

3388
return { create };

0 commit comments

Comments
 (0)