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 1 commit
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
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_to_add_regions_flags(self) -> Tuple[int, int]:
rwols marked this conversation as resolved.
Show resolved Hide resolved
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
47 changes: 25 additions & 22 deletions plugin/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,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 @@ -597,13 +598,21 @@ 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]))
regions = self.view.get_regions(self._highlights_key(kind, False))
for r in regions:
if r.contains(point):
return True
rwols marked this conversation as resolved.
Show resolved Hide resolved
regions = self.view.get_regions(self._highlights_key(kind, True))
for r in regions:
if r.contains(point):
return True
Expand All @@ -621,31 +630,25 @@ 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():
flags_multi, flags_single = userprefs().document_highlight_style_to_add_regions_flags()
for tup, regions in kind2regions.items():
kind, multiline = tup
key = self._highlights_key(kind, multiline)
if regions:
scope = _kind2scope[kind]
self.view.add_regions(
"lsp_highlight_{}".format(_kind2name[kind]),
regions,
scope=scope,
flags=flags)
flags = flags_multi if multiline else flags_single
self.view.add_regions(key, regions, scope=_kind2scope[kind], flags=flags)
else:
self.view.erase_regions(key)
rwols marked this conversation as resolved.
Show resolved Hide resolved

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