Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.dart_tool
.DS_Store
index.scip
index.scip
snapshots/input/staging-project/lib/**
snapshots/output/staging-project/lib/**
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
regen-snapshots:
dart run scip_dart ./snapshots/input/basic-project --verbose
scip snapshot --to ./snapshots/output/basic-project

run:
dart bin/main.dart ./snapshots/input/basic-project --verbose
dart run scip_dart ./snapshots/input/staging-project --verbose

snap:
scip snapshot --to ./snapshots/output/basic-project
scip snapshot --to ./snapshots/output/staging-project

lint:
scip lint ./index.scip
Expand Down
102 changes: 64 additions & 38 deletions lib/src/scip_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ScipVisitor extends GeneralizingAstVisitor {

@override
void visitNode(AstNode node) {
// print(':: $node ${node.runtimeType}');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#nit leftover print

if (node is Comment) {
// For now, don't parse anything within comments (this was broken for
// local references). Later update to support this
Expand All @@ -55,8 +56,8 @@ class ScipVisitor extends GeneralizingAstVisitor {
// to correctly parse all [Declaration] ast nodes.
if (node is Declaration) {
_visitDeclaration(node);
} else if (node is SimpleFormalParameter) {
_visitSimpleFormalParameter(node);
} else if (node is NormalFormalParameter) {
_visitNormalFormalParameter(node);
Comment on lines +59 to +60
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NormalFormalParameter both field and regular parameters

} else if (node is SimpleIdentifier) {
_visitSimpleIdentifier(node);
}
Expand All @@ -68,38 +69,26 @@ class ScipVisitor extends GeneralizingAstVisitor {
if (node.declaredElement == null) return;

final element = node.declaredElement!;

final symbol = _symbolGenerator.symbolFor(element);
if (symbol != null) {
_registerSymbol(symbol, element);

occurrences.add(Occurrence(
range: _lineInfo.getRange(element.nameOffset, element.nameLength),
symbol: symbol,
symbolRoles: SymbolRole.Definition.value,
));
}
_registerAsDefinition(element);
}

void _visitSimpleFormalParameter(SimpleFormalParameter node) {
if (node.declaredElement == null) return;

final element = node.declaredElement!;

final symbol = _symbolGenerator.symbolFor(element);
if (symbol != null) {
_registerSymbol(symbol, element);

occurrences.add(Occurrence(
range: _lineInfo.getRange(element.nameOffset, element.nameLength),
symbol: symbol,
symbolRoles: SymbolRole.Definition.value,
));
void _visitNormalFormalParameter(NormalFormalParameter node) {
final element = node.declaredElement;
if (element == null) return;

// FieldFormalParameters reference a field instead of define a declaration
// register them as such
if (element is FieldFormalParameterElement) {
_registerAsReference(
element.field!,
offset: node.name!.offset,
length: node.name!.length,
);
} else {
_registerAsDefinition(element);
}
}

// references to a type, String, int, SomeClass... anything that can be GoTo'ed
// This will be utilized for defining `Occurrences`
void _visitSimpleIdentifier(SimpleIdentifier node) {
final element = node.staticElement;

Expand All @@ -108,16 +97,40 @@ class ScipVisitor extends GeneralizingAstVisitor {
// EX: `color(path, front: Styles.YELLOW);` where `color` comes from the chalk-dart package
if (element == null || element.source == null) return;

if (node.inDeclarationContext()) {
_registerAsDefinition(element);
} else {
_registerAsReference(
element,
offset: node.offset,
length: node.name.length,
);
}
}

/// Registers the provided [element] as a reference to an existing definition
///
/// [node] refers to the ast node where the reference exists, [element]
/// is the resolved element of the downstream element.
///
/// If [element] exists outside of the projects source, it will be added to the
/// [globalExternalSymbols].
void _registerAsReference(
Element element, {
required int offset,
required int length,
}) {
final symbol = _symbolGenerator.symbolFor(element);
if (symbol != null) {
occurrences.add(Occurrence(
range: _lineInfo.getRange(node.offset, node.name.length),
range: _lineInfo.getRange(offset, length),
symbol: symbol,
));

if (!element.source!.fullName.startsWith(_projectRoot)) {
if (globalExternalSymbols
.every((symbolInfo) => symbolInfo.symbol != symbol)) {
if (!globalExternalSymbols.any(
(symbolInfo) => symbolInfo.symbol == symbol,
)) {
final meta = getSymbolMetadata(element);
globalExternalSymbols.add(SymbolInformation(
symbol: symbol,
Expand All @@ -128,11 +141,24 @@ class ScipVisitor extends GeneralizingAstVisitor {
}
}

void _registerSymbol(String symbol, Element ele) {
final meta = getSymbolMetadata(ele);
symbols.add(SymbolInformation(
symbol: symbol,
documentation: meta.documentation,
));
/// Registers a provided [element] as a definition
///
/// This adds both a symbol, and an occurrence for the element and it's
/// name
void _registerAsDefinition(Element element) {
final symbol = _symbolGenerator.symbolFor(element);
if (symbol != null) {
final meta = getSymbolMetadata(element);
symbols.add(SymbolInformation(
symbol: symbol,
documentation: meta.documentation,
));

occurrences.add(Occurrence(
range: _lineInfo.getRange(element.nameOffset, element.nameLength),
symbol: symbol,
symbolRoles: SymbolRole.Definition.value,
));
}
}
}
4 changes: 3 additions & 1 deletion snapshots/input/basic-project/lib/more.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dart:math';
import 'dart:math' as math;

enum AnimalType {
cat,
Expand Down Expand Up @@ -65,4 +65,6 @@ void main() {
print(cat);
print(dog);
print('The sum of $numbers is $sum');

print(math.Rectangle(1,2,3,4));
}
3 changes: 3 additions & 0 deletions snapshots/input/staging-project/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# snapshots/staging-project

This project is designed to be used as a place to test/stage small implementations of dart source. No changes within `lib/` are committed
5 changes: 5 additions & 0 deletions snapshots/input/staging-project/pubspec.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages: {}
sdks:
dart: ">=2.18.0 <3.0.0"
5 changes: 5 additions & 0 deletions snapshots/input/staging-project/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: dart_test
version: 1.0.0

environment:
sdk: ">=2.18.0 <3.0.0"
11 changes: 10 additions & 1 deletion snapshots/output/basic-project/lib/more.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dart:math';
import 'dart:math' as math;
// definition scip-dart pub dart_test 1.0.0 lib/more.dart/
// ^^^^ definition scip-dart pub dart_test 1.0.0 lib/more.dart/math.
// documentation ```dart
Comment on lines +1 to +4
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as math was added to demonstrate the as indexing fix

Before, math was considered a reference, but it is actually a definition


enum AnimalType {
// ^^^^^^^^^^ definition scip-dart pub dart_test 1.0.0 lib/more.dart/AnimalType#
Expand Down Expand Up @@ -51,6 +53,8 @@
// ^^^^^^ definition scip-dart pub dart_test 1.0.0 lib/more.dart/Animal#<constructor>().
// documentation ```dart
// ^^^^^^ 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.
Comment on lines +56 to +57
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main fix in this pr, instead of skipping field parameters, they are indexed

These are not private, so they are not considered to be local

switch (type) {
// ^^^^ reference scip-dart pub dart_test 1.0.0 lib/more.dart/Animal#type.
case AnimalType.cat:
Expand Down Expand Up @@ -169,5 +173,10 @@
// ^^^^^ reference scip-dart pub dart:core 2.18.0 lib/core/print.dart/print().
// ^^^^^^^ reference local 3
// ^^^ reference local 4

print(math.Rectangle(1,2,3,4));
// ^^^^^ reference scip-dart pub dart:core 2.18.0 lib/core/print.dart/print().
// ^^^^ reference scip-dart pub dart_test 1.0.0 lib/more.dart/math.
// ^^^^^^^^^ reference scip-dart pub dart:math 2.18.0 lib/math/rectangle.dart/Rectangle#
}

1 change: 1 addition & 0 deletions snapshots/output/basic-project/lib/other.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
// ^^^ definition scip-dart pub dart_test 1.0.0 lib/other.dart/Foo#<constructor>().
// documentation ```dart
// ^^^ reference scip-dart pub dart_test 1.0.0 lib/other.dart/Foo#
// ^^^^ reference local 0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

named field parameters are now indexed, this is a local reference because its private

}