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

Insert inlay hint text edits on double instead of single click #2175

Merged
merged 27 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a8df5af
feat: add double click command
predragnikolic Jan 22, 2023
c4deab1
refactor: LspClick that accepts a (click)count instead of LspDoubleClick
predragnikolic Jan 22, 2023
01a650f
fix lint
predragnikolic Jan 22, 2023
d6ddc6e
refactor
predragnikolic Jan 22, 2023
5c0e9c5
remove ()
predragnikolic Jan 22, 2023
dea82b9
rename to LspDoubleClick, remove count and move to tooling
predragnikolic Jan 22, 2023
3c7f07c
WIP experiment toolitp
predragnikolic Jan 22, 2023
4460481
show Double Click + tooltip if clickable else show tooltip
predragnikolic Jan 22, 2023
f79da71
rename to LspOnDoubleClickCommand
predragnikolic Jan 22, 2023
c46b643
move to class method
predragnikolic Jan 22, 2023
6eff3ec
show tooltip content first than "Double Click)" if clickable
predragnikolic Jan 22, 2023
d512ca5
lint
predragnikolic Jan 22, 2023
027feed
Merge branch 'main' into lsp_double_click
predragnikolic Jan 22, 2023
a5ab27a
Update plugin/tooling.py
predragnikolic Jan 23, 2023
af4253c
remove tooltip title that is not necessary and be more clear for the …
predragnikolic Jan 23, 2023
216eabf
Change "Double-click to run" to "Double-click to execute"
predragnikolic Jan 23, 2023
bc5fd33
Double-click to insert
predragnikolic Jan 23, 2023
128682e
rename to LspOnDoubleClickCommand
predragnikolic Jan 23, 2023
6e48d6a
show tooltip, or show "Double-click to insert/execute" if clickable e…
predragnikolic Jan 23, 2023
17590ab
remove classmethod and only increase counter if the same command(and …
predragnikolic Jan 23, 2023
ac053af
also reset prev_command and prev_args when
predragnikolic Jan 23, 2023
3660505
order
rchl Jan 24, 2023
c032bef
fix condition
predragnikolic Jan 26, 2023
b6d3726
rename is_clicable to something more explicit: has_text_edits and has…
predragnikolic Jan 26, 2023
03472f8
forgot to add "or can_resolve_inlay_hint" in a condition bellow
predragnikolic Jan 26, 2023
ea8df03
always show instructions if the inlay hint has a command or text edits
predragnikolic Jan 29, 2023
3d866c4
strip tooltip content
rchl Jan 30, 2023
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
1 change: 1 addition & 0 deletions boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
from .plugin.tooling import LspCopyToClipboardFromBase64Command
from .plugin.tooling import LspDumpBufferCapabilities
from .plugin.tooling import LspDumpWindowConfigs
from .plugin.tooling import LspOnDoubleClickCommand
from .plugin.tooling import LspParseVscodePackageJson
from .plugin.tooling import LspTroubleshootServerCommand

Expand Down
48 changes: 30 additions & 18 deletions plugin/inlay_hint.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ 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:
tooltip = format_inlay_hint_tooltip(inlay_hint.get("tooltip"))
label = format_inlay_hint_label(inlay_hint, session, phantom_uuid)
font = view.settings().get('font_face') or "monospace"
html = """
Expand All @@ -91,12 +90,11 @@ def get_inlay_hint_html(view: sublime.View, inlay_hint: InlayHint, session: Sess
text-decoration: none;
}}
</style>
<div class="inlay-hint" title="{tooltip}">
<div class="inlay-hint">
{label}
</div>
</body>
""".format(
tooltip=tooltip,
font=font,
label=label
)
Expand All @@ -112,40 +110,54 @@ def format_inlay_hint_tooltip(tooltip: Optional[Union[str, MarkupContent]]) -> s


def format_inlay_hint_label(inlay_hint: InlayHint, session: Session, phantom_uuid: str) -> str:
tooltip = format_inlay_hint_tooltip(inlay_hint.get("tooltip"))
result = ""
can_resolve_inlay_hint = session.has_capability('inlayHintProvider.resolveProvider')
label = inlay_hint['label']
is_clickable = bool(inlay_hint.get('textEdits')) or can_resolve_inlay_hint
has_text_edits = bool(inlay_hint.get('textEdits'))
is_clickable = has_text_edits or can_resolve_inlay_hint
if isinstance(label, str):
if is_clickable:
inlay_hint_click_command = sublime.command_url('lsp_inlay_hint_click', {
'session_name': session.config.name,
'inlay_hint': cast(dict, inlay_hint),
'phantom_uuid': phantom_uuid
inlay_hint_click_command = sublime.command_url('lsp_on_double_click', {
'command': 'lsp_inlay_hint_click',
'args': {
'session_name': session.config.name,
'inlay_hint': cast(dict, inlay_hint),
'phantom_uuid': phantom_uuid
}
})
result += '<a href="{command}">'.format(command=inlay_hint_click_command)
result += html.escape(label)
instruction_text = '\nDouble-click to insert' if has_text_edits else ""
result += '<span title="{tooltip}">{value}</span>'.format(
tooltip=tooltip + instruction_text,
rchl marked this conversation as resolved.
Show resolved Hide resolved
value=html.escape(label)
)
if is_clickable:
result += "</a>"
return result

for label_part in label:
value = ""
is_clickable = is_clickable or bool(label_part.get('command'))
if is_clickable:
inlay_hint_click_command = sublime.command_url('lsp_inlay_hint_click', {
'session_name': session.config.name,
'inlay_hint': cast(dict, inlay_hint),
'phantom_uuid': phantom_uuid,
'label_part': cast(dict, label_part)
tooltip = format_inlay_hint_tooltip(label_part.get("tooltip"))
has_command = bool(label_part.get('command'))
if has_command:
inlay_hint_click_command = sublime.command_url('lsp_on_double_click', {
'command': 'lsp_inlay_hint_click',
'args': {
'session_name': session.config.name,
'inlay_hint': cast(dict, inlay_hint),
'phantom_uuid': phantom_uuid,
'label_part': cast(dict, label_part)
}
})
value += '<a href="{command}">'.format(command=inlay_hint_click_command)
value += html.escape(label_part['value'])
if is_clickable:
if has_command:
value += "</a>"
# InlayHintLabelPart.location is not supported
instruction_text = '\nDouble-click to execute' if has_command else ""
result += "<span title=\"{tooltip}\">{value}</span>".format(
tooltip=format_inlay_hint_tooltip(label_part.get("tooltip")),
tooltip=tooltip + instruction_text,
value=value
)
return result
23 changes: 23 additions & 0 deletions plugin/tooling.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,26 @@ def on_transport_close(self, exit_code: int, exception: Optional[Exception]) ->
self._transport = None
output = str(exception) if exception else '\n'.join(self._stderr_lines).rstrip()
sublime.set_timeout(lambda: self._on_close(self._resolved_command, output, exit_code))


class LspOnDoubleClickCommand(sublime_plugin.TextCommand):
click_count = 0
prev_command = None # type: Optional[str]
prev_args = None # type: Optional[Dict[Any, Any]]

def run(self, edit: sublime.Edit, command: str, args: Dict[Any, Any]) -> None:
if self.prev_command != command or self.prev_args != args:
self.click_count = 0
self.prev_command = command
self.prev_args = args
self.click_count += 1
if self.click_count == 2:
self.view.run_command(command, args)
self.reset()
return
sublime.set_timeout(self.reset, 500)

def reset(self) -> None:
self.click_count = 0
self.prev_command = None
self.prev_args = None