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

Use box-style for multiline doc highlights #1712

Merged
merged 5 commits into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
2 changes: 1 addition & 1 deletion LSP.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@

// Highlighting style of "highlights": accentuating nearby text entities that
// are related to the one under your cursor.
// Valid values are "fill", "box", "underline", "stippled", "squiggly" or "".
// Valid values are "fill", "underline", or "".
// When set to the empty string (""), no document highlighting is requested.
"document_highlight_style": "underline",

Expand Down
9 changes: 6 additions & 3 deletions plugin/core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def r(name: str, default: Union[bool, int, str, list, dict]) -> None:
r("diagnostics_highlight_style", "underline")
r("diagnostics_panel_include_severity_level", 4)
r("disabled_capabilities", [])
r("document_highlight_style", "stippled")
r("document_highlight_style", "underline")
r("log_debug", False)
r("log_max_size", 8 * 1024)
r("lsp_code_actions_on_save", {})
Expand Down Expand Up @@ -236,8 +236,11 @@ def r(name: str, default: Union[bool, int, str, list, dict]) -> None:

set_debug_logging(self.log_debug)

def document_highlight_style_to_add_regions_flags(self) -> int:
return _settings_style_to_add_regions_flag(self.document_highlight_style)
def document_highlight_style_region_flags(self) -> Tuple[int, int]:
if self.document_highlight_style == "fill":
return sublime.DRAW_NO_OUTLINE, sublime.DRAW_NO_OUTLINE
else:
return sublime.DRAW_NO_FILL, sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE | sublime.DRAW_SOLID_UNDERLINE

def diagnostics_highlight_style_to_add_regions_flag(self) -> int:
return _settings_style_to_add_regions_flag(self.diagnostics_highlight_style)
Expand Down
58 changes: 30 additions & 28 deletions plugin/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from weakref import WeakSet
from weakref import WeakValueDictionary
import functools
import itertools
import sublime
import sublime_plugin
import textwrap
Expand Down Expand Up @@ -306,8 +307,9 @@ def on_text_changed_async(self, change_count: int, changes: Iterable[sublime.Tex
if not different:
return
self._clear_highlight_regions()
self._when_selection_remains_stable_async(self._do_highlights_async, current_region,
after_ms=self.highlights_debounce_time)
if userprefs().document_highlight_style:
self._when_selection_remains_stable_async(self._do_highlights_async, current_region,
after_ms=self.highlights_debounce_time)
self._when_selection_remains_stable_async(self._do_color_boxes_async, current_region,
after_ms=self.color_boxes_debounce_time)
self.do_signature_help_async(manual=False)
Expand Down Expand Up @@ -341,8 +343,9 @@ def on_selection_modified_async(self) -> None:
if different:
if not self._is_in_higlighted_region(current_region.b):
self._clear_highlight_regions()
self._when_selection_remains_stable_async(self._do_highlights_async, current_region,
after_ms=self.highlights_debounce_time)
if userprefs().document_highlight_style:
self._when_selection_remains_stable_async(self._do_highlights_async, current_region,
after_ms=self.highlights_debounce_time)
self._clear_code_actions_annotation()
if userprefs().show_code_actions:
self._when_selection_remains_stable_async(self._do_code_actions, current_region,
Expand Down Expand Up @@ -597,16 +600,22 @@ def _unresolved_code_lenses(

# --- textDocument/documentHighlight -------------------------------------------------------------------------------

def _highlights_key(self, kind: int, multiline: bool) -> str:
return "lsp_highlight_{}{}".format(_kind2name[kind], "m" if multiline else "s")

def _clear_highlight_regions(self) -> None:
for kind in range(1, 4):
self.view.erase_regions("lsp_highlight_{}".format(_kind2name[kind]))
self.view.erase_regions(self._highlights_key(kind, False))
self.view.erase_regions(self._highlights_key(kind, True))

def _is_in_higlighted_region(self, point: int) -> bool:
for kind in range(1, 4):
regions = self.view.get_regions("lsp_highlight_{}".format(_kind2name[kind]))
for r in regions:
if r.contains(point):
return True
regions = itertools.chain(
self.view.get_regions(self._highlights_key(kind, False)),
self.view.get_regions(self._highlights_key(kind, True))
) # type: Iterable[sublime.Region]
if any(region.contains(point) for region in regions):
return True
return False

def _do_highlights_async(self) -> None:
Expand All @@ -621,31 +630,24 @@ def _do_highlights_async(self) -> None:
session.send_request_async(request, self._on_highlights)

def _on_highlights(self, response: Optional[List]) -> None:
if not response:
self._clear_highlight_regions()
return
kind2regions = {} # type: Dict[int, List[sublime.Region]]
for kind in range(1, 4):
kind2regions[kind] = []
if not isinstance(response, list):
response = []
kind2regions = {} # type: Dict[Tuple[int, bool], List[sublime.Region]]
for highlight in response:
r = range_to_region(Range.from_lsp(highlight["range"]), self.view)
kind = highlight.get("kind", DocumentHighlightKind.Text)
if kind in kind2regions:
kind2regions[kind].append(r)
else:
debug("unknown DocumentHighlightKind", kind)
kind2regions.setdefault((kind, len(self.view.split_by_newlines(r)) > 1), []).append(r)

def render_highlights_on_main_thread() -> None:
self._clear_highlight_regions()
flags = userprefs().document_highlight_style_to_add_regions_flags()
for kind, regions in kind2regions.items():
if regions:
scope = _kind2scope[kind]
self.view.add_regions(
"lsp_highlight_{}".format(_kind2name[kind]),
regions,
scope=scope,
flags=flags)
flags_multi, flags_single = userprefs().document_highlight_style_region_flags()
for tup, regions in kind2regions.items():
if not regions:
continue
kind, multiline = tup
key = self._highlights_key(kind, multiline)
flags = flags_multi if multiline else flags_single
self.view.add_regions(key, regions, scope=_kind2scope[kind], flags=flags)

sublime.set_timeout(render_highlights_on_main_thread)

Expand Down
3 changes: 0 additions & 3 deletions sublime-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,7 @@
"document_highlight_style": {
"enum": [
"fill",
"box",
"underline",
"stippled",
"squiggly",
""
],
"default": "underline",
Expand Down