Deduplicate some Inlay definitions

- Remove match conversion for InlayKind since we're using remote
This commit is contained in:
Steffen Lyngbaek 2020-03-10 18:59:49 -07:00
parent cfb48df149
commit 974ed7155a
3 changed files with 31 additions and 36 deletions

View File

@ -7,40 +7,13 @@
//! configure the server itself, feature flags are passed into analysis, and
//! tweak things like automatic insertion of `()` in completions.
use ra_ide::{InlayConfig, InlayKind};
use crate::req::InlayConfigDef;
use ra_ide::InlayConfig;
use rustc_hash::FxHashMap;
use ra_project_model::CargoFeatures;
use serde::{Deserialize, Deserializer};
#[derive(Deserialize)]
#[serde(remote = "InlayKind")]
pub enum InlayKindDef {
TypeHint,
ParameterHint,
}
// Work-around until better serde support is added
// https://github.com/serde-rs/serde/issues/723#issuecomment-382501277
fn vec_inlay_kind<'de, D>(deserializer: D) -> Result<Vec<InlayKind>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct Wrapper(#[serde(with = "InlayKindDef")] InlayKind);
let v = Vec::deserialize(deserializer)?;
Ok(v.into_iter().map(|Wrapper(a)| a).collect())
}
#[derive(Deserialize)]
#[serde(remote = "InlayConfig")]
pub struct InlayConfigDef {
#[serde(deserialize_with = "vec_inlay_kind")]
pub display_type: Vec<InlayKind>,
pub max_length: Option<usize>,
}
/// Client provided initialization options
#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "camelCase", default)]

View File

@ -37,7 +37,7 @@
},
diagnostics::DiagnosticTask,
from_json,
req::{self, Decoration, InlayHint, InlayHintsParams, InlayKind},
req::{self, Decoration, InlayHint, InlayHintsParams},
semantic_tokens::SemanticTokensBuilder,
world::WorldSnapshot,
LspError, Result,
@ -1002,10 +1002,7 @@ pub fn handle_inlay_hints(
.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::InlayKind::TypeHint => InlayKind::TypeHint,
ra_ide::InlayKind::ParameterHint => InlayKind::ParameterHint,
},
kind: api_type.kind,
})
.collect())
}

View File

@ -2,7 +2,9 @@
use lsp_types::{Location, Position, Range, TextDocumentIdentifier, Url};
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Deserializer, Serialize};
use ra_ide::{InlayConfig, InlayKind};
pub use lsp_types::{
notification::*, request::*, ApplyWorkspaceEditParams, CodeActionParams, CodeLens,
@ -196,14 +198,37 @@ pub struct InlayHintsParams {
}
#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
pub enum InlayKind {
#[serde(remote = "InlayKind")]
pub enum InlayKindDef {
TypeHint,
ParameterHint,
}
// Work-around until better serde support is added
// https://github.com/serde-rs/serde/issues/723#issuecomment-382501277
fn vec_inlay_kind<'de, D>(deserializer: D) -> Result<Vec<InlayKind>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct Wrapper(#[serde(with = "InlayKindDef")] InlayKind);
let v = Vec::deserialize(deserializer)?;
Ok(v.into_iter().map(|Wrapper(a)| a).collect())
}
#[derive(Deserialize)]
#[serde(remote = "InlayConfig")]
pub struct InlayConfigDef {
#[serde(deserialize_with = "vec_inlay_kind")]
pub display_type: Vec<InlayKind>,
pub max_length: Option<usize>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct InlayHint {
pub range: Range,
#[serde(with = "InlayKindDef")]
pub kind: InlayKind,
pub label: String,
}