From 553254973e24a2c0bdf1475fccbbc4603e8421f0 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Thu, 27 Feb 2020 00:12:26 +0800 Subject: Skip trival token in original_range --- crates/ra_hir/src/semantics.rs | 12 +++++++----- crates/ra_ide/src/extend_selection.rs | 24 +++++++----------------- crates/ra_ide/src/hover.rs | 27 +++++++++++++++++++++++++++ crates/ra_syntax/src/algo.rs | 14 +++++++++++++- 4 files changed, 54 insertions(+), 23 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 0b40bf9af..5b0b94e34 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -8,8 +8,9 @@ use hir_def::{ }; use ra_db::{FileId, FileRange}; use ra_syntax::{ - algo::find_covering_element, ast, match_ast, AstNode, NodeOrToken, SyntaxElement, SyntaxNode, - SyntaxToken, TextRange, TextUnit, + algo::{find_covering_element, skip_trivia_token}, + ast, match_ast, AstNode, Direction, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxToken, + TextRange, TextUnit, }; use rustc_hash::{FxHashMap, FxHashSet}; @@ -384,11 +385,12 @@ fn original_range_and_origin( }; // the input node has only one token ? - let single = node.value.first_token()? == node.value.last_token()?; + let single = skip_trivia_token(node.value.first_token()?, Direction::Next)? + == skip_trivia_token(node.value.last_token()?, Direction::Prev)?; return Some(node.value.descendants().find_map(|it| { - let first = it.first_token()?; - let last = it.last_token()?; + let first = skip_trivia_token(it.first_token()?, Direction::Next)?; + let last = skip_trivia_token(it.last_token()?, Direction::Prev)?; if !single && first == last { return None; diff --git a/crates/ra_ide/src/extend_selection.rs b/crates/ra_ide/src/extend_selection.rs index 86e6f12d7..2e09bd1ec 100644 --- a/crates/ra_ide/src/extend_selection.rs +++ b/crates/ra_ide/src/extend_selection.rs @@ -5,7 +5,7 @@ use std::iter::successors; use hir::Semantics; use ra_ide_db::RootDatabase; use ra_syntax::{ - algo::{self, find_covering_element}, + algo::{self, find_covering_element, skip_trivia_token}, ast::{self, AstNode, AstToken}, Direction, NodeOrToken, SyntaxKind::{self, *}, @@ -118,14 +118,14 @@ fn extend_tokens_from_range( NodeOrToken::Token(it) => (it.clone(), it), }; - let mut first_token = skip_whitespace(first_token, Direction::Next)?; - let mut last_token = skip_whitespace(last_token, Direction::Prev)?; + let mut first_token = skip_trivia_token(first_token, Direction::Next)?; + let mut last_token = skip_trivia_token(last_token, Direction::Prev)?; while !first_token.text_range().is_subrange(&original_range) { - first_token = skip_whitespace(first_token.next_token()?, Direction::Next)?; + first_token = skip_trivia_token(first_token.next_token()?, Direction::Next)?; } while !last_token.text_range().is_subrange(&original_range) { - last_token = skip_whitespace(last_token.prev_token()?, Direction::Prev)?; + last_token = skip_trivia_token(last_token.prev_token()?, Direction::Prev)?; } // compute original mapped token range @@ -149,14 +149,14 @@ fn extend_tokens_from_range( // Find the first and last text range under expanded parent let first = successors(Some(first_token), |token| { let token = token.prev_token()?; - skip_whitespace(token, Direction::Prev) + skip_trivia_token(token, Direction::Prev) }) .take_while(validate) .last()?; let last = successors(Some(last_token), |token| { let token = token.next_token()?; - skip_whitespace(token, Direction::Next) + skip_trivia_token(token, Direction::Next) }) .take_while(validate) .last()?; @@ -169,16 +169,6 @@ fn extend_tokens_from_range( } } -fn skip_whitespace(mut token: SyntaxToken, direction: Direction) -> Option { - while token.kind() == WHITESPACE { - token = match direction { - Direction::Next => token.next_token()?, - Direction::Prev => token.prev_token()?, - } - } - Some(token) -} - fn union_range(range: TextRange, r: TextRange) -> TextRange { let start = range.start().min(r.start()); let end = range.end().max(r.end()); diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index 29b16e602..177038e20 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs @@ -174,6 +174,10 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Optionfoo; }; } assert_eq!(hover_on, "bar") } + #[test] + fn test_hover_through_literal_string_in_macro() { + // FIXME: Currently `hover::type_of` do not work inside + // macro expansion + check_hover_no_result( + r#" + //- /lib.rs + macro_rules! arr { + ($($tt:tt)*) => { [$($tt)*)] } + } + fn foo() { + let mastered_for_itunes = ""; + let _ = arr!("Tr<|>acks", &mastered_for_itunes); + } + "#, + ); + } + #[test] fn test_hover_non_ascii_space_doc() { check_hover_result( diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs index f14bcbb35..ebf59288a 100644 --- a/crates/ra_syntax/src/algo.rs +++ b/crates/ra_syntax/src/algo.rs @@ -7,7 +7,8 @@ use ra_text_edit::TextEditBuilder; use rustc_hash::{FxHashMap, FxHashSet}; use crate::{ - AstNode, Direction, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxNodePtr, TextRange, TextUnit, + AstNode, Direction, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxNodePtr, SyntaxToken, + TextRange, TextUnit, }; /// Returns ancestors of the node at the offset, sorted by length. This should @@ -37,6 +38,17 @@ pub fn find_node_at_offset(syntax: &SyntaxNode, offset: TextUnit) -> ancestors_at_offset(syntax, offset).find_map(N::cast) } +/// Skip to next non `trivia` token +pub fn skip_trivia_token(mut token: SyntaxToken, direction: Direction) -> Option { + while token.kind().is_trivia() { + token = match direction { + Direction::Next => token.next_token()?, + Direction::Prev => token.prev_token()?, + } + } + Some(token) +} + /// Finds the first sibling in the given direction which is not `trivia` pub fn non_trivia_sibling(element: SyntaxElement, direction: Direction) -> Option { return match element { -- cgit v1.2.3