Skip to content

Commit

Permalink
Implemented is_enabled() on all derived classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
evandrocoan committed Feb 4, 2018
1 parent 6b23fec commit c314e37
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 18 deletions.
3 changes: 3 additions & 0 deletions plugin/code_actions.py
Expand Up @@ -19,6 +19,9 @@ class LspCodeActionsCommand(LspTextCommand):
def __init__(self, view):
super().__init__(view, 'codeActionProvider')

def is_enabled(self):
return self.has_client_with_capability()

def run(self, edit, event=None):
client = client_for_view(self.view)
if client:
Expand Down
8 changes: 3 additions & 5 deletions plugin/core/clients.py
Expand Up @@ -37,21 +37,19 @@ def __init__(self, state=ClientStates.STARTING, client=None):


class LspTextCommand(TextCommand):
def __init__(self, view, capability='', last_check=lambda: True):
def __init__(self, view, capability=''):
super().__init__(view)
self.capability = capability
self.last_check = last_check

def is_visible(self):
return is_supported_view(self.view)

def is_enabled(self):
def has_client_with_capability(self):
client = client_for_view(self.view)
if client and client.has_capability(self.capability):
return self.last_check()
return True
return False


def window_configs(window: sublime.Window) -> 'Dict[str, ConfigState]':
if window.id() in clients_by_window:
return clients_by_window[window.id()]
Expand Down
3 changes: 3 additions & 0 deletions plugin/definition.py
Expand Up @@ -12,6 +12,9 @@ class LspSymbolDefinitionCommand(LspTextCommand):
def __init__(self, view):
super().__init__(view, 'definitionProvider')

def is_enabled(self):
return self.has_client_with_capability()

def run(self, edit, event=None):
client = client_for_view(self.view)
if client:
Expand Down
11 changes: 8 additions & 3 deletions plugin/formatting.py
Expand Up @@ -9,6 +9,9 @@ class LspFormatDocumentCommand(LspTextCommand):
def __init__(self, view):
super().__init__(view, 'documentFormattingProvider')

def is_enabled(self):
return self.has_client_with_capability()

def run(self, edit):
client = client_for_view(self.view)
if client:
Expand All @@ -33,13 +36,15 @@ def handle_response(self, response, pos):

class LspFormatDocumentRangeCommand(LspTextCommand):
def __init__(self, view):
def last_check():
super().__init__(view, 'documentRangeFormattingProvider')

def is_enabled(self):
if self.has_client_with_capability():
if len(self.view.sel()) == 1:
region = self.view.sel()[0]
if region.begin() != region.end():
return True
return False
super().__init__(view, 'documentRangeFormattingProvider', last_check)
return False

def run(self, _):
client = client_for_view(self.view)
Expand Down
15 changes: 7 additions & 8 deletions plugin/hover.py
Expand Up @@ -3,9 +3,9 @@
import sublime_plugin
import webbrowser

from .core.configurations import is_supported_syntax, is_supported_view
from .core.configurations import is_supported_syntax
from .core.diagnostics import get_point_diagnostics
from .core.clients import client_for_view
from .core.clients import LspTextCommand
from .core.protocol import Request
from .core.documents import get_document_position
from .core.popups import popup_css, popup_class
Expand All @@ -29,14 +29,13 @@ def on_hover(self, point, hover_zone):
self.view.run_command("lsp_hover", {"point": point})


class LspHoverCommand(sublime_plugin.TextCommand):
class LspHoverCommand(LspTextCommand):
def __init__(self, view):
super().__init__(view, 'definitionProvider')

def is_enabled(self):
# TODO: check what kind of scope we're in.
if is_supported_view(self.view):
client = client_for_view(self.view)
if client:
return client.has_capability('hoverProvider')
return False
return self.has_client_with_capability()

def run(self, edit, point=None):
if point is None:
Expand Down
7 changes: 6 additions & 1 deletion plugin/references.py
Expand Up @@ -30,7 +30,12 @@ def create_references_panel(window: sublime.Window):

class LspSymbolReferencesCommand(LspTextCommand):
def __init__(self, view):
super().__init__(view, 'referencesProvider', lambda: is_at_word(view, None))
super().__init__(view, 'referencesProvider')

def is_enabled(self):
if self.has_client_with_capability():
return is_at_word(self.view, None)
return False

def run(self, edit, event=None):
client = client_for_view(self.view)
Expand Down
7 changes: 6 additions & 1 deletion plugin/rename.py
Expand Up @@ -7,8 +7,13 @@

class LspSymbolRenameCommand(LspTextCommand):
def __init__(self, view):
super().__init__(view, 'renameProvider')

def is_enabled(self):
# TODO: check what kind of scope we're in.
super().__init__(view, 'renameProvider', lambda: is_at_word(view, None))
if self.has_client_with_capability():
return is_at_word(self.view, None)
return False

def run(self, edit, event=None):
pos = get_position(self.view, event)
Expand Down
3 changes: 3 additions & 0 deletions plugin/symbols.py
Expand Up @@ -40,6 +40,9 @@ class LspDocumentSymbolsCommand(LspTextCommand):
def __init__(self, view):
super().__init__(view, 'documentSymbolProvider')

def is_enabled(self):
self.has_client_with_capability()

def run(self, edit):
client = client_for_view(self.view)
if client:
Expand Down

0 comments on commit c314e37

Please sign in to comment.