Skip to content

Commit

Permalink
Use public imports and adapt to new LSP version
Browse files Browse the repository at this point in the history
  • Loading branch information
rchl authored and predragnikolic committed Oct 7, 2022
1 parent 2058041 commit 1609d94
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
15 changes: 9 additions & 6 deletions core/decorations.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from .. commands.lsp_metals_text_command import LspMetalsTextCommand
from functools import reduce, partial
from LSP.plugin.core.css import css
from LSP.plugin.core.protocol import Range
from LSP.plugin.core.sessions import Session
from LSP.plugin import css, Session
from LSP.plugin.core.typing import Any, List, Dict, Optional, Union
from LSP.plugin.core.views import range_to_region, FORMAT_MARKED_STRING, FORMAT_MARKUP_CONTENT, minihtml

Expand All @@ -17,6 +15,7 @@ def on_modified(self) -> None:
if file_name and file_name.endswith('.worksheet.sc'):
self.view.run_command('lsp_metals_clear_phantoms')


class LspMetalsClearPhantomsCommand(LspMetalsTextCommand):
def run(self, edit: sublime.Edit) -> None:
fname = self.view.file_name()
Expand All @@ -30,6 +29,7 @@ def run_async(self, fname: str) -> None:
return
handle_decorations(session, fname)


def handle_decorations(session: Session, params: Union[Dict[str, Any], str]) -> None:
phantom_key = "metals_decoraction"
field_name = "_lsp_metals_decorations"
Expand Down Expand Up @@ -61,10 +61,12 @@ def handle_decorations(session: Session, params: Union[Dict[str, Any], str]) ->

phantom_set.update(phantoms)


PHANTOM_HTML = """
<style>div.phantom {{font-style: italic; color: {}}}</style>
<div class='phantom'>{}{}</div>"""


def show_popup(content: Dict[str, Any], view: sublime.View, location: int) -> None:
html = minihtml(view, content, allowed_formats=FORMAT_MARKED_STRING | FORMAT_MARKUP_CONTENT)
viewport_width = view.viewport_extent()[0]
Expand All @@ -81,13 +83,12 @@ def show_popup(content: Dict[str, Any], view: sublime.View, location: int) -> No


def decoration_to_phantom(option: Dict[str, Any], view: sublime.View) -> Optional[sublime.Phantom]:
decoration_range = Range.from_lsp(option['range'])
region = range_to_region(decoration_range, view)
region = range_to_region(option['range'], view)
region.a = region.b # make the start point equal to the end point
hoverMessage = deep_get(option, 'hoverMessage')
contentText = deep_get(option, 'renderOptions', 'after', 'contentText')
link = ''
point = view.text_point(decoration_range.start.row, decoration_range.start.col)
point = view.text_point(option['start'].row, option['start'].col)
if hoverMessage:
link = " <a href='more'>more</a>"

Expand All @@ -101,8 +102,10 @@ def decoration_to_phantom(option: Dict[str, Any], view: sublime.View) -> Optiona

return phantom


def decorations_to_phantom(options: Dict[str, Any], view: sublime.View) -> List[sublime.Phantom]:
return map(lambda o: decoration_to_phantom(o, view), options)


def deep_get(dictionary: Dict[str, Any], *keys):
return reduce(lambda d, key: d.get(key) if d else None, keys, dictionary)
6 changes: 4 additions & 2 deletions core/handle_execute_client.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from collections import OrderedDict
from . status import status_key
from .. commands.utils import open_location
from LSP.plugin.core.sessions import Session
from LSP.plugin import Session
from LSP.plugin.core.types import Any
import json
import sublime
import mdpopups


Expand All @@ -26,18 +25,21 @@ def handle_execute_client(session: Session, params: Any) -> None:
msg = "Unknown command {}".format(command_name)
session.set_window_status_async(status_key, msg)


def goto_location(session: Session, args: Any) -> None:
"""https://scalameta.org/metals/docs/integrations/new-editor/#goto-location"""

if isinstance(args, list) and args:
open_location(session.window, args[0])


def show_stacktrace(session: Session, args: Any) -> None:
"""https://scalameta.org/metals/docs/integrations/new-editor/#show-the-stacktrace-in-the-client"""

if isinstance(args, list) and args:
session.window.new_html_sheet('Stacktrace', args[0])


def run_doctor(session: Session, args: Any) -> None:
if isinstance(args, list) and args:

Expand Down
6 changes: 3 additions & 3 deletions core/handle_input_box.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from LSP.plugin import Response
from LSP.plugin.core.sessions import Session
from LSP.plugin import Response, Session
from LSP.plugin.core.types import Optional, Any


def handle_input_box(session: Session, params: Any, request_id: Any) -> None:
"""Handle the metals/inputBox request."""
if not isinstance(params, dict):
return

def send_response(input: Optional[str]) -> None:
p = { 'value': input, 'cancelled': False } if input else {'cancelled': True}
p = {'value': input, 'cancelled': False} if input else {'cancelled': True}
session.send_response(Response(request_id, p))

session.window.show_input_panel(
Expand Down
5 changes: 4 additions & 1 deletion core/metals.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from LSP.plugin import AbstractPlugin
from LSP.plugin import ClientConfig
from LSP.plugin import WorkspaceFolder
from LSP.plugin.core.types import Optional, Any, Tuple, List
from LSP.plugin.core.types import Optional, Any, List
from urllib.request import urlopen, Request
import json

Expand All @@ -18,6 +18,7 @@
_LATEST_STABLE = "latest-stable"
_LATEST_SNAPSHOT = "latest-snapshot"


class Metals(AbstractPlugin):

@classmethod
Expand Down Expand Up @@ -88,6 +89,7 @@ def m_metals_inputBox(self, params: Any, request_id: Any) -> None:
return
handle_input_box(session, params, request_id)


def get_java_path(settings: sublime.Settings) -> str:
java_home = settings.get("java_home")
if isinstance(java_home, str) and java_home:
Expand All @@ -97,6 +99,7 @@ def get_java_path(settings: sublime.Settings) -> str:
return os.path.join(java_home, "bin", "java")
return "java"


def create_launch_command(java_path: str, artifact_version: str, server_properties: List[str]) -> List[str]:
binary_version = "2.12"
if LooseVersion(artifact_version) > LooseVersion("0.11.2"):
Expand Down
3 changes: 2 additions & 1 deletion core/status.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from LSP.plugin import Session
from LSP.plugin.core.types import Any
from LSP.plugin.core.sessions import Session

status_key = "metals-status"


def handle_status(session: Session, params: Any) -> None:
"""Handle the metals/status notification."""

Expand Down

0 comments on commit 1609d94

Please sign in to comment.