From a247703af5a9ca9c2c5e75bfd3555d7ab08e8760 Mon Sep 17 00:00:00 2001 From: Matthew Nitschke Date: Mon, 6 Mar 2023 11:47:38 -0700 Subject: [PATCH 1/3] added support for indexing paths --- bin/scip_dart.dart | 5 +-- lib/src/flags.dart | 4 +++ lib/src/indexer.dart | 77 ++++++++++++++++++++++++++++---------------- 3 files changed, 57 insertions(+), 29 deletions(-) diff --git a/bin/scip_dart.dart b/bin/scip_dart.dart index 846165e..3c0fc86 100644 --- a/bin/scip_dart.dart +++ b/bin/scip_dart.dart @@ -10,8 +10,9 @@ import 'package:scip_dart/src/flags.dart'; Future main(List args) async { final result = (ArgParser() ..addFlag('performance', aliases: ['perf'], defaultsTo: false) - ..addFlag('verbose', abbr: 'v', defaultsTo: false)) - .parse(args); + ..addFlag('verbose', abbr: 'v', defaultsTo: false) + ..addMultiOption('path', abbr: 'p', defaultsTo: ['./lib']) + ).parse(args); Flags.instance.init(result); diff --git a/lib/src/flags.dart b/lib/src/flags.dart index 1598858..509e01f 100644 --- a/lib/src/flags.dart +++ b/lib/src/flags.dart @@ -7,9 +7,13 @@ class Flags { bool get performance => _performance; bool _performance = false; + List get paths => _paths; + List _paths = []; + void init(ArgResults results) { _verbose = results['verbose'] ?? false; _performance = results['performance'] ?? false; + _paths = (results['path'] as Iterable?)?.toList() ?? []; } static Flags get instance => _instance; diff --git a/lib/src/indexer.dart b/lib/src/indexer.dart index 87a1a13..088c0c5 100644 --- a/lib/src/indexer.dart +++ b/lib/src/indexer.dart @@ -28,18 +28,39 @@ Future indexPackage( .map((package) => p.normalize(package.packageUriRoot.toFilePath())) .toList(); - final collection = AnalysisContextCollection(includedPaths: allPackageRoots); + final indexedPaths = Flags.instance.paths.map( + (relPath) => p.normalize(p.join(dirPath, relPath)), + ); + + final collection = AnalysisContextCollection( + includedPaths: [ + ...allPackageRoots, + ...indexedPaths, + ], + ); if (Flags.instance.performance) print('Analyzing Source'); final st = Stopwatch()..start(); - final context = collection.contextFor(p.join(dirPath, 'lib')); - final files = context.contextRoot - .analyzedFiles() - .where((file) => p.extension(file) == '.dart'); + final resolvedUnitFutures = indexedPaths.map( + (path) { + final context = collection.contextFor(path); + final files = context.contextRoot + .analyzedFiles() + .where((file) => p.extension(file) == '.dart'); + + return files.map(context.currentSession.getResolvedUnit); + }).expand((resUnits) => resUnits); - final resolvedUnits = - await Future.wait(files.map(context.currentSession.getResolvedUnit)); + final resolvedUnits = await Future.wait(resolvedUnitFutures); + + // final context = collection.contextFor(p.join(dirPath, 'lib')); + // final files = context.contextRoot + // .analyzedFiles() + // .where((file) => p.extension(file) == '.dart'); + + // final resolvedUnits = + // await Future.wait(files.map(context.currentSession.getResolvedUnit)); if (Flags.instance.performance) { print('Analyzing Source took: ${st.elapsedMilliseconds}ms'); @@ -47,26 +68,28 @@ Future indexPackage( print('Parsing Ast'); } - final documents = - resolvedUnits.whereType().map((resUnit) { - final relativePath = p.relative(resUnit.path, from: dirPath); - - final visitor = ScipVisitor( - relativePath, - dirPath, - resUnit.lineInfo, - packageConfig, - pubspec, - ); - resUnit.unit.accept(visitor); - - return Document( - language: Language.Dart.name, - relativePath: relativePath, - occurrences: visitor.occurrences, - symbols: visitor.symbols, - ); - }).toList(); + final documents = resolvedUnits + .whereType() + .map((resUnit) { + final relativePath = p.relative(resUnit.path, from: dirPath); + + final visitor = ScipVisitor( + relativePath, + dirPath, + resUnit.lineInfo, + packageConfig, + pubspec, + ); + resUnit.unit.accept(visitor); + + return Document( + language: Language.Dart.name, + relativePath: relativePath, + occurrences: visitor.occurrences, + symbols: visitor.symbols, + ); + }) + .toList(); if (Flags.instance.performance) { print('Parsing Ast took: ${st.elapsedMilliseconds}ms'); From 62273dcdd64031ae56984da013d209d1513f4a55 Mon Sep 17 00:00:00 2001 From: Matthew Nitschke Date: Mon, 6 Mar 2023 11:48:47 -0700 Subject: [PATCH 2/3] fmt --- bin/scip_dart.dart | 4 ++-- lib/src/indexer.dart | 55 +++++++++++++++++++++----------------------- 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/bin/scip_dart.dart b/bin/scip_dart.dart index 3c0fc86..67a3cce 100644 --- a/bin/scip_dart.dart +++ b/bin/scip_dart.dart @@ -11,8 +11,8 @@ Future main(List args) async { final result = (ArgParser() ..addFlag('performance', aliases: ['perf'], defaultsTo: false) ..addFlag('verbose', abbr: 'v', defaultsTo: false) - ..addMultiOption('path', abbr: 'p', defaultsTo: ['./lib']) - ).parse(args); + ..addMultiOption('path', abbr: 'p', defaultsTo: ['./lib'])) + .parse(args); Flags.instance.init(result); diff --git a/lib/src/indexer.dart b/lib/src/indexer.dart index 088c0c5..1869a3e 100644 --- a/lib/src/indexer.dart +++ b/lib/src/indexer.dart @@ -42,17 +42,16 @@ Future indexPackage( if (Flags.instance.performance) print('Analyzing Source'); final st = Stopwatch()..start(); - final resolvedUnitFutures = indexedPaths.map( - (path) { - final context = collection.contextFor(path); - final files = context.contextRoot + final resolvedUnitFutures = indexedPaths.map((path) { + final context = collection.contextFor(path); + final files = context.contextRoot .analyzedFiles() .where((file) => p.extension(file) == '.dart'); - return files.map(context.currentSession.getResolvedUnit); - }).expand((resUnits) => resUnits); + return files.map(context.currentSession.getResolvedUnit); + }).expand((resUnits) => resUnits); - final resolvedUnits = await Future.wait(resolvedUnitFutures); + final resolvedUnits = await Future.wait(resolvedUnitFutures); // final context = collection.contextFor(p.join(dirPath, 'lib')); // final files = context.contextRoot @@ -68,28 +67,26 @@ Future indexPackage( print('Parsing Ast'); } - final documents = resolvedUnits - .whereType() - .map((resUnit) { - final relativePath = p.relative(resUnit.path, from: dirPath); - - final visitor = ScipVisitor( - relativePath, - dirPath, - resUnit.lineInfo, - packageConfig, - pubspec, - ); - resUnit.unit.accept(visitor); - - return Document( - language: Language.Dart.name, - relativePath: relativePath, - occurrences: visitor.occurrences, - symbols: visitor.symbols, - ); - }) - .toList(); + final documents = + resolvedUnits.whereType().map((resUnit) { + final relativePath = p.relative(resUnit.path, from: dirPath); + + final visitor = ScipVisitor( + relativePath, + dirPath, + resUnit.lineInfo, + packageConfig, + pubspec, + ); + resUnit.unit.accept(visitor); + + return Document( + language: Language.Dart.name, + relativePath: relativePath, + occurrences: visitor.occurrences, + symbols: visitor.symbols, + ); + }).toList(); if (Flags.instance.performance) { print('Parsing Ast took: ${st.elapsedMilliseconds}ms'); From c8f33fbcbd7e093b197db9d6aa3ef7798183d565 Mon Sep 17 00:00:00 2001 From: Matthew Nitschke Date: Mon, 6 Mar 2023 11:52:26 -0700 Subject: [PATCH 3/3] updated readme --- README.md | 12 ++++++++++++ lib/src/indexer.dart | 8 -------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2c91743..9ac01dd 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,18 @@ The following command will output a `index.scip` file dart pub global run scip_dart ./path/to/project/root ``` +> Note: by default, only `./lib` is indexed. This can be extended to include other paths using `--path/-p` +> +> ```bash +> dart pub global run scip_dart \ +> -p ./lib \ # path relative to project root +> -p ./test \ +> ./path/to/project/root +>``` +> +> Indexing test directories will impact performance of the indexer + + This file can be analyzed / displayed using the [scip cli](https://github.com/sourcegraph/scip) ```sh diff --git a/lib/src/indexer.dart b/lib/src/indexer.dart index 1869a3e..bcf9884 100644 --- a/lib/src/indexer.dart +++ b/lib/src/indexer.dart @@ -53,14 +53,6 @@ Future indexPackage( final resolvedUnits = await Future.wait(resolvedUnitFutures); - // final context = collection.contextFor(p.join(dirPath, 'lib')); - // final files = context.contextRoot - // .analyzedFiles() - // .where((file) => p.extension(file) == '.dart'); - - // final resolvedUnits = - // await Future.wait(files.map(context.currentSession.getResolvedUnit)); - if (Flags.instance.performance) { print('Analyzing Source took: ${st.elapsedMilliseconds}ms'); st.reset();