CLion Nova refactorings
note
The refactorings described below are only available in CLion Nova. Make sure to switch to CLion Nova in Advanced Settings.
CLion Nova provides the following additional refactorings:
This refactoring creates a new field based on the selected expression, initializes it with the expression or from the constructor, and replaces the occurrences of the expression with references to the newly introduced field.
To invoke this refactoring, press CtrlAlt0F.
In the example below, we use this refactoring to replace two occurrences of the same string with a new private field and initialize it from the existing constructor:
Before refactoring
#include <exception>#include <iostream>class ErrorHandler { ErrorHandler() {} public: void logError(std::exception& e) { auto errorLogFIle = fopen("log.txt", "w"); fprintf(errorLogFIle, "Something has failed: %s", e.what()); fclose(errorLogFIle); } void printError(std::exception& e) { printf("Something has failed: %s", e.what()); }};
After refactoring
#include <exception>#include <iostream>class ErrorHandler { ErrorHandler(): error_message("Something has failed: %s") {} public: void logError(std::exception& e) { auto errorLogFIle = fopen("log.txt", "w"); fprintf(errorLogFIle, error_message, e.what()); fclose(errorLogFIle); } void printError(std::exception& e) { printf(error_message, e.what()); } private: const char* error_message;};
This refactoring helps you create a namespace alias for a namespace usage and replace the currently selected usage or all usages in the current file with the alias. Depending on the selected usages, the namespace alias is declared in the closest possible scope to the usages.
Place the caret at a namespace usage, press CtrlAltShift0T, and then select Introduce namespace alias in the Refactor This popup.
If there are multiple occurrences of the namespace usage in the document, you will be able to choose whether to replace the current usage or all usages.
In the example below, we use this refactoring to add a namespace alias for the SpaceOne::SpaceTwo
namespace.
Before refactoring
namespace SpaceOne{ namespace SpaceTwo { int ten = 10; inline void foo() { // do something } }}inline int test(){ SpaceOne::SpaceTwo::foo(); return SpaceOne::SpaceTwo::ten;}
After refactoring
namespace SpaceOne{ namespace SpaceTwo { int ten = 10; inline void foo() { // do something } }}inline int test(){ namespace s_two_alias = SpaceOne::SpaceTwo; s_two_alias::foo(); return s_two_alias::ten;}
The C++20 using enum
syntax allows you to add all the enumerators from the target enumeration. As a result, you can omit repetitions of the enumeration name when using its member enumerators.
Place the caret at a namespace usage, press CtrlAltShift0T, and then select Introduce Using Enum in the Refactor This popup.
CLion Nova also detects the code pieces where
using enum
is applicable and suggests the refactoring in the AltEnter menu:If there are multiple occurrences of the enumeration usage in the document, choose whether to replace the current usage or all usages:
CLion Nova will perform the refactoring in-place:
The Convert to Scoped Enum refactoring helps you convert a C-style enumeration declaration into a C++11 scoped enumeration.
To invoke it, place the caret at an enumerator, press CtrlAltShift0T and select Convert to Scoped Enum.
Thanks for your feedback!