CLion Nova refactorings
Last modified: 26 May 2024note
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:
Introduce Field
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 .
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;};
Introduce Namespace Alias
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.
Invoke Introduce Namespace Alias
Place the caret at a namespace usage, press , 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;}
Introduce Using Enum
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.
Invoke Introduce Using Enum
Place the caret at a namespace usage, press , 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 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:
Convert to Scoped Enum
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 and select Convert to Scoped Enum.