Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow limiting inlay hint text length #1213

Merged
merged 1 commit into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions crates/base-db/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub struct SymbolConfig {
pub struct InlayHintConfig {
pub label_definitions: bool,
pub label_references: bool,
pub max_length: Option<usize>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -140,6 +141,7 @@ impl Default for InlayHintConfig {
Self {
label_definitions: true,
label_references: true,
max_length: None,
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions crates/texlab/src/features/inlay_hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ pub fn find_all(
params: lsp_types::InlayHintParams,
) -> Option<Vec<lsp_types::InlayHint>> {
let params = from_proto::inlay_hint_params(workspace, params)?;
let line_index = &params.feature.document.line_index;
let max_length = workspace.config().inlay_hints.max_length;
let hints = inlay_hints::find_all(&params)?
.into_iter()
.filter_map(|hint| to_proto::inlay_hint(hint, &params.feature.document.line_index))
.collect();
.filter_map(|hint| to_proto::inlay_hint(hint, line_index, max_length));

Some(hints)
Some(hints.collect())
}
1 change: 1 addition & 0 deletions crates/texlab/src/server/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub struct DiagnosticsOptions {
pub struct InlayHintOptions {
pub label_definitions: Option<bool>,
pub label_references: Option<bool>,
pub max_length: Option<usize>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
Expand Down
1 change: 1 addition & 0 deletions crates/texlab/src/util/from_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 19 additions & 4 deletions crates/texlab/src/util/to_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,25 @@ pub fn diagnostic(
})
}

pub fn inlay_hint(hint: InlayHint, line_index: &LineIndex) -> Option<lsp_types::InlayHint> {
pub fn inlay_hint(
hint: InlayHint,
line_index: &LineIndex,
max_length: Option<usize>,
) -> Option<lsp_types::InlayHint> {
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)
}
Expand All @@ -211,9 +223,11 @@ pub fn inlay_hint(hint: InlayHint, line_index: &LineIndex) -> Option<lsp_types::
RenderedObject::EnumItem => 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,
Expand All @@ -223,7 +237,8 @@ pub fn inlay_hint(hint: InlayHint, line_index: &LineIndex) -> Option<lsp_types::
}
}
InlayHintData::LabelReference(label) => {
let text = label.reference();
let mut text = label.reference();
trim_text(&mut text);

lsp_types::InlayHint {
position,
Expand Down