From ac52d9a1f1a94e2c836c8a04a316f6454936a79a Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Sun, 3 Mar 2019 12:02:55 +0200 Subject: Add optional range parameter to SyntaxTreeParams When range is provided, instead of showing the syntax for the whole file, we'll show the syntax tree for the given range. --- crates/ra_ide_api/src/lib.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'crates/ra_ide_api/src/lib.rs') diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index 6546d0644..3e7cfbb54 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -38,7 +38,7 @@ mod marks; use std::sync::Arc; -use ra_syntax::{SourceFile, TreeArc, TextRange, TextUnit, AstNode}; +use ra_syntax::{SourceFile, TreeArc, TextRange, TextUnit, AstNode, algo}; use ra_text_edit::TextEdit; use ra_db::{ SourceDatabase, CheckCanceled, @@ -245,8 +245,14 @@ impl Analysis { /// Returns a syntax tree represented as `String`, for debug purposes. // FIXME: use a better name here. - pub fn syntax_tree(&self, file_id: FileId) -> String { - self.db.parse(file_id).syntax().debug_dump() + pub fn syntax_tree(&self, file_id: FileId, text_range: Option) -> String { + if let Some(text_range) = text_range { + let file = self.db.parse(file_id); + let node = algo::find_covering_node(file.syntax(), text_range); + node.debug_dump() + } else { + self.db.parse(file_id).syntax().debug_dump() + } } /// Returns an edit to remove all newlines in the range, cleaning up minor -- cgit v1.2.3 From 16ecd276f036de9b5dccdbcce55b25a2a5699385 Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Mon, 4 Mar 2019 08:54:54 +0200 Subject: Implement syntax tree support for syntax inside string This allows us to select a string or portions of it and try parsing it as rust syntax. This is mostly helpful when developing tests where the test itself contains some rust syntax as a string. --- crates/ra_ide_api/src/lib.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'crates/ra_ide_api/src/lib.rs') diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index 3e7cfbb54..b8a4adbce 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -32,13 +32,14 @@ mod references; mod impls; mod assists; mod diagnostics; +mod syntax_tree; #[cfg(test)] mod marks; use std::sync::Arc; -use ra_syntax::{SourceFile, TreeArc, TextRange, TextUnit, AstNode, algo}; +use ra_syntax::{SourceFile, TreeArc, TextRange, TextUnit}; use ra_text_edit::TextEdit; use ra_db::{ SourceDatabase, CheckCanceled, @@ -246,13 +247,7 @@ impl Analysis { /// Returns a syntax tree represented as `String`, for debug purposes. // FIXME: use a better name here. pub fn syntax_tree(&self, file_id: FileId, text_range: Option) -> String { - if let Some(text_range) = text_range { - let file = self.db.parse(file_id); - let node = algo::find_covering_node(file.syntax(), text_range); - node.debug_dump() - } else { - self.db.parse(file_id).syntax().debug_dump() - } + syntax_tree::syntax_tree(&self.db, file_id, text_range) } /// Returns an edit to remove all newlines in the range, cleaning up minor -- cgit v1.2.3