Skip to content

Commit e6855ec

Browse files
added convert to arrow function refactor command, improved extract method refactor command, updated default_autocomplete and surround_with_command with more options
1 parent ca1da10 commit e6855ec

9 files changed

+469
-54
lines changed

Context.sublime-menu

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@
7373
"caption": "JavaScript Refactor",
7474
"id": "refactor",
7575
"children": [
76+
{
77+
"caption": "Converto to arrow function",
78+
"command": "refactor",
79+
"args": {
80+
"case": "convert_to_arrow_function"
81+
}
82+
},
83+
{ "caption": "-" },
7684
{
7785
"caption": "Safe Copy",
7886
"command": "refactor",
@@ -94,6 +102,7 @@
94102
"case": "safe_delete"
95103
}
96104
},
105+
{ "caption": "-" },
97106
{
98107
"caption": "Extract",
99108
"children": [
@@ -174,15 +183,36 @@
174183
"command": "surround_with",
175184
"args": {"case": "try_catch_finally_statement"}
176185
},
186+
{ "caption": "-" },
177187
{
178-
"caption": "block {}",
188+
"caption": "function",
179189
"command": "surround_with",
180-
"args": {"case": "block"}
190+
"args": {"case": "function"}
181191
},
182192
{
183-
"caption": "function",
193+
"caption": "anonymous function",
184194
"command": "surround_with",
185-
"args": {"case": "function"}
195+
"args": {"case": "anonymous_function"}
196+
},
197+
{
198+
"caption": "arrow function",
199+
"command": "surround_with",
200+
"args": {"case": "arrow_function"}
201+
},
202+
{
203+
"caption": "async function",
204+
"command": "surround_with",
205+
"args": {"case": "async_function"}
206+
},
207+
{
208+
"caption": "iife function",
209+
"command": "surround_with",
210+
"args": {"case": "iife_function"}
211+
},
212+
{
213+
"caption": "block {}",
214+
"command": "surround_with",
215+
"args": {"case": "block"}
186216
}
187217
]
188218
},

Main.sublime-menu

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@
7070
]
7171
},
7272
{ "caption": "-" },
73+
{
74+
"caption": ".flowconfig docs",
75+
"command": "open_url",
76+
"args": {"url": "https://flow.org/en/docs/config/"}
77+
},
78+
{ "caption": "-" },
7379
{
7480
"caption": "GitHub Project",
7581
"command": "open_url",

_generated_2018_02_04_at_23_42_01.py renamed to _generated_2018_02_05_at_03_39_47.py

Lines changed: 200 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3642,9 +3642,25 @@ def run(self, edit, **args):
36423642
view.replace(edit, selection, new_text)
36433643

36443644
elif case == "function" :
3645+
new_text = Util.replace_with_tab(view, selection, space+"\n"+space+"function func_name () {\n"+space, "\n"+space+"}\n"+space)
3646+
view.replace(edit, selection, new_text)
3647+
3648+
elif case == "anonymous_function" :
36453649
new_text = Util.replace_with_tab(view, selection, space+"\n"+space+"function () {\n"+space, "\n"+space+"}\n"+space)
36463650
view.replace(edit, selection, new_text)
36473651

3652+
elif case == "arrow_function" :
3653+
new_text = Util.replace_with_tab(view, selection, space+"\n"+space+"() => {\n"+space, "\n"+space+"}\n"+space)
3654+
view.replace(edit, selection, new_text)
3655+
3656+
elif case == "async_function" :
3657+
new_text = Util.replace_with_tab(view, selection, space+"\n"+space+"async function func_name () {\n"+space, "\n"+space+"}\n"+space)
3658+
view.replace(edit, selection, new_text)
3659+
3660+
elif case == "iife_function" :
3661+
new_text = Util.replace_with_tab(view, selection, space+"\n"+space+"(function () {\n"+space, "\n"+space+"})()\n"+space)
3662+
view.replace(edit, selection, new_text)
3663+
36483664
elif case == "block" :
36493665
new_text = Util.replace_with_tab(view, selection, space+"\n"+space+"{\n"+space, "\n"+space+"}\n"+space)
36503666
view.replace(edit, selection, new_text)
@@ -6085,7 +6101,7 @@ class RefactorCommand(sublime_plugin.TextCommand):
60856101
def run(self, edit, **args):
60866102
view = self.view
60876103
case = args.get("case")
6088-
scope = view.scope_name(view.sel()[0].begin())
6104+
scope = view.scope_name(view.sel()[0].begin()).strip()
60896105

60906106
if case == "safe_move" :
60916107
windowView = WindowView(title="Refactor - Safe Move", use_compare_layout=True)
@@ -6133,7 +6149,8 @@ def run(self, edit, **args):
61336149
select_options = ['Global scope', 'Current scope', 'Class method']
61346150
if not view.match_selector(view.sel()[0].begin(), 'meta.class.js'):
61356151
select_options.remove('Class method')
6136-
if len(scope.split(" ")) < 2:
6152+
print(scope, len(scope.split(" ")))
6153+
if len(scope.split(" ")) <= 2:
61376154
select_options.remove('Global scope')
61386155

61396156
windowView = WindowView(title="Refactor - Extract Method", use_compare_layout=True)
@@ -6180,6 +6197,9 @@ def run(self, edit, **args):
61806197
windowView.addCloseButton(text="CANCEL", scope="javascriptenhancements.button_cancel")
61816198
windowView.add(text=" \n")
61826199

6200+
elif case == "convert_to_arrow_function" :
6201+
self.view.run_command("refactor_convert_to_arrow_function")
6202+
61836203
def is_enabled(self, **args) :
61846204

61856205
view = self.view
@@ -6738,10 +6758,9 @@ def is_visible(self, **args) :
67386758
class RefactorExtractMethodCommand(sublime_plugin.TextCommand):
67396759
def run(self, edit, **args):
67406760
view = self.view
6741-
6761+
selection = view.sel()[0]
67426762
inputs = args.get("inputs")
6743-
scope = view.scope_name(view.sel()[0].begin())
6744-
space = Util.get_whitespace_from_line_begin(view, view.sel()[0])
6763+
scope = view.scope_name(selection.begin()).strip()
67456764
function_name = inputs["function_name"].strip()
67466765
parameters = inputs["parameters"].strip()
67476766
if not parameters.startswith("("):
@@ -6751,32 +6770,92 @@ def run(self, edit, **args):
67516770

67526771
if inputs["scope"] == "Class method":
67536772

6754-
view.replace(edit, view.sel()[0], "this."+function_name+parameters)
6755-
region_class = Util.get_region_scope_first_match(view, scope, view.sel()[0], 'meta.class.js')["region"]
6756-
new_text = Util.replace_with_tab(view, view.sel()[0], ("\t\n" if not Util.prev_line_is_empty(view, sublime.Region(region_class.end(), region_class.end())) else "")+"\t"+function_name+" "+parameters+" {\n\t", "\n\t}\n")
6773+
view.replace(edit, selection, "this."+function_name+parameters)
6774+
region_class = Util.get_region_scope_first_match(view, scope, selection, 'meta.class.js')["region"]
6775+
new_text = Util.replace_with_tab(view, selection, ("\t\n" if not Util.prev_line_is_empty(view, sublime.Region(region_class.end(), region_class.end())) else "")+"\t"+function_name+" "+parameters+" {\n\t", "\n\t}\n")
67576776

67586777
view.insert(edit, region_class.end()-1, new_text)
67596778

6760-
elif inputs["scope"] == "Current Scope":
6779+
elif inputs["scope"] == "Current scope":
67616780

6762-
new_text = Util.replace_with_tab(view, view.sel()[0], "function "+function_name+" "+parameters+" {\n"+space, "\n"+space+"}\n"+space)
6781+
flow_cli = "flow"
6782+
is_from_bin = True
6783+
chdir = ""
6784+
use_node = True
6785+
bin_path = ""
67636786

6764-
if Util.region_contains_scope(view, view.sel()[0], "variable.language.this.js"):
6765-
view.replace(edit, view.sel()[0], function_name+".call(this"+(", "+parameters[1:-1] if parameters[1:-1].strip() else "")+")" )
6766-
else:
6767-
view.replace(edit, view.sel()[0], function_name+parameters)
6768-
view.insert(edit, view.sel()[0].begin(), new_text)
6787+
settings = get_project_settings()
6788+
if settings and settings["project_settings"]["flow_cli_custom_path"]:
6789+
flow_cli = os.path.basename(settings["project_settings"]["flow_cli_custom_path"])
6790+
bin_path = os.path.dirname(settings["project_settings"]["flow_cli_custom_path"])
6791+
is_from_bin = False
6792+
chdir = settings["project_dir_name"]
6793+
use_node = False
6794+
6795+
node = NodeJS(check_local=True)
6796+
6797+
result = node.execute_check_output(
6798+
flow_cli,
6799+
[
6800+
'ast',
6801+
'--from', 'sublime_text'
6802+
],
6803+
is_from_bin=is_from_bin,
6804+
use_fp_temp=True,
6805+
fp_temp_contents=view.substr(sublime.Region(0, view.size())),
6806+
is_output_json=True,
6807+
chdir=chdir,
6808+
bin_path=bin_path,
6809+
use_node=use_node
6810+
)
6811+
6812+
if result[0]:
6813+
if "body" in result[1]:
6814+
body = result[1]["body"]
6815+
items = Util.nested_lookup("type", ["BlockStatement"], body)
6816+
last_block_statement = None
6817+
last_item = None
6818+
region = None
6819+
6820+
for item in items:
6821+
region = sublime.Region(int(item["range"][0]), int(item["range"][1]))
6822+
if region.contains(selection):
6823+
last_block_statement = region
6824+
last_item = item
6825+
6826+
if last_block_statement:
6827+
for item in last_item["body"]:
6828+
r = sublime.Region(int(item["range"][0]), int(item["range"][1]))
6829+
if r.contains(selection):
6830+
region = r
6831+
break
6832+
else:
6833+
for item in body:
6834+
r = sublime.Region(int(item["range"][0]), int(item["range"][1]))
6835+
if r.contains(selection):
6836+
region = r
6837+
break
6838+
6839+
if region:
6840+
6841+
space = Util.get_whitespace_from_line_begin(view, region)
6842+
new_text = Util.replace_with_tab(view, selection, "function "+function_name+" "+parameters+" {\n"+space, "\n"+space+"}\n"+space)
6843+
if Util.region_contains_scope(view, selection, "variable.language.this.js"):
6844+
view.replace(edit, selection, function_name+".call(this"+(", "+parameters[1:-1] if parameters[1:-1].strip() else "")+")" )
6845+
else:
6846+
view.replace(edit, selection, function_name+parameters)
6847+
view.insert(edit, region.begin(), new_text)
67696848

67706849
elif inputs["scope"] == "Global scope":
67716850

6772-
region_class = Util.get_region_scope_first_match(view, scope, view.sel()[0], scope.split(" ")[1])["region"]
6851+
region_class = Util.get_region_scope_first_match(view, scope, selection, scope.split(" ")[1])["region"]
67736852
space = Util.get_whitespace_from_line_begin(view, region_class)
6774-
new_text = Util.replace_with_tab(view, view.sel()[0], "function "+function_name+" "+parameters+" {\n"+space, "\n"+space+"}\n\n"+space)
6853+
new_text = Util.replace_with_tab(view, selection, "function "+function_name+" "+parameters+" {\n"+space, "\n"+space+"}\n\n"+space)
67756854

6776-
if Util.region_contains_scope(view, view.sel()[0], "variable.language.this.js"):
6777-
view.replace(edit, view.sel()[0], function_name+".call(this"+(", "+parameters[1:-1] if parameters[1:-1].strip() else "")+")" )
6855+
if Util.region_contains_scope(view, selection, "variable.language.this.js"):
6856+
view.replace(edit, selection, function_name+".call(this"+(", "+parameters[1:-1] if parameters[1:-1].strip() else "")+")" )
67786857
else:
6779-
view.replace(edit, view.sel()[0], function_name+parameters)
6858+
view.replace(edit, selection, function_name+parameters)
67806859
view.insert(edit, region_class.begin(), new_text)
67816860

67826861
def is_enabled(self, **args) :
@@ -7370,6 +7449,107 @@ def is_visible(self, **args) :
73707449
return selection.begin() != selection.end()
73717450

73727451

7452+
import sublime, sublime_plugin
7453+
7454+
class RefactorConvertToArrowFunctionCommand(sublime_plugin.TextCommand):
7455+
def run(self, edit, **args):
7456+
view = self.view
7457+
selection = view.sel()[0]
7458+
7459+
flow_cli = "flow"
7460+
is_from_bin = True
7461+
chdir = ""
7462+
use_node = True
7463+
bin_path = ""
7464+
7465+
settings = get_project_settings()
7466+
if settings and settings["project_settings"]["flow_cli_custom_path"]:
7467+
flow_cli = os.path.basename(settings["project_settings"]["flow_cli_custom_path"])
7468+
bin_path = os.path.dirname(settings["project_settings"]["flow_cli_custom_path"])
7469+
is_from_bin = False
7470+
chdir = settings["project_dir_name"]
7471+
use_node = False
7472+
7473+
node = NodeJS(check_local=True)
7474+
7475+
result = node.execute_check_output(
7476+
flow_cli,
7477+
[
7478+
'ast',
7479+
'--from', 'sublime_text'
7480+
],
7481+
is_from_bin=is_from_bin,
7482+
use_fp_temp=True,
7483+
fp_temp_contents=view.substr(sublime.Region(0, view.size())),
7484+
is_output_json=True,
7485+
chdir=chdir,
7486+
bin_path=bin_path,
7487+
use_node=use_node
7488+
)
7489+
7490+
if result[0]:
7491+
7492+
body = result[1]["body"]
7493+
items = Util.nested_lookup("type", ["FunctionExpression"], body)
7494+
for item in items:
7495+
region = sublime.Region(int(item["range"][0]), int(item["range"][1]))
7496+
if region.contains(selection):
7497+
text = view.substr(region)
7498+
7499+
if not text.startswith("function"):
7500+
return
7501+
7502+
text = text[8:].lstrip()
7503+
block_statement_region = sublime.Region(int(item["body"]["range"][0]), int(item["body"]["range"][1]))
7504+
block_statement = view.substr(block_statement_region)
7505+
index = text.index(block_statement)
7506+
7507+
while text[index - 1] == " ":
7508+
text = text[0:index - 1] + text[index:]
7509+
index = index - 1
7510+
7511+
text = text[0:index] + " => " + text[index:]
7512+
view.replace(edit, region, text)
7513+
7514+
break
7515+
7516+
else:
7517+
sublime.error_message("Cannot convert the function. Some problems occured.")
7518+
7519+
def is_enabled(self, **args) :
7520+
view = self.view
7521+
if not Util.selection_in_js_scope(view) :
7522+
return False
7523+
selection = view.sel()[0]
7524+
7525+
scope = view.scope_name(selection.begin()).strip()
7526+
if "meta.block.js" in scope:
7527+
region_scope = Util.get_region_scope_last_match(view, scope, selection, "meta.block.js")
7528+
else:
7529+
region_scope = Util.get_region_scope_last_match(view, scope, selection, "meta.group.braces.curly.js")
7530+
7531+
if not region_scope:
7532+
return False
7533+
7534+
return True
7535+
7536+
def is_visible(self, **args) :
7537+
view = self.view
7538+
if not Util.selection_in_js_scope(view) :
7539+
return False
7540+
selection = view.sel()[0]
7541+
7542+
scope = view.scope_name(selection.begin()).strip()
7543+
if "meta.block.js" in scope:
7544+
region_scope = Util.get_region_scope_last_match(view, scope, selection, "meta.block.js")
7545+
else:
7546+
region_scope = Util.get_region_scope_last_match(view, scope, selection, "meta.group.braces.curly.js")
7547+
7548+
if not region_scope:
7549+
return False
7550+
7551+
return True
7552+
73737553
import sublime, sublime_plugin
73747554
import os
73757555

changelog/0.14.0.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ v0.14.0
1919
* Extract Parameter
2020
* Extract Variable
2121
* Extract Field
22+
* Convert a function to an arrow function
2223
- Added "Open TerminalView Here…" command to Context Menu and Side Bar Menu
2324
- Forced "auto_complete_delay" option to 0 when showing JavaScript auto-completions
25+
- Updated default_autocomplete and surround_with_command with more options
2426

2527
## Misc
2628

0 commit comments

Comments
 (0)