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

Possibility to add extra line above imports #65

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ If you use import_sorter a lot or need to ignore certain files you should look a
import_sorter:
emojis: true # Optional, defaults to false
comments: false # Optional, defaults to true
header: 'Extra header above' # Optional, adds extra line above all imports.
ignored_files: # Optional, defaults to []
- \/lib\/*
```
Expand Down
5 changes: 4 additions & 1 deletion bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void main(List<String> args) {

var emojis = false;
var noComments = false;
String? header = null;
final ignored_files = [];

// Reading from config in pubspec.yaml safely
Expand All @@ -54,6 +55,7 @@ void main(List<String> args) {
final config = pubspecYaml['import_sorter'];
if (config.containsKey('emojis')) emojis = config['emojis'];
if (config.containsKey('comments')) noComments = !config['comments'];
if (config.containsKey('header')) header = config['header'];
if (config.containsKey('ignored_files')) {
ignored_files.addAll(config['ignored_files']);
}
Expand Down Expand Up @@ -96,7 +98,8 @@ void main(List<String> args) {
}

final sortedFile = sort.sortImports(
file.readAsLinesSync(), packageName, emojis, exitOnChange, noComments);
file.readAsLinesSync(), packageName, emojis, exitOnChange, noComments,
header: header);
if (!sortedFile.updated) {
continue;
}
Expand Down
9 changes: 8 additions & 1 deletion lib/sort.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ ImportSortData sortImports(
bool emojis,
bool exitIfChanged,
bool noComments, {
String? header,
String? filePath,
}) {
String? headerComment = header != null ? '// $header' : null;
String dartImportComment(bool emojis) =>
'//${emojis ? ' 🎯 ' : ' '}Dart imports:';
String flutterImportComment(bool emojis) =>
Expand Down Expand Up @@ -75,6 +77,9 @@ ImportSortData sortImports(
lines[i + 1].startsWith('import ') &&
lines[i + 1].endsWith(';')) {
} else if (noImports()) {
if (headerComment != null && lines[i].contains(headerComment)) {
continue;
}
beforeImportLines.add(lines[i]);
} else {
afterImportLines.add(lines[i]);
Expand All @@ -99,7 +104,9 @@ ImportSortData sortImports(
}
}

final sortedLines = <String>[...beforeImportLines];
final sortedLines = headerComment != null
? <String>[headerComment, '', ...beforeImportLines]
: <String>[...beforeImportLines];

// Adding content conditionally
if (beforeImportLines.isNotEmpty) {
Expand Down