Code review fixes

This commit is contained in:
Kirill Bulatov 2019-07-22 21:52:47 +03:00
parent 25398ad30d
commit 8f3377d9f9
4 changed files with 70 additions and 30 deletions

View file

@ -9,16 +9,15 @@ use ra_syntax::{
#[derive(Debug, PartialEq, Eq)]
pub enum InlayKind {
LetBinding,
ClosureParameter,
LetBindingType,
ClosureParameterType,
}
#[derive(Debug)]
pub struct InlayHint {
pub range: TextRange,
pub text: SmolStr,
pub inlay_kind: InlayKind,
pub inlay_type_string: SmolStr,
pub kind: InlayKind,
pub label: SmolStr,
}
pub(crate) fn inlay_hints(db: &RootDatabase, file_id: FileId, file: &SourceFile) -> Vec<InlayHint> {
@ -56,9 +55,8 @@ fn get_inlay_hints(
Some(vec![InlayHint {
range: pat_range,
text: let_syntax.text().to_string().into(),
inlay_kind: InlayKind::LetBinding,
inlay_type_string,
kind: InlayKind::LetBindingType,
label: inlay_type_string,
}])
})
.visit(|closure_parameter: ast::LambdaExpr| match closure_parameter.param_list() {
@ -80,9 +78,8 @@ fn get_inlay_hints(
Some(InlayHint {
range: closure_param_syntax.text_range(),
text: closure_param_syntax.text().to_string().into(),
inlay_kind: InlayKind::ClosureParameter,
inlay_type_string,
kind: InlayKind::ClosureParameterType,
label: inlay_type_string,
})
})
.collect(),
@ -149,39 +146,33 @@ fn main() {
assert_debug_snapshot_matches!(analysis.inlay_hints(file_id).unwrap(), @r#"[
InlayHint {
range: [71; 75),
text: "let test = 54;",
inlay_kind: LetBinding,
inlay_type_string: "i32",
kind: LetBindingType,
label: "i32",
},
InlayHint {
range: [121; 125),
text: "let test = OuterStruct {};",
inlay_kind: LetBinding,
inlay_type_string: "OuterStruct",
kind: LetBindingType,
label: "OuterStruct",
},
InlayHint {
range: [297; 305),
text: "let mut test = 33;",
inlay_kind: LetBinding,
inlay_type_string: "i32",
kind: LetBindingType,
label: "i32",
},
InlayHint {
range: [417; 426),
text: "let i_squared = i * i;",
inlay_kind: LetBinding,
inlay_type_string: "u32",
kind: LetBindingType,
label: "u32",
},
InlayHint {
range: [496; 502),
text: "let (x, c) = (42, \'a\');",
inlay_kind: LetBinding,
inlay_type_string: "(i32, char)",
kind: LetBindingType,
label: "(i32, char)",
},
InlayHint {
range: [524; 528),
text: "let test = (42, \'a\');",
inlay_kind: LetBinding,
inlay_type_string: "(i32, char)",
kind: LetBindingType,
label: "(i32, char)",
},
]"#
);

View file

@ -362,6 +362,7 @@ fn on_request(
.on::<req::References>(handlers::handle_references)?
.on::<req::Formatting>(handlers::handle_formatting)?
.on::<req::DocumentHighlightRequest>(handlers::handle_document_highlight)?
.on::<req::InlayHints>(handlers::handle_inlay_hints)?
.finish();
Ok(())
}

View file

@ -21,7 +21,7 @@ use url_serde::Ser;
use crate::{
cargo_target_spec::{runnable_args, CargoTargetSpec},
conv::{to_location, Conv, ConvWith, MapConvWith, TryConvWith, TryConvWithToVec},
req::{self, Decoration},
req::{self, Decoration, InlayHint, InlayHintsParams, InlayKind},
world::WorldSnapshot,
LspError, Result,
};
@ -874,3 +874,24 @@ fn to_diagnostic_severity(severity: Severity) -> DiagnosticSeverity {
WeakWarning => DiagnosticSeverity::Hint,
}
}
pub fn handle_inlay_hints(
world: WorldSnapshot,
params: InlayHintsParams,
) -> Result<Vec<InlayHint>> {
let file_id = params.text_document.try_conv_with(&world)?;
let analysis = world.analysis();
let line_index = analysis.file_line_index(file_id);
Ok(analysis
.inlay_hints(file_id)?
.into_iter()
.map(|api_type| InlayHint {
label: api_type.label.to_string(),
range: api_type.range.conv_with(&line_index),
kind: match api_type.kind {
ra_ide_api::InlayKind::LetBindingType => InlayKind::LetBindingType,
ra_ide_api::InlayKind::ClosureParameterType => InlayKind::ClosureParameterType,
},
})
.collect())
}

View file

@ -196,3 +196,30 @@ pub struct SourceChange {
pub workspace_edit: WorkspaceEdit,
pub cursor_position: Option<TextDocumentPositionParams>,
}
pub enum InlayHints {}
impl Request for InlayHints {
type Params = InlayHintsParams;
type Result = Vec<InlayHint>;
const METHOD: &'static str = "rust-analyzer/inlayHints";
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct InlayHintsParams {
pub text_document: TextDocumentIdentifier,
}
#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
pub enum InlayKind {
LetBindingType,
ClosureParameterType,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct InlayHint {
pub range: Range,
pub kind: InlayKind,
pub label: String,
}