diff --git a/Makefile b/Makefile index bdf9185..ae1a16a 100644 --- a/Makefile +++ b/Makefile @@ -21,4 +21,7 @@ gen-proto: protoc --dart_out=. ./lib/src/gen/scip.proto print: - scip print ./index.scip \ No newline at end of file + scip print ./index.scip + +print-ast: + dart run ./tool/ast_printer.dart ./snapshots/input/staging-project \ No newline at end of file diff --git a/lib/src/symbol_generator.dart b/lib/src/symbol_generator.dart index 76b80c8..b2aa73f 100644 --- a/lib/src/symbol_generator.dart +++ b/lib/src/symbol_generator.dart @@ -44,6 +44,37 @@ class SymbolGenerator { } else if (node is SimpleIdentifier) { var element = node.staticElement; + // A SimpleIdentifier with a direct parent of a ConstructorDeclaration + // is the reference to the class itself. Skip this declaration + if (node.parent is ConstructorDeclaration) { + return null; + } + + // if we're nested under a ConstructorName identifier, use the constructor + // as the element to annotate instead of the reference to the Class + final parentConstructor = node.thisOrAncestorOfType(); + if (parentConstructor != null) { + // ConstructorNames can also include an import PrefixIdentifier: `math.Rectangle()` + // both 'math' and 'Rectangle' are SimpleIdentifiers. We only want the constructor + // element for 'Rectangle' in this case + final parentPrefixIdentifier = + node.thisOrAncestorOfType(); + if (parentPrefixIdentifier?.prefix == node) return element; + + // Constructors can be named: `Foo.bar()`, both `Foo` and `bar` are SimpleIdentifiers + // When the constructor is named, 'bar' is the constructor reference and `Foo` should + // reference the class + if (parentConstructor.name == node) { + return parentConstructor.staticElement; + } else if (parentConstructor.name != null) { + return element; + } + + // Otherwise, constructor is just `Foo()`, so simply return the + // constructor's element + return parentConstructor.staticElement; + } + // Both `.loadLibrary()`, and `.call()` are synthetic functions that // have no definition. These should therefore should not be indexed. if (element is FunctionElement && element.isSynthetic) { diff --git a/snapshots/input/basic-project/lib/more.dart b/snapshots/input/basic-project/lib/more.dart index ae1ccf4..678db4f 100644 --- a/snapshots/input/basic-project/lib/more.dart +++ b/snapshots/input/basic-project/lib/more.dart @@ -36,6 +36,8 @@ class Animal with SleepMixin { } } + factory Animal.cat() => Animal('Timmy', type: AnimalType.cat); + void makeSound() { soundMaker?.call(); } @@ -54,16 +56,19 @@ void main() { List numbers = [1, 2, 3, 4, 5]; int sum = calculateSum(numbers); - Animal cat = Animal('Kitty', type: AnimalType.cat); + Animal bird = Animal('Kitty', type: AnimalType.bird); Animal dog = Animal('Buddy', type: AnimalType.dog); + Animal cat = Animal.cat(); - cat.makeSound(); - cat.sleep(); + bird.makeSound(); + bird.sleep(); dog.makeSound(); dog.sleep(); - print(cat); + cat.makeSound(); + + print(bird); print(dog); print('The sum of $numbers is $sum'); diff --git a/snapshots/input/basic-project/lib/relationships.dart b/snapshots/input/basic-project/lib/relationships.dart index 1ad9923..9218058 100644 --- a/snapshots/input/basic-project/lib/relationships.dart +++ b/snapshots/input/basic-project/lib/relationships.dart @@ -26,5 +26,5 @@ class Dog extends Animal with SwimAction { String get someGetter => 'value'; @override - set someSetter(String v) {}; + set someSetter(String v) => print(v); } \ No newline at end of file diff --git a/snapshots/output/basic-project/lib/main.dart b/snapshots/output/basic-project/lib/main.dart index 37fd565..e9933d3 100755 --- a/snapshots/output/basic-project/lib/main.dart +++ b/snapshots/output/basic-project/lib/main.dart @@ -95,7 +95,7 @@ // ^^^ reference scip-dart pub dart:core 2.19.0 dart:core/`int.dart`/int# // ^^^^^ definition local 5 Foo(1, value: true, value2: ''); -// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo# +// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#``(). // ^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#``().(value) // ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#``().(value2) } diff --git a/snapshots/output/basic-project/lib/more.dart b/snapshots/output/basic-project/lib/more.dart index 5254e4f..22bf7d7 100755 --- a/snapshots/output/basic-project/lib/more.dart +++ b/snapshots/output/basic-project/lib/more.dart @@ -40,7 +40,6 @@ Animal(this.name, {required this.type}) { // ^^^^^^ definition scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#``(). -// ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal# // ^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#name. // ^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#type. // ^^^^ definition scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#``().(type) @@ -77,6 +76,13 @@ } } + factory Animal.cat() => Animal('Timmy', type: AnimalType.cat); +// ^^^ definition scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#cat(). +// ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#``(). +// ^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#``().(type) +// ^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/AnimalType# +// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/AnimalType#cat. + void makeSound() { // ^^^^^^^^^ definition scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#makeSound(). soundMaker?.call(); @@ -122,27 +128,32 @@ // ^^^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/calculateSum(). // ^^^^^^^ reference local 3 - Animal cat = Animal('Kitty', type: AnimalType.cat); + Animal bird = Animal('Kitty', type: AnimalType.bird); // ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal# -// ^^^ definition local 5 -// ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal# -// ^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#``().(type) -// ^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/AnimalType# -// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/AnimalType#cat. +// ^^^^ definition local 5 +// ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#``(). +// ^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#``().(type) +// ^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/AnimalType# +// ^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/AnimalType#bird. Animal dog = Animal('Buddy', type: AnimalType.dog); // ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal# // ^^^ definition local 6 -// ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal# +// ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#``(). // ^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#``().(type) // ^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/AnimalType# // ^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/AnimalType#dog. + Animal cat = Animal.cat(); +// ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal# +// ^^^ definition local 7 +// ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal# +// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#cat(). - cat.makeSound(); -// ^^^ reference local 5 -// ^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#makeSound(). - cat.sleep(); -// ^^^ reference local 5 -// ^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/SleepMixin#sleep(). + bird.makeSound(); +// ^^^^ reference local 5 +// ^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#makeSound(). + bird.sleep(); +// ^^^^ reference local 5 +// ^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/SleepMixin#sleep(). dog.makeSound(); // ^^^ reference local 6 @@ -151,9 +162,13 @@ // ^^^ reference local 6 // ^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/SleepMixin#sleep(). - print(cat); + cat.makeSound(); +// ^^^ reference local 7 +// ^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#makeSound(). + + print(bird); // ^^^^^ reference scip-dart pub dart:core 2.19.0 dart:core/`print.dart`/print(). -// ^^^ reference local 5 +// ^^^^ reference local 5 print(dog); // ^^^^^ reference scip-dart pub dart:core 2.19.0 dart:core/`print.dart`/print(). // ^^^ reference local 6 @@ -165,23 +180,23 @@ print(math.Rectangle(1,2,3,4)); // ^^^^^ reference scip-dart pub dart:core 2.19.0 dart:core/`print.dart`/print(). // ^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/math. -// ^^^^^^^^^ reference scip-dart pub dart:math 2.19.0 dart:math/`rectangle.dart`/Rectangle# +// ^^^^^^^^^ reference scip-dart pub dart:math 2.19.0 dart:math/`rectangle.dart`/Rectangle#``(). [1,2].reduce((a, b) => a + b); // ^^^^^^ reference scip-dart pub dart:core 2.19.0 dart:core/`iterable.dart`/Iterable#reduce(). -// ^ definition local 7 -// ^ definition local 8 -// ^ reference local 7 -// ^ reference local 8 +// ^ definition local 8 +// ^ definition local 9 +// ^ reference local 8 +// ^ reference local 9 } void test(String Function(int) p) {} // ^^^^ definition scip-dart pub dart_test 1.0.0 lib/`more.dart`/test(). // ^^^^^^ reference scip-dart pub dart:core 2.19.0 dart:core/`string.dart`/String# // ^^^ reference scip-dart pub dart:core 2.19.0 dart:core/`int.dart`/int# -// ^ definition local 9 +// ^ definition local 10 void deepTest(String Function(void Function(String test)) p) {} // ^^^^^^^^ definition scip-dart pub dart_test 1.0.0 lib/`more.dart`/deepTest(). // ^^^^^^ reference scip-dart pub dart:core 2.19.0 dart:core/`string.dart`/String# // ^^^^^^ reference scip-dart pub dart:core 2.19.0 dart:core/`string.dart`/String# -// ^ definition local 10 +// ^ definition local 11 diff --git a/snapshots/output/basic-project/lib/other.dart b/snapshots/output/basic-project/lib/other.dart index ae04266..30a1658 100755 --- a/snapshots/output/basic-project/lib/other.dart +++ b/snapshots/output/basic-project/lib/other.dart @@ -18,7 +18,6 @@ // ^^^^^^ definition scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#value3. Foo( // ^^^ definition scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#``(). -// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo# this._far, { // ^^^^ reference local 0 required this.value, @@ -44,7 +43,6 @@ // ^^^^^^^^^^ definition local 1 Bar(this._someValue); // ^^^ definition scip-dart pub dart_test 1.0.0 lib/`other.dart`/Bar#``(). -// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Bar# // ^^^^^^^^^^ reference local 1 void someMethod() { @@ -64,12 +62,12 @@ // ^^^^ reference scip-dart pub dart:async 2.19.0 dart:async/`future.dart`/Future#then(). // ^ definition local 2 Bar('a').someMethod.call() -// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Bar# +// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Bar#``(). // ^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Bar#someMethod(). }); Foo(1, value: true, value2: 'asdf')..value = false; -// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo# +// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#``(). // ^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#``().(value) // ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#``().(value2) // ^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#value. @@ -77,7 +75,7 @@ final someStr = 'someStr'; // ^^^^^^^ definition local 3 Foo(2, value: false, value2: 'some Val!') -// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo# +// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#``(). // ^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#``().(value) // ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#``().(value2) ..value = true diff --git a/snapshots/output/basic-project/lib/relationships.dart b/snapshots/output/basic-project/lib/relationships.dart index dc6a8d9..11b0e1f 100755 --- a/snapshots/output/basic-project/lib/relationships.dart +++ b/snapshots/output/basic-project/lib/relationships.dart @@ -67,9 +67,11 @@ @override // ^^^^^^^^ reference scip-dart pub dart:core 2.19.0 dart:core/`annotations.dart`/override. - set someSetter(String v) {}; + set someSetter(String v) => print(v); // ^^^^^^^^^^ definition scip-dart pub dart_test 1.0.0 lib/`relationships.dart`/Dog#`someSetter`. // relationship scip-dart pub dart_test 1.0.0 lib/`relationships.dart`/Mammal#`someSetter`. implementation reference // ^^^^^^ reference scip-dart pub dart:core 2.19.0 dart:core/`string.dart`/String# // ^ definition local 1 +// ^^^^^ reference scip-dart pub dart:core 2.19.0 dart:core/`print.dart`/print(). +// ^ reference local 1 }