Skip to content

Commit

Permalink
feat: enable localDocs (#1261)
Browse files Browse the repository at this point in the history
Opening docs (rust-analyzer.openDocs) was broken by commit
1410329. This commit contains changes
preparing for the rust-analyzer "localDocs" feature
https://github.com/rust-lang/rust-analyzer/blob/master/docs/dev/lsp-extensions.md#local-documentation
However, this feature is only activated by specifying the "localDocs"
capability. This commits does so in src/client.ts.

To actually make use of the local documentation, the
src/commands.ts:openDocs function also needs adjustment.
I wrote it such that if a local URI is provided and a corresponding file
exists, it is opened.
Otherwise, if a web link is provided, this link is opened.

Note that "vscode.open" would open the local URI in the editor instead
of the browser.

Whether local documentation exists depends on whether 'cargo doc' has
been executed. If it has been executed but not recently, the
documentation might be outdated, so it might be reasonable to generate
local documentation before trying to open it.
This is not done so far, but could be added.
It might make sense to run 'cargo doc --no-deps'.

Co-authored-by: Daniel Rainer <daniel.rainer@localhost>
  • Loading branch information
danielrainer and Daniel Rainer authored Aug 6, 2024
1 parent aff970b commit 71921db
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class ExperimentalFeatures implements StaticFeature {
const caps: any = capabilities.experimental ?? {};
caps.snippetTextEdit = true;
caps.serverStatusNotification = true;
caps.localDocs = true;
caps.commands = {
commands: [
'rust-analyzer.runSingle',
Expand Down
19 changes: 16 additions & 3 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import {
window,
workspace,
type WorkspaceEdit,
nvim,
} from 'coc.nvim';
import { randomBytes } from 'node:crypto';
import { writeFileSync } from 'node:fs';
import { existsSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import readline from 'node:readline';
Expand Down Expand Up @@ -695,9 +696,21 @@ export function openDocs(ctx: Ctx): Cmd {
textDocument: { uri: document.uri },
position,
};
// TODO: Should 'cargo doc' run at this point? Or 'cargo doc --no-deps'?
const doclink = await ctx.client.sendRequest(ra.openDocs, param);
if (doclink?.web) {
await commands.executeCommand('vscode.open', Uri.parse(doclink.web));
if (doclink) {
if (doclink.local) {
// remove leading 'file://'
const absolutePath = doclink.local.substring(7);
const isReadable = existsSync(absolutePath);
if (isReadable) {
await nvim.call('coc#ui#open_url', doclink.local);
return;
}
}
if (doclink.web) {
await commands.executeCommand('vscode.open', Uri.parse(doclink.web));
}
}
};
}
Expand Down

0 comments on commit 71921db

Please sign in to comment.