AI Driven Code Refactoring

Index of All Documentation » Wing Pro Reference Manual » AI Assisted Development »

Wing Pro can use AI to refactor, redesign, or rewrite existing code. This is done from the AI Refactoring page of the AI tool that is available in the Tools menu. You can initiate AI assisted refactoring by pressing Ctrl-!. This shows the AI tool, where you can select the target you want to refactor and enter your instructions.

The target to be refactored may be one of:

  • Current Selection refactors only the current selection, or if there is no selection then it will add code at the current insertion point according to your instructions.
  • Current Block restricts changes to the current like-or-greater indented block of code.
  • Current Scope limits refactoring to the innermost named scope (a class or def) at the current caret position in the editor.
  • Whole File allows the AI to change code anywhere in the current file.

Once you have entered your instructions, press AI Refactor to initiate the process. AI refactoring requests may take quite a while to complete, up to several minutes, but they are capable of producing changes that would take far more time to make manually.

You can cancel AI refactoring requests by pressing the Abort button in the AI tool. If you do not cancel and the requests completes, then Wing will enter the AI refactored code into the editor. By default this is done by replacing the selected target. Other options are available in the AI tool's Options menu, as described below. A single Undo will undo the changes if it turns out that you do not want them.


By default Wing sends only part or all of the current file with your AI suggestion request. If the code you are asking the AI to write needs access to other files in your project, you can ask Wing to include those by enabling the AI Suggest Uses Auto-Context option on the Request Context page of the AI tool.

Doing this causes requests to take longer to complete, and they may significantly cost more, but it can produce more accurate and precise results if the added context is relevant to your request. Be sure to read and understand AI Request Context and Managing Cost.

Managing Requests

The Requests menu in the AI tool can be used to traverse the history of your AI refactoring requests, and to save and reuse selected requests.

To traverse the AI request history, use History Previous and History Next or press Ctrl-Up and Ctrl-Down while focus is in the AI tool's request entry area.

To save the current request, select Save Current Request and enter a name for the request. Once there are saved requests, they are listed at the end of the Requests menu.

You can use Manage Saved Requests in the Requests menu to see all saved requests, and rename, duplicate, or delete them, or add new requests. This dialog also allows you to bind a key binding to each saved request, making it easier to invoke that particular request as you are working with code. An example would be to bind a key to a request that instructs the AI to "clean up this code" or "add a summary comment at the top".


Enter Key Executes Refactor in the Options menu causes Wing to immediately execute the currently entered refactoring request when Enter is pressed in the entry area. When this is enabled, you can still enter a newline into the request by pressing Ctrl-Enter.

The Options menu also provides control over how the AI tool places refactoring results into the editor when the request completes:

  • Refactor Replaces Old Code replaces the existing code with the AI refactor of the code. This is the default option.
  • Refactor Comments Out Old Code causes Wing to comment out the old code and then paste the new code after it.
  • Refactor Starts Comparison asks Wing to save a temporary file that contains the old version of your code and then enter into Diff/Merge mode so that you can compare the changes and selectively undo or modify them. The old version of your code will be on the left and the new version on the right. Use the toolbar merge icon merge to undo any unwanted changes.

The Options menu also provides a few other commands:

Show Console shows a diagnostic console in the AI tool, which can be used to inspect the AI requests that are made and their results. This should only be needed when there are problems with the AI integration or if you want to see what data is being sent to the AI provider.

Show Documentation displays this documentation.

Key Binding

If you prefer a different key binding for this feature, you can bind the command ai-refactor to any key sequence with the User Interface > Keyboard > Custom Key Bindings preference.