Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vfmt: Add imports grouping and sorting #22939

Open
2 tasks
gechandesu opened this issue Nov 21, 2024 · 0 comments
Open
2 tasks

vfmt: Add imports grouping and sorting #22939

gechandesu opened this issue Nov 21, 2024 · 0 comments
Labels
Feature Request This issue is made to request a feature.

Comments

@gechandesu
Copy link

gechandesu commented Nov 21, 2024

Describe the feature

I propose adding to vfmt grouping and sorting of imports almost the same as Python isort does. For now vfmt just deletes line breaks between imports.

I found an old feature request #9353, here I describe the feature in more detail.

Use Case

The goal is to make imports more visual and nice looking.

No more:

import markdown
import foobar // local module
import log
import whisker.datamodel
import whisker.template
import net.http
import context
import net.html
import mylocalmod
import io

Proposed Solution

Groups

It is worth dividing imports into three groups:

  1. Modules from vlib
  2. Third-party modules, installed via v install [--git] <module>
  3. Local modules (found in the working directory relative to v.mod)

Groups should be separated by line breaks and follow each other in the above order.

General sorting

Each group individually must be sorted by type in alphabetical order. For example:

import context
import io
import log
import net.html
import net.http

import markdown
import whisker.datamodel
import whisker.template

import foobar
import mylocalmod

Sorting imports by type

Sorting imports types in this order looks good:

import net            // simple import
import net.http.file  // submodule import
import term.ui as tui // aliased import
import toml { Any }   // selective import

Sorting selective imports

Symbols in selective imports should also be sorted:

import toml { Config, Any } // No
import toml { Any, Config } // Yes

For better look, lowercase named symbols must be placed after symbols starting with a capital letter:

import toml { Config, ast_to_any, Any, parse_file } // No
import toml { Any, Config, ast_to_any, parse_file } // Yes

Sorting precedence

The highest priority is sorting by groups. Then each group should be sorted by import type. Alphabetical sorting is performed last.

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

Version used

V 0.4.8 58fc4de

Environment details (OS name and version, etc.)

Not important.

Huly®: V_0.6-21381

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@gechandesu gechandesu added the Feature Request This issue is made to request a feature. label Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request This issue is made to request a feature.
Projects
None yet
Development

No branches or pull requests

1 participant