This is a VS Code extension that sorts your dart / flutter imports with a single command.
There are three ways to activate the extension:
Using the shortcut: ctrl+alt+o
Using the command palette:
Dart: Sort Imports
Save your active document (with sortOnSave
set to true
).
By default, this extension will group your dart imports according to source and package:<namespace>
.
You can set the extension to sort your dart imports whenever you save your
current active document. This is set to false
by default, but can be changed in the extension settings.
You can set your own sorting rules so that imports will be grouped and ordered according to your preference.
The sorting algorithm only looks at a certain part of the import statement, namely the parts between the quotes. Keep this in mind when writing your own rules.
For example, the following:
import 'package:something/path/to/import.dart' as something_else;
is simplified to
'package:something/path/to/import.dart'
See the following example:
// settings.json
{
"dartimportsorter.matchingRules": [
{
"label": "Flutter",
"regex": "^package:flutter.*$",
"order": 1,
"regexFlags": ["m"]
},
{
"label": "Dart",
"regex": "^dart:.*$",
"order": 2,
"regexFlags": ["m"]
},
{
"label": "Everything else",
"regex": ".*",
"order": 3,
"regexFlags": ["m"]
}
]
}
The extension comes with the following default rules:
[
{
"label": "Dart",
"regex": "^dart:.*$",
"order": 1,
"regexFlags": ["m"]
},
{
"label": "Flutter",
"regex": "^package:flutter/.*$",
"order": 10,
"regexFlags": ["m"]
},
{
"label": "Package imports that are NOT your app",
"regex": "^package:(?!<app_name>).*$",
"order": 100,
"regexFlags": ["m"]
},
{
"label": "Package imports that ARE your app",
"regex": "^package:<app_name>.*$",
"order": 101,
"regexFlags": ["m"]
},
{
"label": "Relative",
"regex": "^\\..*$",
"order": 1000,
"regexFlags": ["m"]
}
]
You project name is detected automatically from pubspec.yaml and replaces <app_name>
when the extension is used. You can use the <app_name>
placeholder in any custom rules you write.
If you don't provide custom rules in settings.json, then the extension will use these rules by default. If you provide any configuration whatsoever, then the extension will use only your configuration, completely disregarding the defaults.
You can set the extension to sort your imports within each sorted group according to the following rules:
[
{
"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
}
]
}
]
In the above example, imports are first grouped together, then sorted within each group according to the subgroupSortingRules
property.
Since the subgroupSortingRules
property is only defined for the last group, the other groups imports are sorted alphabetically.
For the last group, app imports are placed first, then relative imports. More complex sorting rules can be defined for each group.
{
"dartimportsorter.leaveEmptyLinesBetweenGroups": true
}
See Changelog
Make a pull request with your changes to be merged with the develop
branch as it's what I use for staging. Reference an issue if it's available and add me (@aziznal) as a reviewer. Make sure unit tests pass and we'll get your PR sorted in no time! Oh, and please add unit tests for any new features that may require them.
Configuration inspired by this extension (typescript import sorter)