Skip to content

Commit

Permalink
Improvement - trigger lsp rename command on typescript rename request
Browse files Browse the repository at this point in the history
  • Loading branch information
predragnikolic committed Nov 17, 2019
1 parent ef391d6 commit d60d378
Showing 1 changed file with 39 additions and 20 deletions.
59 changes: 39 additions & 20 deletions plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import subprocess

from LSP.plugin.core.handlers import LanguageHandler
from LSP.plugin.core.settings import ClientConfig, LanguageConfig, read_client_config
from LSP.plugin.core.settings import ClientConfig, read_client_config
from LSP.plugin.core.rpc import Client
from LSP.plugin.core.views import point_to_offset
from LSP.plugin.core.protocol import Point
from LSP.plugin.core.url import uri_to_filename


package_path = os.path.dirname(__file__)
Expand Down Expand Up @@ -70,7 +74,39 @@ def logAndShowMessage(msg, additional_logs=None):
sublime.active_window().status_message(msg)


class LspTypeScriptPlugin(LanguageHandler):
def on_typescript_rename(textDocumentPositionParams, cancellationToken):
view = sublime.active_window().open_file(
uri_to_filename(
textDocumentPositionParams['textDocument']['uri']
)
)

if not view:
return

lsp_point = Point.from_lsp(textDocumentPositionParams['position'])
point = point_to_offset(lsp_point, view)

sel = view.sel()
sel.clear()
sel.add_all([point])
view.run_command('lsp_symbol_rename')


class SharedState:
def on_start(self, window) -> bool:
if not is_node_installed():
sublime.status_message('Please install Node.js for the TypeScript Language Server to work.')
return False
return True

def on_initialized(self, client: Client) -> None:
client.on_request(
"_typescript.rename",
lambda params, token: on_typescript_rename(params, token))


class LspTypeScriptPlugin(SharedState, LanguageHandler):
@property
def name(self) -> str:
return 'lsp-typescript'
Expand Down Expand Up @@ -105,17 +141,8 @@ def config(self) -> ClientConfig:
default_configuration.update(client_configuration)
return read_client_config('lsp-typescript', default_configuration)

def on_start(self, window) -> bool:
if not is_node_installed():
sublime.status_message('Please install Node.js for the TypeScript Language Server to work.')
return False
return True

def on_initialized(self, client) -> None:
pass # extra initialization here.


class LspJavaScriptPlugin(LanguageHandler):
class LspJavaScriptPlugin(SharedState, LanguageHandler):
@property
def name(self) -> str:
return 'lsp-javascript'
Expand Down Expand Up @@ -151,11 +178,3 @@ def config(self) -> ClientConfig:
default_configuration.update(client_configuration)
return read_client_config('lsp-javascript', default_configuration)

def on_start(self, window) -> bool:
if not is_node_installed():
sublime.status_message('Please install Node.js for the JavaScript Language Server to work.')
return False
return True

def on_initialized(self, client) -> None:
pass # extra initialization here.

0 comments on commit d60d378

Please sign in to comment.