Javascript Refactoring in Vim

I don't need anything supernatural, but some knowledge refactoring would be nice.

Refactoring something in the area of ​​functions is one of the most common scenarios for me:

var funyfun = function(arg1, arg2) { arg1 = ...arg2; arg2....(); } 

Is there a vim plugin that would allow me to refactor arg1, for example, as part of this function, or do I need to come up with my own shortcut "select block, find, replace".

For added value, to “reorganize on the fly” when I print, so that I can see where changes are taking place. Netbeans does a great job of this.

+6
source share
7 answers

This is not limited to a specific block, but this is how I would do it with a simple Vim:

  • Move the cursor to the top of arg1 and type *N
  • Type ciw and paste the replacement.
  • Now you can use n and n to navigate entries and by pressing . Vim will redo the replacement in the current match

Here is a shortcut for it:

 " Simple word refactoring shortcut. Hit <Leader>r<new word> on a word to " refactor it. Navigate to more matches with `n` and `N` and redo refactoring " by hitting the dot key. map <Leader>r *Nciw 
+5
source

It sounds a bit like you want to rename instead of refactoring. Full refactoring is difficult for Javascript, although some IDEs provide approximate values. Since the question is about Vim specifically, and hacks are not excluded, I’ll just move on to the scope aspect:

I have a modified DoctorJS for creating visibility tags with the Vim plugin for navigating scopes based on these tags (see blog post / screencast ).

The hacker part is how the navigation for Vim is implemented: I generate a search template that includes the scope of the variable and excludes all nested areas for the same name. Thus, you can use this search pattern (function scoped_tags#FindTagInScope ) to implement renaming (only if all uses are in the same file and comments are not excluded, etc.). Or you can use scope navigation to manually navigate through the entry variables and use '.' rename them.

+4
source

Several JavaScript-enabled commands for Vim are provided by tern_for_vim , for example :TernRename to rename variables :TernDef to go to the definition of a thing under the cursor, etc. You will need to install nodejs and npm, be sure to read the installation instructions in the repo.

+4
source

As a partial solution, you can use Eclim with JSDT , which allows you to use the Eclipse refactoring / debugging / automatic completion / plugins features with Vim.

In my experience, this may be a bit slow on older machines, but worth a try.

+3
source

In addition to performing actual refactoring based on volume, etc. consider :%s/varName/newNav/gc . In the :help :s_c the c flag passed to :s goes into quick confirmation mode for search / replace operations, which offers you (y / n) whether to replace each match or not.

+1
source

You can do:

 :.,/^}/ s/\<arg1\>/new_name/g 

.,/^}/ is the range that accepts many Ex commands: from the cursor line to the next line starting with a closing parenthesis.

0
source

Benoit and Epeli have some good points, but it’s very difficult for me to write .,/^}/ Before my wildcard expression, and since it only changes the code from the cursor position to the next line, starting with } , it depends on the presence of the cursor position at the beginning function or block (and it will not work for an entire function with an if statement).

So instead, I use visual mode in combination with text objects. For example, by typing vi{ , we’ll select the entire code within the next pair of matches {} , va{ will contain the characters {} , and if you do this using the visual line ( vi{V ), you will also receive a full function declaration. Then you can simply do :s/\<arg1\>/new_name/g to rename arg1 to new_name , including function parameters.

0
source

Source: https://habr.com/ru/post/896214/


All Articles