Clean up Legacy Functions for Testability in Cursor (0.50+) with cmd+k

Cursor's Cmd+K is fantastic for targeted code edits, but sometimes you need the AI to operate beyond the confines of your current selection—like extracting a piece of logic into a brand-new, top-level function. Standard Cmd+K often falls short, keeping changes localized. This lesson reveals how to overcome this limitation using Cursor's "Edit Full File" mode, turning complex refactoring of legacy code into a streamlined process.

Workflow demonstrated in this lesson:

  • Contrast the behavior of standard Cmd+K (Edit Selection) with the "Edit Full File" mode.
  • Use Cmd+K, then Cmd+Shift+Enter (or the dropdown) to invoke "Edit Full File" mode for broader AI refactoring.
  • Instruct the AI to extract selected code into new functions at the top level of the module.
  • Demonstrate breaking down a large, monolithic function into multiple smaller, exported, and testable functions.
  • Utilize the "Fix in Chat" feature (Cmd+Shift+D) to send error messages and surrounding code directly to an AI agent for quick resolution.

Key benefits:

  • True Top-Level Extraction: Move code out of its current scope and into new, independent functions anywhere in the file.
  • Simplified Legacy Code Refactoring: Easily break apart large, untestable functions into manageable, testable units.
  • Improved Code Structure: Quickly organize and modularize your codebase with AI assistance.
  • Contextual Error Fixing: Send errors and relevant code snippets to an AI agent via "Fix in Chat" for efficient debugging.

This lesson shows a powerful technique for serious refactoring in Cursor, especially when dealing with large or legacy codebases, making your functions more modular, testable, and maintainable.

Share with a coworker

Transcript

[00:00] In Cursor, when you select a block of code, which I'll do with the expand selection shortcut, which I highly recommend learning, so we'll select this code, hit command k, and ask it to please extract this into a function at the top level of this module. And we'll let this run, and it may kind of attempt to extract this into a function, but the best that it could do was create a function in line inside of our current function because that version of Command-K is limited to edits within its own selection. So if I undo all of this and select this again and hit Command-K, I can press up to restore a previous message and within this drop-down cursor recently introduced a edit full file option. So I'm going to invoke that with command shift enter. The dialog will shift over to the center.

[00:45] I'll hit accept and you'll now see we have a process string function which if I scroll down you'll see this green mark right here Which is brand new and it's outside of our current function. In my opinion this is a massive win for working with a lot of legacy code where you have massive functions that you just need to clean up and you can select the internals of the function, hit command K, say things like please extract all of these conditionals into functions at the top level of this module and export them as functions, and then let something like this run. Make sure you hit command shift enter not just enter. You'll see the dialog shift. We'll hit command enter to accept file and then you'll see a much smaller function here.

[01:22] We can work on some types here in a second but we'll get all of these functions which are all exported and which are all now testable. And there's just so many bugs in legacy software that are due to huge untestable functions and this new feature just makes me so very happy. Then just as a quick fix we'll come up to this error. I'm going to hit command shift D. If you hover over this you'll see fix in chat which is the only current way I know of which will take this error message and that code.

[01:49] So we'll command shift D, then that just automatically triggers an agent to start fixing on this, you can just sit back and kind of let it happen.