mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
Simplify
This commit is contained in:
parent
511ae17d07
commit
066bc4f3a4
3 changed files with 24 additions and 28 deletions
|
@ -6,15 +6,11 @@ use ide_db::{
|
||||||
search::{FileReference, ReferenceAccess, SearchScope},
|
search::{FileReference, ReferenceAccess, SearchScope},
|
||||||
RootDatabase,
|
RootDatabase,
|
||||||
};
|
};
|
||||||
use syntax::{
|
use syntax::{ast, match_ast, AstNode, SyntaxNode, SyntaxToken, TextRange, WalkEvent, T};
|
||||||
ast, match_ast, AstNode,
|
|
||||||
SyntaxKind::{ASYNC_KW, AWAIT_KW, QUESTION, RETURN_KW, THIN_ARROW},
|
|
||||||
SyntaxNode, SyntaxToken, TextRange, WalkEvent,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{display::TryToNav, references, NavigationTarget};
|
use crate::{display::TryToNav, references, NavigationTarget};
|
||||||
|
|
||||||
pub struct DocumentHighlight {
|
pub struct HighlightedRange {
|
||||||
pub range: TextRange,
|
pub range: TextRange,
|
||||||
pub access: Option<ReferenceAccess>,
|
pub access: Option<ReferenceAccess>,
|
||||||
}
|
}
|
||||||
|
@ -28,19 +24,19 @@ pub struct DocumentHighlight {
|
||||||
pub(crate) fn highlight_related(
|
pub(crate) fn highlight_related(
|
||||||
sema: &Semantics<RootDatabase>,
|
sema: &Semantics<RootDatabase>,
|
||||||
position: FilePosition,
|
position: FilePosition,
|
||||||
) -> Option<Vec<DocumentHighlight>> {
|
) -> Option<Vec<HighlightedRange>> {
|
||||||
let _p = profile::span("document_highlight");
|
let _p = profile::span("highlight_related");
|
||||||
let syntax = sema.parse(position.file_id).syntax().clone();
|
let syntax = sema.parse(position.file_id).syntax().clone();
|
||||||
|
|
||||||
let token = pick_best_token(syntax.token_at_offset(position.offset), |kind| match kind {
|
let token = pick_best_token(syntax.token_at_offset(position.offset), |kind| match kind {
|
||||||
QUESTION => 2, // prefer `?` when the cursor is sandwiched like `await$0?`
|
T![?] => 2, // prefer `?` when the cursor is sandwiched like `await$0?`
|
||||||
AWAIT_KW | ASYNC_KW | THIN_ARROW | RETURN_KW => 1,
|
T![await] | T![async] | T![->] | T![return] => 1,
|
||||||
_ => 0,
|
_ => 0,
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
match token.kind() {
|
match token.kind() {
|
||||||
QUESTION | RETURN_KW | THIN_ARROW => highlight_exit_points(sema, token),
|
T![?] | T![return] | T![->] => highlight_exit_points(sema, token),
|
||||||
AWAIT_KW | ASYNC_KW => highlight_yield_points(token),
|
T![await] | T![async] => highlight_yield_points(token),
|
||||||
_ => highlight_references(sema, &syntax, position),
|
_ => highlight_references(sema, &syntax, position),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,7 +45,7 @@ fn highlight_references(
|
||||||
sema: &Semantics<RootDatabase>,
|
sema: &Semantics<RootDatabase>,
|
||||||
syntax: &SyntaxNode,
|
syntax: &SyntaxNode,
|
||||||
FilePosition { offset, file_id }: FilePosition,
|
FilePosition { offset, file_id }: FilePosition,
|
||||||
) -> Option<Vec<DocumentHighlight>> {
|
) -> Option<Vec<HighlightedRange>> {
|
||||||
let def = references::find_def(sema, syntax, offset)?;
|
let def = references::find_def(sema, syntax, offset)?;
|
||||||
let usages = def.usages(sema).set_scope(Some(SearchScope::single_file(file_id))).all();
|
let usages = def.usages(sema).set_scope(Some(SearchScope::single_file(file_id))).all();
|
||||||
|
|
||||||
|
@ -63,7 +59,7 @@ fn highlight_references(
|
||||||
.and_then(|decl| {
|
.and_then(|decl| {
|
||||||
let range = decl.focus_range?;
|
let range = decl.focus_range?;
|
||||||
let access = references::decl_access(&def, syntax, range);
|
let access = references::decl_access(&def, syntax, range);
|
||||||
Some(DocumentHighlight { range, access })
|
Some(HighlightedRange { range, access })
|
||||||
});
|
});
|
||||||
|
|
||||||
let file_refs = usages.references.get(&file_id).map_or(&[][..], Vec::as_slice);
|
let file_refs = usages.references.get(&file_id).map_or(&[][..], Vec::as_slice);
|
||||||
|
@ -72,7 +68,7 @@ fn highlight_references(
|
||||||
res.extend(
|
res.extend(
|
||||||
file_refs
|
file_refs
|
||||||
.iter()
|
.iter()
|
||||||
.map(|&FileReference { access, range, .. }| DocumentHighlight { range, access }),
|
.map(|&FileReference { access, range, .. }| HighlightedRange { range, access }),
|
||||||
);
|
);
|
||||||
Some(res)
|
Some(res)
|
||||||
}
|
}
|
||||||
|
@ -80,24 +76,24 @@ fn highlight_references(
|
||||||
fn highlight_exit_points(
|
fn highlight_exit_points(
|
||||||
sema: &Semantics<RootDatabase>,
|
sema: &Semantics<RootDatabase>,
|
||||||
token: SyntaxToken,
|
token: SyntaxToken,
|
||||||
) -> Option<Vec<DocumentHighlight>> {
|
) -> Option<Vec<HighlightedRange>> {
|
||||||
fn hl(
|
fn hl(
|
||||||
sema: &Semantics<RootDatabase>,
|
sema: &Semantics<RootDatabase>,
|
||||||
body: Option<ast::Expr>,
|
body: Option<ast::Expr>,
|
||||||
) -> Option<Vec<DocumentHighlight>> {
|
) -> Option<Vec<HighlightedRange>> {
|
||||||
let mut highlights = Vec::new();
|
let mut highlights = Vec::new();
|
||||||
let body = body?;
|
let body = body?;
|
||||||
walk(&body, |node| {
|
walk(&body, |node| {
|
||||||
match_ast! {
|
match_ast! {
|
||||||
match node {
|
match node {
|
||||||
ast::ReturnExpr(expr) => if let Some(token) = expr.return_token() {
|
ast::ReturnExpr(expr) => if let Some(token) = expr.return_token() {
|
||||||
highlights.push(DocumentHighlight {
|
highlights.push(HighlightedRange {
|
||||||
access: None,
|
access: None,
|
||||||
range: token.text_range(),
|
range: token.text_range(),
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
ast::TryExpr(try_) => if let Some(token) = try_.question_mark_token() {
|
ast::TryExpr(try_) => if let Some(token) = try_.question_mark_token() {
|
||||||
highlights.push(DocumentHighlight {
|
highlights.push(HighlightedRange {
|
||||||
access: None,
|
access: None,
|
||||||
range: token.text_range(),
|
range: token.text_range(),
|
||||||
});
|
});
|
||||||
|
@ -105,7 +101,7 @@ fn highlight_exit_points(
|
||||||
ast::Expr(expr) => match expr {
|
ast::Expr(expr) => match expr {
|
||||||
ast::Expr::MethodCallExpr(_) | ast::Expr::CallExpr(_) | ast::Expr::MacroCall(_) => {
|
ast::Expr::MethodCallExpr(_) | ast::Expr::CallExpr(_) | ast::Expr::MacroCall(_) => {
|
||||||
if sema.type_of_expr(&expr).map_or(false, |ty| ty.is_never()) {
|
if sema.type_of_expr(&expr).map_or(false, |ty| ty.is_never()) {
|
||||||
highlights.push(DocumentHighlight {
|
highlights.push(HighlightedRange {
|
||||||
access: None,
|
access: None,
|
||||||
range: expr.syntax().text_range(),
|
range: expr.syntax().text_range(),
|
||||||
});
|
});
|
||||||
|
@ -128,7 +124,7 @@ fn highlight_exit_points(
|
||||||
e => Some(e),
|
e => Some(e),
|
||||||
};
|
};
|
||||||
if let Some(tail) = tail {
|
if let Some(tail) = tail {
|
||||||
highlights.push(DocumentHighlight { access: None, range: tail.syntax().text_range() });
|
highlights.push(HighlightedRange { access: None, range: tail.syntax().text_range() });
|
||||||
}
|
}
|
||||||
Some(highlights)
|
Some(highlights)
|
||||||
}
|
}
|
||||||
|
@ -149,19 +145,19 @@ fn highlight_exit_points(
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
fn highlight_yield_points(token: SyntaxToken) -> Option<Vec<DocumentHighlight>> {
|
fn highlight_yield_points(token: SyntaxToken) -> Option<Vec<HighlightedRange>> {
|
||||||
fn hl(
|
fn hl(
|
||||||
async_token: Option<SyntaxToken>,
|
async_token: Option<SyntaxToken>,
|
||||||
body: Option<ast::Expr>,
|
body: Option<ast::Expr>,
|
||||||
) -> Option<Vec<DocumentHighlight>> {
|
) -> Option<Vec<HighlightedRange>> {
|
||||||
let mut highlights = Vec::new();
|
let mut highlights = Vec::new();
|
||||||
highlights.push(DocumentHighlight { access: None, range: async_token?.text_range() });
|
highlights.push(HighlightedRange { access: None, range: async_token?.text_range() });
|
||||||
if let Some(body) = body {
|
if let Some(body) = body {
|
||||||
walk(&body, |node| {
|
walk(&body, |node| {
|
||||||
match_ast! {
|
match_ast! {
|
||||||
match node {
|
match node {
|
||||||
ast::AwaitExpr(expr) => if let Some(token) = expr.await_token() {
|
ast::AwaitExpr(expr) => if let Some(token) = expr.await_token() {
|
||||||
highlights.push(DocumentHighlight {
|
highlights.push(HighlightedRange {
|
||||||
access: None,
|
access: None,
|
||||||
range: token.text_range(),
|
range: token.text_range(),
|
||||||
});
|
});
|
||||||
|
|
|
@ -76,7 +76,7 @@ pub use crate::{
|
||||||
expand_macro::ExpandedMacro,
|
expand_macro::ExpandedMacro,
|
||||||
file_structure::{StructureNode, StructureNodeKind},
|
file_structure::{StructureNode, StructureNodeKind},
|
||||||
folding_ranges::{Fold, FoldKind},
|
folding_ranges::{Fold, FoldKind},
|
||||||
highlight_related::DocumentHighlight,
|
highlight_related::HighlightedRange,
|
||||||
hover::{HoverAction, HoverConfig, HoverDocFormat, HoverGotoTypeData, HoverResult},
|
hover::{HoverAction, HoverConfig, HoverDocFormat, HoverGotoTypeData, HoverResult},
|
||||||
inlay_hints::{InlayHint, InlayHintsConfig, InlayKind},
|
inlay_hints::{InlayHint, InlayHintsConfig, InlayKind},
|
||||||
markup::Markup,
|
markup::Markup,
|
||||||
|
@ -489,7 +489,7 @@ impl Analysis {
|
||||||
pub fn highlight_related(
|
pub fn highlight_related(
|
||||||
&self,
|
&self,
|
||||||
position: FilePosition,
|
position: FilePosition,
|
||||||
) -> Cancellable<Option<Vec<DocumentHighlight>>> {
|
) -> Cancellable<Option<Vec<HighlightedRange>>> {
|
||||||
self.with_db(|db| highlight_related::highlight_related(&Semantics::new(db), position))
|
self.with_db(|db| highlight_related::highlight_related(&Semantics::new(db), position))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1174,7 +1174,7 @@ pub(crate) fn handle_document_highlight(
|
||||||
};
|
};
|
||||||
let res = refs
|
let res = refs
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|ide::DocumentHighlight { range, access }| lsp_types::DocumentHighlight {
|
.map(|ide::HighlightedRange { range, access }| lsp_types::DocumentHighlight {
|
||||||
range: to_proto::range(&line_index, range),
|
range: to_proto::range(&line_index, range),
|
||||||
kind: access.map(to_proto::document_highlight_kind),
|
kind: access.map(to_proto::document_highlight_kind),
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue