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

Commit

Permalink
feat: support import installed module intelligent. close #4
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Jan 26, 2020
1 parent 4b6a23a commit 6d9baaa
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 14 deletions.
101 changes: 90 additions & 11 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as path from "path";
import { readFile, writeFile } from "fs";
import { promisify } from "util";
import { promises as fs } from "fs";

import {
workspace,
Expand All @@ -16,7 +15,11 @@ import {
languages,
TextDocument,
Range,
TextEdit
TextEdit,
Position,
CancellationToken,
CompletionItem,
CompletionItemKind
} from "vscode";

import {
Expand All @@ -43,6 +46,11 @@ interface TypescriptAPI {
configurePlugin(pluginId: string, configuration: {}): void;
}

const config: SynchronizedConfiguration = {
enable: true,
dtsFilepaths: []
};

async function pickFolder(
folders: WorkspaceFolder[],
placeHolder: string
Expand Down Expand Up @@ -167,13 +175,12 @@ function synchronizeConfiguration(api: TypescriptAPI) {
}

function getConfiguration(): SynchronizedConfiguration {
const config = workspace.getConfiguration(configurationSection);
const outConfig: SynchronizedConfiguration = {};
const _config = workspace.getConfiguration(configurationSection);

withConfigValue(config, outConfig, "enable");
withConfigValue(config, outConfig, "dtsFilepaths");
withConfigValue(_config, config, "enable");
withConfigValue(_config, config, "dtsFilepaths");

return outConfig;
return config;
}

function withConfigValue<C, K extends Extract<keyof C, string>>(
Expand Down Expand Up @@ -230,6 +237,46 @@ async function getTypescriptAPI(): Promise<TypescriptAPI> {
return api;
}

interface Deps {
url: string;
filepath: string;
}

async function getDepsFile(
rootDir = path.join(deno.DENO_DIR, "deps"),
deps: Deps[] = []
): Promise<Deps[]> {
const files = await fs.readdir(rootDir);

const promises = files.map(filename => {
const filepath = path.join(rootDir, filename);
return fs.stat(filepath).then(stat => {
if (stat.isDirectory()) {
return getDepsFile(filepath, deps);
} else if (
stat.isFile() &&
/\.tsx?$/.test(filepath) &&
!filepath.endsWith(".d.ts")
) {
const url = filepath
.replace(path.join(deno.DENO_DIR, "deps"), "")
.replace(/^(\/|\\\\)/, "")
.replace(/http(\/|\\\\)/, "http://")
.replace(/https(\/|\\\\)/, "https://");

deps.push({
url: url,
filepath: filepath
});
}
});
});

await Promise.all(promises);

return deps;
}

export async function activate(context: ExtensionContext) {
try {
await deno.init();
Expand All @@ -239,16 +286,16 @@ export async function activate(context: ExtensionContext) {

// if dst file not exist. then create a new one
if (!isExistDtsFile) {
await promisify(writeFile)(typeFilepath, currentDenoTypesContent, {
await fs.writeFile(typeFilepath, currentDenoTypesContent, {
encoding: "utf8"
});
} else {
const typesContent = await promisify(readFile)(typeFilepath, {
const typesContent = await fs.readFile(typeFilepath, {
encoding: "utf8"
});

if (typesContent.toString() !== currentDenoTypesContent.toString()) {
await promisify(writeFile)(typeFilepath, currentDenoTypesContent, {
await fs.writeFile(typeFilepath, currentDenoTypesContent, {
encoding: "utf8"
});
}
Expand Down Expand Up @@ -349,6 +396,38 @@ export async function activate(context: ExtensionContext) {
}
}
),
languages.registerCompletionItemProvider(
["typescript", "typescriptreact"],
{
provideCompletionItems: async (
document: TextDocument,
position: Position,
token: CancellationToken
) => {
if (!config.enable) {
return [];
}
const deps = await getDepsFile();

const completes: CompletionItem[] = deps.map(dep => {
return {
label: dep.url,
detail: dep.url,
sortText: dep.url,
documentation: dep.filepath.replace(deno.DENO_DIR, "$DENO_DIR"),
kind: CompletionItemKind.File,
insertText: dep.url,
cancel: token,
range: new Range(position.translate(0, -5), position)
} as CompletionItem;
});

return completes;
}
},
"http",
"https"
),
commands.registerCommand("deno.enable", enable),
commands.registerCommand("deno.disable", disable)
];
Expand Down
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { stat } from "fs";
import { promisify } from "util";
import { promises as fs } from "fs";
import { TextDocument } from "vscode";

export function isTypeScriptDocument(document: TextDocument) {
Expand All @@ -19,7 +18,8 @@ export function isJavaScriptDocument(document: TextDocument) {
}

export async function isFilepathExist(filepath: string): Promise<boolean> {
return promisify(stat)(filepath)
return fs
.stat(filepath)
.then(() => {
return Promise.resolve(true);
})
Expand Down

0 comments on commit 6d9baaa

Please sign in to comment.