|
| 1 | +import sublime_plugin |
| 2 | + |
| 3 | +from .core.configurations import is_supported_syntax |
| 4 | +from .core.protocol import Request, Range, DocumentHighlightKind |
| 5 | +from .core.clients import client_for_view |
| 6 | +from .core.documents import get_document_position |
| 7 | +from .core.settings import settings |
| 8 | + |
| 9 | +import sublime # only for typing |
| 10 | + |
| 11 | + |
| 12 | +_kind2name = { |
| 13 | + DocumentHighlightKind.Unknown: "unknown", |
| 14 | + DocumentHighlightKind.Text: "text", |
| 15 | + DocumentHighlightKind.Read: "read", |
| 16 | + DocumentHighlightKind.Write: "write" |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +class DocumentHighlightListener(sublime_plugin.ViewEventListener): |
| 21 | + |
| 22 | + @classmethod |
| 23 | + def is_applicable(cls, settings): |
| 24 | + syntax = settings.get('syntax') |
| 25 | + return syntax and is_supported_syntax(syntax) |
| 26 | + |
| 27 | + def __init__(self, view: sublime.View) -> None: |
| 28 | + super().__init__(view) |
| 29 | + self._initialized = False |
| 30 | + self._enabled = False |
| 31 | + self._version = 0 |
| 32 | + |
| 33 | + def on_selection_modified_async(self) -> None: |
| 34 | + if not self._initialized: |
| 35 | + self._initialize() |
| 36 | + if self._enabled: |
| 37 | + self._clear_regions() |
| 38 | + self._queue() |
| 39 | + |
| 40 | + def _queue(self) -> None: |
| 41 | + self._version += 1 |
| 42 | + current_version = self._version |
| 43 | + sublime.set_timeout_async(lambda: self._purge(current_version), 500) |
| 44 | + |
| 45 | + def _purge(self, this_version: int) -> None: |
| 46 | + if this_version == self._version: |
| 47 | + self._on_document_highlight() |
| 48 | + |
| 49 | + def _clear_regions(self) -> None: |
| 50 | + for kind in settings.document_highlight_scopes.keys(): |
| 51 | + self.view.erase_regions("LspDocumentHighlightListener_{}".format(kind)) |
| 52 | + |
| 53 | + def _on_document_highlight(self) -> None: |
| 54 | + self._clear_regions() |
| 55 | + if len(self.view.sel()) != 1: |
| 56 | + return |
| 57 | + point = self.view.sel()[0].begin() |
| 58 | + if self.view.match_selector(point, "comment"): |
| 59 | + # We're inside a comment, go home. |
| 60 | + return |
| 61 | + client = client_for_view(self.view) |
| 62 | + if client: |
| 63 | + params = get_document_position(self.view, point) |
| 64 | + if params: |
| 65 | + request = Request.documentHighlight(params) |
| 66 | + client.send_request(request, self._handle_response) |
| 67 | + |
| 68 | + def _handle_response(self, response: list) -> None: |
| 69 | + if not response: |
| 70 | + return |
| 71 | + kind2regions = {} # type: Dict[str, List[sublime.Region]] |
| 72 | + for kind in range(0, DocumentHighlightKind.Count): |
| 73 | + kind2regions[_kind2name[kind]] = [] |
| 74 | + for highlight in response: |
| 75 | + if highlight: |
| 76 | + r = Range.from_lsp(highlight["range"]).to_region(self.view) |
| 77 | + kind = highlight.get("kind", DocumentHighlightKind.Unknown) |
| 78 | + kind2regions[_kind2name[kind]].append(r) |
| 79 | + flags = sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE |
| 80 | + if settings.document_highlight_style == "underline": |
| 81 | + flags |= sublime.DRAW_SOLID_UNDERLINE |
| 82 | + elif settings.document_highlight_style == "stippled": |
| 83 | + flags |= sublime.DRAW_STIPPLED_UNDERLINE |
| 84 | + elif settings.document_highlight_style == "squiggly": |
| 85 | + flags |= sublime.DRAW_SQUIGGLY_UNDERLINE |
| 86 | + self._clear_regions() |
| 87 | + for kind, regions in kind2regions.items(): |
| 88 | + if regions: |
| 89 | + scope = settings.document_highlight_scopes.get(kind, None) |
| 90 | + self.view.add_regions("LspDocumentHighlightListener_{}".format(kind), |
| 91 | + regions, scope=scope, flags=flags) |
| 92 | + |
| 93 | + def _initialize(self) -> None: |
| 94 | + self._initialized = True |
| 95 | + client = client_for_view(self.view) |
| 96 | + if client: |
| 97 | + self._enabled = client.get_capability("documentHighlightProvider") |
0 commit comments