From 44bc4a13638822e0ee482a2fba0f23c463e16820 Mon Sep 17 00:00:00 2001 From: axetroy Date: Tue, 3 Mar 2020 17:10:10 +0800 Subject: [PATCH] feat: add CodeLens for deno cached module which will show current URL --- client/src/extension.ts | 6 ++++ server/src/language/code_lens.ts | 47 ++++++++++++++++++++++++++++++++ server/src/server.ts | 5 +++- 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 server/src/language/code_lens.ts diff --git a/client/src/extension.ts b/client/src/extension.ts index 753cce8..f8f01f7 100644 --- a/client/src/extension.ts +++ b/client/src/extension.ts @@ -262,6 +262,12 @@ class Extension { } return next(document, position, context, token); + }, + provideCodeLenses: (document, token, next) => { + if (!isInDeno(document.uri.fsPath)) { + return; + } + return next(document, token); } } }; diff --git a/server/src/language/code_lens.ts b/server/src/language/code_lens.ts new file mode 100644 index 0000000..7d1a912 --- /dev/null +++ b/server/src/language/code_lens.ts @@ -0,0 +1,47 @@ +import { + IConnection, + Range, + TextDocuments, + Position +} from "vscode-languageserver"; +import { TextDocument } from "vscode-languageserver-textdocument"; +import { URI } from "vscode-uri"; + +import { CacheModule } from "../../../core/deno_cache"; +import { isInDeno } from "../../../core/deno"; + +export class CodeLens { + constructor(connection: IConnection, documents: TextDocuments) { + connection.onCodeLens(params => { + const { textDocument } = params; + + const document = documents.get(textDocument.uri); + + if (!document) { + return []; + } + + const filepath = URI.parse(document.uri).fsPath; + + if (!isInDeno(filepath)) { + return []; + } + + const cache = CacheModule.create(filepath); + + if (!cache) { + return; + } + + return [ + { + range: Range.create(Position.create(0, 0), Position.create(0, 0)), + command: { + title: `Deno cached module \`${cache.url.href}\``, + command: "" + } + } + ]; + }); + } +} diff --git a/server/src/server.ts b/server/src/server.ts index 6c05979..f4af078 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -26,6 +26,7 @@ import { DocumentHighlight } from "./language/document_highlight"; import { DocumentFormatting } from "./language/document_formatting"; import { Hover } from "./language/hover"; import { Completion } from "./language/completion"; +import { CodeLens } from "./language/code_lens"; import { getDenoDir, getDenoDts } from "../../core/deno"; import { pathExists } from "../../core/util"; @@ -51,6 +52,7 @@ new DocumentHighlight(connection, documents); new DocumentFormatting(connection, documents, bridge); new Hover(connection, documents); new Completion(connection, documents); +new CodeLens(connection, documents); connection.onInitialize( (): InitializeResult => { @@ -71,7 +73,8 @@ connection.onInitialize( documentHighlightProvider: true, hoverProvider: true, referencesProvider: true, - definitionProvider: true + definitionProvider: true, + codeLensProvider: {} } }; }