Skip to content

Commit

Permalink
Switch to NpmClientHandler (#13)
Browse files Browse the repository at this point in the history
* Switch to NpmClientHandler

Removed "_typescript.rename" handler as I don't think it was used.
Renaming symbols is handled by built-in 'textDocument/rename'.

* Remove stray comma

* That was rather wrong typo

* Re-add _typescript.rename handling
  • Loading branch information
rchl authored May 3, 2020
1 parent 4682b57 commit 9d84697
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 82 deletions.
2 changes: 1 addition & 1 deletion LSP-typescript.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"command": "edit_settings",
"args": {
"base_file": "${packages}/LSP-typescript/LSP-typescript.sublime-settings",
"default": "// Settings in here override those in \"LSP-typescript/LSP-typescript.sublime-settings\",\n\n{\n\t$0\n}\n"
"default": "// Settings in here override those in \"LSP-typescript/LSP-typescript.sublime-settings\"\n\n{\n\t$0\n}\n"
}
},
]
2 changes: 1 addition & 1 deletion Main.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"command": "edit_settings",
"args": {
"base_file": "${packages}/LSP-typescript/LSP-typescript.sublime-settings",
"default": "// Settings in here override those in \"LSP-typescript/LSP-typescript.sublime-settings\",\n\n{\n\t$0\n}\n",
"default": "// Settings in here override those in \"LSP-typescript/LSP-typescript.sublime-settings\"\n\n{\n\t$0\n}\n",
}
}
]
Expand Down
102 changes: 22 additions & 80 deletions plugin.py
Original file line number Diff line number Diff line change
@@ -1,100 +1,42 @@
import shutil
import os
import sublime

from LSP.plugin.core.handlers import LanguageHandler
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.typing import Dict
from LSP.plugin.core.url import uri_to_filename
from lsp_utils import ServerNpmResource


PACKAGE_NAME = 'LSP-typescript'
SETTINGS_FILENAME = 'LSP-typescript.sublime-settings'
SERVER_DIRECTORY = 'typescript-language-server'
SERVER_BINARY_PATH = os.path.join(SERVER_DIRECTORY, 'node_modules', 'typescript-language-server', 'lib', 'cli.js')

server = ServerNpmResource(PACKAGE_NAME, SERVER_DIRECTORY, SERVER_BINARY_PATH)
from LSP.plugin.core.views import point_to_offset
from lsp_utils import NpmClientHandler


def plugin_loaded():
server.setup()
LspTypescriptPlugin.setup()


def plugin_unloaded():
server.cleanup()


def is_node_installed():
return shutil.which('node') is not None


class LspTypeScriptPlugin(LanguageHandler):
@property
def name(self) -> str:
return PACKAGE_NAME.lower()

@property
def config(self) -> ClientConfig:
# Calling setup() also here as this might run before `plugin_loaded`.
# Will be a no-op if already ran.
# See https://github.com/sublimelsp/LSP/issues/899
server.setup()

configuration = self.migrate_and_read_configuration()

default_configuration = {
'enabled': True,
'command': ['node', server.binary_path, '--stdio'],
}

default_configuration.update(configuration)
return read_client_config(self.name, default_configuration)

def migrate_and_read_configuration(self) -> Dict:
settings = {}
loaded_settings = sublime.load_settings(SETTINGS_FILENAME) # type: Dict

if loaded_settings:
if loaded_settings.has('client'):
client = loaded_settings.get('client') # type: Dict
loaded_settings.erase('client')
# Migrate old keys
for key, value in client.items():
loaded_settings.set(key, value)
sublime.save_settings(SETTINGS_FILENAME)

# Read configuration keys
for key in ['languages', 'initializationOptions', 'settings']:
settings[key] = loaded_settings.get(key)
LspTypescriptPlugin.cleanup()

return settings

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 server.ready
class LspTypescriptPlugin(NpmClientHandler):
package_name = __package__
server_directory = 'typescript-language-server'
server_binary_path = os.path.join(
server_directory, 'node_modules', 'typescript-language-server', 'lib', 'cli.js'
)

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

def _on_typescript_rename(self, textDocumentPositionParams, cancellationToken):
def _on_typescript_rename(self, textDocumentPositionParams, respond):
view = sublime.active_window().open_file(
uri_to_filename(textDocumentPositionParams['textDocument']['uri'])
)

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

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')

sel = view.sel()
sel.clear()
sel.add_all([point])
view.run_command('lsp_symbol_rename')
# Server doesn't require any specific response.
respond(None)

0 comments on commit 9d84697

Please sign in to comment.