Feat: inlay hint length limit (#13742)

pull/13765/head^2
yuri 2025-06-14 00:09:21 +08:00 committed by GitHub
parent 52192ae29e
commit 1315b7e2b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 2 deletions

View File

@ -157,6 +157,7 @@ The following statusline elements can be configured:
| `display-progress-messages` | Display LSP progress messages below statusline[^1] | `false` | | `display-progress-messages` | Display LSP progress messages below statusline[^1] | `false` |
| `auto-signature-help` | Enable automatic popup of signature help (parameter hints) | `true` | | `auto-signature-help` | Enable automatic popup of signature help (parameter hints) | `true` |
| `display-inlay-hints` | Display inlay hints[^2] | `false` | | `display-inlay-hints` | Display inlay hints[^2] | `false` |
| `inlay-hints-length-limit` | Maximum displayed length (non-zero number) of inlay hints | Unset by default |
| `display-color-swatches` | Show color swatches next to colors | `true` | | `display-color-swatches` | Show color swatches next to colors | `true` |
| `display-signature-help-docs` | Display docs under signature help popup | `true` | | `display-signature-help-docs` | Display docs under signature help popup | `true` |
| `snippets` | Enables snippet completions. Requires a server restart (`:lsp-restart`) to take effect after `:config-reload`/`:set`. | `true` | | `snippets` | Enables snippet completions. Requires a server restart (`:lsp-restart`) to take effect after `:config-reload`/`:set`. | `true` |

View File

@ -1357,6 +1357,7 @@ fn compute_inlay_hints_for_view(
let mut padding_after_inlay_hints = Vec::new(); let mut padding_after_inlay_hints = Vec::new();
let doc_text = doc.text(); let doc_text = doc.text();
let inlay_hints_length_limit = doc.config.load().lsp.inlay_hints_length_limit;
for hint in hints { for hint in hints {
let char_idx = let char_idx =
@ -1367,7 +1368,7 @@ fn compute_inlay_hints_for_view(
None => continue, None => continue,
}; };
let label = match hint.label { let mut label = match hint.label {
lsp::InlayHintLabel::String(s) => s, lsp::InlayHintLabel::String(s) => s,
lsp::InlayHintLabel::LabelParts(parts) => parts lsp::InlayHintLabel::LabelParts(parts) => parts
.into_iter() .into_iter()
@ -1375,6 +1376,31 @@ fn compute_inlay_hints_for_view(
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(""), .join(""),
}; };
// Truncate the hint if too long
if let Some(limit) = inlay_hints_length_limit {
// Limit on displayed width
use helix_core::unicode::{
segmentation::UnicodeSegmentation, width::UnicodeWidthStr,
};
let width = label.width();
let limit = limit.get().into();
if width > limit {
let mut floor_boundary = 0;
let mut acc = 0;
for (i, grapheme_cluster) in label.grapheme_indices(true) {
acc += grapheme_cluster.width();
if acc > limit {
floor_boundary = i;
break;
}
}
label.truncate(floor_boundary);
label.push('…');
}
}
let inlay_hints_vec = match hint.kind { let inlay_hints_vec = match hint.kind {
Some(lsp::InlayHintKind::TYPE) => &mut type_inlay_hints, Some(lsp::InlayHintKind::TYPE) => &mut type_inlay_hints,

View File

@ -29,7 +29,7 @@ use std::{
collections::{BTreeMap, HashMap, HashSet}, collections::{BTreeMap, HashMap, HashSet},
fs, fs,
io::{self, stdin}, io::{self, stdin},
num::NonZeroUsize, num::{NonZeroU8, NonZeroUsize},
path::{Path, PathBuf}, path::{Path, PathBuf},
pin::Pin, pin::Pin,
sync::Arc, sync::Arc,
@ -459,6 +459,9 @@ pub struct LspConfig {
pub display_signature_help_docs: bool, pub display_signature_help_docs: bool,
/// Display inlay hints /// Display inlay hints
pub display_inlay_hints: bool, pub display_inlay_hints: bool,
/// Maximum displayed length of inlay hints (excluding the added trailing `…`).
/// If it's `None`, there's no limit
pub inlay_hints_length_limit: Option<NonZeroU8>,
/// Display document color swatches /// Display document color swatches
pub display_color_swatches: bool, pub display_color_swatches: bool,
/// Whether to enable snippet support /// Whether to enable snippet support
@ -476,6 +479,7 @@ impl Default for LspConfig {
auto_signature_help: true, auto_signature_help: true,
display_signature_help_docs: true, display_signature_help_docs: true,
display_inlay_hints: false, display_inlay_hints: false,
inlay_hints_length_limit: None,
snippets: true, snippets: true,
goto_reference_include_declaration: true, goto_reference_include_declaration: true,
display_color_swatches: true, display_color_swatches: true,