Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit ba50855

Browse files
author
Dart CI
committed
Version 2.15.0-24.0.dev
Merge commit '5b3cadc7e6d7be94ef959e3733357980ff69c684' into 'dev'
2 parents fecde13 + 5b3cadc commit ba50855

File tree

23 files changed

+678
-75
lines changed

23 files changed

+678
-75
lines changed

benchmarks/SDKArtifactSizes/dart/SDKArtifactSizes.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,24 @@ Future<void> main() async {
6363
'$rootDir/dart-sdk/bin/snapshots/$snapshot.dart.snapshot';
6464
await reportArtifactSize(snapshotPath, snapshot);
6565
}
66+
67+
// Measure the (compressed) sdk size.
68+
final tempDir = Directory.systemTemp.createTempSync('dartdev');
69+
final sdkArchive =
70+
compress(File(Platform.resolvedExecutable).parent.parent, tempDir);
71+
await reportArtifactSize(sdkArchive?.path, 'sdk');
72+
tempDir.deleteSync(recursive: true);
73+
}
74+
75+
File compress(Directory sourceDir, Directory targetDir) {
76+
final outFile = File('${targetDir.path}/sdk.zip');
77+
78+
if (Platform.isMacOS || Platform.isLinux) {
79+
Process.runSync(
80+
'zip', ['-r', outFile.absolute.path, sourceDir.absolute.path]);
81+
} else {
82+
return null;
83+
}
84+
85+
return outFile;
6686
}

benchmarks/SDKArtifactSizes/dart2/SDKArtifactSizes.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,24 @@ Future<void> main() async {
6363
'$rootDir/dart-sdk/bin/snapshots/$snapshot.dart.snapshot';
6464
await reportArtifactSize(snapshotPath, snapshot);
6565
}
66+
67+
// Measure the (compressed) sdk size.
68+
final tempDir = Directory.systemTemp.createTempSync('dartdev');
69+
final sdkArchive =
70+
compress(File(Platform.resolvedExecutable).parent.parent, tempDir);
71+
await reportArtifactSize(sdkArchive?.path ?? '', 'sdk');
72+
tempDir.deleteSync(recursive: true);
73+
}
74+
75+
File? compress(Directory sourceDir, Directory targetDir) {
76+
final outFile = File('${targetDir.path}/sdk.zip');
77+
78+
if (Platform.isMacOS || Platform.isLinux) {
79+
Process.runSync(
80+
'zip', ['-r', outFile.absolute.path, sourceDir.absolute.path]);
81+
} else {
82+
return null;
83+
}
84+
85+
return outFile;
6686
}

pkg/analysis_server/lib/src/analysis_server.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import 'package:analysis_server/src/domain_diagnostic.dart';
2424
import 'package:analysis_server/src/domain_execution.dart';
2525
import 'package:analysis_server/src/domain_kythe.dart';
2626
import 'package:analysis_server/src/domain_server.dart';
27+
import 'package:analysis_server/src/domains/analysis/macro_files.dart';
2728
import 'package:analysis_server/src/domains/analysis/occurrences.dart';
2829
import 'package:analysis_server/src/domains/analysis/occurrences_dart.dart';
2930
import 'package:analysis_server/src/edit/edit_domain.dart';
@@ -54,6 +55,7 @@ import 'package:analyzer/src/generated/sdk.dart';
5455
import 'package:analyzer/src/util/file_paths.dart' as file_paths;
5556
import 'package:analyzer_plugin/protocol/protocol_common.dart' hide Element;
5657
import 'package:analyzer_plugin/src/utilities/navigation/navigation.dart';
58+
import 'package:analyzer_plugin/utilities/analyzer_converter.dart';
5759
import 'package:analyzer_plugin/utilities/navigation/navigation_dart.dart';
5860
import 'package:http/http.dart' as http;
5961
import 'package:telemetry/crash_reporting.dart';
@@ -718,7 +720,10 @@ class ServerContextManagerCallbacks extends ContextManagerCallbacks {
718720
server.AnalysisNavigationParams _computeNavigationParams(
719721
String path, CompilationUnit unit) {
720722
var collector = NavigationCollectorImpl();
721-
computeDartNavigation(resourceProvider, collector, unit, null, null);
723+
computeDartNavigation(resourceProvider, collector, unit, null, null,
724+
analyzerConverter: AnalyzerConverter(
725+
locationProvider:
726+
MacroElementLocationProvider(MacroFiles(resourceProvider))));
722727
collector.createRegions();
723728
return server.AnalysisNavigationParams(
724729
path, collector.regions, collector.targets, collector.files);
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analyzer/dart/element/element.dart';
6+
import 'package:analyzer/file_system/file_system.dart';
7+
import 'package:analyzer/source/line_info.dart';
8+
import 'package:analyzer/src/dart/element/element.dart';
9+
import 'package:analyzer/src/util/file_paths.dart' as file_paths;
10+
import 'package:analyzer_plugin/protocol/protocol_common.dart' as protocol;
11+
import 'package:analyzer_plugin/utilities/analyzer_converter.dart';
12+
13+
class MacroElementLocationProvider implements ElementLocationProvider {
14+
final MacroFiles _macroFiles;
15+
16+
MacroElementLocationProvider(this._macroFiles);
17+
18+
@override
19+
protocol.Location? forElement(Element element) {
20+
if (element is HasMacroGenerationData) {
21+
var macro = (element as HasMacroGenerationData).macro;
22+
if (macro != null) {
23+
return _forElement(element, macro);
24+
}
25+
}
26+
}
27+
28+
protocol.Location? _forElement(Element element, MacroGenerationData macro) {
29+
var unitElement = element.thisOrAncestorOfType<CompilationUnitElement>();
30+
if (unitElement is! CompilationUnitElementImpl) {
31+
return null;
32+
}
33+
34+
var generatedFile = _macroFiles.generatedFile(unitElement);
35+
if (generatedFile == null) {
36+
return null;
37+
}
38+
39+
var nameOffset = element.nameOffset;
40+
var nameLength = element.nameLength;
41+
42+
var lineInfo = generatedFile.lineInfo;
43+
var offsetLocation = lineInfo.getLocation(nameOffset);
44+
var endLocation = lineInfo.getLocation(nameOffset + nameLength);
45+
46+
return protocol.Location(generatedFile.path, nameOffset, nameLength,
47+
offsetLocation.lineNumber, offsetLocation.columnNumber,
48+
endLine: endLocation.lineNumber, endColumn: endLocation.columnNumber);
49+
}
50+
}
51+
52+
/// Note, this class changes the file system.
53+
class MacroFiles {
54+
final ResourceProvider _resourceProvider;
55+
56+
/// Keys are source paths.
57+
final Map<String, _MacroGeneratedFile> _files = {};
58+
59+
MacroFiles(this._resourceProvider);
60+
61+
/// If [unitElement] has macro-generated elements, write the combined
62+
/// content into a new file in `.dart_tool`, and return the description of
63+
/// this file.
64+
_MacroGeneratedFile? generatedFile(CompilationUnitElementImpl unitElement) {
65+
var sourcePath = unitElement.source.fullName;
66+
67+
var result = _files[sourcePath];
68+
if (result != null) {
69+
return result;
70+
}
71+
72+
var sourceFile = _resourceProvider.getFile(sourcePath);
73+
74+
// TODO(scheglov) Use workspace?
75+
Folder? packageRoot;
76+
for (var parent in sourceFile.parent2.withAncestors) {
77+
if (parent.getChildAssumingFile(file_paths.pubspecYaml).exists) {
78+
packageRoot = parent;
79+
break;
80+
}
81+
}
82+
if (packageRoot == null) {
83+
return null;
84+
}
85+
86+
var pathContext = _resourceProvider.pathContext;
87+
var relativePath = pathContext.relative(
88+
sourcePath,
89+
from: packageRoot.path,
90+
);
91+
var generatedPath = pathContext.join(
92+
packageRoot.path, '.dart_tool', 'analyzer', 'macro', relativePath);
93+
94+
var generatedContent = unitElement.macroGeneratedContent;
95+
if (generatedContent == null) {
96+
return null;
97+
}
98+
99+
try {
100+
_resourceProvider.getFile(generatedPath)
101+
..parent2.create()
102+
..writeAsStringSync(generatedContent);
103+
} on FileSystemException {
104+
return null;
105+
}
106+
107+
return _files[sourcePath] = _MacroGeneratedFile(
108+
generatedPath,
109+
generatedContent,
110+
);
111+
}
112+
}
113+
114+
class _MacroGeneratedFile {
115+
final String path;
116+
final String content;
117+
final LineInfo lineInfo;
118+
119+
_MacroGeneratedFile(this.path, this.content)
120+
: lineInfo = LineInfo.fromContent(content);
121+
}

pkg/analysis_server/test/analysis/notification_navigation_test.dart

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,64 @@ library my.lib;
775775
assertHasTargetString('my.lib');
776776
}
777777

778+
Future<void> test_macro_simpleIdentifier_getter() async {
779+
// TODO(scheglov) Use PubPackageResolutionTest?
780+
newFile('$projectPath/pubspec.yaml', content: '');
781+
782+
newFile('$projectPath/bin/macro_annotations.dart', content: r'''
783+
library analyzer.macro.annotations;
784+
const observable = 0;
785+
''');
786+
787+
addTestFile(r'''
788+
import 'macro_annotations.dart';
789+
790+
class A {
791+
@observable
792+
int _foo = 0;
793+
}
794+
795+
void f(A a) {
796+
a.foo;
797+
}
798+
''');
799+
800+
await prepareNavigation();
801+
assertHasRegionString('foo;', 3);
802+
803+
var generatedFile = getFile(
804+
'/project/.dart_tool/analyzer/macro/bin/test.dart',
805+
);
806+
807+
var generatedContent = generatedFile.readAsStringSync();
808+
// TODO(scheglov) Improve macro to be more formatted.
809+
expect(generatedContent, r'''
810+
import 'macro_annotations.dart';
811+
812+
class A {
813+
@observable
814+
int _foo = 0;
815+
816+
int get foo => _foo;
817+
818+
set foo(int val) {
819+
print('Setting foo to ${val}');
820+
_foo = val;
821+
}
822+
}
823+
824+
void f(A a) {
825+
a.foo;
826+
}
827+
''');
828+
829+
assertHasFileTarget(
830+
generatedFile.path,
831+
generatedContent.indexOf('foo =>'),
832+
'foo'.length,
833+
);
834+
}
835+
778836
Future<void> test_multiplyDefinedElement() async {
779837
newFile('$projectPath/bin/libA.dart', content: 'library A; int TEST = 1;');
780838
newFile('$projectPath/bin/libB.dart', content: 'library B; int TEST = 2;');

pkg/analyzer_plugin/lib/utilities/analyzer_converter.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ import 'package:analyzer_plugin/protocol/protocol_common.dart' as plugin;
1717
///
1818
/// Clients may not extend, implement or mix-in this class.
1919
class AnalyzerConverter {
20+
/// Optional provider for [analyzer.Element] location, used for example to
21+
/// override the location of macro-generated elements.
22+
final ElementLocationProvider? _locationProvider;
23+
24+
AnalyzerConverter({ElementLocationProvider? locationProvider})
25+
: _locationProvider = locationProvider;
26+
2027
/// Convert the analysis [error] from the 'analyzer' package to an analysis
2128
/// error defined by the plugin API. If a [lineInfo] is provided then the
2229
/// error's location will have a start line and start column. If a [severity]
@@ -199,6 +206,12 @@ class AnalyzerConverter {
199206
if (element == null || element.source == null) {
200207
return null;
201208
}
209+
210+
var result = _locationProvider?.forElement(element);
211+
if (result != null) {
212+
return result;
213+
}
214+
202215
offset ??= element.nameOffset;
203216
length ??= element.nameLength;
204217
if (element is analyzer.CompilationUnitElement ||
@@ -412,3 +425,9 @@ class AnalyzerConverter {
412425
endLine: endLine, endColumn: endColumn);
413426
}
414427
}
428+
429+
abstract class ElementLocationProvider {
430+
/// Return the location of [element] that this provider wants to override,
431+
/// or `null` if the default location should be used.
432+
plugin.Location? forElement(analyzer.Element element);
433+
}

pkg/analyzer_plugin/lib/utilities/navigation/navigation_dart.dart

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ import 'package:analyzer_plugin/utilities/analyzer_converter.dart';
1616
import 'package:analyzer_plugin/utilities/navigation/navigation.dart';
1717

1818
NavigationCollector computeDartNavigation(
19-
ResourceProvider resourceProvider,
20-
NavigationCollector collector,
21-
CompilationUnit unit,
22-
int? offset,
23-
int? length) {
24-
var dartCollector = _DartNavigationCollector(collector, offset, length);
19+
ResourceProvider resourceProvider,
20+
NavigationCollector collector,
21+
CompilationUnit unit,
22+
int? offset,
23+
int? length, {
24+
AnalyzerConverter? analyzerConverter,
25+
}) {
26+
var dartCollector = _DartNavigationCollector(
27+
collector, offset, length, analyzerConverter ?? AnalyzerConverter());
2528
var visitor = _DartNavigationComputerVisitor(resourceProvider, dartCollector);
2629
if (offset == null || length == null) {
2730
unit.accept(visitor);
@@ -66,9 +69,14 @@ class _DartNavigationCollector {
6669
final NavigationCollector collector;
6770
final int? requestedOffset;
6871
final int? requestedLength;
72+
final AnalyzerConverter _analyzerConverter;
6973

7074
_DartNavigationCollector(
71-
this.collector, this.requestedOffset, this.requestedLength);
75+
this.collector,
76+
this.requestedOffset,
77+
this.requestedLength,
78+
this._analyzerConverter,
79+
);
7280

7381
void _addRegion(int offset, int length, Element? element) {
7482
element = element?.nonSynthetic;
@@ -85,15 +93,14 @@ class _DartNavigationCollector {
8593
if (!_isWithinRequestedRange(offset, length)) {
8694
return;
8795
}
88-
var converter = AnalyzerConverter();
89-
var kind = converter.convertElementKind(element.kind);
90-
var location = converter.locationFromElement(element);
96+
var kind = _analyzerConverter.convertElementKind(element.kind);
97+
var location = _analyzerConverter.locationFromElement(element);
9198
if (location == null) {
9299
return;
93100
}
94101

95102
var codeLocation = collector.collectCodeLocations
96-
? _getCodeLocation(element, location, converter)
103+
? _getCodeLocation(element, location)
97104
: null;
98105

99106
collector.addRegion(offset, length, kind, location,
@@ -122,8 +129,8 @@ class _DartNavigationCollector {
122129
}
123130

124131
/// Get the location of the code (excluding leading doc comments) for this element.
125-
protocol.Location? _getCodeLocation(Element element,
126-
protocol.Location location, AnalyzerConverter converter) {
132+
protocol.Location? _getCodeLocation(
133+
Element element, protocol.Location location) {
127134
var codeElement = element;
128135
// For synthetic getters created for fields, we need to access the associated
129136
// variable to get the codeOffset/codeLength.
@@ -162,7 +169,7 @@ class _DartNavigationCollector {
162169
codeOffset = offsetAfterDocs;
163170
}
164171

165-
return converter.locationFromElement(element,
172+
return _analyzerConverter.locationFromElement(element,
166173
offset: codeOffset, length: codeLength);
167174
}
168175

0 commit comments

Comments
 (0)