Auto merge of #15779 - Veykril:open-docs, r=Veykril

Add command for only opening external docs and attempt to fix vscode-remote issue

opening URI in a remote env causes vscode to ask the OS to handle `vscode-remote` URIs as there is no handler registered for such a scheme. This attempts to instruct vscode to handle those.

This is untested, as I can't figure out how to open a debug session on WSL rn.
This commit is contained in:
bors 2023-10-18 12:13:01 +00:00
commit 4586a6b26c
4 changed files with 47 additions and 17 deletions

View file

@ -248,6 +248,11 @@
"title": "Open Docs", "title": "Open Docs",
"category": "rust-analyzer" "category": "rust-analyzer"
}, },
{
"command": "rust-analyzer.openExternalDocs",
"title": "Open External Docs",
"category": "rust-analyzer"
},
{ {
"command": "rust-analyzer.openCargoToml", "command": "rust-analyzer.openCargoToml",
"title": "Open Cargo.toml", "title": "Open Cargo.toml",
@ -260,12 +265,12 @@
}, },
{ {
"command": "rust-analyzer.moveItemUp", "command": "rust-analyzer.moveItemUp",
"title": "Move item up", "title": "Move Item Up",
"category": "rust-analyzer" "category": "rust-analyzer"
}, },
{ {
"command": "rust-analyzer.moveItemDown", "command": "rust-analyzer.moveItemDown",
"title": "Move item down", "title": "Move Item Down",
"category": "rust-analyzer" "category": "rust-analyzer"
}, },
{ {

View file

@ -948,27 +948,51 @@ export function openDocs(ctx: CtxInit): Cmd {
const position = editor.selection.active; const position = editor.selection.active;
const textDocument = { uri: editor.document.uri.toString() }; const textDocument = { uri: editor.document.uri.toString() };
const doclinks = await client.sendRequest(ra.openDocs, { position, textDocument }); const docLinks = await client.sendRequest(ra.openDocs, { position, textDocument });
log.debug(docLinks);
let fileType = vscode.FileType.Unknown; let fileType = vscode.FileType.Unknown;
if (typeof doclinks.local === "string") { if (docLinks.local !== undefined) {
try { try {
fileType = (await vscode.workspace.fs.stat(vscode.Uri.parse(doclinks.local))).type; fileType = (await vscode.workspace.fs.stat(vscode.Uri.parse(docLinks.local))).type;
} catch (e) { } catch (e) {
log.debug("stat() threw error. Falling back to web version", e); log.debug("stat() threw error. Falling back to web version", e);
} }
} }
let doclink; let docLink = fileType & vscode.FileType.File ? docLinks.local : docLinks.web;
if (fileType & vscode.FileType.File) { if (docLink) {
// file does exist locally // instruct vscode to handle the vscode-remote link directly
doclink = doclinks.local; if (docLink.startsWith("vscode-remote://")) {
} else { docLink = docLink.replace("vscode-remote://", "vscode://vscode-remote/");
doclink = doclinks.web; }
const docUri = vscode.Uri.parse(docLink);
await vscode.env.openExternal(docUri);
} }
};
}
if (doclink != null) { export function openExternalDocs(ctx: CtxInit): Cmd {
await vscode.env.openExternal(vscode.Uri.parse(doclink)); return async () => {
const editor = vscode.window.activeTextEditor;
if (!editor) {
return;
}
const client = ctx.client;
const position = editor.selection.active;
const textDocument = { uri: editor.document.uri.toString() };
const docLinks = await client.sendRequest(ra.openDocs, { position, textDocument });
let docLink = docLinks.web;
if (docLink) {
// instruct vscode to handle the vscode-remote link directly
if (docLink.startsWith("vscode-remote://")) {
docLink = docLink.replace("vscode-remote://", "vscode://vscode-remote/");
}
const docUri = vscode.Uri.parse(docLink);
await vscode.env.openExternal(docUri);
} }
}; };
} }

View file

@ -74,8 +74,8 @@ export interface FetchDependencyListParams {}
export interface FetchDependencyListResult { export interface FetchDependencyListResult {
crates: { crates: {
name: string | undefined; name?: string;
version: string | undefined; version?: string;
path: string; path: string;
}[]; }[];
} }
@ -136,8 +136,8 @@ export const openCargoToml = new lc.RequestType<OpenCargoTomlParams, lc.Location
"experimental/openCargoToml", "experimental/openCargoToml",
); );
export interface DocsUrls { export interface DocsUrls {
local: string | void; local?: string;
web: string | void; web?: string;
} }
export const openDocs = new lc.RequestType<lc.TextDocumentPositionParams, DocsUrls, void>( export const openDocs = new lc.RequestType<lc.TextDocumentPositionParams, DocsUrls, void>(
"experimental/externalDocs", "experimental/externalDocs",

View file

@ -170,6 +170,7 @@ function createCommands(): Record<string, CommandFactory> {
debug: { enabled: commands.debug }, debug: { enabled: commands.debug },
newDebugConfig: { enabled: commands.newDebugConfig }, newDebugConfig: { enabled: commands.newDebugConfig },
openDocs: { enabled: commands.openDocs }, openDocs: { enabled: commands.openDocs },
openExternalDocs: { enabled: commands.openExternalDocs },
openCargoToml: { enabled: commands.openCargoToml }, openCargoToml: { enabled: commands.openCargoToml },
peekTests: { enabled: commands.peekTests }, peekTests: { enabled: commands.peekTests },
moveItemUp: { enabled: commands.moveItemUp }, moveItemUp: { enabled: commands.moveItemUp },