zls icon indicating copy to clipboard operation
zls copied to clipboard

make organize imports more flexible about import placement

Open Techatrix opened this issue 1 year ago • 2 comments

The current implementation of source.organizeImports (#2051) always places the imports at the top of the file. Sometimes you find code that has all imports at the bottom of the file or some imports between code. It would be nice if ZLS could handle this.

I can think of two possible solutions:

  1. add a special case where ZLS will place all imports at the bottom of the file (no config option)
  2. never move imports to a different location, only sort imports that are next to each other i.e. not separated by an empty line

The first solution has been implemented #2391. I am going to keep this issue open as there may still be viable to implement the second solution.

Techatrix avatar Nov 30 '24 19:11 Techatrix

+1 to keeping imports together.

Here is an example where it's moved lines from the second "paragraph" of consts into the first paragraph of imports. I like to group imports by having a space inbetween.

image

Srekel avatar Dec 31 '24 20:12 Srekel

Why not making it configurable? I think it would help enforce code style beyond what zig fmt does. I don't know how common it is to have imports both at the top and the bottom in the same codebase.

nihklas avatar Mar 12 '25 19:03 nihklas