Skip to content

Commit

Permalink
Add copy worksheet command (#75)
Browse files Browse the repository at this point in the history
Co-authored-by: Ayoub Benali <[email protected]>
  • Loading branch information
ayoub-benali and ayoub-benali authored Dec 23, 2021
1 parent 80d5b96 commit 1366bbe
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions LSP-metals.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
{ "caption": "LSP-metals: Compile Cascade", "command": "lsp_metals_execute", "args":{"command_name": "compile-cascade"}},
{ "caption": "LSP-metals: Compile Cancel", "command": "lsp_metals_execute", "args":{"command_name": "compile-cancel"}},
{ "caption": "LSP-metals: Compile Clean", "command": "lsp_metals_execute", "args":{"command_name": "compile-clean"}},
{ "caption": "LSP-metals: Copy Worksheet", "command": "lsp_metals_copy_worksheet"},
{ "caption": "LSP-metals: Sources Scan", "command": "lsp_metals_execute", "args":{"command_name": "sources-scan"}},
{ "caption": "LSP-metals: Doctor Run", "command": "lsp_metals_execute", "args":{"command_name": "doctor-run"}},
{ "caption": "LSP-metals: Reset Choice", "command": "lsp_metals_execute", "args":{"command_name": "reset-choice"}},
Expand Down
36 changes: 36 additions & 0 deletions commands/lsp_metals_copy_worksheet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from . lsp_metals_text_command import LspMetalsTextCommand
from . utils import handle_error
from LSP.plugin.core.protocol import Error
from LSP.plugin.core.typing import Any
from LSP.plugin.core.url import filename_to_uri # TODO: deprecated in a future version

import sublime

class LspMetalsCopyWorksheetCommand(LspMetalsTextCommand):

_command_name = 'copy-worksheet-output'

def is_enabled(self) -> bool:
if super().is_enabled(None, None):
return self.view.file_name().endswith('.worksheet.sc')
else:
return False

def run(self, edit: sublime.Edit) -> None:
file_name = self.view.file_name()
session = self.session_by_name(self.session_name)
if self.view.is_dirty():
sublime.message_dialog('Please save your worksheet before using this command.')
elif session:
uri = filename_to_uri(file_name)
params = {
"command": self._command_name,
"arguments": [uri]
}
session.execute_command(params, progress=True).then(self._handle_response)

def _handle_response(self, response: Any) -> None:
if isinstance(response, Error):
handle_error(self._command_name, response)
elif 'value' in response:
sublime.set_clipboard(response['value'])
1 change: 1 addition & 0 deletions plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from . commands.lsp_metals_new_scala_file import LspMetalsNewScalaFileCommand
from . commands.lsp_metals_text_command import LspMetalsTextCommand
from . core.decorations import WorksheetListener, LspMetalsClearPhantomsCommand
from . commands.lsp_metals_copy_worksheet import LspMetalsCopyWorksheetCommand
from . core.metals import Metals

def plugin_loaded() -> None:
Expand Down

0 comments on commit 1366bbe

Please sign in to comment.