Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index Directives (import, export, part, part of) #25

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
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
25 changes: 24 additions & 1 deletion lib/src/scip_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ScipVisitor extends GeneralizingAstVisitor {
packageConfig,
pubspec,
) {
final fileSymbol = _symbolGenerator.fileSymbolFor(_relativePath);
final fileSymbol = _symbolGenerator.symbolForFile(_relativePath);
occurrences.add(Occurrence(
symbol: fileSymbol,
range: [0, 0, 0],
Expand All @@ -56,6 +56,8 @@ class ScipVisitor extends GeneralizingAstVisitor {
_visitNormalFormalParameter(node);
} else if (node is SimpleIdentifier) {
_visitSimpleIdentifier(node);
} else if (node is Directive) {
_visitDirective(node);
}

super.visitNode(node);
Expand Down Expand Up @@ -148,6 +150,27 @@ class ScipVisitor extends GeneralizingAstVisitor {
}
}

void _visitDirective(Directive node) {
final element = node.element!;

StringLiteral uriLiteral;
if (node is UriBasedDirective) {
uriLiteral = node.uri;
} else if (node is PartOfDirective && node.uri != null) {
uriLiteral = node.uri!;
} else {
return;
}

final symbol = _symbolGenerator.symbolForDirective(node, element);
if (symbol != null) {
occurrences.add(Occurrence(
range: _lineInfo.getRange(uriLiteral.offset, uriLiteral.length),
symbol: symbol,
));
}
}

/// Registers the provided [element] as a reference to an existing definition
///
/// [node] refers to the ast node where the reference exists, [element]
Expand Down
51 changes: 50 additions & 1 deletion lib/src/symbol_generator.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:pubspec_parse/pubspec_parse.dart';
import 'package:package_config/package_config.dart';
Expand Down Expand Up @@ -51,14 +52,62 @@ class SymbolGenerator {
].join(' ');
}

String fileSymbolFor(String path) {
String symbolForFile(String path) {
return [
'scip-dart',
'pub ${_pubspec.name} ${_pubspec.version}',
'${_escapeNamespacePath(path)}/',
].join(' ');
}

String? symbolForDirective(Directive node, Element element) {
Uri uri;
if (element is LibraryImportElement) {
uri = (element.uri as DirectiveUriWithSource).source.uri;
} else if (element is LibraryExportElement) {
uri = (element.uri as DirectiveUriWithSource).source.uri;
} else if (element is PartElement) {
uri = (element.uri as DirectiveUriWithSource).source.uri;
} else if (node is PartOfDirective && element is LibraryElement) {
uri = element.source.uri;
} else {
return null;
}

if (uri.toString().startsWith('dart')) {
final packageVersion =
element.library!.languageVersion.package.toString();
return [
'scip-dart',
'pub $uri $packageVersion',
_getPackage(element),
_escapeNamespacePath(_pathForSdkElement(element)) + '/'
].join(' ');
}

if (uri.toString().startsWith('package')) {
final resolvedUri = _packageConfig.resolve(uri);
if (resolvedUri == null) return null;
uri = resolvedUri;
}

final config = _packageConfig.packageOf(uri);
if (config == null) return null;

final relativePath = _escapeNamespacePath(
uri.toFilePath().substring(config.root.toFilePath().length),
);

final packageName = config.name;
final version = PackageVersionCache.versionFor(config.root.toFilePath());

return [
'scip-dart',
'pub $packageName $version',
'$relativePath/',
].join(' ');
}

/// Returns a scip package symbol for a provided [Element].
///
/// <package> ::= <manager> ' ' <package-name> ' ' <version>
Expand Down
9 changes: 9 additions & 0 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,12 @@ extension LineInfoExtension on LineInfo {
];
}
}

/// Removes single or double quotes from a string if they exist
String stripQuotes(String input) {
// check if input is wrapped in quotes. If it is, remove them
if (input.startsWith('"') && input.endsWith('"') || input.startsWith("'") && input.endsWith("'")) {
return input.substring(1, input.length -1);
}
return input;
}
7 changes: 7 additions & 0 deletions snapshots/input/basic-project/lib/nested/directives.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'dart:math' as math;
import 'dart:collection';
import 'package:dart_test/main.dart';
import 'package:test/fake.dart';
import '../other.dart';
export '../relationships.dart';
part 'directives_part.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
part of 'directives.dart';
9 changes: 8 additions & 1 deletion snapshots/input/staging-project/pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages: {}
packages:
collection:
dependency: "direct main"
description:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.17.1"
sdks:
dart: ">=2.18.0 <3.0.0"
3 changes: 3 additions & 0 deletions snapshots/input/staging-project/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ version: 1.0.0

environment:
sdk: ">=2.18.0 <3.0.0"

dependencies:
collection: ^1.17.1
1 change: 1 addition & 0 deletions snapshots/output/basic-project/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:dart_test/other.dart';
// definition scip-dart pub dart_test 1.0.0 lib/`main.dart`/
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/

/// This is a fib function
///
Expand Down
1 change: 1 addition & 0 deletions snapshots/output/basic-project/lib/more.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:math' as math;
// definition scip-dart pub dart_test 1.0.0 lib/`more.dart`/
// ^^^^^^^^^^^ reference scip-dart pub dart:math 2.18.0 pub dart_test 1.0.0 package:dart_test/`more.dart`/
// ^^^^ definition scip-dart pub dart_test 1.0.0 lib/`more.dart`/math.

enum AnimalType {
Expand Down
16 changes: 16 additions & 0 deletions snapshots/output/basic-project/lib/nested/directives.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'dart:math' as math;
// definition scip-dart pub dart_test 1.0.0 lib/nested/`directives.dart`/
// ^^^^^^^^^^^ reference scip-dart pub dart:math 2.18.0 pub dart_test 1.0.0 package:dart_test/nested/`directives.dart`/
// ^^^^ definition scip-dart pub dart_test 1.0.0 lib/nested/`directives.dart`/math.
import 'dart:collection';
// ^^^^^^^^^^^^^^^^^ reference scip-dart pub dart:collection 2.18.0 pub dart_test 1.0.0 package:dart_test/nested/`directives.dart`/
import 'package:dart_test/main.dart';
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`main.dart`/
import 'package:test/fake.dart';
// ^^^^^^^^^^^^^^^^^^^^^^^^ reference scip-dart pub test 1.24.3 lib/`fake.dart`/
import '../other.dart';
// ^^^^^^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/
export '../relationships.dart';
// ^^^^^^^^^^^^^^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`relationships.dart`/
part 'directives_part.dart';
// ^^^^^^^^^^^^^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/nested/`directives_part.dart`/
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
part of 'directives.dart';
// definition scip-dart pub dart_test 1.0.0 lib/nested/`directives_part.dart`/
// ^^^^^^^^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/nested/`directives.dart`/
1 change: 1 addition & 0 deletions snapshots/output/basic-project/lib/other.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'more.dart' deferred as more;
// definition scip-dart pub dart_test 1.0.0 lib/`other.dart`/
// ^^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/
// ^^^^ definition scip-dart pub dart_test 1.0.0 lib/`other.dart`/more.

class Foo {
Expand Down
1 change: 1 addition & 0 deletions snapshots/output/basic-project/test/basic_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:test/test.dart';
// definition scip-dart pub dart_test 1.0.0 test/`basic_test.dart`/
// ^^^^^^^^^^^^^^^^^^^^^^^^ reference scip-dart pub test 1.24.3 lib/`test.dart`/

void main() {
// ^^^^ definition scip-dart pub dart_test 1.0.0 test/`basic_test.dart`/main().
Expand Down
Loading