mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 13:51:31 +00:00
Resolve types on the server
This commit is contained in:
parent
24784c60df
commit
09c7c86696
3 changed files with 84 additions and 79 deletions
|
@ -1,3 +1,6 @@
|
||||||
|
use crate::{db::RootDatabase, FileId};
|
||||||
|
use hir::{HirDisplay, Ty};
|
||||||
|
use ra_syntax::ast::Pat;
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
algo::visit::{visitor, Visitor},
|
algo::visit::{visitor, Visitor},
|
||||||
ast::{self, PatKind, TypeAscriptionOwner},
|
ast::{self, PatKind, TypeAscriptionOwner},
|
||||||
|
@ -15,63 +18,101 @@ pub struct InlayHint {
|
||||||
pub range: TextRange,
|
pub range: TextRange,
|
||||||
pub text: SmolStr,
|
pub text: SmolStr,
|
||||||
pub inlay_kind: InlayKind,
|
pub inlay_kind: InlayKind,
|
||||||
|
pub inlay_type_string: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn inlay_hints(file: &SourceFile) -> Vec<InlayHint> {
|
pub(crate) fn inlay_hints(db: &RootDatabase, file_id: FileId, file: &SourceFile) -> Vec<InlayHint> {
|
||||||
file.syntax().descendants().map(|node| get_inlay_hints(&node)).flatten().collect()
|
file.syntax()
|
||||||
|
.descendants()
|
||||||
|
.map(|node| get_inlay_hints(db, file_id, &node).unwrap_or_default())
|
||||||
|
.flatten()
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_inlay_hints(node: &SyntaxNode) -> Vec<InlayHint> {
|
fn get_inlay_hints(
|
||||||
|
db: &RootDatabase,
|
||||||
|
file_id: FileId,
|
||||||
|
node: &SyntaxNode,
|
||||||
|
) -> Option<Vec<InlayHint>> {
|
||||||
visitor()
|
visitor()
|
||||||
.visit(|let_statement: ast::LetStmt| {
|
.visit(|let_statement: ast::LetStmt| {
|
||||||
let let_syntax = let_statement.syntax();
|
let let_syntax = let_statement.syntax();
|
||||||
|
|
||||||
if let_statement.ascribed_type().is_some() {
|
if let_statement.ascribed_type().is_some() {
|
||||||
return Vec::new();
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let pat_range = match let_statement.pat().map(|pat| pat.kind()) {
|
let let_pat = let_statement.pat()?;
|
||||||
Some(PatKind::BindPat(bind_pat)) => bind_pat.syntax().text_range(),
|
let inlay_type_string = get_node_displayable_type(db, file_id, let_syntax, &let_pat)?
|
||||||
Some(PatKind::TuplePat(tuple_pat)) => tuple_pat.syntax().text_range(),
|
.display(db)
|
||||||
_ => return Vec::new(),
|
.to_string();;
|
||||||
|
|
||||||
|
let pat_range = match let_pat.kind() {
|
||||||
|
PatKind::BindPat(bind_pat) => bind_pat.syntax().text_range(),
|
||||||
|
PatKind::TuplePat(tuple_pat) => tuple_pat.syntax().text_range(),
|
||||||
|
_ => return None,
|
||||||
};
|
};
|
||||||
|
|
||||||
vec![InlayHint {
|
Some(vec![InlayHint {
|
||||||
range: pat_range,
|
range: pat_range,
|
||||||
text: let_syntax.text().to_smol_string(),
|
text: let_syntax.text().to_smol_string(),
|
||||||
inlay_kind: InlayKind::LetBinding,
|
inlay_kind: InlayKind::LetBinding,
|
||||||
}]
|
inlay_type_string,
|
||||||
|
}])
|
||||||
})
|
})
|
||||||
.visit(|closure_parameter: ast::LambdaExpr| {
|
.visit(|closure_parameter: ast::LambdaExpr| match closure_parameter.param_list() {
|
||||||
if let Some(param_list) = closure_parameter.param_list() {
|
Some(param_list) => Some(
|
||||||
param_list
|
param_list
|
||||||
.params()
|
.params()
|
||||||
.filter(|closure_param| closure_param.ascribed_type().is_none())
|
.filter(|closure_param| closure_param.ascribed_type().is_none())
|
||||||
.map(|closure_param| {
|
.filter_map(|closure_param| {
|
||||||
let closure_param_syntax = closure_param.syntax();
|
let closure_param_syntax = closure_param.syntax();
|
||||||
InlayHint {
|
let inlay_type_string = get_node_displayable_type(
|
||||||
|
db,
|
||||||
|
file_id,
|
||||||
|
closure_param_syntax,
|
||||||
|
&closure_param.pat()?,
|
||||||
|
)?
|
||||||
|
.display(db)
|
||||||
|
.to_string();
|
||||||
|
Some(InlayHint {
|
||||||
range: closure_param_syntax.text_range(),
|
range: closure_param_syntax.text_range(),
|
||||||
text: closure_param_syntax.text().to_smol_string(),
|
text: closure_param_syntax.text().to_smol_string(),
|
||||||
inlay_kind: InlayKind::ClosureParameter,
|
inlay_kind: InlayKind::ClosureParameter,
|
||||||
}
|
inlay_type_string,
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.collect()
|
.collect(),
|
||||||
} else {
|
),
|
||||||
Vec::new()
|
None => None,
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.accept(&node)
|
.accept(&node)?
|
||||||
.unwrap_or_default()
|
}
|
||||||
|
|
||||||
|
fn get_node_displayable_type(
|
||||||
|
db: &RootDatabase,
|
||||||
|
file_id: FileId,
|
||||||
|
node_syntax: &SyntaxNode,
|
||||||
|
node_pat: &Pat,
|
||||||
|
) -> Option<Ty> {
|
||||||
|
let analyzer = hir::SourceAnalyzer::new(db, file_id, node_syntax, None);
|
||||||
|
analyzer.type_of_pat(db, node_pat).and_then(|resolved_type| {
|
||||||
|
if let Ty::Apply(_) = resolved_type {
|
||||||
|
Some(resolved_type)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use crate::mock_analysis::single_file;
|
||||||
use insta::assert_debug_snapshot_matches;
|
use insta::assert_debug_snapshot_matches;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_inlay_hints() {
|
fn test_inlay_hints() {
|
||||||
let file = SourceFile::parse(
|
let (analysis, file_id) = single_file(
|
||||||
r#"
|
r#"
|
||||||
struct OuterStruct {}
|
struct OuterStruct {}
|
||||||
|
|
||||||
|
@ -99,66 +140,45 @@ fn main() {
|
||||||
let (x, c) = (42, 'a');
|
let (x, c) = (42, 'a');
|
||||||
let test = (42, 'a');
|
let test = (42, 'a');
|
||||||
}
|
}
|
||||||
|
|
||||||
"#,
|
"#,
|
||||||
)
|
);
|
||||||
.ok()
|
|
||||||
.unwrap();
|
assert_debug_snapshot_matches!(analysis.inlay_hints(file_id).unwrap(), @r#"[
|
||||||
assert_debug_snapshot_matches!(inlay_hints(&file), @r#"[
|
|
||||||
InlayHint {
|
InlayHint {
|
||||||
range: [71; 75),
|
range: [71; 75),
|
||||||
text: "let test = 54;",
|
text: "let test = 54;",
|
||||||
inlay_kind: LetBinding,
|
inlay_kind: LetBinding,
|
||||||
},
|
inlay_type_string: "i32",
|
||||||
InlayHint {
|
|
||||||
range: [90; 94),
|
|
||||||
text: "let test = InnerStruct {};",
|
|
||||||
inlay_kind: LetBinding,
|
|
||||||
},
|
},
|
||||||
InlayHint {
|
InlayHint {
|
||||||
range: [121; 125),
|
range: [121; 125),
|
||||||
text: "let test = OuterStruct {};",
|
text: "let test = OuterStruct {};",
|
||||||
inlay_kind: LetBinding,
|
inlay_kind: LetBinding,
|
||||||
},
|
inlay_type_string: "OuterStruct",
|
||||||
InlayHint {
|
|
||||||
range: [152; 156),
|
|
||||||
text: "let test = vec![222];",
|
|
||||||
inlay_kind: LetBinding,
|
|
||||||
},
|
|
||||||
InlayHint {
|
|
||||||
range: [178; 186),
|
|
||||||
text: "let mut test = Vec::new();",
|
|
||||||
inlay_kind: LetBinding,
|
|
||||||
},
|
|
||||||
InlayHint {
|
|
||||||
range: [229; 233),
|
|
||||||
text: "let test = test.into_iter().map(|i| i * i).collect::<Vec<_>>();",
|
|
||||||
inlay_kind: LetBinding,
|
|
||||||
},
|
|
||||||
InlayHint {
|
|
||||||
range: [258; 259),
|
|
||||||
text: "i",
|
|
||||||
inlay_kind: ClosureParameter,
|
|
||||||
},
|
},
|
||||||
InlayHint {
|
InlayHint {
|
||||||
range: [297; 305),
|
range: [297; 305),
|
||||||
text: "let mut test = 33;",
|
text: "let mut test = 33;",
|
||||||
inlay_kind: LetBinding,
|
inlay_kind: LetBinding,
|
||||||
|
inlay_type_string: "i32",
|
||||||
},
|
},
|
||||||
InlayHint {
|
InlayHint {
|
||||||
range: [417; 426),
|
range: [417; 426),
|
||||||
text: "let i_squared = i * i;",
|
text: "let i_squared = i * i;",
|
||||||
inlay_kind: LetBinding,
|
inlay_kind: LetBinding,
|
||||||
|
inlay_type_string: "u32",
|
||||||
},
|
},
|
||||||
InlayHint {
|
InlayHint {
|
||||||
range: [496; 502),
|
range: [496; 502),
|
||||||
text: "let (x, c) = (42, \'a\');",
|
text: "let (x, c) = (42, \'a\');",
|
||||||
inlay_kind: LetBinding,
|
inlay_kind: LetBinding,
|
||||||
|
inlay_type_string: "(i32, char)",
|
||||||
},
|
},
|
||||||
InlayHint {
|
InlayHint {
|
||||||
range: [524; 528),
|
range: [524; 528),
|
||||||
text: "let test = (42, \'a\');",
|
text: "let test = (42, \'a\');",
|
||||||
inlay_kind: LetBinding,
|
inlay_kind: LetBinding,
|
||||||
|
inlay_type_string: "(i32, char)",
|
||||||
},
|
},
|
||||||
]"#
|
]"#
|
||||||
);
|
);
|
||||||
|
|
|
@ -400,7 +400,7 @@ impl Analysis {
|
||||||
|
|
||||||
/// Returns a list of the places in the file where type hints can be displayed.
|
/// Returns a list of the places in the file where type hints can be displayed.
|
||||||
pub fn inlay_hints(&self, file_id: FileId) -> Cancelable<Vec<InlayHint>> {
|
pub fn inlay_hints(&self, file_id: FileId) -> Cancelable<Vec<InlayHint>> {
|
||||||
self.with_db(|db| inlay_hints::inlay_hints(&db.parse(file_id).tree()))
|
self.with_db(|db| inlay_hints::inlay_hints(db, file_id, &db.parse(file_id).tree()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the set of folding ranges.
|
/// Returns the set of folding ranges.
|
||||||
|
|
|
@ -9,8 +9,7 @@ use lsp_types::{
|
||||||
TextDocumentIdentifier, TextEdit, WorkspaceEdit,
|
TextDocumentIdentifier, TextEdit, WorkspaceEdit,
|
||||||
};
|
};
|
||||||
use ra_ide_api::{
|
use ra_ide_api::{
|
||||||
AssistId, Cancelable, FileId, FilePosition, FileRange, FoldKind, InlayKind, Query,
|
AssistId, Cancelable, FileId, FilePosition, FileRange, FoldKind, Query, RunnableKind, Severity,
|
||||||
RunnableKind, Severity,
|
|
||||||
};
|
};
|
||||||
use ra_prof::profile;
|
use ra_prof::profile;
|
||||||
use ra_syntax::{AstNode, SyntaxKind, TextRange, TextUnit};
|
use ra_syntax::{AstNode, SyntaxKind, TextRange, TextUnit};
|
||||||
|
@ -750,29 +749,15 @@ pub fn handle_code_lens(
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
lenses.extend(
|
lenses.extend(analysis.inlay_hints(file_id)?.into_iter().map(|inlay_hint| CodeLens {
|
||||||
analysis
|
range: inlay_hint.range.conv_with(&line_index),
|
||||||
.inlay_hints(file_id)
|
command: Some(Command {
|
||||||
.into_iter()
|
title: inlay_hint.inlay_type_string,
|
||||||
.filter(|hint| hint.inlay_kind == InlayKind::LetBinding)
|
command: String::new(),
|
||||||
.filter_map(|inlay_hint| {
|
arguments: None,
|
||||||
let resolved_type = analysis
|
}),
|
||||||
.type_of(FileRange { range: inlay_hint.range, file_id })
|
data: None,
|
||||||
.ok()
|
}));
|
||||||
.and_then(std::convert::identity)
|
|
||||||
.filter(|resolved_type| "{unknown}" != resolved_type);
|
|
||||||
resolved_type.map(|resolved_type| (resolved_type, inlay_hint.range))
|
|
||||||
})
|
|
||||||
.map(|(resolved_type, range)| CodeLens {
|
|
||||||
range: range.conv_with(&line_index),
|
|
||||||
command: Some(Command {
|
|
||||||
title: resolved_type,
|
|
||||||
command: String::new(),
|
|
||||||
arguments: None,
|
|
||||||
}),
|
|
||||||
data: None,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
Ok(Some(lenses))
|
Ok(Some(lenses))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue