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

Feature: Truncate inlay hint label #2514

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
1d85bcc
Truncate inlay hint label
bivashy Sep 15, 2024
41150db
Split long line into multiple lines
bivashy Sep 15, 2024
0c9f49c
Add `inlay_truncate_limit` into settings
bivashy Sep 15, 2024
9755ed2
Add `inlay_truncate_limit` into sublime package
bivashy Sep 15, 2024
5c86172
Fix missing ellipsis at specific cases
bivashy Sep 15, 2024
528a430
Rename property `inlay_truncate_limit` into `inlay_hints_truncate_limit`
bivashy Sep 17, 2024
6335b74
Do not modify method variable, use local variable instead
bivashy Sep 17, 2024
8265c1b
Make sure that truncation limit is exclusive
bivashy Sep 19, 2024
c43d722
Split long line into multiple lines
bivashy Sep 19, 2024
a882902
Replace three dots with a compact character
bivashy Sep 19, 2024
808e817
Inlay hint tooltip implementation
bivashy Sep 19, 2024
1ba72a6
Read from user preference directly
bivashy Sep 22, 2024
1c313b5
Show truncation tooltip if label is raw string
bivashy Sep 22, 2024
25be2dd
Merge branch 'main' into main
bivashy Sep 22, 2024
c618857
Fix typo
bivashy Sep 26, 2024
cc4ad00
Merge remote-tracking branch 'origin/main'
bivashy Sep 26, 2024
08b9795
Remove truncate_limit parameter
bivashy Sep 26, 2024
9f0f3c7
Use ternary operator instead of plain if else
bivashy Sep 26, 2024
1a09aa5
Fix flake8 formatting
bivashy Sep 26, 2024
1a5bae4
Remove redundant blank line
bivashy Sep 26, 2024
0fcdf79
Properly show truncation tooltip
bivashy Sep 27, 2024
bc41741
Remove redundant newline in truncation tooltip
bivashy Oct 2, 2024
abe0987
Inclusive truncate limit
bivashy Oct 2, 2024
17591c1
Change default truncate limit to `100`
bivashy Oct 2, 2024
09162a2
Support negative, or zero truncate limit
bivashy Oct 2, 2024
79fee60
Merge branch 'main' into main
bivashy Oct 3, 2024
19a991a
Merge branch 'main' into main
bivashy Oct 4, 2024
0496789
Rename `inlay_hints_truncate_limit` into `inlay_hints_max_length`
bivashy Oct 4, 2024
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
3 changes: 3 additions & 0 deletions LSP.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@
// or a custom keybinding.
"show_inlay_hints": false,

// Truncate limit for inlay hints. Truncates and adds an ellipsis at the end.
"inlay_hints_truncate_limit": 1000,
bivashy marked this conversation as resolved.
Show resolved Hide resolved
bivashy marked this conversation as resolved.
Show resolved Hide resolved

// 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_truncate_limit = 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_truncate_limit", 1000)
r("show_diagnostics_in_hover", True)
r("show_code_actions_in_hover", True)
r("show_diagnostics_annotations_severity_level", 0)
Expand Down
25 changes: 20 additions & 5 deletions plugin/inlay_hint.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ def inlay_hint_to_phantom(view: sublime.View, inlay_hint: InlayHint, session: Se


def get_inlay_hint_html(view: sublime.View, inlay_hint: InlayHint, session: Session, phantom_uuid: str) -> str:
label = format_inlay_hint_label(inlay_hint, session, phantom_uuid)
truncate_limit = view.settings().get("inlay_hints_truncate_limit", userprefs().inlay_hints_truncate_limit)
bivashy marked this conversation as resolved.
Show resolved Hide resolved
label = format_inlay_hint_label(inlay_hint, session, phantom_uuid, truncate_limit)
font = view.settings().get('font_face') or "monospace"
html = f"""
<body id="lsp-inlay-hint">
Expand All @@ -123,7 +124,7 @@ def format_inlay_hint_tooltip(tooltip: str | MarkupContent | None) -> str:
return ""


def format_inlay_hint_label(inlay_hint: InlayHint, session: Session, phantom_uuid: str) -> str:
def format_inlay_hint_label(inlay_hint: InlayHint, session: Session, phantom_uuid: str, truncate_limit: int) -> str:
tooltip = format_inlay_hint_tooltip(inlay_hint.get("tooltip"))
result = ""
can_resolve_inlay_hint = session.has_capability('inlayHintProvider.resolveProvider')
Expand All @@ -142,12 +143,16 @@ def format_inlay_hint_label(inlay_hint: InlayHint, session: Session, phantom_uui
})
result += f'<a href="{inlay_hint_click_command}">'
instruction_text = '\nDouble-click to insert' if has_text_edits else ""
result += f'<span title="{(tooltip + instruction_text).strip()}">{html.escape(label)}</span>'
truncated_label = label[:truncate_limit] + '…' if len(label) > truncate_limit else label
result += f'<span title="{(tooltip + instruction_text).strip()}">{html.escape(truncated_label)}</span>'
bivashy marked this conversation as resolved.
Show resolved Hide resolved
if is_clickable:
result += "</a>"
return result

remaining_truncate_limit = truncate_limit
for label_part in label:
if remaining_truncate_limit < 0:
bivashy marked this conversation as resolved.
Show resolved Hide resolved
break
value = ""
tooltip = format_inlay_hint_tooltip(label_part.get("tooltip"))
has_command = bool(label_part.get('command'))
Expand All @@ -162,10 +167,20 @@ 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
if truncated:
truncated_label = raw_label[:remaining_truncate_limit] + '…'
else:
truncated_label = raw_label

bivashy marked this conversation as resolved.
Show resolved Hide resolved
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>"
tooltip_label = "".join(label_part['value'] for label_part in label)
truncation_tooltip = html.escape('\n' + tooltip_label) if 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_truncate_limit": {
"type": "integer",
"default": 1000,
"minimum": 0,
"markdownDescription": "Truncate limit for inlay hints. Truncates and adds an ellipsis at the end."
},
"initially_folded": {
"type": "array",
"items": {
Expand Down