Skip to content

Commit

Permalink
Optimizations and Refactoring of String operations
Browse files Browse the repository at this point in the history
  • Loading branch information
oppahansi committed Apr 28, 2024
1 parent 4e6438a commit 0eb3289
Show file tree
Hide file tree
Showing 15 changed files with 469 additions and 481 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.1

- Optimizations and Refactoring of String operations

## 1.1.0

- Added an option to disable dart formatter
Expand Down
4 changes: 2 additions & 2 deletions bin/better_imports.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import "package:better_imports/src/arg_parser.dart";
import "package:better_imports/src/cfg.dart";
import "package:better_imports/src/constants.dart";
import "package:better_imports/src/file_extension.dart";
import "package:better_imports/src/file_path_collector.dart";
import "package:better_imports/src/import_sorter.dart";
import "package:better_imports/src/file_paths_collector.dart";
import "package:better_imports/src/files_sorter.dart";
import "package:better_imports/src/log.dart";
import "package:better_imports/src/sorted_result.dart";

Expand Down
Binary file removed bin/better_imports.exe
Binary file not shown.
4 changes: 2 additions & 2 deletions lib/src/cfg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Cfg {
_setTrace();
_setRelative();
_setDryRun();
_setNoDartFmt();
_setDartFmt();

_setFolders();

Expand Down Expand Up @@ -270,7 +270,7 @@ class Cfg {
}
}

void _setNoDartFmt() {
void _setDartFmt() {
log.fine("┠─ Setting dart fmt..");

if (_biYamlSection != null && _biYamlSection![Constants.dartFmt] != null) {
Expand Down
10 changes: 5 additions & 5 deletions lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ class Constants {

static const projectNameOption = "project-name";

static const dartImportsComment = "// Dart Imports";
static const flutterImportsComment = "// Flutter Imports";
static const packageImportsComment = "// Package Imports";
static const projectImportsComment = "// Project Imports";
static const relativeProjectImportsComment = "// Relative Project Imports";
static const dartComment = "// Dart Imports";
static const flutterComment = "// Flutter Imports";
static const packageComment = "// Package Imports";
static const projectComment = "// Project Imports";
static const relativeComment = "// Relative Project Imports";

static final title = """
_ _ _
Expand Down
2 changes: 1 addition & 1 deletion lib/src/import_type.dart → lib/src/directive_type.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
enum ImportType {
enum DirectiveType {
library,
dart,
flutter,
Expand Down
130 changes: 130 additions & 0 deletions lib/src/directives_sorter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Project Imports
import 'package:better_imports/src/cfg.dart';
import 'package:better_imports/src/constants.dart';
import 'package:better_imports/src/directive_type.dart';
import 'package:better_imports/src/log.dart';

String sort(
String path,
Map<DirectiveType, Map<String, List<String>>> directivesWithComments,
Cfg cfg,
) {
final buffer = StringBuffer();
var projectDirectives = directivesWithComments[DirectiveType.project]!;
var convertedProjectDirectives =
_convertProjectImports(path, projectDirectives, cfg);

_add(buffer, directivesWithComments[DirectiveType.library]!, "", cfg);
_add(buffer, directivesWithComments[DirectiveType.dart]!,
Constants.dartComment, cfg);
_add(buffer, directivesWithComments[DirectiveType.flutter]!,
Constants.flutterComment, cfg);
_add(buffer, directivesWithComments[DirectiveType.package]!,
Constants.packageComment, cfg);
_add(buffer, convertedProjectDirectives, Constants.projectComment, cfg);
_add(buffer, directivesWithComments[DirectiveType.relative]!,
Constants.relativeComment, cfg);
_add(buffer, directivesWithComments[DirectiveType.part]!, "", cfg);

return buffer.toString();
}

Map<String, List<String>> _convertProjectImports(
String path,
Map<String, List<String>> projectDirectives,
Cfg cfg,
) {
log.fine("┠── Processing project imports..");

var newProjectImports = <String, List<String>>{};

for (var directiveValue in projectDirectives.keys) {
var convertedDirectiveValue = "";

if (cfg.relative) {
convertedDirectiveValue =
_convertToRelativeProjectImport(path, directiveValue, cfg);
} else {
convertedDirectiveValue =
_convertToPackageProjectImport(path, directiveValue, cfg);
}

newProjectImports.putIfAbsent(
convertedDirectiveValue, () => projectDirectives[directiveValue]!);
}

return newProjectImports;
}

String _convertToRelativeProjectImport(
String path,
String importLine,
Cfg cfg,
) {
log.fine("┠─── Converting to relative project import..");

if (importLine.contains("..")) {
return importLine;
}

if (importLine.contains("'package:${cfg.projectName}")) {
if (!path.contains("lib")) {
return importLine.replaceFirst("package:${cfg.projectName}", "../lib");
} else {
return importLine.replaceFirst("package:${cfg.projectName}", "..");
}
}

return importLine;
}

String _convertToPackageProjectImport(
String path,
String directiveValue,
Cfg cfg,
) {
log.fine("┠─── Converting to project import..");

if (directiveValue.startsWith("import 'package:${cfg.projectName}")) {
return directiveValue;
}

if (!directiveValue.contains("lib")) {
directiveValue = directiveValue.replaceFirst("lib/", "");
}

if (directiveValue.contains("..")) {
return directiveValue.replaceFirst("..", "package:${cfg.projectName}");
} else {
return directiveValue.replaceFirst(
"import '", "import 'package:${cfg.projectName}/");
}
}

void _add(
StringBuffer buffer,
Map<String, List<String>> directives,
String directiveTypeComment,
Cfg cfg,
) {
try {
if (directives.isNotEmpty) {
buffer.writeln();

if (cfg.comments && directiveTypeComment.isNotEmpty) {
buffer.writeln(directiveTypeComment);
}

for (var directive in directives.keys) {
var comments = directives[directive]!;
if (comments.isNotEmpty) {
buffer.writeln(comments.join("\n"));
}

buffer.writeln(directive);
}
}
} catch (e, s) {
log.severe("Error adding directives and comments: $e\n$s");
}
}
7 changes: 0 additions & 7 deletions lib/src/file_path_collector_result.dart

This file was deleted.

6 changes: 6 additions & 0 deletions lib/src/file_paths.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class FilePaths {
final List<String> allPaths;
final List<String> filteredPaths;

FilePaths({required this.allPaths, required this.filteredPaths});
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:io';

// Project Imports
import 'package:better_imports/src/cfg.dart';
import 'package:better_imports/src/file_path_collector_result.dart';
import 'package:better_imports/src/file_paths.dart';
import 'package:better_imports/src/log.dart';

class FilePathsCollector {
Expand All @@ -13,14 +13,14 @@ class FilePathsCollector {

FilePathsCollector({required this.cfg});

FilePathCollectorResult collect() {
FilePaths collect() {
_collectInFolders();

_filteredFilePaths.addAll(List.from(_allFilePaths));

_filterFilePaths();

return FilePathCollectorResult(
return FilePaths(
allPaths: _allFilePaths,
filteredPaths: _filteredFilePaths,
);
Expand Down
Loading

0 comments on commit 0eb3289

Please sign in to comment.