From 1feead7e5b211a7d23fb218db6a726edb3cd18dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20F=C3=B6rster?= Date: Sun, 15 Sep 2024 18:10:01 +0200 Subject: [PATCH] Add texlab.inlayHints.maxLength setting (#1213) Add `texlab.inlayHints.maxLength` setting. --- CHANGELOG.md | 4 ++++ crates/base-db/src/config.rs | 2 ++ crates/texlab/src/features/inlay_hint.rs | 7 ++++--- crates/texlab/src/server/options.rs | 1 + crates/texlab/src/util/from_proto.rs | 1 + crates/texlab/src/util/to_proto.rs | 23 +++++++++++++++++++---- 6 files changed, 31 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a5c78a01..5c4427293 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add `texlab.inlayHints.maxLength` setting to allow limiting inlay hint text length ([#1212](https://github.com/latex-lsp/texlab/issues/1212)) + ### Fixed - Fix enabling `texlab.build.useFileList` setting diff --git a/crates/base-db/src/config.rs b/crates/base-db/src/config.rs index e4c827e61..455fdc2a6 100644 --- a/crates/base-db/src/config.rs +++ b/crates/base-db/src/config.rs @@ -80,6 +80,7 @@ pub struct SymbolConfig { pub struct InlayHintConfig { pub label_definitions: bool, pub label_references: bool, + pub max_length: Option, } #[derive(Debug)] @@ -140,6 +141,7 @@ impl Default for InlayHintConfig { Self { label_definitions: true, label_references: true, + max_length: None, } } } diff --git a/crates/texlab/src/features/inlay_hint.rs b/crates/texlab/src/features/inlay_hint.rs index ddd8ff26d..0689f07af 100644 --- a/crates/texlab/src/features/inlay_hint.rs +++ b/crates/texlab/src/features/inlay_hint.rs @@ -7,10 +7,11 @@ pub fn find_all( params: lsp_types::InlayHintParams, ) -> Option> { let params = from_proto::inlay_hint_params(workspace, params)?; + let line_index = ¶ms.feature.document.line_index; + let max_length = workspace.config().inlay_hints.max_length; let hints = inlay_hints::find_all(¶ms)? .into_iter() - .filter_map(|hint| to_proto::inlay_hint(hint, ¶ms.feature.document.line_index)) - .collect(); + .filter_map(|hint| to_proto::inlay_hint(hint, line_index, max_length)); - Some(hints) + Some(hints.collect()) } diff --git a/crates/texlab/src/server/options.rs b/crates/texlab/src/server/options.rs index f4853d7cb..bc785c56c 100644 --- a/crates/texlab/src/server/options.rs +++ b/crates/texlab/src/server/options.rs @@ -104,6 +104,7 @@ pub struct DiagnosticsOptions { pub struct InlayHintOptions { pub label_definitions: Option, pub label_references: Option, + pub max_length: Option, } #[derive(Debug, Clone, Default, Serialize, Deserialize)] diff --git a/crates/texlab/src/util/from_proto.rs b/crates/texlab/src/util/from_proto.rs index 750a34c7e..2f4fbe64c 100644 --- a/crates/texlab/src/util/from_proto.rs +++ b/crates/texlab/src/util/from_proto.rs @@ -305,6 +305,7 @@ pub fn config(value: Options) -> Config { config.inlay_hints.label_definitions = value.inlay_hints.label_definitions.unwrap_or(true); config.inlay_hints.label_references = value.inlay_hints.label_references.unwrap_or(true); + config.inlay_hints.max_length = value.inlay_hints.max_length; config.completion.matcher = match value.completion.matcher { CompletionMatcher::Fuzzy => base_db::MatchingAlgo::Skim, diff --git a/crates/texlab/src/util/to_proto.rs b/crates/texlab/src/util/to_proto.rs index d2c261c8f..82d013243 100644 --- a/crates/texlab/src/util/to_proto.rs +++ b/crates/texlab/src/util/to_proto.rs @@ -191,13 +191,25 @@ pub fn diagnostic( }) } -pub fn inlay_hint(hint: InlayHint, line_index: &LineIndex) -> Option { +pub fn inlay_hint( + hint: InlayHint, + line_index: &LineIndex, + max_length: Option, +) -> Option { let position = line_index.line_col_lsp(hint.offset)?; + let trim_text = |text: &mut String| match max_length { + Some(max_length) if text.len() > max_length => { + text.truncate(max_length); + text.push('…'); + } + _ => {} + }; + Some(match hint.data { InlayHintData::LabelDefinition(label) => { let number = label.number?; - let text = match &label.object { + let mut text = match &label.object { RenderedObject::Section { prefix, .. } => { format!("{} {}", prefix, number) } @@ -211,9 +223,11 @@ pub fn inlay_hint(hint: InlayHint, line_index: &LineIndex) -> Option format!("Item {}", number), }; + trim_text(&mut text); + lsp_types::InlayHint { position, - label: lsp_types::InlayHintLabel::String(format!(" {text} ")), + label: lsp_types::InlayHintLabel::String(format!(" {text} ",)), kind: None, text_edits: None, tooltip: None, @@ -223,7 +237,8 @@ pub fn inlay_hint(hint: InlayHint, line_index: &LineIndex) -> Option { - let text = label.reference(); + let mut text = label.reference(); + trim_text(&mut text); lsp_types::InlayHint { position,