diff options
author | Kitson Kelly <me@kitsonkelly.com> | 2022-10-16 13:39:43 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-16 13:39:43 +1100 |
commit | 7d78f58187cdcb9bed632992cde347fd5f3c83eb (patch) | |
tree | 80bd06e16db12e7578b2946eaf0b72ca0ac34c6c /cli/lsp/tsc.rs | |
parent | 6d2656fd56e8ac0f1b1443e28d474bf3ceca89bb (diff) |
feat: support inlay hints (#16287)
Closes: #11853
Diffstat (limited to 'cli/lsp/tsc.rs')
-rw-r--r-- | cli/lsp/tsc.rs | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/cli/lsp/tsc.rs b/cli/lsp/tsc.rs index 51dd74240..6c2136990 100644 --- a/cli/lsp/tsc.rs +++ b/cli/lsp/tsc.rs @@ -618,6 +618,15 @@ pub struct TextSpan { } impl TextSpan { + pub fn from_range( + range: &lsp::Range, + line_index: Arc<LineIndex>, + ) -> Result<Self, AnyError> { + let start = line_index.offset_tsc(range.start)?; + let length = line_index.offset_tsc(range.end)? - start; + Ok(Self { start, length }) + } + pub fn to_range(&self, line_index: Arc<LineIndex>) -> lsp::Range { lsp::Range { start: line_index.position_tsc(self.start.into()), @@ -933,6 +942,48 @@ impl NavigateToItem { } #[derive(Debug, Clone, Deserialize)] +pub enum InlayHintKind { + Type, + Parameter, + Enum, +} + +impl InlayHintKind { + pub fn to_lsp(&self) -> Option<lsp::InlayHintKind> { + match self { + Self::Enum => None, + Self::Parameter => Some(lsp::InlayHintKind::PARAMETER), + Self::Type => Some(lsp::InlayHintKind::TYPE), + } + } +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct InlayHint { + pub text: String, + pub position: u32, + pub kind: InlayHintKind, + pub whitespace_before: Option<bool>, + pub whitespace_after: Option<bool>, +} + +impl InlayHint { + pub fn to_lsp(&self, line_index: Arc<LineIndex>) -> lsp::InlayHint { + lsp::InlayHint { + position: line_index.position_tsc(self.position.into()), + label: lsp::InlayHintLabel::String(self.text.clone()), + kind: self.kind.to_lsp(), + padding_left: self.whitespace_before, + padding_right: self.whitespace_after, + text_edits: None, + tooltip: None, + data: None, + } + } +} + +#[derive(Debug, Clone, Deserialize)] #[serde(rename_all = "camelCase")] pub struct NavigationTree { pub text: String, @@ -2830,6 +2881,18 @@ pub enum IncludeInlayParameterNameHints { All, } +impl From<&config::InlayHintsParamNamesEnabled> + for IncludeInlayParameterNameHints +{ + fn from(setting: &config::InlayHintsParamNamesEnabled) -> Self { + match setting { + config::InlayHintsParamNamesEnabled::All => Self::All, + config::InlayHintsParamNamesEnabled::Literals => Self::Literals, + config::InlayHintsParamNamesEnabled::None => Self::None, + } + } +} + #[derive(Debug, Serialize)] #[serde(rename_all = "kebab-case")] #[allow(dead_code)] @@ -2910,6 +2973,8 @@ pub struct UserPreferences { #[serde(skip_serializing_if = "Option::is_none")] pub include_inlay_variable_type_hints: Option<bool>, #[serde(skip_serializing_if = "Option::is_none")] + pub include_inlay_variable_type_hints_when_type_matches_name: Option<bool>, + #[serde(skip_serializing_if = "Option::is_none")] pub include_inlay_property_declaration_type_hints: Option<bool>, #[serde(skip_serializing_if = "Option::is_none")] pub include_inlay_function_like_return_type_hints: Option<bool>, @@ -2921,6 +2986,43 @@ pub struct UserPreferences { pub auto_import_file_exclude_patterns: Option<Vec<String>>, } +impl From<&config::WorkspaceSettings> for UserPreferences { + fn from(workspace_settings: &config::WorkspaceSettings) -> Self { + let inlay_hints = &workspace_settings.inlay_hints; + Self { + include_inlay_parameter_name_hints: Some( + (&inlay_hints.parameter_names.enabled).into(), + ), + include_inlay_parameter_name_hints_when_argument_matches_name: Some( + inlay_hints + .parameter_names + .suppress_when_argument_matches_name, + ), + include_inlay_function_parameter_type_hints: Some( + inlay_hints.parameter_types.enabled, + ), + include_inlay_variable_type_hints: Some( + inlay_hints.variable_types.enabled, + ), + include_inlay_variable_type_hints_when_type_matches_name: Some( + inlay_hints + .variable_types + .suppress_when_argument_matches_name, + ), + include_inlay_property_declaration_type_hints: Some( + inlay_hints.property_declaration_types.enabled, + ), + include_inlay_function_like_return_type_hints: Some( + inlay_hints.function_like_return_types.enabled, + ), + include_inlay_enum_member_value_hints: Some( + inlay_hints.enum_member_values.enabled, + ), + ..Default::default() + } + } +} + #[derive(Debug, Serialize)] #[serde(rename_all = "camelCase")] pub struct SignatureHelpItemsOptions { @@ -3053,6 +3155,8 @@ pub enum RequestMethod { ProvideCallHierarchyIncomingCalls((ModuleSpecifier, u32)), /// Resolve outgoing call hierarchy items for a specific position. ProvideCallHierarchyOutgoingCalls((ModuleSpecifier, u32)), + /// Resolve inlay hints for a specific text span + ProvideInlayHints((ModuleSpecifier, TextSpan, UserPreferences)), // Special request, used only internally by the LSP Restart, @@ -3269,6 +3373,15 @@ impl RequestMethod { "position": position }) } + RequestMethod::ProvideInlayHints((specifier, span, preferences)) => { + json!({ + "id": id, + "method": "provideInlayHints", + "specifier": state.denormalize_specifier(specifier), + "span": span, + "preferences": preferences, + }) + } RequestMethod::Restart => json!({ "id": id, "method": "restart", |