简体   繁体   English

编辑javascript时使用emacs进行重构

[英]Refactoring with emacs while editing javascript

Hi so I am writing a lot of server side javascript and I would like the ability to refactor while editing with emacs. 嗨所以我正在编写很多服务器端的javascript,我希望能够在使用emacs进行编辑时进行重构。 Is this possible? 这可能吗? Thanks! 谢谢!

By refactor I mean like how in eclipse while editing Java you can refactor one variable called for example "variableOne" into "variable1" and now all other 15 times you wrote "variableOne" becomes "variable1". 通过重构我的意思就像在编辑Java时在eclipse中你可以将一个名为“variableOne”的变量重构为“variable1”,现在所有其他15次你写的“variableOne”变成“variable1”。

Probably the most sophisticated JavaScript refactoring library for Emacs is Magnar Sveen's js2-refactor . 可能是Emacs最复杂的JavaScript重构库是Magnar Sveen的js2-refactor Its list of supported refactorings includes 支持的重构列表包括

rv is rename-var : Renames the variable on point and all occurrences in its lexical scope. rvrename-var :将点上的变量重命名,并在其词法范围内重命名。

which sounds a lot like what you're looking for. 这听起来很像你正在寻找的东西。 It also supports a number of other very useful common rafactoring actions. 它还支持许多其他非常有用的常见重要行为。

Assuming you're on Emacs 24, I recommend installing it using the MELPA repository. 假设您使用的是Emacs 24,我建议使用MELPA存储库进行安装。 If you're still on Emacs 23 you'll have to upgrade or manually install package.el before you can MELPA. 如果您仍在使用Emacs 23,则必须升级或手动安装package.el才能使用MELPA。

If you are looking for just renaming variables, you might also want take a look at tern . 如果您正在寻找renaming变量,您可能还需要看一下tern The advantage it has compared to js2-refactor (which I use too) is that it has a concept of projects so you can rename a certain variable across multiple files in a project. 它与js2-refactor (我也使用它)相比的优势在于它具有项目概念,因此您可以在项目中的多个文件中重命名某个变量。 It also provides other features like jump-to-definition and auto-completions (which are quite accurate). 它还提供其他功能,如jump-to-definitionauto-completions (非常准确)。

Here are some general options for renaming a variable 以下是重命名变量的一些常规选项

1) Multiple cursors - It has a useful command mc/mark-all-like-this-dwim , which marks all the occurences of the selected text in current context you can then edit all the occurrences simultaneously. 1) 多个游标 - 它有一个有用的命令mc/mark-all-like-this-dwim ,它标记当前context所选文本的所有出现,然后您可以同时编辑所有出现的事件。

2) Wgrep - This package enables one to apply changes done in grep buffer to respective files. 2) Wgrep - 该软件包允许将grep缓冲区中完成的更改应用于各自的文件。 This is useful when I have to replace a word across many files, in such situations use rgrep to search the word in multiple files. 当我必须在许多文件中替换单词时,这很有用,在这种情况下使用rgrep在多个文件中搜索单词。 Then enable wgrep in the resulting grep buffer, mark the word to replaced with multiple-cursors (you can also use query-replace), make the changes and then do wgrep-save-all-buffers and all my changes are saved! 然后在生成的grep缓冲区中启用wgrep ,将单词标记为替换为multiple-cursors (您也可以使用query-replace),进行更改然后执行wgrep-save-all-buffers并保存所有更改!

Your question seems to be more about renaming variables than about refactoring in general. 您的问题似乎更多地是关于重命名变量而不是一般的重构。 The two places to start for information about using Emacs to rename parts of your code are these: 有关使用Emacs重命名代码部分的信息的两个地方是:

  • Emacs Wiki Search and Replace category page. Emacs Wiki 搜索和替换类别页面。 This includes search-and-replace across multiple files (eg of your project). 这包括跨多个文件 (例如您的项目)的搜索和替换。
  • The Emacs manual: use Ch r to enter the manual from Emacs. Emacs手册:使用Ch r从Emacs输入手册。

    Then use hit the key i to look something up in the index (with completion): 然后使用按键i来查找索引中的内容(完成):

    i search and replace commands takes you to the section about replacement commands. i search and replace commands将带您到关于替换命令的部分。

    i search and replace in multiple files takes you to the section about Searching and Replacing with Tags Tables . i search and replace in multiple files将带您进入搜索和替换标签表的部分

For Emacs support for projects, see the Emacs Wiki Projects category page. 有关Emacs对项目的支持,请参阅Emacs Wiki 项目类别页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM