11861: internal: Add "view file text" command to debug sync issues r=jonas-schievink a=jonas-schievink

I saw a file sync bug the other day but didn't know how to further debug it. This command might give a clue as to what's wrong and help debug issues like https://github.com/rust-analyzer/rust-analyzer/issues/4829.

bors r+

Co-authored-by: Jonas Schievink <jonas.schievink@ferrous-systems.com>
This commit is contained in:
bors[bot] 2022-03-31 12:52:11 +00:00 committed by GitHub
commit 9da9418661
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 89 additions and 1 deletions

View file

@ -123,6 +123,14 @@ pub(crate) fn handle_view_hir(
Ok(res)
}
pub(crate) fn handle_view_file_text(
snap: GlobalStateSnapshot,
params: lsp_types::TextDocumentIdentifier,
) -> Result<String> {
let file_id = from_proto::file_id(&snap, &params.uri)?;
Ok(snap.analysis.file_text(file_id)?.to_string())
}
pub(crate) fn handle_view_item_tree(
snap: GlobalStateSnapshot,
params: lsp_ext::ViewItemTreeParams,

View file

@ -70,6 +70,14 @@ impl Request for ViewHir {
const METHOD: &'static str = "rust-analyzer/viewHir";
}
pub enum ViewFileText {}
impl Request for ViewFileText {
type Params = lsp_types::TextDocumentIdentifier;
type Result = String;
const METHOD: &'static str = "rust-analyzer/viewFileText";
}
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ViewCrateGraphParams {

View file

@ -590,6 +590,7 @@ impl GlobalState {
.on::<lsp_ext::AnalyzerStatus>(handlers::handle_analyzer_status)
.on::<lsp_ext::SyntaxTree>(handlers::handle_syntax_tree)
.on::<lsp_ext::ViewHir>(handlers::handle_view_hir)
.on::<lsp_ext::ViewFileText>(handlers::handle_view_file_text)
.on::<lsp_ext::ViewCrateGraph>(handlers::handle_view_crate_graph)
.on::<lsp_ext::ViewItemTree>(handlers::handle_view_item_tree)
.on::<lsp_ext::ExpandMacro>(handlers::handle_expand_macro)

View file

@ -1,5 +1,5 @@
<!---
lsp_ext.rs hash: 854109e98d02a780
lsp_ext.rs hash: a61de7db4504a4d1
If you need to change the above hash to make the test pass, please check if you
need to adjust this doc as well and ping this issue:
@ -494,6 +494,17 @@ Primarily for debugging, but very useful for all people working on rust-analyzer
Returns a textual representation of the HIR of the function containing the cursor.
For debugging or when working on rust-analyzer itself.
## View File Text
**Method:** `rust-analyzer/viewFileText`
**Request:** `TextDocumentIdentifier`
**Response:** `string`
Returns the text of a file as seen by the server.
This is for debugging file sync problems.
## View ItemTree
**Method:** `rust-analyzer/viewItemTree`

View file

@ -104,6 +104,11 @@
"title": "View Hir",
"category": "Rust Analyzer"
},
{
"command": "rust-analyzer.viewFileText",
"title": "View File Text (as seen by the server)",
"category": "Rust Analyzer"
},
{
"command": "rust-analyzer.viewItemTree",
"title": "Debug ItemTree",
@ -1408,6 +1413,10 @@
"command": "rust-analyzer.viewHir",
"when": "inRustProject"
},
{
"command": "rust-analyzer.viewFileText",
"when": "inRustProject"
},
{
"command": "rust-analyzer.expandMacro",
"when": "inRustProject"

View file

@ -432,6 +432,54 @@ export function viewHir(ctx: Ctx): Cmd {
};
}
export function viewFileText(ctx: Ctx): Cmd {
const tdcp = new class implements vscode.TextDocumentContentProvider {
readonly uri = vscode.Uri.parse('rust-analyzer://viewFileText/file.rs');
readonly eventEmitter = new vscode.EventEmitter<vscode.Uri>();
constructor() {
vscode.workspace.onDidChangeTextDocument(this.onDidChangeTextDocument, this, ctx.subscriptions);
vscode.window.onDidChangeActiveTextEditor(this.onDidChangeActiveTextEditor, this, ctx.subscriptions);
}
private onDidChangeTextDocument(event: vscode.TextDocumentChangeEvent) {
if (isRustDocument(event.document)) {
// We need to order this after language server updates, but there's no API for that.
// Hence, good old sleep().
void sleep(10).then(() => this.eventEmitter.fire(this.uri));
}
}
private onDidChangeActiveTextEditor(editor: vscode.TextEditor | undefined) {
if (editor && isRustEditor(editor)) {
this.eventEmitter.fire(this.uri);
}
}
provideTextDocumentContent(_uri: vscode.Uri, ct: vscode.CancellationToken): vscode.ProviderResult<string> {
const rustEditor = ctx.activeRustEditor;
const client = ctx.client;
if (!rustEditor || !client) return '';
const params = client.code2ProtocolConverter.asTextDocumentIdentifier(rustEditor.document);
return client.sendRequest(ra.viewFileText, params, ct);
}
get onDidChange(): vscode.Event<vscode.Uri> {
return this.eventEmitter.event;
}
};
ctx.pushCleanup(vscode.workspace.registerTextDocumentContentProvider('rust-analyzer', tdcp));
return async () => {
const document = await vscode.workspace.openTextDocument(tdcp.uri);
tdcp.eventEmitter.fire(tdcp.uri);
void await vscode.window.showTextDocument(document, {
viewColumn: vscode.ViewColumn.Two,
preserveFocus: true
});
};
}
export function viewItemTree(ctx: Ctx): Cmd {
const tdcp = new class implements vscode.TextDocumentContentProvider {
readonly uri = vscode.Uri.parse('rust-analyzer://viewItemTree/itemtree.rs');

View file

@ -36,6 +36,8 @@ export const syntaxTree = new lc.RequestType<SyntaxTreeParams, string, void>("ru
export const viewHir = new lc.RequestType<lc.TextDocumentPositionParams, string, void>("rust-analyzer/viewHir");
export const viewFileText = new lc.RequestType<lc.TextDocumentIdentifier, string, void>("rust-analyzer/viewFileText");
export interface ViewItemTreeParams {
textDocument: lc.TextDocumentIdentifier;
}

View file

@ -114,6 +114,7 @@ async function initCommonContext(context: vscode.ExtensionContext, ctx: Ctx) {
ctx.registerCommand('parentModule', commands.parentModule);
ctx.registerCommand('syntaxTree', commands.syntaxTree);
ctx.registerCommand('viewHir', commands.viewHir);
ctx.registerCommand('viewFileText', commands.viewFileText);
ctx.registerCommand('viewItemTree', commands.viewItemTree);
ctx.registerCommand('viewCrateGraph', commands.viewCrateGraph);
ctx.registerCommand('viewFullCrateGraph', commands.viewFullCrateGraph);