Skip to content

Commit

Permalink
fix: padding theming for LSP inlay hints
Browse files Browse the repository at this point in the history
  • Loading branch information
poliorcetics committed Feb 13, 2023
1 parent c6bfd98 commit b96469d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions book/src/themes.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ These scopes are used for theming the editor interface.
| `ui.virtual.whitespace` | Visible whitespace characters |
| `ui.virtual.indent-guide` | Vertical indent width guides |
| `ui.virtual.inlay-hint` | Default style for inlay hints of all kinds |
| `ui.virtual.inlay-hint.padding` | Style for inlay hint padding (helps not confusing it with regular whitespaces) |
| `ui.virtual.inlay-hint.parameter` | Style for inlay hints of kind `parameter` (LSPs are not required to set a kind) |
| `ui.virtual.inlay-hint.type` | Style for inlay hints of kind `type` (LSPs are not required to set a kind) |
| `ui.virtual.wrap` | Soft-wrap indicator (see the [`editor.soft-wrap` config][editor-section]) |
Expand Down
8 changes: 6 additions & 2 deletions helix-term/src/commands/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1437,9 +1437,11 @@ fn compute_inlay_hints_for_view(
// avoid errors on our end.
hints.sort_unstable_by_key(|inlay_hint| inlay_hint.position);

let mut padding_before_inlay_hints = Vec::new();
let mut type_inlay_hints = Vec::new();
let mut parameter_inlay_hints = Vec::new();
let mut other_inlay_hints = Vec::new();
let mut padding_after_inlay_hints = Vec::new();

let doc_text = doc.text();

Expand Down Expand Up @@ -1470,13 +1472,13 @@ fn compute_inlay_hints_for_view(
};

if let Some(true) = hint.padding_left {
inlay_hints_vec.push(InlineAnnotation::new(char_idx, " "));
padding_before_inlay_hints.push(InlineAnnotation::new(char_idx, " "));
}

inlay_hints_vec.push(InlineAnnotation::new(char_idx, label));

if let Some(true) = hint.padding_right {
inlay_hints_vec.push(InlineAnnotation::new(char_idx, " "));
padding_after_inlay_hints.push(InlineAnnotation::new(char_idx, " "));
}
}

Expand All @@ -1487,6 +1489,8 @@ fn compute_inlay_hints_for_view(
type_inlay_hints: type_inlay_hints.into(),
parameter_inlay_hints: parameter_inlay_hints.into(),
other_inlay_hints: other_inlay_hints.into(),
padding_before_inlay_hints: padding_before_inlay_hints.into(),
padding_after_inlay_hints: padding_after_inlay_hints.into(),
},
);
},
Expand Down
16 changes: 13 additions & 3 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,10 @@ pub struct Document {
/// them anywhere in the text and will sometime offer config options to move them where the user
/// wants them but it shouldn't be Helix who decides that so we use the most precise positioning.
///
/// The padding for inlay hints is stored directly before/after the hint text itself. This is
/// necessary to ensure it's placed correctly instead of after all the time (or ignored because it
/// came in a later layer and at the same `char_idx`).
/// The padding for inlay hints needs to be stored separately for before and after (the LSP spec
/// uses 'left' and 'right' but not all text is left to right so let's be correct) padding because
/// the 'before' padding must be added to a layer *before* the regular inlay hints and the 'after'
/// padding comes ... after.
#[derive(Debug, Clone)]
pub struct DocumentInlayHints {
/// Identifier for the inlay hints stored in this structure. To be checked to know if they have
Expand All @@ -187,6 +188,11 @@ pub struct DocumentInlayHints {
/// currently never does (February 2023) and the LSP spec may add new kinds in the future that
/// we want to display even if we don't have some special highlighting for them.
pub other_inlay_hints: Rc<[InlineAnnotation]>,

/// Inlay hint padding. When creating the final `TextAnnotations`, the `before` padding must be
/// added first, then the regular inlay hints, then the `after` padding.
pub padding_before_inlay_hints: Rc<[InlineAnnotation]>,
pub padding_after_inlay_hints: Rc<[InlineAnnotation]>,
}

/// Associated with a [`Document`] and [`ViewId`], uniquely identifies the state of inlay hints for
Expand Down Expand Up @@ -955,11 +961,15 @@ impl Document {
type_inlay_hints,
parameter_inlay_hints,
other_inlay_hints,
padding_before_inlay_hints,
padding_after_inlay_hints,
} = text_annotation;

apply_inlay_hint_changes(padding_before_inlay_hints);
apply_inlay_hint_changes(type_inlay_hints);
apply_inlay_hint_changes(parameter_inlay_hints);
apply_inlay_hint_changes(other_inlay_hints);
apply_inlay_hint_changes(padding_after_inlay_hints);
}

// emit lsp notification
Expand Down
10 changes: 9 additions & 1 deletion helix-view/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ impl View {
type_inlay_hints,
parameter_inlay_hints,
other_inlay_hints,
padding_before_inlay_hints,
padding_after_inlay_hints,
} = match doc.inlay_hints.get(&self.id) {
Some(doc_inlay_hints) => doc_inlay_hints,
None => return text_annotations,
Expand All @@ -429,6 +431,9 @@ impl View {
let other_style = theme
.and_then(|t| t.find_scope_index("ui.virtual.inlay-hint"))
.map(Highlight);
let padding_style = theme
.and_then(|t| t.find_scope_index("ui.virtual.inlay-hint.padding"))
.map(Highlight);

let mut add_annotations = |annotations: &Rc<[_]>, style| {
if !annotations.is_empty() {
Expand All @@ -437,10 +442,13 @@ impl View {
};

// Overlapping annotations are ignored apart from the first so the order here is not random:
// types -> parameters -> others should hopefully be the "correct" order for most use cases.
// types -> parameters -> others should hopefully be the "correct" order for most use cases,
// with the padding coming before and after as expected.
add_annotations(padding_before_inlay_hints, padding_style);
add_annotations(type_inlay_hints, type_style);
add_annotations(parameter_inlay_hints, parameter_style);
add_annotations(other_inlay_hints, other_style);
add_annotations(padding_after_inlay_hints, padding_style);

text_annotations
}
Expand Down

0 comments on commit b96469d

Please sign in to comment.