Skip to content
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

feat: vscode: Support opening local documentation if available #15728

Merged
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
1 change: 1 addition & 0 deletions editors/code/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ class ExperimentalFeatures implements lc.StaticFeature {
serverStatusNotification: true,
colorDiagnosticOutput: true,
openServerLogs: true,
localDocs: true,
commands: {
commands: [
"rust-analyzer.runSingle",
Expand Down
22 changes: 20 additions & 2 deletions editors/code/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type { LanguageClient } from "vscode-languageclient/node";
import { LINKED_COMMANDS } from "./client";
import type { DependencyId } from "./dependencies_provider";
import { unwrapUndefinable } from "./undefinable";
import { log } from "./util";

export * from "./ast_inspector";
export * from "./run";
Expand Down Expand Up @@ -947,10 +948,27 @@ export function openDocs(ctx: CtxInit): Cmd {
const position = editor.selection.active;
const textDocument = { uri: editor.document.uri.toString() };

const doclink = await client.sendRequest(ra.openDocs, { position, textDocument });
const doclinks = await client.sendRequest(ra.openDocs, { position, textDocument });

let fileType = vscode.FileType.Unknown;
if (typeof doclinks.local === "string") {
try {
fileType = (await vscode.workspace.fs.stat(vscode.Uri.parse(doclinks.local))).type;
} catch (e) {
log.debug("stat() threw error. Falling back to web version", e);
}
}

let doclink;
if (fileType & vscode.FileType.File) {
// file does exist locally
doclink = doclinks.local;
} else {
doclink = doclinks.web;
}

if (doclink != null) {
await vscode.commands.executeCommand("vscode.open", vscode.Uri.parse(doclink));
await vscode.env.openExternal(vscode.Uri.parse(doclink));
}
};
}
Expand Down
6 changes: 5 additions & 1 deletion editors/code/src/lsp_ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ export const onEnter = new lc.RequestType<lc.TextDocumentPositionParams, lc.Text
export const openCargoToml = new lc.RequestType<OpenCargoTomlParams, lc.Location, void>(
"experimental/openCargoToml",
);
export const openDocs = new lc.RequestType<lc.TextDocumentPositionParams, string | void, void>(
export interface DocsUrls {
local: string | void;
web: string | void;
}
export const openDocs = new lc.RequestType<lc.TextDocumentPositionParams, DocsUrls, void>(
"experimental/externalDocs",
);
export const parentModule = new lc.RequestType<
Expand Down