danilo
danilo

Reputation: 9415

collapse or folding "import" statements in vscode (for java code)

Is it possible to enable some option to fold or collapse groups of import statements in java code using vscode?

Are there any extensions that do this job?

Upvotes: 27

Views: 8744

Answers (2)

VonC
VonC

Reputation: 1328602

As an alternative, consider VSCode 1.59 (Juil. 2021), which includes:

Automatically Fold Imports

Use the setting editor.foldingImportsByDefault to have import statements automatically folded.

The fold state is stored once a file has been opened once.

The feature works with TypeScript, JavaScript, Java, C# and C++ and with all language that have a folding range provider that annotates import statements with FoldingRangeKind.Imports.

Upvotes: 45

thehale
thehale

Reputation: 1776

I use the VS Code extension "Language Support for Java" from Red Hat (redhat.java) which comes bundled in the "Java Extension Pack" from Microsoft (vscjava.vscode-java-pack). With this extension there are many ways to fold various parts of my code, including the import statements.

The Carrot in the Sidebar

Next to the import statements I have a little carrot symbol that toggles the folding of the import statements.

enter image description here

Keyboard Shortcuts

VS Code also ships with a large selection of customizable commands for folding and unfolding code. You can explore the list your self at

`Manage` (the gear icon in the bottom left) -> `Keyboard Shortcuts` -> Search for "fold".

(CTRL + K + CTRL + S also brings up this same menu).

enter image description here

Upvotes: 1

Related Questions