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 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.
  • Whole File allows the AI to change code anywhere in the current file.
  • 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.

Once you have entered your instructions, press AI Refactor to initiate the process. AI refactoring queries 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 queries by pressing the Abort button in the AI tool. If you do not cancel and the query 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.


To the right of the target selector, Auto-Context lets you choose whether to automatically add additional context to your request.

When this is disabled, Wing includes only the current file with your request, as well as any individual files you have specified with Context Files on the Request Context page of the AI tool.

When Auto-Context is enabled, other files may be added as well, to provide more context for accurate completion of your request. Doing this causes requests to take longer to complete and cost significantly more, but 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 Queries

The Queries 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 query, select Save Current Query and enter a name for the query. Once there are saved queries, they are listed at the end of the Queries menu.

You can use Manage Saved Queries in the Queries menu to see all saved queries, and rename, duplicate, or delete them, or add new queries. This dialog also allows you to bind a key binding to each saved query, making it easier to invoke that particular query as you are working with code. An example would be to bind a key to a query 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 queries 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.