From 6603c976da6e65cc0f5892444f6a46fe9c1f2368 Mon Sep 17 00:00:00 2001 From: Aaron Cottle Date: Mon, 29 Aug 2022 14:35:16 +1000 Subject: [PATCH] Upstream Inlay Hints --- LSP-rust-analyzer.sublime-settings | 4 -- plugin.py | 107 +---------------------------- 2 files changed, 1 insertion(+), 110 deletions(-) diff --git a/LSP-rust-analyzer.sublime-settings b/LSP-rust-analyzer.sublime-settings index 8442a96..159325b 100644 --- a/LSP-rust-analyzer.sublime-settings +++ b/LSP-rust-analyzer.sublime-settings @@ -166,16 +166,12 @@ "rust-analyzer.hoverActions.run": true, //Whether to show inlay type hints for method chains. "rust-analyzer.inlayHints.chainingHints": true, - //Whether to show inlay hints. - "rust-analyzer.inlayHints.enable": true, // Whether to hide inlay hints for constructors. "rust-analyzer.inlayHints.hideNamedConstructorHints": false, //Maximum length for inlay hints. Set to null to have an unlimited length. "rust-analyzer.inlayHints.maxLength": 25, //Whether to show function parameter name inlay hints at the call site. "rust-analyzer.inlayHints.parameterHints": true, - //Whether inlay hints font size should be smaller than editor's font size. - "rust-analyzer.inlayHints.smallerHints": true, //Whether to show inlay type hints for variables. "rust-analyzer.inlayHints.typeHints": true, //Join lines merges consecutive declaration and initialization of an assignment. diff --git a/plugin.py b/plugin.py index 2dc9891..1689226 100644 --- a/plugin.py +++ b/plugin.py @@ -41,16 +41,10 @@ # Update this single git tag to download a newer version. # After changing this tag, go through the server settings again to see # if any new server settings are added or old ones removed. -TAG = "2022-01-10" +TAG = "2022-08-22" URL = "https://github.com/rust-analyzer/rust-analyzer/releases/download/{tag}/rust-analyzer-{arch}-{platform}.gz" -InlayHint = TypedDict("InlayHint", { - "kind": str, - "range": RangeLsp, - "label": str, -}, total=True) - RunnableArgs = TypedDict('RunnableArgs', { 'cargoArgs': List[str], 'cargoExtraArgs': List[str], @@ -94,55 +88,6 @@ def platform() -> str: return "unknown-linux-gnu" -def inlay_hint_css(view: sublime.View) -> str: - style = view.style_for_scope("comment") - rules = [ - "color: {};".format(style["foreground"]), - ] - - css = """ - body {{ - padding: 0px; - margin: 0px; - border: 0px; - font-size: 0.8em; - }} - - .rust-analyzer-inlay-hints {{ - {0} - }} - """ - return css.format('\n'.join(rules)) - - -def inlay_hint_to_phantom(view: sublime.View, css: str, hint: InlayHint) -> sublime.Phantom: - rng = Range.from_lsp(hint["range"]) - html = """ - - -
- {label} -
- - """ - - label = html_escape(hint["label"]) - if hint["kind"] == "TypeHint": - # For a type hint, the end range is where you want to put it - region = sublime.Region(point_to_offset(rng.end, view)) - label = ": {}".format(label) - elif hint["kind"] == "ParameterHint": - # For parameter hints, you actually want it to start where it's started - region = sublime.Region(point_to_offset(rng.start, view)) - label = "{}: ".format(label) - else: - # The last kind is ChainingHint, we want those at the end too - region = sublime.Region(point_to_offset(rng.end, view)) - label = ": {}".format(label) - - html = html.format(css=css, label=label) - return sublime.Phantom(region, html, sublime.LAYOUT_INLINE) - def open_runnables_in_terminus(window: Optional[sublime.Window], runnables: List[Runnable]) -> None: filtered_runnables = [r for r in runnables if r["kind"] == "cargo"] @@ -286,54 +231,6 @@ def is_valid_for_view(self, view: sublime.View) -> bool: session = self.weaksession() return bool(session and session.session_view_for_view_async(view)) - def request_inlay_hints_async(self, view: sublime.View) -> None: - if not get_setting(view, "rust-analyzer.inlayHints.enable", True): - return - session = self.weaksession() - if session is None: - return - params = { - "textDocument": text_document_identifier(view), - } - session.send_request_async( - Request("rust-analyzer/inlayHints", params), - functools.partial(self.on_inlay_hints_async, view) - ) - - def on_inlay_hints_async(self, view: sublime.View, hints: List[InlayHint]) -> None: - session = self.weaksession() - if session is None: - return - buffer = session.get_session_buffer_for_uri_async(uri_from_view(view)) - if not buffer: - return - key = "_lsp_rust_analyzer_inlay_hints" - phantom_set = getattr(buffer, key, None) - if phantom_set is None: - phantom_set = sublime.PhantomSet(view, key) - setattr(buffer, key, phantom_set) - css = inlay_hint_css(view) - phantoms = [inlay_hint_to_phantom(view, css, hint) for hint in hints] - sublime.set_timeout( - functools.partial( - self.present_inlay_hints, - view, - phantom_set, - phantoms - ) - ) - - def present_inlay_hints( - self, - view: sublime.View, - phantom_set: sublime.PhantomSet, - phantoms: List[sublime.Phantom] - ) -> None: - if not view.is_valid(): - return - phantom_set.update(phantoms) - - class RustAnalyzerCommand(LspTextCommand): session_name = SESSION_NAME @@ -610,7 +507,6 @@ def on_modified_async(self) -> None: if not different: return debounced( - functools.partial(plugin.request_inlay_hints_async, self.view), FEATURES_TIMEOUT, lambda: self._stored_region == region, async_thread=True, @@ -620,7 +516,6 @@ def on_load_async(self) -> None: plugin = RustAnalyzer.plugin_from_view(self.view) if plugin is None: return - plugin.request_inlay_hints_async(self.view) on_activated_async = on_load_async