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

Closed
2 tasks
gechandesu opened this issue Nov 21, 2024 · 3 comments
Closed
2 tasks

vfmt: Add imports grouping and sorting #22939

gechandesu opened this issue Nov 21, 2024 · 3 comments
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.

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.

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.

Huly®: V_0.6-21381

@gechandesu gechandesu added the Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one. label Nov 21, 2024
@spytheman
Copy link
Member

I prefer vfmt to not sort anything at all. That can lead to subtle bugs, because modules can have init functions with side effects, and the programmer has more information about what he/she prefers, not vfmt.

@spytheman spytheman closed this as not planned Won't fix, can't repro, duplicate, stale Nov 23, 2024
@jorgeluismireles
Copy link

I prefer vfmt to not sort anything at all. That can lead to subtle bugs, because modules can have init functions with side effects, and the programmer has more information about what he/she prefers, not vfmt.

Now I understand that a lot of official V code have not sorted the imports. I though that like in Golang, we can sort the names and later the language will call all the corresponding init() correctly. I think our eyes like to see the beginning of any V file as "nice" as possible.

@spytheman
Copy link
Member

In most cases the order does not matter (the modules are sorted in topological order when there are dependencies), but not in all (in the presence of side effects, inside the same file), and forcing vfmt to reorder stuff for aesthetic reasons will break that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants