Skip to content

Commit

Permalink
feat: truncate inlay hint (#2514)
Browse files Browse the repository at this point in the history
  • Loading branch information
bivashy authored Nov 10, 2024
1 parent 875a96c commit 2f37a49
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
4 changes: 4 additions & 0 deletions LSP.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@
// or a custom keybinding.
"show_inlay_hints": false,

// Maximum length for inlay hints. Truncates exceeding characters and adds an ellipsis.
// Set to 0 for unlimited length.
"inlay_hints_max_length": 30,

// Highlighting style of "highlights": accentuating nearby text entities that
// are related to the one under your cursor.
// Valid values are "background", "underline", "stippled", "outline", or "".
Expand Down
2 changes: 2 additions & 0 deletions plugin/core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ class Settings:
show_code_actions = cast(str, None)
show_code_lens = cast(str, None)
show_inlay_hints = cast(bool, None)
inlay_hints_max_length = cast(int, None)
show_diagnostics_in_hover = cast(bool, None)
show_code_actions_in_hover = cast(bool, None)
show_diagnostics_annotations_severity_level = cast(int, None)
Expand Down Expand Up @@ -272,6 +273,7 @@ def r(name: str, default: bool | int | str | list | dict) -> None:
r("show_code_actions", "annotation")
r("show_code_lens", "annotation")
r("show_inlay_hints", False)
r("inlay_hints_max_length", 30)
r("show_diagnostics_in_hover", True)
r("show_code_actions_in_hover", True)
r("show_diagnostics_annotations_severity_level", 0)
Expand Down
22 changes: 18 additions & 4 deletions plugin/inlay_hint.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def format_inlay_hint_tooltip(tooltip: str | MarkupContent | None) -> str:


def format_inlay_hint_label(inlay_hint: InlayHint, session: Session, phantom_uuid: str) -> str:
truncate_limit = userprefs().inlay_hints_max_length
tooltip = format_inlay_hint_tooltip(inlay_hint.get("tooltip"))
result = ""
can_resolve_inlay_hint = session.has_capability('inlayHintProvider.resolveProvider')
Expand All @@ -140,13 +141,21 @@ def format_inlay_hint_label(inlay_hint: InlayHint, session: Session, phantom_uui
}
})
result += f'<a href="{inlay_hint_click_command}">'
truncated = len(label) > truncate_limit and truncate_limit > 0
truncated_label = label[:truncate_limit - 1] + '…' if truncated else label
instruction_text = '\nDouble-click to insert' if has_text_edits else ""
result += f'<span title="{(tooltip + instruction_text).strip()}">{html.escape(label)}</span>'
truncation_tooltip = f'\n{html.escape(label)}' if truncated else ""
result_tooltip = (tooltip + instruction_text + truncation_tooltip).strip()
result += f'<span title="{result_tooltip}">{html.escape(truncated_label)}</span>'
if is_clickable:
result += "</a>"
return result

remaining_truncate_limit = truncate_limit
full_label = "".join(label_part['value'] for label_part in label)
full_label_truncated = len(full_label) > truncate_limit and truncate_limit > 0
for label_part in label:
if remaining_truncate_limit < 0 and truncate_limit > 0:
break
value = ""
tooltip = format_inlay_hint_tooltip(label_part.get("tooltip"))
has_command = bool(label_part.get('command'))
Expand All @@ -161,10 +170,15 @@ def format_inlay_hint_label(inlay_hint: InlayHint, session: Session, phantom_uui
}
})
value += f'<a href="{inlay_hint_click_command}">'
value += html.escape(label_part['value'])
raw_label = label_part['value']
truncated = len(raw_label) > remaining_truncate_limit and truncate_limit > 0
truncated_label = raw_label[:remaining_truncate_limit - 1] + '…' if truncated else raw_label
remaining_truncate_limit -= len(raw_label)
value += html.escape(truncated_label)
if has_command:
value += "</a>"
# InlayHintLabelPart.location is not supported
instruction_text = '\nDouble-click to execute' if has_command else ""
result += f"<span title=\"{(tooltip + instruction_text).strip()}\">{value}</span>"
truncation_tooltip = f'\n{html.escape(full_label)}' if full_label_truncated else ""
result += f"<span title=\"{(tooltip + instruction_text + truncation_tooltip).strip()}\">{value}</span>"
return result
6 changes: 6 additions & 0 deletions sublime-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,12 @@
"default": false,
"markdownDescription": "Show inlay hints in the editor. Inlay hints are short annotations within the code, which show variable types or parameter names.\nThis is the default value used for new windows but can be overriden per-window using the `LSP: Toggle Inlay Hints` command from the Command Palette, Main Menu or a custom keybinding."
},
"inlay_hints_max_length": {
"type": "integer",
"default": 30,
"minimum": 0,
"markdownDescription": "Maximum length for inlay hints. Truncates exceeding characters and adds an ellipsis. Set to 0 for unlimited length."
},
"initially_folded": {
"type": "array",
"items": {
Expand Down

0 comments on commit 2f37a49

Please sign in to comment.