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

Merged
merged 42 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
42 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
c6e0055
Append truncation tooltip instead of wrapping result
bivashy Oct 9, 2024
e5b9050
Merge branch 'main' into main
bivashy Oct 14, 2024
c4c74cc
Merge branch 'main' into main
bivashy Oct 22, 2024
b83a147
Remove empty lines
bivashy Oct 25, 2024
9be92d9
Change truncation limit to exclusive one.
bivashy Oct 25, 2024
dc7d5ca
Match `sublime-package.json` with settings description
bivashy Oct 25, 2024
87db8f3
Merge branch 'main' into main
bivashy Oct 31, 2024
d6a544a
Change truncation limit to exclusive
bivashy Nov 4, 2024
0a4a246
Merge branch 'main' into main
bivashy Nov 4, 2024
a43e68a
Lower the limit of the inlay hint
bivashy Nov 4, 2024
875a7a0
Merge branch 'main' into main
bivashy Nov 8, 2024
572b1cd
Specify missing truncation case in description
bivashy Nov 8, 2024
b039ea8
Tweak setting description
jwortmann Nov 10, 2024
8e86dc3
Merge branch 'main' into main
bivashy Nov 10, 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,

// Maximum length for inlay hints. Truncates exceeding characters and adds an ellipsis.
"inlay_hints_max_length": 100,

// 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", 100)
r("show_diagnostics_in_hover", True)
r("show_code_actions_in_hover", True)
r("show_diagnostics_annotations_severity_level", 0)
Expand Down
23 changes: 20 additions & 3 deletions plugin/inlay_hint.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ 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 +142,23 @@ 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] + '…' if truncated else label

bivashy marked this conversation as resolved.
Show resolved Hide resolved
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also be > here or not?


I have tested the PR with a server that uses the regular label (no label parts) and I think it works well with the tooltips.

My only suggestion left would be to use a lower default value for the setting. I think 30 would be a sane default. I would say the truncation is actually a good thing to do, and some users might not check settings or read update notes, so it should be applied by default. Opinions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also be > here or not?

I have tested the PR with a server that uses the regular label (no label parts) and I think it works well with the tooltips.

My only suggestion left would be to use a lower default value for the setting. I think 30 would be a sane default. I would say the truncation is actually a good thing to do, and some users might not check settings or read update notes, so it should be applied by default. Opinions?

I'll check it out. Thanks for pointing that out.


I think inlay hint is not enabled by default, so most users will not be affected. So I agree that this feature should be enabled by default. I'll wait for more opinions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll check it out. Thanks for pointing that out.

Done in d6a544a.


Done in a43e68a

I will not resolve this conversation in case of reconsideration, should I?

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 +173,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] + '…' 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": 100,
"minimum": 0,
"markdownDescription": "Truncate limit for inlay hints. Truncates and adds an ellipsis at the end."
bivashy marked this conversation as resolved.
Show resolved Hide resolved
},
"initially_folded": {
"type": "array",
"items": {
Expand Down