Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

Commit

Permalink
fix: completion show everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Jan 30, 2020
1 parent a9760fe commit 21741a2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
3 changes: 2 additions & 1 deletion client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ export async function activate(context: ExtensionContext) {
],
synchronize: {
configurationSection: configurationSection
}
},
progressOnInitialization: true
};

// Create the language client and start the client.
Expand Down
1 change: 1 addition & 0 deletions server/src/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Deno {
public version!: Version | void;
public executablePath!: string | void;
public readonly DENO_DIR = this.getDenoDir();
public readonly DENO_DEPS_DIR = join(this.DENO_DIR, "deps");
public readonly dtsFilepath = join(this.DENO_DIR, "lib.deno_runtime.d.ts");
constructor() {}
public async init() {
Expand Down
41 changes: 30 additions & 11 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ connection.onInitialize(
capabilities: {
documentFormattingProvider: true,
completionProvider: {
triggerCharacters: ["http", "https"],
resolveProvider: true
triggerCharacters: ["http", "https"]
}
}
};
Expand Down Expand Up @@ -134,7 +133,7 @@ interface Deps {
}

async function getDepsFile(
rootDir = path.join(deno.DENO_DIR, "deps"),
rootDir = deno.DENO_DEPS_DIR,
deps: Deps[] = []
): Promise<Deps[]> {
const files = await fs.readdir(rootDir);
Expand All @@ -150,7 +149,7 @@ async function getDepsFile(
!filepath.endsWith(".d.ts")
) {
const url = filepath
.replace(path.join(deno.DENO_DIR, "deps"), "")
.replace(deno.DENO_DEPS_DIR, "")
.replace(/^(\/|\\\\)/, "")
.replace(/http(\/|\\\\)/, "http://")
.replace(/https(\/|\\\\)/, "https://");
Expand All @@ -168,17 +167,37 @@ async function getDepsFile(
return deps;
}

connection.onCompletionResolve(
(item: CompletionItem): CompletionItem => {
return item;
// FIXME: all completion will trigger this.
// It seem it's a bug for vscode
connection.onCompletion(async params => {
const { position, partialResultToken, context, textDocument } = params;

const doc = documents.get(textDocument.uri);

if (!globalSettings.enable || !doc) {
return [];
}
);

connection.onCompletion(async params => {
if (!globalSettings.enable) {
const currentLine = doc.getText(
Range.create(Position.create(position.line, 0), position)
);

const IMPORT_REG = /import\s['"][a-zA-Z]$/;
const IMPORT_FROM_REG = /import\s(([^\s]*)|(\*\sas\s[^\s]*))\sfrom\s['"][a-zA-Z]$/;
const DYNAMIC_REG = /import\s*\(['"][a-zA-Z]$/;

const isImport =
IMPORT_REG.test(currentLine) || // import "https://xxxx.xxxx"
IMPORT_FROM_REG.test(currentLine) || // import xxxx from "https://xxxx.xxxx"
DYNAMIC_REG.test(currentLine); // import("https://xxxx.xxxx")

if (
currentLine.length > 1000 || // if is a large file
!isImport
) {
return [];
}
const { position, partialResultToken } = params;

const deps = await getDepsFile();

const range = Range.create(
Expand Down

0 comments on commit 21741a2

Please sign in to comment.