Skip to content

Commit

Permalink
fix: exception occurs when first LSP popup
Browse files Browse the repository at this point in the history
- The syntax should be gotten dynamically since it can be changed at anytime.
- The syntax may be None somehow... Use a empty string then.

Signed-off-by: Jack Cherng <[email protected]>
  • Loading branch information
jfcherng authored and cristianl committed Jun 23, 2020
1 parent 7399201 commit 281df2b
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions hyper_click_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ def __init__(self, view):
self.view = view
self.settings = sublime.load_settings('hyper_click.sublime-settings')
self.window = view.window()
self.syntax = self.view.settings().get('syntax')
self.lang = self.get_lang(self.syntax)

def run(self, edit):
self.window = self.view.window()
v = self.view
lang = self.get_lang()

if len(v.sel()) != 1:
return
Expand All @@ -31,12 +30,12 @@ def run(self, edit):
cursor = v.sel()[0].a
line_range = v.line(cursor)
line_content = v.substr(line_range).strip()
matched = self.is_valid_line(line_content)
matched = self.is_valid_line(lang, line_content)
if matched:
destination_str = matched.group(1)
file_path = HyperClickPathResolver(
v, destination_str,
self.roots, self.lang, self.settings,
self.roots, lang, self.settings,
self.proj_settings
)
resolved_path = file_path.resolve()
Expand All @@ -46,20 +45,22 @@ def run(self, edit):
else:
self.window.open_file(resolved_path)

def is_valid_line(self, line_content):
def is_valid_line(self, lang, line_content):
import_lines = self.settings.get('import_line_regex', {})
for regex_str in import_lines.get(self.lang, []):
for regex_str in import_lines.get(lang, []):
pattern = re.compile(regex_str)
matched = pattern.match(line_content)
if matched:
return matched
return False

def get_lang(self, syntax):
def get_lang(self):
syntax = self.view.settings().get('syntax') or ''

supported_syntaxes = self.settings.get('supported_syntaxes')
for (lang, syntax_names) in supported_syntaxes.items():
for syn in syntax_names:
if self.syntax.endswith('/' + syn):
if syntax.endswith('/' + syn):
return lang
return ''

Expand All @@ -72,13 +73,10 @@ def is_enabled(self):
cursor = v.sel()[0]
line_range = v.line(cursor)
line_content = v.substr(line_range).strip()
matched = self.is_valid_line(line_content)
matched = self.is_valid_line(self.get_lang(), line_content)
if matched:
return True
return False

def is_visible(self):
if len(self.lang) == 0:
return False
else:
return self.is_enabled()
return self.get_lang() != '' and self.is_enabled()

0 comments on commit 281df2b

Please sign in to comment.