aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock7
-rw-r--r--crates/ide/Cargo.toml1
-rw-r--r--crates/ide/src/lib.rs5
-rw-r--r--crates/ide/src/view_crate_graph.rs111
-rw-r--r--crates/rust-analyzer/src/handlers.rs6
-rw-r--r--crates/rust-analyzer/src/lsp_ext.rs8
-rw-r--r--crates/rust-analyzer/src/main_loop.rs1
-rw-r--r--docs/dev/lsp-extensions.md12
-rw-r--r--editors/code/package.json5
-rw-r--r--editors/code/src/commands.ts8
-rw-r--r--editors/code/src/lsp_ext.ts2
-rw-r--r--editors/code/src/main.ts1
12 files changed, 166 insertions, 1 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 0e1234b72..f9c34547e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -320,6 +320,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
320checksum = "fc4b29f4b9bb94bf267d57269fd0706d343a160937108e9619fe380645428abb" 320checksum = "fc4b29f4b9bb94bf267d57269fd0706d343a160937108e9619fe380645428abb"
321 321
322[[package]] 322[[package]]
323name = "dot"
324version = "0.1.4"
325source = "registry+https://github.com/rust-lang/crates.io-index"
326checksum = "a74b6c4d4a1cff5f454164363c16b72fa12463ca6b31f4b5f2035a65fa3d5906"
327
328[[package]]
323name = "drop_bomb" 329name = "drop_bomb"
324version = "0.1.5" 330version = "0.1.5"
325source = "registry+https://github.com/rust-lang/crates.io-index" 331source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -588,6 +594,7 @@ version = "0.0.0"
588dependencies = [ 594dependencies = [
589 "cfg", 595 "cfg",
590 "cov-mark", 596 "cov-mark",
597 "dot",
591 "either", 598 "either",
592 "expect-test", 599 "expect-test",
593 "hir", 600 "hir",
diff --git a/crates/ide/Cargo.toml b/crates/ide/Cargo.toml
index f04bcf531..88f3d09d3 100644
--- a/crates/ide/Cargo.toml
+++ b/crates/ide/Cargo.toml
@@ -20,6 +20,7 @@ oorandom = "11.1.2"
20pulldown-cmark-to-cmark = "6.0.0" 20pulldown-cmark-to-cmark = "6.0.0"
21pulldown-cmark = { version = "0.8.0", default-features = false } 21pulldown-cmark = { version = "0.8.0", default-features = false }
22url = "2.1.1" 22url = "2.1.1"
23dot = "0.1.4"
23 24
24stdx = { path = "../stdx", version = "0.0.0" } 25stdx = { path = "../stdx", version = "0.0.0" }
25syntax = { path = "../syntax", version = "0.0.0" } 26syntax = { path = "../syntax", version = "0.0.0" }
diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs
index 8e5b72044..34360501a 100644
--- a/crates/ide/src/lib.rs
+++ b/crates/ide/src/lib.rs
@@ -49,6 +49,7 @@ mod syntax_tree;
49mod typing; 49mod typing;
50mod markdown_remove; 50mod markdown_remove;
51mod doc_links; 51mod doc_links;
52mod view_crate_graph;
52 53
53use std::sync::Arc; 54use std::sync::Arc;
54 55
@@ -287,6 +288,10 @@ impl Analysis {
287 self.with_db(|db| view_hir::view_hir(&db, position)) 288 self.with_db(|db| view_hir::view_hir(&db, position))
288 } 289 }
289 290
291 pub fn view_crate_graph(&self) -> Cancelable<Result<String, String>> {
292 self.with_db(|db| view_crate_graph::view_crate_graph(&db))
293 }
294
290 pub fn expand_macro(&self, position: FilePosition) -> Cancelable<Option<ExpandedMacro>> { 295 pub fn expand_macro(&self, position: FilePosition) -> Cancelable<Option<ExpandedMacro>> {
291 self.with_db(|db| expand_macro::expand_macro(db, position)) 296 self.with_db(|db| expand_macro::expand_macro(db, position))
292 } 297 }
diff --git a/crates/ide/src/view_crate_graph.rs b/crates/ide/src/view_crate_graph.rs
new file mode 100644
index 000000000..5e4ba881e
--- /dev/null
+++ b/crates/ide/src/view_crate_graph.rs
@@ -0,0 +1,111 @@
1use std::{
2 error::Error,
3 io::{Read, Write},
4 process::{Command, Stdio},
5 sync::Arc,
6};
7
8use dot::{Id, LabelText};
9use ide_db::{
10 base_db::{CrateGraph, CrateId, Dependency, SourceDatabase, SourceDatabaseExt},
11 RootDatabase,
12};
13use rustc_hash::FxHashSet;
14
15// Feature: View Crate Graph
16//
17// Renders the currently loaded crate graph as an SVG graphic. Requires the `dot` tool, which
18// is part of graphviz, to be installed.
19//
20// Only workspace crates are included, no crates.io dependencies or sysroot crates.
21//
22// |===
23// | Editor | Action Name
24//
25// | VS Code | **Rust Analyzer: View Crate Graph**
26// |===
27pub(crate) fn view_crate_graph(db: &RootDatabase) -> Result<String, String> {
28 let crate_graph = db.crate_graph();
29 let crates_to_render = crate_graph
30 .iter()
31 .filter(|krate| {
32 // Only render workspace crates
33 let root_id = db.file_source_root(crate_graph[*krate].root_file_id);
34 !db.source_root(root_id).is_library
35 })
36 .collect();
37 let graph = DotCrateGraph { graph: crate_graph, crates_to_render };
38
39 let mut dot = Vec::new();
40 dot::render(&graph, &mut dot).unwrap();
41
42 render_svg(&dot).map_err(|e| e.to_string())
43}
44
45fn render_svg(dot: &[u8]) -> Result<String, Box<dyn Error>> {
46 // We shell out to `dot` to render to SVG, as there does not seem to be a pure-Rust renderer.
47 let child = Command::new("dot")
48 .arg("-Tsvg")
49 .stdin(Stdio::piped())
50 .stdout(Stdio::piped())
51 .spawn()
52 .map_err(|err| format!("failed to spawn `dot`: {}", err))?;
53 child.stdin.unwrap().write_all(&dot)?;
54
55 let mut svg = String::new();
56 child.stdout.unwrap().read_to_string(&mut svg)?;
57 Ok(svg)
58}
59
60struct DotCrateGraph {
61 graph: Arc<CrateGraph>,
62 crates_to_render: FxHashSet<CrateId>,
63}
64
65type Edge<'a> = (CrateId, &'a Dependency);
66
67impl<'a> dot::GraphWalk<'a, CrateId, Edge<'a>> for DotCrateGraph {
68 fn nodes(&'a self) -> dot::Nodes<'a, CrateId> {
69 self.crates_to_render.iter().copied().collect()
70 }
71
72 fn edges(&'a self) -> dot::Edges<'a, Edge<'a>> {
73 self.crates_to_render
74 .iter()
75 .flat_map(|krate| {
76 self.graph[*krate]
77 .dependencies
78 .iter()
79 .filter(|dep| self.crates_to_render.contains(&dep.crate_id))
80 .map(move |dep| (*krate, dep))
81 })
82 .collect()
83 }
84
85 fn source(&'a self, edge: &Edge<'a>) -> CrateId {
86 edge.0
87 }
88
89 fn target(&'a self, edge: &Edge<'a>) -> CrateId {
90 edge.1.crate_id
91 }
92}
93
94impl<'a> dot::Labeller<'a, CrateId, Edge<'a>> for DotCrateGraph {
95 fn graph_id(&'a self) -> Id<'a> {
96 Id::new("rust_analyzer_crate_graph").unwrap()
97 }
98
99 fn node_id(&'a self, n: &CrateId) -> Id<'a> {
100 Id::new(format!("_{}", n.0)).unwrap()
101 }
102
103 fn node_shape(&'a self, _node: &CrateId) -> Option<LabelText<'a>> {
104 Some(LabelText::LabelStr("box".into()))
105 }
106
107 fn node_label(&'a self, n: &CrateId) -> LabelText<'a> {
108 let name = self.graph[*n].display_name.as_ref().map_or("(unnamed crate)", |name| &*name);
109 LabelText::LabelStr(name.into())
110 }
111}
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs
index f6e40f872..dafbab6d0 100644
--- a/crates/rust-analyzer/src/handlers.rs
+++ b/crates/rust-analyzer/src/handlers.rs
@@ -117,6 +117,12 @@ pub(crate) fn handle_view_hir(
117 Ok(res) 117 Ok(res)
118} 118}
119 119
120pub(crate) fn handle_view_crate_graph(snap: GlobalStateSnapshot, (): ()) -> Result<String> {
121 let _p = profile::span("handle_view_crate_graph");
122 let res = snap.analysis.view_crate_graph()??;
123 Ok(res)
124}
125
120pub(crate) fn handle_expand_macro( 126pub(crate) fn handle_expand_macro(
121 snap: GlobalStateSnapshot, 127 snap: GlobalStateSnapshot,
122 params: lsp_ext::ExpandMacroParams, 128 params: lsp_ext::ExpandMacroParams,
diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs
index b8835a534..3bd098058 100644
--- a/crates/rust-analyzer/src/lsp_ext.rs
+++ b/crates/rust-analyzer/src/lsp_ext.rs
@@ -61,6 +61,14 @@ impl Request for ViewHir {
61 const METHOD: &'static str = "rust-analyzer/viewHir"; 61 const METHOD: &'static str = "rust-analyzer/viewHir";
62} 62}
63 63
64pub enum ViewCrateGraph {}
65
66impl Request for ViewCrateGraph {
67 type Params = ();
68 type Result = String;
69 const METHOD: &'static str = "rust-analyzer/viewCrateGraph";
70}
71
64pub enum ExpandMacro {} 72pub enum ExpandMacro {}
65 73
66impl Request for ExpandMacro { 74impl Request for ExpandMacro {
diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs
index ce7ece559..c7bd7eee1 100644
--- a/crates/rust-analyzer/src/main_loop.rs
+++ b/crates/rust-analyzer/src/main_loop.rs
@@ -513,6 +513,7 @@ impl GlobalState {
513 .on::<lsp_ext::AnalyzerStatus>(handlers::handle_analyzer_status) 513 .on::<lsp_ext::AnalyzerStatus>(handlers::handle_analyzer_status)
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::ExpandMacro>(handlers::handle_expand_macro) 517 .on::<lsp_ext::ExpandMacro>(handlers::handle_expand_macro)
517 .on::<lsp_ext::ParentModule>(handlers::handle_parent_module) 518 .on::<lsp_ext::ParentModule>(handlers::handle_parent_module)
518 .on::<lsp_ext::Runnables>(handlers::handle_runnables) 519 .on::<lsp_ext::Runnables>(handlers::handle_runnables)
diff --git a/docs/dev/lsp-extensions.md b/docs/dev/lsp-extensions.md
index f0f981802..8fcd72d5d 100644
--- a/docs/dev/lsp-extensions.md
+++ b/docs/dev/lsp-extensions.md
@@ -1,5 +1,5 @@
1<!--- 1<!---
2lsp_ext.rs hash: 28a9d5a24b7ca396 2lsp_ext.rs hash: 6e57fc1b345b00e9
3 3
4If you need to change the above hash to make the test pass, please check if you 4If you need to change the above hash to make the test pass, please check if you
5need to adjust this doc as well and ping this issue: 5need to adjust this doc as well and ping this issue:
@@ -486,6 +486,16 @@ Primarily for debugging, but very useful for all people working on rust-analyzer
486Returns a textual representation of the HIR of the function containing the cursor. 486Returns a textual representation of the HIR of the function containing the cursor.
487For debugging or when working on rust-analyzer itself. 487For debugging or when working on rust-analyzer itself.
488 488
489## View Crate Graph
490
491**Method:** `rust-analyzer/viewCrateGraph`
492
493**Request:** `null`
494
495**Response:** `string`
496
497Renders rust-analyzer's crate graph as an SVG image.
498
489## Expand Macro 499## Expand Macro
490 500
491**Method:** `rust-analyzer/expandMacro` 501**Method:** `rust-analyzer/expandMacro`
diff --git a/editors/code/package.json b/editors/code/package.json
index f35d30898..0f38a1673 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.viewCrateGraph",
114 "title": "View Crate Graph",
115 "category": "Rust Analyzer"
116 },
117 {
113 "command": "rust-analyzer.expandMacro", 118 "command": "rust-analyzer.expandMacro",
114 "title": "Expand macro recursively", 119 "title": "Expand macro recursively",
115 "category": "Rust Analyzer" 120 "category": "Rust Analyzer"
diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts
index 4092435db..8ab259af2 100644
--- a/editors/code/src/commands.ts
+++ b/editors/code/src/commands.ts
@@ -429,6 +429,14 @@ export function viewHir(ctx: Ctx): Cmd {
429 }; 429 };
430} 430}
431 431
432export function viewCrateGraph(ctx: Ctx): Cmd {
433 return async () => {
434 const panel = vscode.window.createWebviewPanel("rust-analyzer.crate-graph", "rust-analyzer crate graph", vscode.ViewColumn.Two);
435 const svg = await ctx.client.sendRequest(ra.viewCrateGraph);
436 panel.webview.html = svg;
437 };
438}
439
432// Opens the virtual file that will show the syntax tree 440// Opens the virtual file that will show the syntax tree
433// 441//
434// The contents of the file come from the `TextDocumentContentProvider` 442// The contents of the file come from the `TextDocumentContentProvider`
diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts
index f78de894b..aa745a65c 100644
--- a/editors/code/src/lsp_ext.ts
+++ b/editors/code/src/lsp_ext.ts
@@ -27,6 +27,8 @@ 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 const viewCrateGraph = new lc.RequestType0<string, void>("rust-analyzer/viewCrateGraph");
31
30export interface ExpandMacroParams { 32export interface ExpandMacroParams {
31 textDocument: lc.TextDocumentIdentifier; 33 textDocument: lc.TextDocumentIdentifier;
32 position: lc.Position; 34 position: lc.Position;
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts
index 643fb643f..516322d03 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('viewCrateGraph', commands.viewCrateGraph);
109 ctx.registerCommand('expandMacro', commands.expandMacro); 110 ctx.registerCommand('expandMacro', commands.expandMacro);
110 ctx.registerCommand('run', commands.run); 111 ctx.registerCommand('run', commands.run);
111 ctx.registerCommand('copyRunCommandLine', commands.copyRunCommandLine); 112 ctx.registerCommand('copyRunCommandLine', commands.copyRunCommandLine);