@@ -6,6 +6,7 @@ import 'package:analyzer/dart/ast/standard_ast_factory.dart';
6
6
import 'package:analyzer/dart/ast/token.dart' ;
7
7
import 'package:analyzer/dart/ast/visitor.dart' ;
8
8
import 'package:analyzer/src/generated/source.dart' ;
9
+ import 'package:dart_style/src/io.dart' ;
9
10
10
11
import 'argument_list_visitor.dart' ;
11
12
import 'call_chain_visitor.dart' ;
@@ -192,6 +193,8 @@ class SourceVisitor extends ThrowingAstVisitor {
192
193
/// from the output.
193
194
final Set <Token > _suppressPrecedingCommentsAndNewLines = {};
194
195
196
+ late final String ? _currentPackage = packageForUri (_source.uri);
197
+
195
198
/// Initialize a newly created visitor to write source code representing
196
199
/// the visited nodes to the given [writer] .
197
200
SourceVisitor (this ._formatter, this ._lineInfo, this ._source)
@@ -931,7 +934,7 @@ class SourceVisitor extends ThrowingAstVisitor {
931
934
932
935
token (node.rightParenthesis);
933
936
space ();
934
- visit (node.uri);
937
+ _visitUri (node.uri);
935
938
}
936
939
937
940
@override
@@ -1190,7 +1193,7 @@ class SourceVisitor extends ThrowingAstVisitor {
1190
1193
_simpleStatement (node, () {
1191
1194
token (node.keyword);
1192
1195
space ();
1193
- visit (node.uri);
1196
+ _visitUri (node.uri);
1194
1197
1195
1198
_visitConfigurations (node.configurations);
1196
1199
@@ -2082,7 +2085,7 @@ class SourceVisitor extends ThrowingAstVisitor {
2082
2085
_simpleStatement (node, () {
2083
2086
token (node.keyword);
2084
2087
space ();
2085
- visit (node.uri);
2088
+ _visitUri (node.uri);
2086
2089
2087
2090
_visitConfigurations (node.configurations);
2088
2091
@@ -2100,6 +2103,68 @@ class SourceVisitor extends ThrowingAstVisitor {
2100
2103
});
2101
2104
}
2102
2105
2106
+ void _visitUri (StringLiteral uriLiteral) {
2107
+ if (_formatter.fixes.contains (StyleFix .uriShorthand)) {
2108
+ var uri = Uri .parse (uriLiteral.stringValue! );
2109
+
2110
+ String ? result;
2111
+ if (uri.scheme == 'package' &&
2112
+ uri.pathSegments.length > 1 &&
2113
+ uri.path.endsWith ('.dart' )) {
2114
+ var packageName = uri.pathSegments.first;
2115
+ var packageShortName = packageName.split ('.' ).last;
2116
+
2117
+ var path = uri.pathSegments.skip (1 ).join ('/' );
2118
+ // Remove ".dart".
2119
+ path = path.substring (0 , path.length - 5 );
2120
+
2121
+ if (packageShortName == path) {
2122
+ // package:name/name.dart -> name
2123
+ // package:dotted.name/name.dart -> name
2124
+ result = packageName;
2125
+ } else if (packageName == _currentPackage) {
2126
+ // TODO: The issue uses "/" here but the proposal says ":". Using
2127
+ // "/" to match relative paths below.
2128
+ result = '/$path ' ;
2129
+ } else {
2130
+ // package:name/path.dart -> name:path
2131
+ result = '$packageName :$path ' ;
2132
+ }
2133
+
2134
+ // TODO: Detect if package is same as current package and use ":path".
2135
+ // package:current_package/path.dart -> :path
2136
+ } else if (uri.scheme == 'dart' ) {
2137
+ result = 'dart:${uri .path }' ;
2138
+ } else if (uri.scheme == '' && uri.path.endsWith ('.dart' )) {
2139
+ // Shorthand for relative imports.
2140
+
2141
+ // TODO: This is described in the issue, but not the proposal.
2142
+
2143
+ var path = uri.path;
2144
+ // Remove ".dart".
2145
+ path = path.substring (0 , path.length - 5 );
2146
+
2147
+ if (! path.startsWith ('.' )) {
2148
+ // TODO: If path is in root directory, should just do "/".
2149
+ path = './$path ' ;
2150
+ }
2151
+
2152
+ result = path;
2153
+ } else {
2154
+ // No shorthand.
2155
+ }
2156
+
2157
+ if (result != null ) {
2158
+ writePrecedingCommentsAndNewlines (uriLiteral.beginToken);
2159
+ _writeText (result, uriLiteral.offset);
2160
+ } else {
2161
+ visit (uriLiteral);
2162
+ }
2163
+ } else {
2164
+ visit (uriLiteral);
2165
+ }
2166
+ }
2167
+
2103
2168
@override
2104
2169
void visitIndexExpression (IndexExpression node) {
2105
2170
builder.nestExpression ();
@@ -2411,7 +2476,7 @@ class SourceVisitor extends ThrowingAstVisitor {
2411
2476
_simpleStatement (node, () {
2412
2477
token (node.keyword);
2413
2478
space ();
2414
- visit (node.uri);
2479
+ _visitUri (node.uri);
2415
2480
});
2416
2481
}
2417
2482
@@ -2427,7 +2492,7 @@ class SourceVisitor extends ThrowingAstVisitor {
2427
2492
// Part-of may have either a name or a URI. Only one of these will be
2428
2493
// non-null. We visit both since visit() ignores null.
2429
2494
visit (node.libraryName);
2430
- visit (node.uri);
2495
+ if (node.uri != null ) _visitUri (node.uri ! );
2431
2496
});
2432
2497
}
2433
2498
0 commit comments