@@ -732,7 +732,12 @@ def trim_Region(view, region):
732732
733733 @staticmethod
734734 def selection_in_js_scope (view , point = - 1 , except_for = "" ):
735- sel_begin = view .sel ()[0 ].begin () if point == - 1 else point
735+ selections = view .sel ()
736+
737+ if not selections :
738+ return False
739+
740+ sel_begin = selections [0 ].begin () if point == - 1 else point
736741
737742 return view .match_selector (
738743 sel_begin ,
@@ -783,14 +788,7 @@ def replace_without_tab(view, region, pre="", after="", add_to_each_line_before=
783788
784789 @staticmethod
785790 def get_whitespace_from_line_begin (view , region ) :
786- line = view .line (region )
787- whitespace = ""
788- count = line .begin ()
789- sel_begin = region .begin ()
790- while count != sel_begin :
791- count = count + 1
792- whitespace = whitespace + " "
793- return whitespace
791+ return " " * ( region .begin () - view .line (region ).begin () )
794792
795793 @staticmethod
796794 def add_whitespace_indentation (view , region , string , replace = "\t " , add_whitespace_end = True ) :
@@ -1658,8 +1656,20 @@ def run(self, **kwargs):
16581656 else :
16591657 self .path_cli = self .settings ["project_settings" ]["npm_custom_path" ] or javascriptCompletions .get ("npm_custom_path" ) or NPM_EXEC
16601658 else :
1661- self .path_cli = self .settings [self .settings_name ]["cli_custom_path" ] if self .settings [self .settings_name ]["cli_custom_path" ] else ( javascriptCompletions .get (self .custom_name + "_custom_path" ) if javascriptCompletions .get (self .custom_name + "_custom_path" ) else self .cli )
1662- self .command = kwargs .get ("command" )
1659+ self .path_cli = self .settings [self .settings_name ]["cli_custom_path" ] if self .settings [self .settings_name ]["cli_custom_path" ] else ( javascriptCompletions .get (self .custom_name + "_custom_path" ) if javascriptCompletions .get (self .custom_name + "_custom_path" ) else self .cli )
1660+
1661+ if sublime .platform () != "windows" and (self .settings ["project_settings" ]["node_js_custom_path" ] or javascriptCompletions .get ("node_js_custom_path" )):
1662+ if os .path .isabs (self .path_cli ) :
1663+ self .command = [shlex .quote (self .path_cli )]
1664+ else :
1665+ self .command = ["$(which " + shlex .quote (self .path_cli )+ ")" ]
1666+ self .path_cli = self .settings ["project_settings" ]["node_js_custom_path" ] or javascriptCompletions .get ("node_js_custom_path" )
1667+
1668+
1669+ if not self .command :
1670+ self .command = kwargs .get ("command" )
1671+ else :
1672+ self .command += kwargs .get ("command" )
16631673
16641674 self .prepare_command (** kwargs )
16651675
@@ -3350,12 +3360,12 @@ def on_modified_async_with_thread(self, *args, **kwargs):
33503360 return
33513361
33523362
3363+ import sublime , sublime_plugin
3364+
33533365class surround_withCommand (sublime_plugin .TextCommand ):
33543366 def run (self , edit , ** args ):
33553367 view = self .view
33563368 selections = view .sel ()
3357- region = None
3358- sub = None
33593369 case = args .get ("case" )
33603370 if case == "if_else_statement" :
33613371 if len (selections ) != 2 :
@@ -3397,9 +3407,21 @@ def run(self, edit, **args):
33973407 new_text = Util .replace_with_tab (view , selection , space + "\n " + space + "try {\n " + space , "\n " + space + "} catch (e) {\n " + space + "\n " + space + "}\n " + space )
33983408 view .replace (edit , selection , new_text )
33993409
3410+ elif case == "try_finally_statement" :
3411+ new_text = Util .replace_with_tab (view , selection , space + "\n " + space + "try {\n " + space , "\n " + space + "} finally {\n " + space + "\n " + space + "}\n " + space )
3412+ view .replace (edit , selection , new_text )
3413+
34003414 elif case == "try_catch_finally_statement" :
34013415 new_text = Util .replace_with_tab (view , selection , space + "\n " + space + "try {\n " + space , "\n " + space + "} catch (e) {\n " + space + "\n " + space + "} finally {\n " + space + "\n " + space + "}\n " + space )
34023416 view .replace (edit , selection , new_text )
3417+
3418+ elif case == "function" :
3419+ new_text = Util .replace_with_tab (view , selection , space + "\n " + space + "function () {\n " + space , "\n " + space + "}\n " + space )
3420+ view .replace (edit , selection , new_text )
3421+
3422+ elif case == "block" :
3423+ new_text = Util .replace_with_tab (view , selection , space + "\n " + space + "{\n " + space , "\n " + space + "}\n " + space )
3424+ view .replace (edit , selection , new_text )
34033425
34043426 def is_enabled (self , ** args ) :
34053427 view = self .view
@@ -4711,9 +4733,7 @@ class navigate_flow_errorsCommand(navigate_regionsCommand, sublime_plugin.TextCo
47114733 region_key = "flow_error"
47124734
47134735import sublime , sublime_plugin
4714- import traceback , os , json , io , sys , imp
4715-
4716- import shlex , tempfile
4736+ import traceback , os , json , io , sys , imp ,shlex , tempfile
47174737
47184738class evaluate_javascriptCommand (manage_cliCommand ):
47194739
@@ -4776,7 +4796,6 @@ def is_visible(self, **args) :
47764796 return False
47774797 return True
47784798
4779-
47804799import sublime , sublime_plugin
47814800import json , time
47824801
@@ -5731,13 +5750,101 @@ def get_imports(self):
57315750
57325751 def is_enabled (self ):
57335752 view = self .view
5734- return Util .selection_in_js_scope (view ) and not view .find_by_selector ('source.js.embedded.html' )
5753+ if not Util .selection_in_js_scope (view ) and view .find_by_selector ('source.js.embedded.html' ):
5754+ return False
5755+
5756+ if view .find_by_selector ('meta.import.js' ):
5757+ return True
5758+
5759+ # try JavaScript (Babel) syntax
5760+ import_regions = view .find_by_selector ('keyword.operator.module.js' )
5761+ for import_region in import_regions :
5762+ if (view .substr (import_region ).startswith ("import" )) :
5763+ return True
5764+
5765+ return False
57355766
57365767 def is_visible (self ):
57375768 view = self .view
5738- return Util .selection_in_js_scope (view ) and not view .find_by_selector ('source.js.embedded.html' )
5769+ if not Util .selection_in_js_scope (view ) and view .find_by_selector ('source.js.embedded.html' ):
5770+ return False
5771+
5772+ if view .find_by_selector ('meta.import.js' ):
5773+ return True
5774+
5775+ # try JavaScript (Babel) syntax
5776+ import_regions = view .find_by_selector ('keyword.operator.module.js' )
5777+ for import_region in import_regions :
5778+ if (view .substr (import_region ).startswith ("import" )) :
5779+ return True
5780+
5781+ return False
57395782
57405783
5784+ # import sublime, sublime_plugin
5785+ # import re
5786+
5787+
5788+ # { "caption": "-" },
5789+ # {
5790+ # "caption": "Refactor (Working on it ...)",
5791+ # "id": "refactor",
5792+ # "children": [
5793+ # {
5794+ # "caption": "Extract",
5795+ # "children": [
5796+ # {
5797+ # "caption": "Method",
5798+ # "command": "refactor",
5799+ # "args": {"case": "extract_method"}
5800+ # }
5801+ # ]
5802+ # }
5803+ # ]
5804+ # }
5805+
5806+ # class refactorCommand(sublime_plugin.TextCommand):
5807+ # def run(self, edit, **args):
5808+ # view = self.view
5809+ # case = args.get("case")
5810+ # if not "answer" in args :
5811+ # caption = ""
5812+ # initial_text = ""
5813+
5814+ # if case == "extract_method" :
5815+ # caption = "Method:"
5816+ # initial_text = "func ()"
5817+
5818+ # view.window().show_input_panel(caption, initial_text, lambda answer: view.run_command('refactor', args={"case": case, "answer": answer}), None, None)
5819+ # else :
5820+ # answer = args.get("answer").strip()
5821+ # scope = view.scope_name(view.sel()[0].begin())
5822+ # space = Util.get_whitespace_from_line_begin(view, view.sel()[0])
5823+ # if case == "extract_method" :
5824+ # new_text = Util.replace_with_tab(view, view.sel()[0], "\t\n\t"+answer+" {\n\t", "\n\t}\n")
5825+ # view.replace(edit, view.sel()[0], "this."+(re.sub('\s+\(', '(', answer)) )
5826+ # region_class = Util.get_region_scope_first_match(view, scope, view.sel()[0], 'meta.class.js')["region"]
5827+ # view.insert(edit, region_class.end()-1, new_text)
5828+
5829+ # def is_enabled(self, **args) :
5830+ # view = self.view
5831+ # if not Util.selection_in_js_scope(view) :
5832+ # return False
5833+ # selections = view.sel()
5834+ # for selection in selections :
5835+ # if view.substr(selection).strip() != "" :
5836+ # return True
5837+ # return False
5838+
5839+ # def is_visible(self, **args) :
5840+ # view = self.view
5841+ # if not Util.selection_in_js_scope(view) :
5842+ # return False
5843+ # selections = view.sel()
5844+ # for selection in selections :
5845+ # if view.substr(selection).strip() != "" :
5846+ # return True
5847+ # return False
57415848
57425849def start ():
57435850
0 commit comments