aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2021-05-21 22:59:52 +0100
committerJonas Schievink <[email protected]>2021-05-21 22:59:52 +0100
commit271ec6b990523c79f93468a5b0ab5e1aceab50f6 (patch)
tree82e6df7e0d66797bae5ee26fa16af40f482ae365
parent8d13864440ba8b6ede1097c79b28e4981caf714a (diff)
Add a "Debug ItemTree" LSP request
-rw-r--r--crates/ide/src/lib.rs5
-rw-r--r--crates/ide/src/view_item_tree.rs16
-rw-r--r--crates/rust-analyzer/src/handlers.rs10
-rw-r--r--crates/rust-analyzer/src/lsp_ext.rs14
-rw-r--r--crates/rust-analyzer/src/main_loop.rs1
-rw-r--r--editors/code/package.json5
-rw-r--r--editors/code/src/commands.ts50
-rw-r--r--editors/code/src/lsp_ext.ts6
-rw-r--r--editors/code/src/main.ts1
9 files changed, 108 insertions, 0 deletions
diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs
index f4b90db3a..ff2a54117 100644
--- a/crates/ide/src/lib.rs
+++ b/crates/ide/src/lib.rs
@@ -50,6 +50,7 @@ mod typing;
50mod markdown_remove; 50mod markdown_remove;
51mod doc_links; 51mod doc_links;
52mod view_crate_graph; 52mod view_crate_graph;
53mod view_item_tree;
53 54
54use std::sync::Arc; 55use std::sync::Arc;
55 56
@@ -288,6 +289,10 @@ impl Analysis {
288 self.with_db(|db| view_hir::view_hir(&db, position)) 289 self.with_db(|db| view_hir::view_hir(&db, position))
289 } 290 }
290 291
292 pub fn view_item_tree(&self, file_id: FileId) -> Cancelable<String> {
293 self.with_db(|db| view_item_tree::view_item_tree(&db, file_id))
294 }
295
291 /// Renders the crate graph to GraphViz "dot" syntax. 296 /// Renders the crate graph to GraphViz "dot" syntax.
292 pub fn view_crate_graph(&self) -> Cancelable<Result<String, String>> { 297 pub fn view_crate_graph(&self) -> Cancelable<Result<String, String>> {
293 self.with_db(|db| view_crate_graph::view_crate_graph(&db)) 298 self.with_db(|db| view_crate_graph::view_crate_graph(&db))
diff --git a/crates/ide/src/view_item_tree.rs b/crates/ide/src/view_item_tree.rs
new file mode 100644
index 000000000..3dc03085d
--- /dev/null
+++ b/crates/ide/src/view_item_tree.rs
@@ -0,0 +1,16 @@
1use hir::db::DefDatabase;
2use ide_db::base_db::FileId;
3use ide_db::RootDatabase;
4
5// Feature: Debug ItemTree
6//
7// Displays the ItemTree of the currently open file, for debugging.
8//
9// |===
10// | Editor | Action Name
11//
12// | VS Code | **Rust Analyzer: Debug ItemTree**
13// |===
14pub(crate) fn view_item_tree(db: &RootDatabase, file_id: FileId) -> String {
15 db.file_item_tree(file_id.into()).pretty_print()
16}
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs
index 51041d7a0..aa12fd94b 100644
--- a/crates/rust-analyzer/src/handlers.rs
+++ b/crates/rust-analyzer/src/handlers.rs
@@ -117,6 +117,16 @@ pub(crate) fn handle_view_hir(
117 Ok(res) 117 Ok(res)
118} 118}
119 119
120pub(crate) fn handle_view_item_tree(
121 snap: GlobalStateSnapshot,
122 params: lsp_ext::ViewItemTreeParams,
123) -> Result<String> {
124 let _p = profile::span("handle_view_item_tree");
125 let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
126 let res = snap.analysis.view_item_tree(file_id)?;
127 Ok(res)
128}
129
120pub(crate) fn handle_view_crate_graph(snap: GlobalStateSnapshot, (): ()) -> Result<String> { 130pub(crate) fn handle_view_crate_graph(snap: GlobalStateSnapshot, (): ()) -> Result<String> {
121 let _p = profile::span("handle_view_crate_graph"); 131 let _p = profile::span("handle_view_crate_graph");
122 let dot = snap.analysis.view_crate_graph()??; 132 let dot = snap.analysis.view_crate_graph()??;
diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs
index 34b53a7a8..905048793 100644
--- a/crates/rust-analyzer/src/lsp_ext.rs
+++ b/crates/rust-analyzer/src/lsp_ext.rs
@@ -70,6 +70,20 @@ impl Request for ViewCrateGraph {
70 const METHOD: &'static str = "rust-analyzer/viewCrateGraph"; 70 const METHOD: &'static str = "rust-analyzer/viewCrateGraph";
71} 71}
72 72
73#[derive(Deserialize, Serialize, Debug)]
74#[serde(rename_all = "camelCase")]
75pub struct ViewItemTreeParams {
76 pub text_document: TextDocumentIdentifier,
77}
78
79pub enum ViewItemTree {}
80
81impl Request for ViewItemTree {
82 type Params = ViewItemTreeParams;
83 type Result = String;
84 const METHOD: &'static str = "rust-analyzer/viewItemTree";
85}
86
73pub enum ExpandMacro {} 87pub enum ExpandMacro {}
74 88
75impl Request for ExpandMacro { 89impl Request for ExpandMacro {
diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs
index 4e0791611..f837b89dd 100644
--- a/crates/rust-analyzer/src/main_loop.rs
+++ b/crates/rust-analyzer/src/main_loop.rs
@@ -514,6 +514,7 @@ impl GlobalState {
514 .on::<lsp_ext::SyntaxTree>(handlers::handle_syntax_tree) 514 .on::<lsp_ext::SyntaxTree>(handlers::handle_syntax_tree)
515 .on::<lsp_ext::ViewHir>(handlers::handle_view_hir) 515 .on::<lsp_ext::ViewHir>(handlers::handle_view_hir)
516 .on::<lsp_ext::ViewCrateGraph>(handlers::handle_view_crate_graph) 516 .on::<lsp_ext::ViewCrateGraph>(handlers::handle_view_crate_graph)
517 .on::<lsp_ext::ViewItemTree>(handlers::handle_view_item_tree)
517 .on::<lsp_ext::ExpandMacro>(handlers::handle_expand_macro) 518 .on::<lsp_ext::ExpandMacro>(handlers::handle_expand_macro)
518 .on::<lsp_ext::ParentModule>(handlers::handle_parent_module) 519 .on::<lsp_ext::ParentModule>(handlers::handle_parent_module)
519 .on::<lsp_ext::Runnables>(handlers::handle_runnables) 520 .on::<lsp_ext::Runnables>(handlers::handle_runnables)
diff --git a/editors/code/package.json b/editors/code/package.json
index 1743b374c..17d9281ff 100644
--- a/editors/code/package.json
+++ b/editors/code/package.json
@@ -110,6 +110,11 @@
110 "category": "Rust Analyzer" 110 "category": "Rust Analyzer"
111 }, 111 },
112 { 112 {
113 "command": "rust-analyzer.viewItemTree",
114 "title": "Debug ItemTree",
115 "category": "Rust Analyzer"
116 },
117 {
113 "command": "rust-analyzer.viewCrateGraph", 118 "command": "rust-analyzer.viewCrateGraph",
114 "title": "View Crate Graph", 119 "title": "View Crate Graph",
115 "category": "Rust Analyzer" 120 "category": "Rust Analyzer"
diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts
index 8ab259af2..8f672e68d 100644
--- a/editors/code/src/commands.ts
+++ b/editors/code/src/commands.ts
@@ -429,6 +429,56 @@ export function viewHir(ctx: Ctx): Cmd {
429 }; 429 };
430} 430}
431 431
432export function viewItemTree(ctx: Ctx): Cmd {
433 const tdcp = new class implements vscode.TextDocumentContentProvider {
434 readonly uri = vscode.Uri.parse('rust-analyzer://viewItemTree/itemtree.txt');
435 readonly eventEmitter = new vscode.EventEmitter<vscode.Uri>();
436 constructor() {
437 vscode.workspace.onDidChangeTextDocument(this.onDidChangeTextDocument, this, ctx.subscriptions);
438 vscode.window.onDidChangeActiveTextEditor(this.onDidChangeActiveTextEditor, this, ctx.subscriptions);
439 }
440
441 private onDidChangeTextDocument(event: vscode.TextDocumentChangeEvent) {
442 if (isRustDocument(event.document)) {
443 // We need to order this after language server updates, but there's no API for that.
444 // Hence, good old sleep().
445 void sleep(10).then(() => this.eventEmitter.fire(this.uri));
446 }
447 }
448 private onDidChangeActiveTextEditor(editor: vscode.TextEditor | undefined) {
449 if (editor && isRustEditor(editor)) {
450 this.eventEmitter.fire(this.uri);
451 }
452 }
453
454 provideTextDocumentContent(_uri: vscode.Uri, ct: vscode.CancellationToken): vscode.ProviderResult<string> {
455 const rustEditor = ctx.activeRustEditor;
456 const client = ctx.client;
457 if (!rustEditor || !client) return '';
458
459 const params = {
460 textDocument: client.code2ProtocolConverter.asTextDocumentIdentifier(rustEditor.document),
461 };
462 return client.sendRequest(ra.viewItemTree, params, ct);
463 }
464
465 get onDidChange(): vscode.Event<vscode.Uri> {
466 return this.eventEmitter.event;
467 }
468 };
469
470 ctx.pushCleanup(vscode.workspace.registerTextDocumentContentProvider('rust-analyzer', tdcp));
471
472 return async () => {
473 const document = await vscode.workspace.openTextDocument(tdcp.uri);
474 tdcp.eventEmitter.fire(tdcp.uri);
475 void await vscode.window.showTextDocument(document, {
476 viewColumn: vscode.ViewColumn.Two,
477 preserveFocus: true
478 });
479 };
480}
481
432export function viewCrateGraph(ctx: Ctx): Cmd { 482export function viewCrateGraph(ctx: Ctx): Cmd {
433 return async () => { 483 return async () => {
434 const panel = vscode.window.createWebviewPanel("rust-analyzer.crate-graph", "rust-analyzer crate graph", vscode.ViewColumn.Two); 484 const panel = vscode.window.createWebviewPanel("rust-analyzer.crate-graph", "rust-analyzer crate graph", vscode.ViewColumn.Two);
diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts
index aa745a65c..6d5c2ea72 100644
--- a/editors/code/src/lsp_ext.ts
+++ b/editors/code/src/lsp_ext.ts
@@ -27,6 +27,12 @@ export const syntaxTree = new lc.RequestType<SyntaxTreeParams, string, void>("ru
27 27
28export const viewHir = new lc.RequestType<lc.TextDocumentPositionParams, string, void>("rust-analyzer/viewHir"); 28export const viewHir = new lc.RequestType<lc.TextDocumentPositionParams, string, void>("rust-analyzer/viewHir");
29 29
30export interface ViewItemTreeParams {
31 textDocument: lc.TextDocumentIdentifier;
32}
33
34export const viewItemTree = new lc.RequestType<ViewItemTreeParams, string, void>("rust-analyzer/viewItemTree");
35
30export const viewCrateGraph = new lc.RequestType0<string, void>("rust-analyzer/viewCrateGraph"); 36export const viewCrateGraph = new lc.RequestType0<string, void>("rust-analyzer/viewCrateGraph");
31 37
32export interface ExpandMacroParams { 38export interface ExpandMacroParams {
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts
index 516322d03..92c797d47 100644
--- a/editors/code/src/main.ts
+++ b/editors/code/src/main.ts
@@ -106,6 +106,7 @@ async function tryActivate(context: vscode.ExtensionContext) {
106 ctx.registerCommand('parentModule', commands.parentModule); 106 ctx.registerCommand('parentModule', commands.parentModule);
107 ctx.registerCommand('syntaxTree', commands.syntaxTree); 107 ctx.registerCommand('syntaxTree', commands.syntaxTree);
108 ctx.registerCommand('viewHir', commands.viewHir); 108 ctx.registerCommand('viewHir', commands.viewHir);
109 ctx.registerCommand('viewItemTree', commands.viewItemTree);
109 ctx.registerCommand('viewCrateGraph', commands.viewCrateGraph); 110 ctx.registerCommand('viewCrateGraph', commands.viewCrateGraph);
110 ctx.registerCommand('expandMacro', commands.expandMacro); 111 ctx.registerCommand('expandMacro', commands.expandMacro);
111 ctx.registerCommand('run', commands.run); 112 ctx.registerCommand('run', commands.run);