How can I refactor C++ source code using emacs?

前端 未结 9 785
孤独总比滥情好
孤独总比滥情好 2021-01-30 06:34

I\'m interested mostly in C++ and method/class name/signature automatic changes.

相关标签:
9条回答
  • 2021-01-30 07:07

    Build cscope symbols.

    lookup the symbol you want to refactor.

    get into the cscope window, and start a macro after placing cursor on first occurence

    • ret
    • c-f your symbol start
    • navigate to start of your symbol
    • modify the word
    • c-x o (back to cscope)
    • n (for next cscope symbol)

    you have to just c-x c-e now

    0 讨论(0)
  • 2021-01-30 07:13

    I do this a lot, so I'm axiously awaiting other replies too.

    The only tricks I know are really basic. Here are my best friends in Emacs when refactoring code:

    M-x query-replace
    

    This allows you to do a global search and replace. You'll be doing this a ton when you move methods and commonly-accessed data to other classes or namespaces.

    C-x 3
    

    This gives you a display with two buffers side-by side. You can then proceed to load different files in them, and move your cursor from one to the other with C-x o. This is pretty basic stuff, but I mention it because of how powerful it makes the next one...

    C-x (
    (type any amount of stuff and/or emacs commands here)
    C-x )
    

    This is how you define a macro in emacs. Any time you find yourself needing to do the same thing over and over to a bunch of code (and it is too complex for query-replace), this is a lifesaver. If you mess up, you can hit C-g to stop the macro definition, and then undo (C-_) until you are back to where you started. The keys to invoke the macro are C-x e. If you want to do it a bunch of times, you can hit Esc and type in a number first. Eg: Esc 100 C-x e will try to invoke your macro 100 times.

    (Note: On Windows you can get "Meta" by hitting the Esc key, or holding down Alt).

    0 讨论(0)
  • 2021-01-30 07:13

    If you can program in elisp, you can look to combination of cedet + srecode from CEDET libraries - it provide all instruments for this task - find callers of functions, get signature, etc. But you need to create refactory tool yourself, using these instruments

    0 讨论(0)
  • 2021-01-30 07:14

    A friend of mine was playing with xrefactory and said it worked pretty well. It isn't cheap though.

    0 讨论(0)
  • 2021-01-30 07:23

    With the ccls or cquery, which provide the "language server protocol" (lsp), you can refactor names with:

    M-x lsp-rename

    Alternative: srefactor uses the emacs semantic-mode framework: https://github.com/tuhdo/semantic-refactor/blob/master/srefactor-demos/demos.org

    You can get it with M-x package-install from MELPA or at https://github.com/tuhdo/semantic-refactor/.

    0 讨论(0)
  • 2021-01-30 07:24

    In recent Emacs versions (24), Semantic is able to this.

    1. Possibly activate semantic mode M-x semantic-mode RET.
    2. Bring up the Symref buffer with C-c , g.
    3. Press C-c C-e to open all references.
    4. Rename with R.
    0 讨论(0)
提交回复
热议问题