Skip to content

Commit

Permalink
feat: implement custom join lines command/keybinding (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
rchl authored Jan 4, 2024
1 parent 3b89bea commit ba7a991
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Default.sublime-keymap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"keys": ["primary+shift+j"],
"command": "rust_analyzer_join_lines",
"context": [
{"key": "lsp.session_with_name", "operand": "rust-analyzer"}
]
},
]
4 changes: 4 additions & 0 deletions LSP-rust-analyzer.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"default": "// Settings in here override those in \"LSP-rust-analyzer/LSP-rust-analyzer.sublime-settings\"\n{\n\t$0\n}\n"
}
},
{
"caption": "LSP-rust-analyzer: Join Lines",
"command": "rust_analyzer_join_lines"
},
{
"caption": "LSP-rust-analyzer: Open Docs Under Cursor",
"command": "rust_analyzer_open_docs"
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ Select a cargo command from the submenu. This spawns a shell with [Terminus](htt
![Example](./images/commands.gif)

### LSP-rust-analyzer: Join Lines

Joins lines accounting for rust-specific logic.

Also bound to the default join-lines key binding (<kbd>ctrl</kbd><kbd>shift</kbd><kbd>j</kbd> or <kbd>command</kbd><kbd>shift</kbd><kbd>j</kbd> on Windows/Linux and Mac respectively).

### LSP-rust-analyzer: Open Docs Under Cursor

Opens the URL to documentation for the symbol under the cursor, if available.
Expand Down
56 changes: 56 additions & 0 deletions plugin_commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from .plugin import RustAnalyzerCommand
from LSP.plugin import Request
from LSP.plugin.core.protocol import Error
from LSP.plugin.core.protocol import Range
from LSP.plugin.core.protocol import TextDocumentIdentifier
from LSP.plugin.core.protocol import TextEdit
from LSP.plugin.core.typing import List, TypedDict, Union
from LSP.plugin.core.views import region_to_range
from LSP.plugin.core.views import text_document_identifier
from LSP.plugin.formatting import apply_text_edits_to_view
import sublime


class JoinLinesRequest:
Type = 'experimental/joinLines'
ParamsType = TypedDict('ParamsType', {
'textDocument': TextDocumentIdentifier,
'ranges': List[Range],
})
ReturnType = List[TextEdit]


class RustAnalyzerJoinLinesCommand(RustAnalyzerCommand):

def run(self, edit: sublime.Edit) -> None:
sublime.set_timeout_async(self.make_request_async)

def make_request_async(self) -> None:
session = self.session_by_name(self.session_name)
if session is None:
return
session_view = session.session_view_for_view_async(self.view)
if not session_view:
return
view_listener = session_view.listener()
if not view_listener:
return
params = {
'textDocument': text_document_identifier(self.view),
'ranges': [region_to_range(self.view, region) for region in self.view.sel()],
} # type: JoinLinesRequest.ParamsType
request = Request(JoinLinesRequest.Type, params) # type: Request[JoinLinesRequest.ReturnType]
document_version = self.view.change_count()
view_listener.purge_changes_async()
session.send_request_task(request).then(lambda result: self.on_result_async(result, document_version))

def on_result_async(self, edits: Union[JoinLinesRequest.ReturnType, Error], document_version: int) -> None:
if document_version != self.view.change_count():
return
if isinstance(edits, Error):
print('[{}] Error handling the "{}" request. Falling back to native join.'.format(
self.session_name, JoinLinesRequest.Type))
self.view.run_command('join_lines')
return
if edits:
apply_text_edits_to_view(edits, self.view)

0 comments on commit ba7a991

Please sign in to comment.