vscode-cpptools icon indicating copy to clipboard operation
vscode-cpptools copied to clipboard

Add support for the Extract Variable refactoring

Open sean-mcmanus opened this issue 2 years ago • 8 comments

I've filed a VS request at https://developercommunity.visualstudio.com/t/C:-Add-Extract-Variable-refactoring/10357919

"Extract to constant (field)" or "Extract to constexpr (field)" might be possibilities in the future too (not sure if those would need to be tracked separately though).

Our previous issue is at https://github.com/microsoft/vscode-cpptools/issues/1162, but that got changed to track only Extract Function/Method.

sean-mcmanus avatar May 09 '23 02:05 sean-mcmanus

Related on Stack Overflow (I'll update the answer post with progress updates): VS Code "Extract Method" and "Extract Variable" not in context menu for C++.

starball5 avatar May 09 '23 21:05 starball5

Is this feature still an ongoing effort?

yhyu13 avatar Jan 28 '24 06:01 yhyu13

Is this fixed?

ashishpawar517 avatar Jun 06 '25 09:06 ashishpawar517

@yhyu13 @ashishpawar517 It is not fixed and there is not currently being worked on. The extract variable feature can be done by using GitHub Copilot, but since it's driven by AI, the results might be inconsistent and your prompt has to be specific on the details of how you want it extracted.

Image****

sean-mcmanus avatar Jun 06 '25 18:06 sean-mcmanus

Instead of using Copilot, I wanted to have a similar refactoring option that we have as an extract function.

Could you guide me towards relevant code files? I can work on this.

ashishpawar517 avatar Jun 07 '25 15:06 ashishpawar517

@ashishpawar517 It would require modifications to our closed source cpptools/cpptools-srv process.

sean-mcmanus avatar Jun 09 '25 19:06 sean-mcmanus

Is there any other workaround then? Maybe changing language server or similar?

ashishpawar517 avatar Jun 10 '25 01:06 ashishpawar517

@ashishpawar517 There is no other workaround. Changing the language server would be implementing the feature. You could potentially install another extension that implements the feature (I don't know of one).

Well, it might be possible to implement it in the open source TypeScript via making a call to the existing Extract to Function call and then applying post-processing to turn the function into a variable, however, I don't know how reliable that approach would be or if it would require more complicated C++ parsing than what is available in the Typescript part.

sean-mcmanus avatar Jun 11 '25 19:06 sean-mcmanus