Refactoring in Comma
Renaming
Comma supports renaming of a wide range of program elements. In the editor, place the cursor
on the element to rename, and press Shift+F6
. Elements that may be renamed include:
- Variables
- Subs
- Private methods
- Grammar rules
- Types (
module
,class
,role
,grammar
)
When these are renamed, all usages within the project will be located and updated.
It is also possible to rename a module file, by renaming it in the Project tree. This is
again done by selecting the file and pressing Shift+F6
, or right-clicking and choosing
Refactor
, then Rename
. In this case, all use
statements will be updated, as well
as the provides
entry in META6.json
.
Extract variable and constant
Note: This feature is only found in Comma Complete.
The Extract Variable refactor allows an expression to be taken and factored out. It will be assigned into a variable, which you then get to name, and the variable will be used at the location of the eliminated expression. If the extracted code is used in multiple places - a common trigger for such a refactor - it will offer to replace all occurrences.
To perform this refactor, place the cursor within the expression that you wish to extract, and
press Ctrl+Alt+V
. Use the arrow keys to select the expression to extract, and - if prompted -
whether to extract this occurrence or all of them, pressing Enter
after each selection.
Finally, rename the variable into something appropriate.
Extract constant works just the same, except the result is a constant
. This is triggered with
Ctrl+Alt+C
.
Extract routine
Note: This feature is only found in Comma Complete.
This is actually three refactors:
- Extract to subroutine (
Ctrl+Alt+R
) - Extract to method (
Ctrl+Alt+M
) - Extract to private method (
Ctrl+Alt+P
)
All of them presently work at the statement level, however extracting expressions is planned in an upcoming release.
Extracting code into a new routine correctly can be a little tedious to do manually, since it needs to be determined with parameters the new routine will take and then to pass them. The extract routine refactor takes care of this analysis for you.
The first step of the refactor is to select the scope that the extracted routine should be placed in. For methods this will be the enclosing package, however subroutines are lexical and may be placed in any scope. This will be used to decide which values will, by default, be passed as parameters, and which - if any - are lexically available.
After selecting the target scope, the Extract Code dialog opens.
By default, symbols available lexically will be used that way, but it is possible to change this to pass them as a parameter instead. The routine must be given a name. It is also possible to rename the parameters, as well as to re-order them, before completing the refactor. The extracted code will be placed into the new routine, and a call to that routine will be put in its place.