Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
00cf3f0
feat(code actions): extract variables and functions, expand a macro o…
16bit-ykiko Dec 2, 2026
8914a36
test(code actions): broken code for the extract and inline actions
16bit-ykiko Dec 2, 2026
a30efc3
fix(code actions): keep code reaching names through block-scope direc…
16bit-ykiko Dec 2, 2026
6e2a2c3
fix(code actions): address review findings on extracting and inlining
16bit-ykiko Dec 2, 2026
b8c4693
fix(code actions): address the second review round on extracting and …
16bit-ykiko Dec 2, 2026
f059d28
test(code actions): one-level spacing case beside the other forms
16bit-ykiko Dec 2, 2026
714d0ab
docs(code actions): extract and inline actions in the code action page
16bit-ykiko Dec 2, 2026
6451c1d
fix(code actions): keep types, promotions, overloads and macros where…
16bit-ykiko Dec 2, 2026
4c35a12
fix(code actions): keep const bindings, access and lifetimes the code…
16bit-ykiko Dec 2, 2026
9edc4b0
fix(code actions): keep escaped locals, constants and promotions as t…
16bit-ykiko Dec 2, 2026
3cbf678
fix(code actions): handle the review bots' findings on extracting and…
16bit-ykiko Dec 2, 2026
c592d70
fix(code actions): keep names, aliases and promotions per occurrence
16bit-ykiko Dec 2, 2026
8755d92
fix(code actions): last review round on extracting, inlining and expa…
16bit-ykiko Dec 2, 2026
555c6b9
fix(code actions): treat effect-free calls as reads that may change
16bit-ykiko Dec 2, 2026
1a442fd
fix(code actions): keep objects a member's result refers into
16bit-ykiko Dec 2, 2026
0897d8d
fix(code actions): keep C++11 constexpr bodies and numbers apart from…
16bit-ykiko Dec 2, 2026
020fcc4
fix(code actions): keep directives from moving past the names they ch…
16bit-ykiko Dec 2, 2026
8912472
fix(code actions): keep pointers into C compound literals in their block
16bit-ykiko Dec 2, 2026
0ec8ae1
fix(code actions): refuse va_start, static locals in lambdas and dire…
16bit-ykiko Dec 3, 2026
707d93f
fix(code actions): variadic-only comma elision and reference bindings…
16bit-ykiko Dec 3, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
203 changes: 201 additions & 2 deletions docs/en/features/code-action.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ Each refactoring has its own code action kind, so an editor can ask for one alon
| Add the missing enum cases to a switch | `refactor.rewrite.populateSwitch` |
| Expand a deduced type | `refactor.rewrite.expandDeducedType` |
| Reorder definitions by declaration order | `refactor.rewrite.reorderDefinitions` |
| Extract a variable | `refactor.extract.variable` |
| Inline a variable | `refactor.inline.variable` |
| Extract a function | `refactor.extract.function` |
| Expand a macro | `refactor.inline.macro` |
| Expand a macro one level | `refactor.inline.macroOneLevel` |
| Add an include or an import, apply a diagnostic's fix | `quickfix` |

The VS Code extension has a command for each refactoring, named `clice.` followed by its kind and titled after the action in the Command Palette ("clice: Add Missing Enum Cases to Switch"). It applies the action at once when it is the only one offered, and lets you pick otherwise. Bind a key to it in `keybindings.json`:
Expand Down Expand Up @@ -574,9 +578,187 @@ tests/code_actions/reorder/05_dependencies

<!-- END GENERATED ITEMS -->

## Extracting Variables

A selected expression becomes a local variable, declared right before the statement that uses it. It is offered only where computing the expression there means the same as computing it in place: not for an expression the statement evaluates conditionally, repeatedly or not at all, nor for one that could interfere with what the statement evaluates before it.

<!-- BEGIN GENERATED ITEMS: code_actions/extract_variable -->

<!-- BEGIN CAPABILITY: supported clangd#446 -->

**Extract a variable**

A selected expression becomes a variable declared right before the statement that uses it

A value is copied into the variable; an object the expression names, such as an array element, is bound by reference, so that writing through the variable still writes the element.

```case
tests/code_actions/extract_variable/01_extract_expression
```

<!-- END CAPABILITY -->

<!-- BEGIN CAPABILITY: supported clangd#924 -->

**Replace every occurrence**

When the block repeats the expression, one variable can replace every occurrence of it

This is offered where every occurrence has the same value: the expression calls nothing, and nothing it reads is written between the first occurrence and the last.

```case
tests/code_actions/extract_variable/02_all_occurrences
```

<!-- END CAPABILITY -->

<!-- BEGIN CAPABILITY: supported clangd#1197 -->

**Inside macro arguments**

An expression in a macro's argument is extracted to before the statement invoking the macro

The macro must use the argument once and neither stringize nor paste it, as `ADD` does here; where `MAX` evaluates it twice, or `CHECK` prints it, the variable would change what the program does.

```case
tests/code_actions/extract_variable/03_macro_argument
```

<!-- END CAPABILITY -->

<!-- END GENERATED ITEMS -->

## Inlining Variables

A local variable is replaced by its initializer wherever it is used, and its declaration removed, where the initializer computes the same value at each use.

<!-- BEGIN GENERATED ITEMS: code_actions/inline_variable -->

<!-- BEGIN CAPABILITY: supported clangd#446 -->

**Inline a variable**

A local variable's uses are replaced by its initializer, and its declaration is removed

The action is offered on the declaration and on any use. An initializer that calls nothing can replace any number of uses while nothing it reads changes; parentheses keep its operators together where it lands.

```case
tests/code_actions/inline_variable/01_inline_uses
```

<!-- END CAPABILITY -->

<!-- BEGIN CAPABILITY: supported -->

**Calls stay computed once**

An initializer that calls a function is inlined only into a single use in the next statement

Inlined anywhere else, the call would run more than once, or after code it ran before.

```case
tests/code_actions/inline_variable/02_computed_once
```

<!-- END CAPABILITY -->

<!-- BEGIN CAPABILITY: supported -->

**References inline as their object**

A reference is replaced by the expression naming its object, writes through it included

That expression must keep naming the same object at every use, and have the reference's own qualifiers: a reference to const of a non-const object stays.

```case
tests/code_actions/inline_variable/04_references
```

<!-- END CAPABILITY -->

<!-- END GENERATED ITEMS -->

## Extracting Functions

Selected statements, or a selected expression, become a new function, and a call to it takes their place.

<!-- BEGIN GENERATED ITEMS: code_actions/extract_function -->

<!-- BEGIN CAPABILITY: supported clangd#698 -->

**Extract statements into a function**

Selected statements become a function declared before the enclosing one, and a call replaces them

What the statements read of the enclosing function become parameters, in the order they are declared: a number or a pointer is passed by value, anything else by reference to const, and what the statements write by reference.

```case
tests/code_actions/extract_function/01_statements
```

<!-- END CAPABILITY -->

<!-- BEGIN CAPABILITY: supported clangd#698 -->

**Expression into a function**

A selected expression becomes a function returning its value, called where the expression was

An expression naming an object, rather than computing a value, is returned by reference.

```case
tests/code_actions/extract_function/02_expression
```

<!-- END CAPABILITY -->

<!-- BEGIN CAPABILITY: supported clangd#653 -->

**Statements that return**

Statements returning on every path move whole, and the call returns what the new function returns

The new function has the enclosing one's return type as written: an `auto` stays `auto`, so the type is deduced from the same expressions.

```case
tests/code_actions/extract_function/03_return_values
```

<!-- END CAPABILITY -->

<!-- BEGIN CAPABILITY: supported -->

**A number computed for later**

When the code after the statements reads a number they declare, the new function returns it

The call declares the variable as before; any other declaration the later code needs keeps the statements where they are.

```case
tests/code_actions/extract_function/04_result_variable
```

<!-- END CAPABILITY -->

<!-- BEGIN CAPABILITY: supported -->

**Code using its class**

Code using the object or the names of its class becomes a private member of that class

A member written in the class is defined there; one defined out of line gets a declaration in the class and a definition before it. The new member is `const` where the enclosing one is.

```case
tests/code_actions/extract_function/05_members
```

<!-- END CAPABILITY -->

<!-- END GENERATED ITEMS -->

## Macros

A macro invocation is replaced by the code it expands to.
A macro invocation is replaced by the code it expands to, fully or one level at a time.

<!-- BEGIN GENERATED ITEMS: code_actions/macro -->

Expand Down Expand Up @@ -606,6 +788,20 @@ tests/code_actions/macro/02_nested_expansion

<!-- END CAPABILITY -->

<!-- BEGIN CAPABILITY: supported clangd#820 -->

**Expand a macro one level**

A macro invocation can also be replaced by its own body alone, the macros that body invokes left as written

The arguments are put in as written, stringized and pasted as the body says. The text stays on one line, spaced as the definition and the arguments are, without the project's formatting: a line break would change the line a nested macro's `__LINE__` reports, a space the string its `#` makes. The action is not offered where expanding the arguments first, as the preprocessor does, would give other code.

```case
tests/code_actions/macro/09_one_level
```

<!-- END CAPABILITY -->

<!-- END GENERATED ITEMS -->

## Missing Includes and Imports
Expand Down Expand Up @@ -735,7 +931,7 @@ tests/code_actions/fix/03_note_fixes

## Formatting

Generated text is formatted with the project's clang-format style when one applies to the file, so a definition or a block of `case` labels lands in the surrounding code's layout.
Generated text is formatted with the project's clang-format style when one applies to the file, so a definition or a block of `case` labels lands in the surrounding code's layout; a macro expanded one level stays on its line.

<!-- BEGIN GENERATED ITEMS: code_actions/formatting -->

Expand All @@ -761,3 +957,6 @@ tests/code_actions/formatting/01_project_style
- Members already defined in another source file are left out of a "define missing members" action only when the project index knows that definition; while the index does not hold the file defining them, every undefined member is offered.
- In a class template, the special members action recognizes a copy or move the class declares only when its parameter names the class itself: one spelled through a type that becomes the class only in an instantiation (`std::pair`'s conditional copy assignment), or followed by a parameter pack, is declared again, and the class no longer compiles. The copies it declares take a `const` parameter, which an instantiation whose member copies only from a non-const object (`std::auto_ptr`) deletes.
- Missing-include candidates come from the standard library mapping and from headers the project index has seen; a header no indexed source file includes is not suggested.
- An extracted variable lives until the end of its block, where the temporary the expression made ended with its statement.
- Code that needs its class, extracted from a member function defined outside the class, is offered only when the class is defined in the same file: the new member is declared there.
- Extracting a function from a function template, or from a generic lambda, is not offered: the new function would have to be a template of its own.
2 changes: 1 addition & 1 deletion docs/en/features/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Language Server Protocol features available when using clice as an editor backen
| [Code Navigation](./navigation.md) | 125 supported · 1 partial · 6 unsupported |
| [Rename](./rename.md) | 9 supported |
| [Diagnostics](./diagnostics.md) | 13 supported · 1 partial |
| [Code Action](./code-action.md) | 46 supported |
| [Code Action](./code-action.md) | 58 supported |
| [Semantic Tokens](./semantic-tokens.md) | 61 supported |
| [Inlay Hints](./inlay-hints.md) | 40 supported |
| [Document Links](./document-links.md) | 5 supported · 1 partial |
Expand Down
55 changes: 52 additions & 3 deletions docs/meta/translations/features/code-action.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/meta/translations/features/overview.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading