e8b2cec Feb 18, 2015
142 lines (105 sloc) 5.54 KB

NOTICE: All demos begin when the string `START DEMO` is in the minibuffer.

Context-sensitive menu

Description: When user runs the command, a menu appears and offer refactoring choices based on current scope of semantic tag. For example, if the cursor is inside a class, the menu lists choices such as generate function implementations for the class, generate class getters/setters… Each menu item also includes its own set of options, such as perform a refactoring option in current file or other file.

Generate class and function implementation

Description: From the header file, all function prototypes of a class can be generated into corresponding empty function implementation in a source file. The generated function implementations also include all of their (nested) parents as prefix in the names, if any. If the class is a template, then the generated functions also includes all templates declarations and in the parent prefix properly.

Since all function implementations can be generated a class, this feature should be present.

Demonstration 1 (with nested classes):


Demonstration 2 (with nested classes and templates):


Note that in the demos, you see some regions highlighted with red colors. Those regions are spaces inserted by Semantic Refactor, so when you move the cursor inside a generated function body, you can start typing code immediately without indenting.

Generate class getters and setters

Description: All getters and setters of all variables in a class can be automatically generated with appropriate type information. Obviously, generating individual getter and setter for each variable works as well.



Generate function prototype

Description: When the cursor is in a function implementation, a function prototype can be generated and placed in a selected file. When the prototype is moved into, its prefix is stripped.



Convert function to function pointer

Description: Any function can be converted to a function pointer with typedef.



Convert function to function parameter

The converted function pointer can also be placed as a parameter of a function. In this case, all the parameter names of the function pointer is stripped.



Description: * Move semantic units Any meaningful tags recognized by Semantic (class, function, variable, namespace…) can be moved relative to other tags in current file or any other file.



Extract function

Description: Select a region and turn it into a function, with relevant variables turned into function parameters and preserve full type information. Notice that after the region is replaced with a function call, in the minibuffer (at the bottom), Semantic shows the interface of newly function immediately if global-semantic-idle-summary-mode is enabled.

Demonstration (C mode):


Demonstration (C++ mode):


Rename local variable

Description: User can move a cursor to a variable and rename all occurrences of it. Note that if there are texts equivalent to the variable name but not represent the underlying object of the variable, Semantic Refactor won’t wrongly rename such instances.

Local rename is really useful when you first write code, you just want to quickly think of a temporary variable name (i.e. something like i, j…) to continue with your problem as fast as possible. It is useful because it’s not certain that all variables remain when you finish solving your problem. Once you’re done with your coding problem, you can start beautifying your code later.



As the demo shows, the functions print_viable_colors in the class Test which is called through the object test and in the namespace test_ns were not renamed, while the correct occurrences of the local lambda function print_viable_colors are renamed accordingly. After it is renamed to new_name, I move the cursor to each new occurrence to verify and everything was renamed accordingly.

Current limitation

In the C++ demo, the extracted function does not include namespace prefix for its parameters. This is because currently Semantic Refactor can only operate with Semantic tags in current buffer. Things starts getting much more complicated outside of current file. For example, the namespace information of map is not in map header file that we include, but in bits/stl_map.h. To search for such information requires Semantic to perform exhaustive search for all the included files, which would take a long time and block Emacs.

One solution to this problem is to use a fast external indexer like GNU Global along with Semantic. GNU Global can generate tag database fast, but lack necessary information for smart refactoring; Semantic is smart but is slow. We can combine them to make the best of both: Global collects tag positions and Semantic decides which tag position is valid and how to refactor with valid tags.

This is just a plan.