Reformat Code
Use Code | Reformat Code (⌥⌘L on macOS, Ctrl+Alt+L on Windows/Linux) to clean up messy or pasted code: it re-indents every line by its nesting and puts one space around operators. It never moves a line to another place. With nothing selected it reformats the whole file; with a selection, only those lines.
Try it
Section titled “Try it”The steps use the sample project. They end with the file as it was.
- Open
rtl/counter.sv, select all (⌘A / Ctrl+A) and press ⇧Tab three times. The deepest lines in this file are three levels in, so now every line starts at the left edge, includingmodule counterandinput logic clk,. - Press ⌥⌘L (Ctrl+Alt+L), or choose Code | Reformat Code. The file is exactly as it was before step 1: the
ports and the body one level in (2 spaces), for example
input logic clk,, and theif/elsebranches insidealways_ffone level deeper. - If it is not, press ⌘Z (Ctrl+Z) until it is, or unzip the sample project again.
If nothing changes, see If it doesn’t work.
What it does
Section titled “What it does”initial begin if (done) $display("done"); for (int i = 0; i < 4; i++) begin #1; end fork begin #2; end joinendThe rules, in short:
- scopes are indented one level, a single statement under
if,elseor a loop is indented one level, and end keywords line up with the line that opened the scope; - wrapped operands, arguments and list items line up under the first one;
- assignments (
=,<=) and binary operators get one space on each side (a+b*c→a + b * c). Bit ranges and bit selects ([7:0],[i],[W-1:0]) keep their compact form, and the extra spaces you used to line up names inside a line, such as the port names ininput logic clk,, are kept; - line breaks are not changed: the formatter re-indents lines and fixes spaces within them.
The rules in full
- The body of every scope is indented one level: modules, interfaces, programs, packages, classes, functions,
tasks, checkers, user-defined primitives and their
table … endtable,begin … end,fork … join,caseitems,generateregions and blocks, covergroups, clocking blocks, properties, sequences, constraint blocks, struct and enum bodies, and port, parameter and connection lists. Attributes ((* keep *)) stay where they are. - The single statement under
if,else, a loop,wait (…), an event or delay control (@(posedge clk)↵x = 1;) or acaselabel is indented one level; abegin … endblock or anelse ifstays at the level of its header, and a nestedifis indented under the outer one. - End keywords, end labels and closing brackets line up with the line that opened the scope.
- A wrapped operand lines up under the first operand (
if (a &&↵b)). Wrapped arguments and list items (ports, parameters, connections, concatenations) line up under the first item when it follows the opening bracket on the same line — a trailing comment does not change that; when the bracket ends its line, the items are indented one level. Wrapped subroutine header arguments and other wrapped lines get the continuation indent (4 spaces by default). - Assignments (
=,<=) and binary operators get one space on each side (a+b*c→a + b * c). Ranges and selects keep their compact form ([W-1:0]), and the columns you aligned inside a line (port lists) are kept.
Indent size
Section titled “Indent size”One level is 2 spaces by default. Open Settings (⌘, on macOS / Ctrl+Alt+S on Windows/Linux), go to Editor | Code Style | SystemVerilog | Tabs and Indents and set Indent and Continuation indent. The Indentation page uses the same setting.
Macros and inactive code
Section titled “Macros and inactive code”Lines are indented by the code they belong to, not by the macros around them: the `uvm_field_* lines between
`uvm_object_utils_begin and `uvm_object_utils_end get the same indent as the macros, and a conditional
compilation line (`ifdef, `endif) inside a class or module gets the indent of the items around it. The
code of an inactive `ifdef branch is left as written (Conditional compilation).
Limitations
Section titled “Limitations”- Code with syntax errors (shown as red underlines; see Known limitations) is not re-indented reliably: its lines keep or lose indentation, and a second Reformat Code may still move them. Correct code around it is formatted normally.
- Wrapped call arguments always line up under the first argument; there is no setting to indent them by a fixed amount instead.
If it doesn’t work
Section titled “If it doesn’t work”| You see | Likely cause | What to do |
|---|---|---|
| Nothing changes | The file is not a SystemVerilog file, or the plugin is disabled | See Installation |
| Only some lines change | You had a selection, and only its lines are reformatted | Select nothing, or select all |
| The shortcut does nothing, or locks the screen | Another application or your keymap uses Ctrl+Alt+L (on some Linux desktops it locks the screen) | Use Code | Reformat Code in the menu |
| Some lines look wrong | The code has syntax errors, or it is inside an inactive `ifdef branch (greyed out in the editor) |
Fix the syntax errors; see Conditional compilation |