Skip to content

Commit 00a50e4

Browse files
code refactoring, extended surround_with options, started working on refactor command, added in manage_cli_command when it is defined custom nodejs path
1 parent b490fec commit 00a50e4

16 files changed

+301
-76
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ For _Linux-based OS_ **REMEMBER** to add `:` (for _Windows OS_ **REMEMBER** to a
102102

103103
### Errors
104104
![](https://drive.google.com/uc?authuser=0&id=1r8IDItL03tPFwCCsTIdW54rRpascnHAF&export=download)
105+
![](https://drive.google.com/uc?authuser=0&id=1hjtcvuMNZe7NP3_nE10X_6qEEbLvl-AA&export=download)
105106

106107
### Projects with terminal ([TerminalView](https://github.com/Wramberg/TerminalView))
107108
![](https://drive.google.com/uc?authuser=0&id=1gmC6GROJXyhV8DZTHw8Zw_KGlB13g_bL&export=download)

_generated_2018_01_13_at_02_34_47.py renamed to _generated_2018_01_14_at_01_07_53.py

Lines changed: 126 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
33533365
class 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

47134735
import 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

47184738
class evaluate_javascriptCommand(manage_cliCommand):
47194739

@@ -4776,7 +4796,6 @@ def is_visible(self, **args) :
47764796
return False
47774797
return True
47784798

4779-
47804799
import sublime, sublime_plugin
47814800
import 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

57425849
def start():
57435850

helper/Context.sublime-menu

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
[
2+
{ "caption": "-" },
3+
{
4+
"caption": "Evaluate JavaScript",
5+
"children": [
6+
{
7+
"caption": "Evaluate JavaScript",
8+
"id": "evaluate_javascript",
9+
"command": "evaluate_javascript",
10+
},
11+
{ "caption": "-" },
12+
{
13+
"caption": "Evaluate JavaScript on this line",
14+
"id": "evaluate_javascript_line",
15+
"command": "evaluate_javascript",
16+
"args": {
17+
"is_line": true
18+
}
19+
}
20+
]
21+
},
222
{"caption": "-"},
323
{
424
"caption": "Split string lines to variable",
@@ -78,10 +98,25 @@
7898
"command": "surround_with",
7999
"args": {"case": "try_catch_statement"}
80100
},
101+
{
102+
"caption": "try finally statement",
103+
"command": "surround_with",
104+
"args": {"case": "try_finally_statement"}
105+
},
81106
{
82107
"caption": "try catch finally statement",
83108
"command": "surround_with",
84109
"args": {"case": "try_catch_finally_statement"}
110+
},
111+
{
112+
"caption": "block {}",
113+
"command": "surround_with",
114+
"args": {"case": "block"}
115+
},
116+
{
117+
"caption": "function",
118+
"command": "surround_with",
119+
"args": {"case": "function"}
85120
}
86121
]
87122
},
@@ -97,6 +132,11 @@
97132
]
98133
},
99134
{ "caption": "-" },
135+
{
136+
"caption": "Sort JavaScript Imports",
137+
"id": "sort_javascript_imports",
138+
"command": "sort_javascript_imports"
139+
},
100140
{
101141
"caption": "Expand Abbreviation",
102142
"id": "expand_abbreviation",

helper/evaluate_javascript/Context.sublime-menu

Lines changed: 0 additions & 23 deletions
This file was deleted.

helper/evaluate_javascript/main.py

Lines changed: 0 additions & 4 deletions
This file was deleted.

helper/evaluate_javascript/evaluate_javascript_command.py renamed to helper/evaluate_javascript_command.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import shlex, tempfile
1+
import sublime, sublime_plugin
2+
import traceback, os, json, io, sys, imp,shlex, tempfile
23

34
class evaluate_javascriptCommand(manage_cliCommand):
45

helper/main.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
${include javascript_completions/main.py}
2424

25-
${include evaluate_javascript/main.py}
25+
${include evaluate_javascript_command.py}
2626

2727
${include bookmarks/main.py}
2828

@@ -34,4 +34,6 @@
3434

3535
${include unused_variables_view_event_listener.py}
3636

37-
${include sort_javascript_imports/main.py}
37+
${include sort_javascript_imports_command.py}
38+
39+
${include refactor_command.py}

0 commit comments

Comments
 (0)