sublimelsp / LSP-rust-analyzer

Convenience package for rust-analyzer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Implement Structural Search And Replace

lucypero opened this issue · comments

commented

RA has a great feature for refactoring that I'd like to use in ST. How much work would it take to make an interface for this functionality? I believe you can only use this on VS Code for now. I'd love to make a PR if someone can give me some pointers.

Link to docs

There are various examples of custom commands in plugin.py. For example this one:

LSP-rust-analyzer/plugin.py

Lines 467 to 501 in 0e9264f

class RustAnalyzerSyntaxTree(RustAnalyzerCommand):
def is_enabled(self) -> bool:
selection = self.view.sel()
if len(selection) == 0:
return False
return super().is_enabled()
def run(self, edit: sublime.Edit) -> None:
params = text_document_position_params(self.view, self.view.sel()[0].b)
session = self.session_by_name(self.session_name)
if session is None:
return
session.send_request(
Request("rust-analyzer/syntaxTree", params),
lambda response: sublime.set_timeout(functools.partial(self.on_result, response))
)
def on_result(self, out: Optional[str]) -> None:
window = self.view.window()
if window is None:
return
if out is None:
return
sheets = window.selected_sheets()
view = window.new_file(flags=sublime.TRANSIENT)
view.set_scratch(True)
view.set_name("Syntax Tree")
# Resource Aware Session Types Syntax highlighting not available
view.run_command("append", {"characters": out})
view.set_read_only(True)
sheet = view.sheet()
if sheet is not None:
sheets.append(sheet)
window.select_sheets(sheets)

One for this feature would likely need to trigger an input (or two) to ask for a specific input but it shouldn't be that hard. You can check ST API at http://www.sublimetext.com/docs/api_reference.html .

BTW. It seems like RA also supports this feature through a custom comment:

Also available as an assist, by writing a comment containing the structural search and replace rule. You will only see the assist if the comment can be parsed as a valid structural search and replace rule.

I feel like this could work already in ST but haven't tried.

commented

I feel like this could work already in ST but haven't tried.

Yes, you are right. I just tried it. It already works by writing the query in a comment. It would still be great to have another interface for it. I might take a look later. Thanks.