In this chapter, we explore one of GitHub Copilot’s most powerful features in Visual Studio Code: Smart Actions. These predefined, AI-enhanced actions streamline your workflow, from documenting code to debugging and refactoring. Whether you're dealing with cryptic legacy functions or just trying to speed up your development, Smart Actions can save the day.
What Are Smart Actions?
Smart Actions are special commands within Copilot that:
-
Add documentation to code
-
Explain code logic
-
Suggest fixes
-
Refactor or rename methods
-
Generate new code elements
Each action starts with a slash (/
) command inside Copilot Chat or Inline Chat.
The most common Smart Actions are:
-
/doc
— Generate documentation -
/explain
— Explain selected code -
/fix
— Propose a solution for broken code -
/generate
— Suggest renames, structure changes, or new code
Example: Documenting a Function
In our working project, we have a JavaScript function called doStuff
that’s unclear and undocumented. Let’s fix that.
-
Highlight the function
-
Open Inline Chat (
Ctrl + I
) -
Type
/doc
and hit Enter
Copilot responds with properly formatted documentation comments, including:
-
A summary of the function
-
Descriptions of parameters
-
Return values
For example:
/** * Processes an array of numbers by filtering and summing them. * @param {number[]} numbers - An array of numbers. * @returns {number} The sum of numbers greater than five. */
This alone makes a world of difference when reading or maintaining unfamiliar code.
Example: Explaining Code
Need to understand what a complex function does?
-
Highlight the code
-
Open Inline Chat again
-
Type
/explain
and press Enter
Copilot will return a step-by-step explanation, breaking down nested functions and control flows. This is ideal for onboarding new developers or working with someone else’s codebase.
Example: Fixing Errors
Say your function throws errors when run. Instead of manually debugging:
-
Highlight the faulty code
-
Type
/fix
in the chat -
Press Enter
Copilot proposes:
-
Diagnosed issues (e.g., syntax errors, undefined functions)
-
A suggested fix, ready to apply
-
Explanations for what’s changed
This is especially helpful when dealing with subtle or hard-to-spot bugs.
Example: Renaming and Refactoring
The function name doStuff
isn’t helpful. Let's make our code more readable using /generate
.
Prompt:
Rename the methods to meaningful function names. Also rename the function calls accordingly.
Copilot renames the function to something like processNumbers
, and the internal methods to:
-
isGreaterThanFive
-
sumNumbers
-
filterNumbers
It also updates the actual call site:
const result = processNumbers(numbers);
The code is now clean, meaningful, and ready for collaboration.
Live Testing the Results
Once your code is updated, test it using the Live Server plugin in VS Code:
-
Open
index.html
-
Click Go Live
-
Open your browser and view the result
Check the Developer Console. In our example, we get 21
as the output—indicating that the function correctly summed numbers greater than five.
There are no console errors, only a missing favicon warning (which we can ignore).
More Smart Action Possibilities
Smart Actions are evolving quickly. According to the official VS Code documentation, additional actions include:
-
Generating test cases
-
Creating commit messages based on staged changes
-
Advanced renaming across large codebases
-
Auto-generating pull request descriptions
These features are especially useful in collaborative or enterprise environments where consistency and documentation matter.
Final Thoughts
In this chapter, we:
-
Used
/doc
to document a function -
Used
/explain
to break down code logic -
Used
/fix
to resolve runtime errors -
Used
/generate
to rename and refactor functions
Smart Actions make AI-assisted development more intuitive and powerful. They’re like mini tools inside Copilot—each designed to tackle a different stage of development or maintenance.
As Copilot continues to evolve, expect even more Smart Actions to emerge. Keep an eye on the documentation and experiment frequently to discover what works best for your workflow.