Use simpler logic on original_range

This commit is contained in:
Edwin Cheng 2019-12-15 02:34:16 +08:00
parent b53587c7bd
commit 3ba4b3c554
2 changed files with 41 additions and 51 deletions

View File

@ -7,14 +7,10 @@ use ra_syntax::{
ast::{self, DocCommentsOwner, NameOwner},
match_ast, AstNode, SmolStr,
SyntaxKind::{self, BIND_PAT, TYPE_PARAM},
SyntaxNode, TextRange,
TextRange,
};
use crate::{
db::RootDatabase,
expand::{original_range_by_kind, OriginalRangeKind},
FileRange, FileSymbol,
};
use crate::{db::RootDatabase, expand::original_range, FileSymbol};
use super::short_label::ShortLabel;
@ -420,14 +416,3 @@ pub(crate) fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) ->
}
}
}
fn original_range(db: &RootDatabase, node: InFile<&SyntaxNode>) -> FileRange {
if let Some(range) = original_range_by_kind(db, node, OriginalRangeKind::CallToken) {
return range;
}
if let Some(range) = original_range_by_kind(db, node, OriginalRangeKind::WholeCall) {
return range;
}
FileRange { file_id: node.file_id.original_file(db), range: node.value.text_range() }
}

View File

@ -7,55 +7,60 @@ use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxToken, TextRange};
use crate::{db::RootDatabase, FileRange};
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum OriginalRangeKind {
/// Return range if any token is matched
#[allow(dead_code)]
Any,
/// Return range if token is inside macro_call
CallToken,
/// Return whole macro call range if matched
WholeCall,
pub(crate) fn original_range(db: &RootDatabase, node: InFile<&SyntaxNode>) -> FileRange {
if let Some((range, Origin::Call)) = original_range_and_origin(db, node) {
return range;
}
if let Some(expansion) = node.file_id.expansion_info(db) {
if let Some(call_node) = expansion.call_node() {
return FileRange {
file_id: call_node.file_id.original_file(db),
range: call_node.value.text_range(),
};
}
}
FileRange { file_id: node.file_id.original_file(db), range: node.value.text_range() }
}
pub(crate) fn original_range_by_kind(
fn original_range_and_origin(
db: &RootDatabase,
node: InFile<&SyntaxNode>,
kind: OriginalRangeKind,
) -> Option<FileRange> {
) -> Option<(FileRange, Origin)> {
let expansion = node.file_id.expansion_info(db)?;
// the input node has only one token ?
let single = node.value.first_token()? == node.value.last_token()?;
// FIXME: We should handle recurside macro expansions
let range = match kind {
OriginalRangeKind::WholeCall => expansion.call_node()?.map(|node| node.text_range()),
_ => node.value.descendants().find_map(|it| {
let first = it.first_token()?;
let last = it.last_token()?;
let (range, origin) = node.value.descendants().find_map(|it| {
let first = it.first_token()?;
let last = it.last_token()?;
if !single && first == last {
return None;
}
if !single && first == last {
return None;
}
// Try to map first and last tokens of node, and, if success, return the union range of mapped tokens
let (first, first_origin) = expansion.map_token_up(node.with_value(&first))?;
let (last, last_origin) = expansion.map_token_up(node.with_value(&last))?;
// Try to map first and last tokens of node, and, if success, return the union range of mapped tokens
let (first, first_origin) = expansion.map_token_up(node.with_value(&first))?;
let (last, last_origin) = expansion.map_token_up(node.with_value(&last))?;
if first.file_id != last.file_id
|| first_origin != last_origin
|| (kind == OriginalRangeKind::CallToken && first_origin != Origin::Call)
{
return None;
}
if first.file_id != last.file_id || first_origin != last_origin {
return None;
}
// FIXME: Add union method in TextRange
Some(first.with_value(union_range(first.value.text_range(), last.value.text_range())))
})?,
};
// FIXME: Add union method in TextRange
Some((
first.with_value(union_range(first.value.text_range(), last.value.text_range())),
first_origin,
))
})?;
return Some(FileRange { file_id: range.file_id.original_file(db), range: range.value });
return Some((
FileRange { file_id: range.file_id.original_file(db), range: range.value },
origin,
));
fn union_range(a: TextRange, b: TextRange) -> TextRange {
let start = a.start().min(b.start());