aziznal / dart-import-sorter

A vscode extension that groups and sorts imports for dart files (https://marketplace.visualstudio.com/items?itemName=aziznal.dart-import-sorter)
MIT License
14 stars 1 forks source link

Option for relative files to be the last ones #56

Closed Gummiees closed 1 year ago

Gummiees commented 1 year ago

If I want to sort a relative path import and have my own package also imported, the default configuration will put import '../../mocks/common.dart'; with an empty line before since it's a different group, which I don't want.

If I unify the two last groups (my packages and relative paths), now import '../../mocks/common.dart'; appears first before my packages.

Is there a way to configure it as desired?

Example

If I have:

import 'dart:convert';
import '../../mocks/common.dart';

import 'package:mockito/mockito.dart';

import 'package/<my-package>.dart';

The desired outcome would be:

import 'dart:convert';

import 'package:mockito/mockito.dart';

import 'package/<my-package>.dart';
import '../../mocks/common.dart';

Thanks!

aziznal commented 1 year ago

Currently imports within the same group are simply sorted alphabetically. This is why the relative import appears before the package import.

I understand your use case. Basically, you want everything related to your package to be grouped together, and also keep the relative imports at the bottom.

I'll think of a solution and reply back here soon. There would basically be an extra config for sorting within groups I think.

Gummiees commented 1 year ago

Awesome! 😄 Thanks 🙌

Also, for context, this is something that I would use since it is for a Flutter project where import_sorter is being used.

aziznal commented 1 year ago

I'm thinking of making it possible to add the same config of regex-based import sorting to groups as well.

Basically, this is how the config would look:

{
    "dartimportsorter.matchingRules": [
        {
            "label": "Flutter",
            "regex": "^package:flutter.*$",
            "order": 1,
            "regexFlags": ["m"],
        },
        {
            "label": "Dart",
            "regex": "^dart:.*$",
            "order": 2,
            "regexFlags": ["m"]
        },
        {
            "label": "Package imports that ARE your app",
            "regex": "^package:<app_name>.*$",
            "order": 101,
            "regexFlags": ["m"],
            "groupSortingRules": [
                {
                    "label": "package imports",
                    "regex": "^package.*$",
                    "suborder": 1
                },
                {
                    "label": "relative imports",
                    "regex": "^\\..*$",
                    "suborder": 10
                }
            ]
        },
        {
            "label": "Everything else",
            "regex": ".*",
            "order": 3,
            "regexFlags": ["m"]
        }
    ]
}

That way, you could (optionally) sort imports within the same group exactly how you want.

aziznal commented 1 year ago

Hey. I just made a release. Check it out here. Let me know how it works out for you!

Gummiees commented 1 year ago

Thank you! 😄 I might be doing something wrong, because I cannot get it to work 🤔

Using the updated configuration from the extension README:

[
    {
        "label": "Dart",
        "regex": "^dart:.*$",
        "regexFlags": ["m"],
        "order": 1,
    },
    {
        "label": "Flutter",
        "regex": "^package:flutter/.*$",
        "regexFlags": ["m"],
        "order": 10,
    },
    {
        "label": "Package imports that are NOT your app",
        "regex": "^package:(?!<app_name>).*$",
        "regexFlags": ["m"],
        "order": 100,
    },
    {
        "label": "Package imports that ARE your app as well as relative imports",
        "regex": "^package:<app_name>.*$|^\\..*$",
        "regexFlags": ["m"],
        "order": 101,
        "subgroupSortingRules": [
            {
                "label": "Package imports that ARE your app",
                "regex": "^package:<app_name>.*$",
                "regexFlags": ["m"],
                "order": 1,
            },
            {
                "label": "Relative",
                "regex": "^\\..*$",
                "regexFlags": ["m"],
                "order": 2,
            }
    }
]

And following my example, if I have:

import 'dart:convert';
import '../../mocks/common.dart';

import 'package:mockito/mockito.dart';

import 'package/<my-package>.dart';

It still gets sorted as:

import 'dart:convert';

import 'package:mockito/mockito.dart';

import '../../mocks/common.dart';
import 'package/<my-package>.dart';

I have v0.3.2 installed, let me know if you need any more information :)

And again, thank you!

(deleted the comment before after double-checking the README)

aziznal commented 1 year ago

I did a little digging in. I found an issue in the code which is preventing user rules from being parsed correctly. Pushing a fix soon. (also there are missing parentheses in the readme. sorry :see_no_evil: also fixing those in a giffy)

aziznal commented 1 year ago

Just made a release and tested it out. All works fine on my end now.

You should try with this config:

    "dartimportsorter.matchingRules": [
        {
            "label": "Dart",
            "regex": "^dart:.*$",
            "regexFlags": [
                "m"
            ],
            "order": 1,
        },
        {
            "label": "Flutter",
            "regex": "^package:flutter/.*$",
            "regexFlags": [
                "m"
            ],
            "order": 10,
        },
        {
            "label": "Package imports that are NOT your app",
            "regex": "^package:(?!<app_name>).*$",
            "regexFlags": [
                "m"
            ],
            "order": 100,
        },
        {
            "label": "Package imports that ARE your app as well as relative imports",
            "regex": "^(package:<app_name>|\\.).*$",
            "regexFlags": [
                "m"
            ],
            "order": 101,
            "subgroupSortingRules": [
                {
                    "label": "Package imports",
                    "regex": "^package:<app_name>.*$",
                    "order": 1,
                },
                {
                    "label": "Relative",
                    "regex": "^\\..*$",
                    "order": 2,
                },
            ]
        }
    ]

Specifically, make sure your app name is set correctly in the import itself.

aziznal commented 1 year ago

I'll keep this issue open till I get your feedback

Gummiees commented 1 year ago

It does work now indeed! 🎉 Thanks a lot for your help and improvements :) will be using the extension even more now 😄