mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 13:25:09 +00:00
Use simpler logic on original_range
This commit is contained in:
parent
b53587c7bd
commit
3ba4b3c554
2 changed files with 41 additions and 51 deletions
|
@ -7,14 +7,10 @@ use ra_syntax::{
|
||||||
ast::{self, DocCommentsOwner, NameOwner},
|
ast::{self, DocCommentsOwner, NameOwner},
|
||||||
match_ast, AstNode, SmolStr,
|
match_ast, AstNode, SmolStr,
|
||||||
SyntaxKind::{self, BIND_PAT, TYPE_PARAM},
|
SyntaxKind::{self, BIND_PAT, TYPE_PARAM},
|
||||||
SyntaxNode, TextRange,
|
TextRange,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{db::RootDatabase, expand::original_range, FileSymbol};
|
||||||
db::RootDatabase,
|
|
||||||
expand::{original_range_by_kind, OriginalRangeKind},
|
|
||||||
FileRange, FileSymbol,
|
|
||||||
};
|
|
||||||
|
|
||||||
use super::short_label::ShortLabel;
|
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() }
|
|
||||||
}
|
|
||||||
|
|
|
@ -7,55 +7,60 @@ use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxToken, TextRange};
|
||||||
|
|
||||||
use crate::{db::RootDatabase, FileRange};
|
use crate::{db::RootDatabase, FileRange};
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
pub(crate) fn original_range(db: &RootDatabase, node: InFile<&SyntaxNode>) -> FileRange {
|
||||||
pub(crate) enum OriginalRangeKind {
|
if let Some((range, Origin::Call)) = original_range_and_origin(db, node) {
|
||||||
/// Return range if any token is matched
|
return range;
|
||||||
#[allow(dead_code)]
|
}
|
||||||
Any,
|
|
||||||
/// Return range if token is inside macro_call
|
if let Some(expansion) = node.file_id.expansion_info(db) {
|
||||||
CallToken,
|
if let Some(call_node) = expansion.call_node() {
|
||||||
/// Return whole macro call range if matched
|
return FileRange {
|
||||||
WholeCall,
|
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,
|
db: &RootDatabase,
|
||||||
node: InFile<&SyntaxNode>,
|
node: InFile<&SyntaxNode>,
|
||||||
kind: OriginalRangeKind,
|
) -> Option<(FileRange, Origin)> {
|
||||||
) -> Option<FileRange> {
|
|
||||||
let expansion = node.file_id.expansion_info(db)?;
|
let expansion = node.file_id.expansion_info(db)?;
|
||||||
|
|
||||||
// the input node has only one token ?
|
// the input node has only one token ?
|
||||||
let single = node.value.first_token()? == node.value.last_token()?;
|
let single = node.value.first_token()? == node.value.last_token()?;
|
||||||
|
|
||||||
// FIXME: We should handle recurside macro expansions
|
// FIXME: We should handle recurside macro expansions
|
||||||
let range = match kind {
|
let (range, origin) = node.value.descendants().find_map(|it| {
|
||||||
OriginalRangeKind::WholeCall => expansion.call_node()?.map(|node| node.text_range()),
|
let first = it.first_token()?;
|
||||||
_ => node.value.descendants().find_map(|it| {
|
let last = it.last_token()?;
|
||||||
let first = it.first_token()?;
|
|
||||||
let last = it.last_token()?;
|
|
||||||
|
|
||||||
if !single && first == last {
|
if !single && first == last {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to map first and last tokens of node, and, if success, return the union range of mapped tokens
|
// 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 (first, first_origin) = expansion.map_token_up(node.with_value(&first))?;
|
||||||
let (last, last_origin) = expansion.map_token_up(node.with_value(&last))?;
|
let (last, last_origin) = expansion.map_token_up(node.with_value(&last))?;
|
||||||
|
|
||||||
if first.file_id != last.file_id
|
if first.file_id != last.file_id || first_origin != last_origin {
|
||||||
|| first_origin != last_origin
|
return None;
|
||||||
|| (kind == OriginalRangeKind::CallToken && first_origin != Origin::Call)
|
}
|
||||||
{
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME: Add union method in TextRange
|
// FIXME: Add union method in TextRange
|
||||||
Some(first.with_value(union_range(first.value.text_range(), last.value.text_range())))
|
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 {
|
fn union_range(a: TextRange, b: TextRange) -> TextRange {
|
||||||
let start = a.start().min(b.start());
|
let start = a.start().min(b.start());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue