Skip to content

Disable completion in string literal #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"homepage": "https://github.com/Monchi/typescript-plugin-namespace-import#readme",
"dependencies": {
"camelcase": "^6.2.0",
"tsutils": "^3.21.0",
"typescript": "^4.3.5"
},
"devDependencies": {
Expand Down
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import ts, { SemanticClassificationFormat } from 'typescript/lib/tsserverlibrary';
import * as tsutils from 'tsutils';
import * as namespaceImportPlugin from './lib/import';

declare global {
Expand All @@ -21,7 +23,14 @@ function init() {
info.languageService.getCompletionsAtPosition = (fileName, position, options) => {
log('getCompletionsAtPosition', { fileName, position, options });
const original = getCompletionsAtPosition(fileName, position, options);
if (original == null || options?.triggerCharacter != null) {
if (
original == null ||
options?.triggerCharacter != null ||
!namespaceImportPlugin.isAutoCompletablePosition(
info.languageService.getProgram()?.getSourceFile(fileName),
position,
)
) {
return original;
}

Expand Down
9 changes: 9 additions & 0 deletions src/lib/import.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ts, { CodeFixAction, ScriptElementKind } from 'typescript/lib/tsserverlibrary';
import * as path from 'path';
import camelCase from 'camelcase';
import * as tsutils from 'tsutils';

export type PluginOptions = {
paths: readonly string[];
Expand Down Expand Up @@ -44,6 +45,14 @@ export function filterNamedImportEntries(
});
}

export function isAutoCompletablePosition(sourceFile: ts.SourceFile | undefined, position: number): boolean {
if (!sourceFile) {
return false;
}
const token = tsutils.getTokenAtPosition(sourceFile!, position)?.kind ?? ts.SyntaxKind.Unknown;
return token !== ts.SyntaxKind.StringLiteral;
}

export function getCompletionEntryDetails(
name: string,
selfPath: string,
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy