@@ -3642,9 +3642,25 @@ def run(self, edit, **args):
3642
3642
view .replace (edit , selection , new_text )
3643
3643
3644
3644
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" :
3645
3649
new_text = Util .replace_with_tab (view , selection , space + "\n " + space + "function () {\n " + space , "\n " + space + "}\n " + space )
3646
3650
view .replace (edit , selection , new_text )
3647
3651
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
+
3648
3664
elif case == "block" :
3649
3665
new_text = Util .replace_with_tab (view , selection , space + "\n " + space + "{\n " + space , "\n " + space + "}\n " + space )
3650
3666
view .replace (edit , selection , new_text )
@@ -6085,7 +6101,7 @@ class RefactorCommand(sublime_plugin.TextCommand):
6085
6101
def run (self , edit , ** args ):
6086
6102
view = self .view
6087
6103
case = args .get ("case" )
6088
- scope = view .scope_name (view .sel ()[0 ].begin ())
6104
+ scope = view .scope_name (view .sel ()[0 ].begin ()). strip ()
6089
6105
6090
6106
if case == "safe_move" :
6091
6107
windowView = WindowView (title = "Refactor - Safe Move" , use_compare_layout = True )
@@ -6133,7 +6149,8 @@ def run(self, edit, **args):
6133
6149
select_options = ['Global scope' , 'Current scope' , 'Class method' ]
6134
6150
if not view .match_selector (view .sel ()[0 ].begin (), 'meta.class.js' ):
6135
6151
select_options .remove ('Class method' )
6136
- if len (scope .split (" " )) < 2 :
6152
+ print (scope , len (scope .split (" " )))
6153
+ if len (scope .split (" " )) <= 2 :
6137
6154
select_options .remove ('Global scope' )
6138
6155
6139
6156
windowView = WindowView (title = "Refactor - Extract Method" , use_compare_layout = True )
@@ -6180,6 +6197,9 @@ def run(self, edit, **args):
6180
6197
windowView .addCloseButton (text = "CANCEL" , scope = "javascriptenhancements.button_cancel" )
6181
6198
windowView .add (text = " \n " )
6182
6199
6200
+ elif case == "convert_to_arrow_function" :
6201
+ self .view .run_command ("refactor_convert_to_arrow_function" )
6202
+
6183
6203
def is_enabled (self , ** args ) :
6184
6204
6185
6205
view = self .view
@@ -6738,10 +6758,9 @@ def is_visible(self, **args) :
6738
6758
class RefactorExtractMethodCommand (sublime_plugin .TextCommand ):
6739
6759
def run (self , edit , ** args ):
6740
6760
view = self .view
6741
-
6761
+ selection = view . sel ()[ 0 ]
6742
6762
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 ()
6745
6764
function_name = inputs ["function_name" ].strip ()
6746
6765
parameters = inputs ["parameters" ].strip ()
6747
6766
if not parameters .startswith ("(" ):
@@ -6751,32 +6770,92 @@ def run(self, edit, **args):
6751
6770
6752
6771
if inputs ["scope" ] == "Class method" :
6753
6772
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 " )
6757
6776
6758
6777
view .insert (edit , region_class .end ()- 1 , new_text )
6759
6778
6760
- elif inputs ["scope" ] == "Current Scope " :
6779
+ elif inputs ["scope" ] == "Current scope " :
6761
6780
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 = ""
6763
6786
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 )
6769
6848
6770
6849
elif inputs ["scope" ] == "Global scope" :
6771
6850
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" ]
6773
6852
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 )
6775
6854
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 "" )+ ")" )
6778
6857
else :
6779
- view .replace (edit , view . sel ()[ 0 ] , function_name + parameters )
6858
+ view .replace (edit , selection , function_name + parameters )
6780
6859
view .insert (edit , region_class .begin (), new_text )
6781
6860
6782
6861
def is_enabled (self , ** args ) :
@@ -7370,6 +7449,107 @@ def is_visible(self, **args) :
7370
7449
return selection .begin () != selection .end ()
7371
7450
7372
7451
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
+
7373
7553
import sublime , sublime_plugin
7374
7554
import os
7375
7555
0 commit comments