From 892b902d11832fb0a5a89e892f40177abb28c5f2 Mon Sep 17 00:00:00 2001 From: Konstantin Shcheglov Date: Tue, 17 Aug 2021 20:23:42 +0000 Subject: [PATCH] Improve macro code formatting. Change-Id: I3c9f1ddaf6b5562a10b767c2f5d7297a5ed655e7 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/210421 Reviewed-by: Brian Wilkerson Commit-Queue: Konstantin Shcheglov --- .../notification_navigation_test.dart | 11 ++-- .../lib/src/dart/analysis/driver.dart | 2 +- .../lib/src/macro/builders/data_class.dart | 10 +-- .../lib/src/macro/builders/observable.dart | 11 ++-- pkg/analyzer/lib/src/macro/impl/macro.dart | 2 +- .../lib/src/summary2/informative_data.dart | 2 +- .../test/src/summary/resynthesize_common.dart | 66 +++++++++---------- 7 files changed, 53 insertions(+), 51 deletions(-) diff --git a/pkg/analysis_server/test/analysis/notification_navigation_test.dart b/pkg/analysis_server/test/analysis/notification_navigation_test.dart index 8a8f918f80b..cccab312f49 100644 --- a/pkg/analysis_server/test/analysis/notification_navigation_test.dart +++ b/pkg/analysis_server/test/analysis/notification_navigation_test.dart @@ -805,7 +805,6 @@ void f(A a) { ); var generatedContent = generatedFile.readAsStringSync(); - // TODO(scheglov) Improve macro to be more formatted. expect(generatedContent, r''' import 'macro_annotations.dart'; @@ -813,12 +812,12 @@ class A { @observable int _foo = 0; -int get foo => _foo; + int get foo => _foo; -set foo(int val) { - print('Setting foo to ${val}'); - _foo = val; -} + set foo(int val) { + print('Setting foo to ${val}'); + _foo = val; + } } void f(A a) { diff --git a/pkg/analyzer/lib/src/dart/analysis/driver.dart b/pkg/analyzer/lib/src/dart/analysis/driver.dart index d8b80caf6b2..80e75946103 100644 --- a/pkg/analyzer/lib/src/dart/analysis/driver.dart +++ b/pkg/analyzer/lib/src/dart/analysis/driver.dart @@ -80,7 +80,7 @@ import 'package:meta/meta.dart'; /// TODO(scheglov) Clean up the list of implicitly analyzed files. class AnalysisDriver implements AnalysisDriverGeneric { /// The version of data format, should be incremented on every format change. - static const int DATA_VERSION = 169; + static const int DATA_VERSION = 170; /// The number of exception contexts allowed to write. Once this field is /// zero, we stop writing any new exception contexts in this process. diff --git a/pkg/analyzer/lib/src/macro/builders/data_class.dart b/pkg/analyzer/lib/src/macro/builders/data_class.dart index 664f5d87dd2..86d99e082a9 100644 --- a/pkg/analyzer/lib/src/macro/builders/data_class.dart +++ b/pkg/analyzer/lib/src/macro/builders/data_class.dart @@ -52,8 +52,9 @@ class HashCodeMacro implements ClassDeclarationMacro { .join(' ^ '); builder.addToClass( Declaration(''' -@override -int get hashCode => $expression;'''), + @override + int get hashCode => $expression; +'''), ); } } @@ -73,8 +74,9 @@ class ToStringMacro implements ClassDeclarationMacro { }).join(', '); builder.addToClass( Declaration(''' -@override -String toString() => '${classElement.name}($fieldsCode)';'''), + @override + String toString() => '${classElement.name}($fieldsCode)'; +'''), ); } } diff --git a/pkg/analyzer/lib/src/macro/builders/observable.dart b/pkg/analyzer/lib/src/macro/builders/observable.dart index 6857d34b081..dac5565e8dd 100644 --- a/pkg/analyzer/lib/src/macro/builders/observable.dart +++ b/pkg/analyzer/lib/src/macro/builders/observable.dart @@ -33,15 +33,16 @@ class ObservableMacro implements FieldDeclarationMacro { var publicName = name.substring(1); var getter = Declaration( - '$typeCode get $publicName => $name;', + ' $typeCode get $publicName => $name;', ); builder.addToClass(getter); var setter = Declaration(''' -set $publicName($typeCode val) { - print('Setting $publicName to \${val}'); - $name = val; -}'''); + set $publicName($typeCode val) { + print('Setting $publicName to \${val}'); + $name = val; + } +'''); builder.addToClass(setter); } } diff --git a/pkg/analyzer/lib/src/macro/impl/macro.dart b/pkg/analyzer/lib/src/macro/impl/macro.dart index 989bdb48484..59748409f3f 100644 --- a/pkg/analyzer/lib/src/macro/impl/macro.dart +++ b/pkg/analyzer/lib/src/macro/impl/macro.dart @@ -114,7 +114,7 @@ class MacroGeneratedContent { return a.data.id - b.data.id; }); - const classMemberCodePrefix = '\n'; + const classMemberCodePrefix = '\n '; const classMemberCodeSuffix = '\n'; // TODO(scheglov) make it required? var generatedContent = linkingUnit.input.sourceContent!; diff --git a/pkg/analyzer/lib/src/summary2/informative_data.dart b/pkg/analyzer/lib/src/summary2/informative_data.dart index f513e75aaf5..0136b194788 100644 --- a/pkg/analyzer/lib/src/summary2/informative_data.dart +++ b/pkg/analyzer/lib/src/summary2/informative_data.dart @@ -669,7 +669,7 @@ class InformativeDataApplier { ) { var macroGenerationDataList = unitElement.macroGenerationDataList; if (macroGenerationDataList != null) { - const classMemberCodePrefix = '\n'; + const classMemberCodePrefix = '\n '; const classMemberCodeSuffix = '\n'; var generatedContent = unitInfoData.content; var shift = 0; diff --git a/pkg/analyzer/test/src/summary/resynthesize_common.dart b/pkg/analyzer/test/src/summary/resynthesize_common.dart index 74fe439e873..a0c24da100f 100644 --- a/pkg/analyzer/test/src/summary/resynthesize_common.dart +++ b/pkg/analyzer/test/src/summary/resynthesize_common.dart @@ -21282,11 +21282,11 @@ library final b @88 type: int? constructors - @92 + @94 parameters - requiredName final this.a @109 + requiredName final this.a @111 type: int - optionalNamed final this.b @117 + optionalNamed final this.b @119 type: int? accessors synthetic get a @-1 @@ -21300,7 +21300,7 @@ class A { final int a; final int? b; -A({required this.a, this.b}); + A({required this.a, this.b}); } '''); } @@ -21335,7 +21335,7 @@ library returnType: int synthetic get b @-1 returnType: int - get hashCode @102 + get hashCode @106 metadata Annotation atSign: @ @0 @@ -21352,8 +21352,8 @@ class A { final int a; final int b; -@override -int get hashCode => a.hashCode ^ b.hashCode; + @override + int get hashCode => a.hashCode ^ b.hashCode; } '''); } @@ -21410,15 +21410,15 @@ library accessors synthetic get b @-1 returnType: int - get hashCode @126 + get hashCode @130 metadata Annotation - atSign: @ @108 + atSign: @ @110 element: dart:core::@getter::override name: SimpleIdentifier staticElement: dart:core::@getter::override staticType: null - token: override @109 + token: override @111 returnType: int macroGeneratedContent import 'macro_annotations.dart'; @@ -21431,8 +21431,8 @@ class A { class B extends A { final int b; -@override -int get hashCode => b.hashCode ^ a.hashCode; + @override + int get hashCode => b.hashCode ^ a.hashCode; } '''); } @@ -21479,11 +21479,11 @@ library requiredPositional __f @-1 type: int returnType: void - get f @80 + get f @82 returnType: int - set f @94 + set f @98 parameters - requiredPositional val @100 + requiredPositional val @104 type: int returnType: void macroGeneratedContent @@ -21492,12 +21492,12 @@ class A { @observable int _f = 0; -int get f => _f; + int get f => _f; -set f(int val) { - print('Setting f to ${val}'); - _f = val; -} + set f(int val) { + print('Setting f to ${val}'); + _f = val; + } } '''); } @@ -21547,11 +21547,11 @@ library requiredPositional __f @-1 type: T returnType: void - get f @75 + get f @77 returnType: T - set f @89 + set f @93 parameters - requiredPositional val @93 + requiredPositional val @97 type: T returnType: void macroGeneratedContent @@ -21560,12 +21560,12 @@ class A { @observable T _f; -T get f => _f; + T get f => _f; -set f(T val) { - print('Setting f to ${val}'); - _f = val; -} + set f(T val) { + print('Setting f to ${val}'); + _f = val; + } } '''); } @@ -21611,15 +21611,15 @@ library synthetic get b @-1 returnType: int methods - toString @101 + toString @105 metadata Annotation - atSign: @ @84 + atSign: @ @86 element: dart:core::@getter::override name: SimpleIdentifier staticElement: dart:core::@getter::override staticType: null - token: override @85 + token: override @87 returnType: String macroGeneratedContent import 'macro_annotations.dart'; @@ -21628,8 +21628,8 @@ class A { final int a; final int b; -@override -String toString() => 'A(a: $a, b: $b)'; + @override + String toString() => 'A(a: $a, b: $b)'; } '''); }