Simpler deserialization

This commit is contained in:
Aleksey Kladov 2020-03-12 18:01:36 +01:00
parent fd3ece2b73
commit b3fa7312a7
6 changed files with 38 additions and 28 deletions

View File

@ -7,8 +7,6 @@
//! configure the server itself, feature flags are passed into analysis, and
//! tweak things like automatic insertion of `()` in completions.
use crate::req::InlayConfigDef;
use ra_ide::InlayHintsOptions;
use rustc_hash::FxHashMap;
use ra_project_model::CargoFeatures;
@ -32,8 +30,11 @@ pub struct ServerConfig {
pub lru_capacity: Option<usize>,
#[serde(with = "InlayConfigDef")]
pub inlay_hints: InlayHintsOptions,
#[serde(deserialize_with = "nullable_bool_true")]
pub inlay_hints_type: bool,
#[serde(deserialize_with = "nullable_bool_true")]
pub inlay_hints_parameter: bool,
pub inlay_hints_max_length: Option<usize>,
pub cargo_watch_enable: bool,
pub cargo_watch_args: Vec<String>,
@ -63,7 +64,9 @@ fn default() -> ServerConfig {
exclude_globs: Vec::new(),
use_client_watching: false,
lru_capacity: None,
inlay_hints: Default::default(),
inlay_hints_type: true,
inlay_hints_parameter: true,
inlay_hints_max_length: None,
cargo_watch_enable: true,
cargo_watch_args: Vec::new(),
cargo_watch_command: "check".to_string(),

View File

@ -11,8 +11,8 @@
use ra_ide::{
translate_offset_with_edit, CompletionItem, CompletionItemKind, FileId, FilePosition,
FileRange, FileSystemEdit, Fold, FoldKind, Highlight, HighlightModifier, HighlightTag,
InsertTextFormat, LineCol, LineIndex, NavigationTarget, RangeInfo, ReferenceAccess, Severity,
SourceChange, SourceFileEdit,
InlayHint, InlayKind, InsertTextFormat, LineCol, LineIndex, NavigationTarget, RangeInfo,
ReferenceAccess, Severity, SourceChange, SourceFileEdit,
};
use ra_syntax::{SyntaxKind, TextRange, TextUnit};
use ra_text_edit::{AtomTextEdit, TextEdit};
@ -323,6 +323,20 @@ fn conv_with(self, ctx: &FoldConvCtx) -> lsp_types::FoldingRange {
}
}
impl ConvWith<&LineIndex> for InlayHint {
type Output = req::InlayHint;
fn conv_with(self, line_index: &LineIndex) -> Self::Output {
req::InlayHint {
label: self.label.to_string(),
range: self.range.conv_with(line_index),
kind: match self.kind {
InlayKind::ParameterHint => req::InlayKind::ParameterHint,
InlayKind::TypeHint => req::InlayKind::TypeHint,
},
}
}
}
impl Conv for Highlight {
type Output = (u32, u32);

View File

@ -18,7 +18,7 @@
use lsp_server::{Connection, ErrorCode, Message, Notification, Request, RequestId, Response};
use lsp_types::{ClientCapabilities, NumberOrString};
use ra_cargo_watch::{url_from_path_with_drive_lowercasing, CheckOptions, CheckTask};
use ra_ide::{Canceled, FileId, LibraryData, SourceRootId};
use ra_ide::{Canceled, FileId, InlayHintsOptions, LibraryData, SourceRootId};
use ra_prof::profile;
use ra_vfs::{VfsFile, VfsTask, Watch};
use relative_path::RelativePathBuf;
@ -177,7 +177,11 @@ pub fn main_loop(
.and_then(|it| it.folding_range.as_ref())
.and_then(|it| it.line_folding_only)
.unwrap_or(false),
inlay_hints: config.inlay_hints,
inlay_hints: InlayHintsOptions {
type_hints: config.inlay_hints_type,
parameter_hints: config.inlay_hints_parameter,
max_length: config.inlay_hints_max_length,
},
cargo_watch: CheckOptions {
enable: config.cargo_watch_enable,
args: config.cargo_watch_args,

View File

@ -999,11 +999,7 @@ pub fn handle_inlay_hints(
Ok(analysis
.inlay_hints(file_id, &world.options.inlay_hints)?
.into_iter()
.map(|api_type| InlayHint {
label: api_type.label.to_string(),
range: api_type.range.conv_with(&line_index),
kind: api_type.kind,
})
.map_conv_with(&line_index)
.collect())
}

View File

@ -4,8 +4,6 @@
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use ra_ide::{InlayHintsOptions, InlayKind};
pub use lsp_types::{
notification::*, request::*, ApplyWorkspaceEditParams, CodeActionParams, CodeLens,
CodeLensParams, CompletionParams, CompletionResponse, DiagnosticTag,
@ -198,24 +196,14 @@ pub struct InlayHintsParams {
}
#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(remote = "InlayKind")]
pub enum InlayKindDef {
pub enum InlayKind {
TypeHint,
ParameterHint,
}
#[derive(Deserialize)]
#[serde(remote = "InlayConfig", rename_all = "camelCase")]
pub struct InlayConfigDef {
pub type_hints: bool,
pub parameter_hints: bool,
pub max_length: Option<usize>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct InlayHint {
pub range: Range,
#[serde(with = "InlayKindDef")]
pub kind: InlayKind,
pub label: String,
}

View File

@ -29,11 +29,16 @@ export async function createClient(config: Config, serverPath: string): Promise<
initializationOptions: {
publishDecorations: !config.highlightingSemanticTokens,
lruCapacity: config.lruCapacity,
inlayHints: config.inlayHints,
inlayHintsType: config.inlayHints.typeHints,
inlayHintsParameter: config.inlayHints.parameterHints,
inlayHintsMaxLength: config.inlayHints.maxLength,
cargoWatchEnable: cargoWatchOpts.enable,
cargoWatchArgs: cargoWatchOpts.arguments,
cargoWatchCommand: cargoWatchOpts.command,
cargoWatchAllTargets: cargoWatchOpts.allTargets,
excludeGlobs: config.excludeGlobs,
useClientWatching: config.useClientWatching,
featureFlags: config.featureFlags,