From d52ee59a712932bc381d8c690dc2f681598760fe Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 21 Jul 2019 13:28:58 +0300 Subject: [PATCH] streamline API --- crates/ra_assists/src/assist_ctx.rs | 5 +++-- crates/ra_hir/src/ty/tests.rs | 6 ++---- .../src/completion/completion_context.rs | 4 ++-- crates/ra_ide_api/src/extend_selection.rs | 6 +++--- crates/ra_ide_api/src/goto_type_definition.rs | 4 ++-- crates/ra_ide_api/src/matching_brace.rs | 6 ++++-- crates/ra_ide_api/src/typing.rs | 15 ++++++++++----- crates/ra_syntax/src/algo.rs | 17 +++++------------ crates/ra_syntax/src/lib.rs | 6 ++---- crates/ra_syntax/src/syntax_node.rs | 1 - 10 files changed, 33 insertions(+), 37 deletions(-) diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs index 4d5a76de673..a12c3ed54da 100644 --- a/crates/ra_assists/src/assist_ctx.rs +++ b/crates/ra_assists/src/assist_ctx.rs @@ -2,8 +2,9 @@ use ra_db::FileRange; use ra_fmt::{leading_indent, reindent}; use ra_syntax::{ - algo::{find_covering_element, find_node_at_offset, find_token_at_offset, TokenAtOffset}, + algo::{find_covering_element, find_node_at_offset}, AstNode, SourceFile, SyntaxElement, SyntaxNode, SyntaxToken, TextRange, TextUnit, + TokenAtOffset, }; use ra_text_edit::TextEditBuilder; @@ -105,7 +106,7 @@ pub(crate) fn build(self) -> Option { } pub(crate) fn token_at_offset(&self) -> TokenAtOffset { - find_token_at_offset(self.source_file.syntax(), self.frange.range.start()) + self.source_file.syntax().token_at_offset(self.frange.range.start()) } pub(crate) fn node_at_offset(&self) -> Option { diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 706500484f8..676711d0adb 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -3211,8 +3211,7 @@ fn foo() -> i32 { ); { let file = db.parse(pos.file_id).ok().unwrap(); - let node = - algo::find_token_at_offset(file.syntax(), pos.offset).right_biased().unwrap().parent(); + let node = file.syntax().token_at_offset(pos.offset).right_biased().unwrap().parent(); let events = db.log_executed(|| { SourceAnalyzer::new(&db, pos.file_id, &node, None); }); @@ -3232,8 +3231,7 @@ fn foo() -> i32 { { let file = db.parse(pos.file_id).ok().unwrap(); - let node = - algo::find_token_at_offset(file.syntax(), pos.offset).right_biased().unwrap().parent(); + let node = file.syntax().token_at_offset(pos.offset).right_biased().unwrap().parent(); let events = db.log_executed(|| { SourceAnalyzer::new(&db, pos.file_id, &node, None); }); diff --git a/crates/ra_ide_api/src/completion/completion_context.rs b/crates/ra_ide_api/src/completion/completion_context.rs index 2f78d5409c6..968f5694bfa 100644 --- a/crates/ra_ide_api/src/completion/completion_context.rs +++ b/crates/ra_ide_api/src/completion/completion_context.rs @@ -1,6 +1,6 @@ use hir::source_binder; use ra_syntax::{ - algo::{find_covering_element, find_node_at_offset, find_token_at_offset}, + algo::{find_covering_element, find_node_at_offset}, ast, AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxNode, SyntaxToken, TextRange, TextUnit, @@ -48,7 +48,7 @@ pub(super) fn new( ) -> Option> { let module = source_binder::module_from_position(db, position); let token = - find_token_at_offset(original_parse.tree().syntax(), position.offset).left_biased()?; + original_parse.tree().syntax().token_at_offset(position.offset).left_biased()?; let analyzer = hir::SourceAnalyzer::new(db, position.file_id, &token.parent(), Some(position.offset)); let mut ctx = CompletionContext { diff --git a/crates/ra_ide_api/src/extend_selection.rs b/crates/ra_ide_api/src/extend_selection.rs index f78c562afa0..edbf622c118 100644 --- a/crates/ra_ide_api/src/extend_selection.rs +++ b/crates/ra_ide_api/src/extend_selection.rs @@ -1,10 +1,10 @@ use ra_db::SourceDatabase; use ra_syntax::{ - algo::{find_covering_element, find_token_at_offset, TokenAtOffset}, + algo::find_covering_element, ast::{self, AstNode, AstToken}, Direction, NodeOrToken, SyntaxKind::*, - SyntaxNode, SyntaxToken, TextRange, TextUnit, T, + SyntaxNode, SyntaxToken, TextRange, TextUnit, TokenAtOffset, T, }; use crate::{db::RootDatabase, FileRange}; @@ -34,7 +34,7 @@ fn try_extend_selection(root: &SyntaxNode, range: TextRange) -> Option Option>> { let parse = db.parse(position.file_id); - let node = find_token_at_offset(parse.tree().syntax(), position.offset).find_map(|token| { + let node = parse.tree().syntax().token_at_offset(position.offset).find_map(|token| { token .parent() .ancestors() diff --git a/crates/ra_ide_api/src/matching_brace.rs b/crates/ra_ide_api/src/matching_brace.rs index 1e2fac8485a..e802d01e4d5 100644 --- a/crates/ra_ide_api/src/matching_brace.rs +++ b/crates/ra_ide_api/src/matching_brace.rs @@ -1,9 +1,11 @@ -use ra_syntax::{algo::find_token_at_offset, ast::AstNode, SourceFile, SyntaxKind, TextUnit, T}; +use ra_syntax::{ast::AstNode, SourceFile, SyntaxKind, TextUnit, T}; pub fn matching_brace(file: &SourceFile, offset: TextUnit) -> Option { const BRACES: &[SyntaxKind] = &[T!['{'], T!['}'], T!['['], T![']'], T!['('], T![')'], T![<], T![>]]; - let (brace_node, brace_idx) = find_token_at_offset(file.syntax(), offset) + let (brace_node, brace_idx) = file + .syntax() + .token_at_offset(offset) .filter_map(|node| { let idx = BRACES.iter().position(|&brace| brace == node.kind())?; Some((node, idx)) diff --git a/crates/ra_ide_api/src/typing.rs b/crates/ra_ide_api/src/typing.rs index 5a1cbcc4983..6b3fd5904e6 100644 --- a/crates/ra_ide_api/src/typing.rs +++ b/crates/ra_ide_api/src/typing.rs @@ -1,11 +1,11 @@ use ra_db::{FilePosition, SourceDatabase}; use ra_fmt::leading_indent; use ra_syntax::{ - algo::{find_node_at_offset, find_token_at_offset, TokenAtOffset}, + algo::find_node_at_offset, ast::{self, AstToken}, AstNode, SmolStr, SourceFile, SyntaxKind::*, - SyntaxToken, TextRange, TextUnit, + SyntaxToken, TextRange, TextUnit, TokenAtOffset, }; use ra_text_edit::{TextEdit, TextEditBuilder}; @@ -14,7 +14,9 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option { let parse = db.parse(position.file_id); let file = parse.tree(); - let comment = find_token_at_offset(file.syntax(), position.offset) + let comment = file + .syntax() + .token_at_offset(position.offset) .left_biased() .and_then(ast::Comment::cast)?; @@ -45,7 +47,7 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option Option { - let ws = match find_token_at_offset(file.syntax(), token.text_range().start()) { + let ws = match file.syntax().token_at_offset(token.text_range().start()) { TokenAtOffset::Between(l, r) => { assert!(r == *token); l @@ -91,7 +93,10 @@ pub(crate) fn on_dot_typed(db: &RootDatabase, position: FilePosition) -> Option< let parse = db.parse(position.file_id); assert_eq!(parse.tree().syntax().text().char_at(position.offset), Some('.')); - let whitespace = find_token_at_offset(parse.tree().syntax(), position.offset) + let whitespace = parse + .tree() + .syntax() + .token_at_offset(position.offset) .left_biased() .and_then(ast::Whitespace::cast)?; diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs index ecd42c13375..45f624810dd 100644 --- a/crates/ra_syntax/src/algo.rs +++ b/crates/ra_syntax/src/algo.rs @@ -5,16 +5,9 @@ use itertools::Itertools; use crate::{ - AstNode, Direction, NodeOrToken, SourceFile, SyntaxElement, SyntaxNode, SyntaxNodePtr, - SyntaxToken, TextRange, TextUnit, + AstNode, Direction, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxNodePtr, TextRange, TextUnit, }; -pub use rowan::TokenAtOffset; - -pub fn find_token_at_offset(node: &SyntaxNode, offset: TextUnit) -> TokenAtOffset { - node.token_at_offset(offset) -} - /// Returns ancestors of the node at the offset, sorted by length. This should /// do the right thing at an edge, e.g. when searching for expressions at `{ /// <|>foo }` we will get the name reference instead of the whole block, which @@ -24,7 +17,7 @@ pub fn ancestors_at_offset( node: &SyntaxNode, offset: TextUnit, ) -> impl Iterator { - find_token_at_offset(node, offset) + node.token_at_offset(offset) .map(|token| token.parent().ancestors()) .kmerge_by(|node1, node2| node1.text_range().len() < node2.text_range().len()) } @@ -137,14 +130,14 @@ fn with_children( let len = new_children.iter().map(|it| it.text_len()).sum::(); let new_node = rowan::GreenNode::new(rowan::cursor::SyntaxKind(parent.kind() as u16), new_children); - let new_file_node = parent.replace_with(new_node); - let file = SourceFile::new(new_file_node); + let new_root_node = parent.replace_with(new_node); + let new_root_node = SyntaxNode::new_root(new_root_node); // FIXME: use a more elegant way to re-fetch the node (#1185), make // `range` private afterwards let mut ptr = SyntaxNodePtr::new(parent); ptr.range = TextRange::offset_len(ptr.range().start(), len); - ptr.to_node(file.syntax()).to_owned() + ptr.to_node(&new_root_node) } fn position_of_child(parent: &SyntaxNode, child: SyntaxElement) -> usize { diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index 7b778f38c5c..d02078256c5 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs @@ -44,12 +44,10 @@ syntax_error::{Location, SyntaxError, SyntaxErrorKind}, syntax_node::{ Direction, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxToken, SyntaxTreeBuilder, - WalkEvent, }, }; -pub use ra_parser::SyntaxKind; -pub use ra_parser::T; -pub use rowan::{SmolStr, SyntaxText, TextRange, TextUnit}; +pub use ra_parser::{SyntaxKind, T}; +pub use rowan::{SmolStr, SyntaxText, TextRange, TextUnit, TokenAtOffset, WalkEvent}; /// `Parse` is the result of the parsing: a syntax tree and a collection of /// errors. diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs index 95795a27af7..b2f5b8c6469 100644 --- a/crates/ra_syntax/src/syntax_node.rs +++ b/crates/ra_syntax/src/syntax_node.rs @@ -14,7 +14,6 @@ Parse, SmolStr, SyntaxKind, TextUnit, }; -pub use rowan::WalkEvent; pub(crate) use rowan::{GreenNode, GreenToken}; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]