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

View file

@ -11,8 +11,8 @@ use lsp_types::{
use ra_ide::{ use ra_ide::{
translate_offset_with_edit, CompletionItem, CompletionItemKind, FileId, FilePosition, translate_offset_with_edit, CompletionItem, CompletionItemKind, FileId, FilePosition,
FileRange, FileSystemEdit, Fold, FoldKind, Highlight, HighlightModifier, HighlightTag, FileRange, FileSystemEdit, Fold, FoldKind, Highlight, HighlightModifier, HighlightTag,
InsertTextFormat, LineCol, LineIndex, NavigationTarget, RangeInfo, ReferenceAccess, Severity, InlayHint, InlayKind, InsertTextFormat, LineCol, LineIndex, NavigationTarget, RangeInfo,
SourceChange, SourceFileEdit, ReferenceAccess, Severity, SourceChange, SourceFileEdit,
}; };
use ra_syntax::{SyntaxKind, TextRange, TextUnit}; use ra_syntax::{SyntaxKind, TextRange, TextUnit};
use ra_text_edit::{AtomTextEdit, TextEdit}; use ra_text_edit::{AtomTextEdit, TextEdit};
@ -323,6 +323,20 @@ impl ConvWith<&FoldConvCtx<'_>> for Fold {
} }
} }
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 { impl Conv for Highlight {
type Output = (u32, u32); type Output = (u32, u32);

View file

@ -18,7 +18,7 @@ use crossbeam_channel::{select, unbounded, RecvError, Sender};
use lsp_server::{Connection, ErrorCode, Message, Notification, Request, RequestId, Response}; use lsp_server::{Connection, ErrorCode, Message, Notification, Request, RequestId, Response};
use lsp_types::{ClientCapabilities, NumberOrString}; use lsp_types::{ClientCapabilities, NumberOrString};
use ra_cargo_watch::{url_from_path_with_drive_lowercasing, CheckOptions, CheckTask}; 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_prof::profile;
use ra_vfs::{VfsFile, VfsTask, Watch}; use ra_vfs::{VfsFile, VfsTask, Watch};
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
@ -177,7 +177,11 @@ pub fn main_loop(
.and_then(|it| it.folding_range.as_ref()) .and_then(|it| it.folding_range.as_ref())
.and_then(|it| it.line_folding_only) .and_then(|it| it.line_folding_only)
.unwrap_or(false), .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 { cargo_watch: CheckOptions {
enable: config.cargo_watch_enable, enable: config.cargo_watch_enable,
args: config.cargo_watch_args, args: config.cargo_watch_args,

View file

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

View file

@ -4,8 +4,6 @@ use lsp_types::{Location, Position, Range, TextDocumentIdentifier, Url};
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use ra_ide::{InlayHintsOptions, InlayKind};
pub use lsp_types::{ pub use lsp_types::{
notification::*, request::*, ApplyWorkspaceEditParams, CodeActionParams, CodeLens, notification::*, request::*, ApplyWorkspaceEditParams, CodeActionParams, CodeLens,
CodeLensParams, CompletionParams, CompletionResponse, DiagnosticTag, CodeLensParams, CompletionParams, CompletionResponse, DiagnosticTag,
@ -198,24 +196,14 @@ pub struct InlayHintsParams {
} }
#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)] #[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(remote = "InlayKind")] pub enum InlayKind {
pub enum InlayKindDef {
TypeHint, TypeHint,
ParameterHint, 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)] #[derive(Debug, Deserialize, Serialize)]
pub struct InlayHint { pub struct InlayHint {
pub range: Range, pub range: Range,
#[serde(with = "InlayKindDef")]
pub kind: InlayKind, pub kind: InlayKind,
pub label: String, pub label: String,
} }

View file

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