Skip to content

Commit 7ecbdc8

Browse files
authored
Merge pull request #11 from Monchi/disable-completion-in-string-literal
2 parents 70a65da + b794b32 commit 7ecbdc8

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

package-lock.json

Lines changed: 13 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
@@ -18,6 +18,7 @@
1818
"homepage": "https://github.com/Monchi/typescript-plugin-namespace-import#readme",
1919
"dependencies": {
2020
"camelcase": "^6.2.0",
21+
"tsutils": "^3.21.0",
2122
"typescript": "^4.3.5"
2223
},
2324
"devDependencies": {

src/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import ts, { SemanticClassificationFormat } from 'typescript/lib/tsserverlibrary';
2+
import * as tsutils from 'tsutils';
13
import * as namespaceImportPlugin from './lib/import';
24

35
declare global {
@@ -21,7 +23,14 @@ function init() {
2123
info.languageService.getCompletionsAtPosition = (fileName, position, options) => {
2224
log('getCompletionsAtPosition', { fileName, position, options });
2325
const original = getCompletionsAtPosition(fileName, position, options);
24-
if (original == null || options?.triggerCharacter != null) {
26+
if (
27+
original == null ||
28+
options?.triggerCharacter != null ||
29+
!namespaceImportPlugin.isAutoCompletablePosition(
30+
info.languageService.getProgram()?.getSourceFile(fileName),
31+
position,
32+
)
33+
) {
2534
return original;
2635
}
2736

src/lib/import.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import ts, { CodeFixAction, ScriptElementKind } from 'typescript/lib/tsserverlibrary';
22
import * as path from 'path';
33
import camelCase from 'camelcase';
4+
import * as tsutils from 'tsutils';
45

56
export type PluginOptions = {
67
paths: readonly string[];
@@ -44,6 +45,14 @@ export function filterNamedImportEntries(
4445
});
4546
}
4647

48+
export function isAutoCompletablePosition(sourceFile: ts.SourceFile | undefined, position: number): boolean {
49+
if (!sourceFile) {
50+
return false;
51+
}
52+
const token = tsutils.getTokenAtPosition(sourceFile!, position)?.kind ?? ts.SyntaxKind.Unknown;
53+
return token !== ts.SyntaxKind.StringLiteral;
54+
}
55+
4756
export function getCompletionEntryDetails(
4857
name: string,
4958
selfPath: string,

0 commit comments

Comments
 (0)