Skip to content

Commit

Permalink
Move shouldFileBeAnalyzed into ContextManager
Browse files Browse the repository at this point in the history
  • Loading branch information
bwilkerson committed Mar 1, 2016
1 parent e83151c commit 841f8e6
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 72 deletions.
71 changes: 28 additions & 43 deletions pkg/analysis_server/lib/src/analysis_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ class AnalysisServer {
*/
final ServerPlugin serverPlugin;

/**
* A list of the globs used to determine which files should be analyzed. The
* list is lazily created and should be accessed using [analyzedFilesGlobs].
*/
List<Glob> _analyzedFilesGlobs = null;

/**
* The [ContextManager] that handles the mapping from analysis roots to
* context directories.
Expand Down Expand Up @@ -321,6 +327,7 @@ class AnalysisServer {
packageResolverProvider,
embeddedResolverProvider,
packageMapProvider,
analyzedFilesGlobs,
instrumentationService,
defaultContextOptions);
ServerContextManagerCallbacks contextManagerCallbacks =
Expand Down Expand Up @@ -354,6 +361,27 @@ class AnalysisServer {
Iterable<AnalysisContext> get analysisContexts =>
contextManager.analysisContexts;

/**
* Return a list of the globs used to determine which files should be analyzed.
*/
List<Glob> get analyzedFilesGlobs {
if (_analyzedFilesGlobs == null) {
_analyzedFilesGlobs = <Glob>[];
List<String> patterns = serverPlugin.analyzedFilePatterns;
for (String pattern in patterns) {
try {
_analyzedFilesGlobs
.add(new Glob(JavaFile.pathContext.separator, pattern));
} catch (exception, stackTrace) {
AnalysisEngine.instance.logger.logError(
'Invalid glob pattern: "$pattern"',
new CaughtException(exception, stackTrace));
}
}
}
return _analyzedFilesGlobs;
}

/**
* Return a table mapping [Folder]s to the [AnalysisContext]s associated with
* them.
Expand Down Expand Up @@ -1460,35 +1488,8 @@ class ServerContextManagerCallbacks extends ContextManagerCallbacks {
*/
final ResourceProvider resourceProvider;

/**
* A list of the globs used to determine which files should be analyzed. The
* list is lazily created and should be accessed using [analyzedFilesGlobs].
*/
List<Glob> _analyzedFilesGlobs = null;

ServerContextManagerCallbacks(this.analysisServer, this.resourceProvider);

/**
* Return a list of the globs used to determine which files should be analyzed.
*/
List<Glob> get analyzedFilesGlobs {
if (_analyzedFilesGlobs == null) {
_analyzedFilesGlobs = <Glob>[];
List<String> patterns = analysisServer.serverPlugin.analyzedFilePatterns;
for (String pattern in patterns) {
try {
_analyzedFilesGlobs
.add(new Glob(JavaFile.pathContext.separator, pattern));
} catch (exception, stackTrace) {
AnalysisEngine.instance.logger.logError(
'Invalid glob pattern: "$pattern"',
new CaughtException(exception, stackTrace));
}
}
}
return _analyzedFilesGlobs;
}

@override
AnalysisContext addContext(
Folder folder, AnalysisOptions options, FolderDisposition disposition) {
Expand Down Expand Up @@ -1542,22 +1543,6 @@ class ServerContextManagerCallbacks extends ContextManagerCallbacks {
context.dispose();
}

@override
bool shouldFileBeAnalyzed(File file) {
for (Glob glob in analyzedFilesGlobs) {
if (glob.matches(file.path)) {
// Emacs creates dummy links to track the fact that a file is open for
// editing and has unsaved changes (e.g. having unsaved changes to
// 'foo.dart' causes a link '.#foo.dart' to be created, which points to
// the non-existent file '[email protected]'. To avoid these dummy
// links causing the analyzer to thrash, just ignore links to
// non-existent files.
return file.exists;
}
}
return false;
}

@override
void updateContextPackageUriResolver(
Folder contextFolder, FolderDisposition disposition) {
Expand Down
40 changes: 30 additions & 10 deletions pkg/analysis_server/lib/src/context_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/generated/source_io.dart';
import 'package:analyzer/src/task/options.dart';
import 'package:analyzer/src/util/absolute_path.dart';
import 'package:analyzer/src/util/glob.dart';
import 'package:analyzer/src/util/yaml.dart';
import 'package:package_config/packages.dart';
import 'package:package_config/packages_file.dart' as pkgfile show parse;
Expand Down Expand Up @@ -346,11 +347,6 @@ abstract class ContextManagerCallbacks {
*/
void removeContext(Folder folder, List<String> flushedFiles);

/**
* Return `true` if the given [file] should be analyzed.
*/
bool shouldFileBeAnalyzed(File file);

/**
* Called when the disposition for a context has changed.
*/
Expand Down Expand Up @@ -453,15 +449,20 @@ class ContextManagerImpl implements ContextManager {
new AnalysisOptionsProvider();

/**
* The instrumentation service used to report instrumentation data.
* A list of the globs used to determine which files should be analyzed.
*/
final InstrumentationService _instrumentationService;
final List<Glob> analyzedFilesGlobs;

/**
* The default options used to create new analysis contexts.
*/
final AnalysisOptionsImpl defaultContextOptions;

/**
* The instrumentation service used to report instrumentation data.
*/
final InstrumentationService _instrumentationService;

@override
ContextManagerCallbacks callbacks;

Expand Down Expand Up @@ -490,6 +491,7 @@ class ContextManagerImpl implements ContextManager {
this.packageResolverProvider,
this.embeddedUriResolverProvider,
this._packageMapProvider,
this.analyzedFilesGlobs,
this._instrumentationService,
this.defaultContextOptions) {
absolutePathContext = resourceProvider.absolutePathContext;
Expand Down Expand Up @@ -796,7 +798,7 @@ class ContextManagerImpl implements ContextManager {
// add files, recurse into folders
if (child is File) {
// ignore if should not be analyzed at all
if (!callbacks.shouldFileBeAnalyzed(child)) {
if (!_shouldFileBeAnalyzed(child)) {
continue;
}
// ignore if was not excluded
Expand Down Expand Up @@ -843,7 +845,7 @@ class ContextManagerImpl implements ContextManager {
}
// add files, recurse into folders
if (child is File) {
if (callbacks.shouldFileBeAnalyzed(child)) {
if (_shouldFileBeAnalyzed(child)) {
Source source = createSourceInContext(info.context, child);
changeSet.addedSource(source);
info.sources[path] = source;
Expand Down Expand Up @@ -1317,7 +1319,7 @@ class ContextManagerImpl implements ContextManager {
// that case don't add it.
if (resource is File) {
File file = resource;
if (callbacks.shouldFileBeAnalyzed(file)) {
if (_shouldFileBeAnalyzed(file)) {
ChangeSet changeSet = new ChangeSet();
Source source = createSourceInContext(info.context, file);
changeSet.addedSource(source);
Expand Down Expand Up @@ -1500,6 +1502,24 @@ class ContextManagerImpl implements ContextManager {
callbacks.updateContextPackageUriResolver(info.folder, disposition);
}

/**
* Return `true` if the given [file] should be analyzed.
*/
bool _shouldFileBeAnalyzed(File file) {
for (Glob glob in analyzedFilesGlobs) {
if (glob.matches(file.path)) {
// Emacs creates dummy links to track the fact that a file is open for
// editing and has unsaved changes (e.g. having unsaved changes to
// 'foo.dart' causes a link '.#foo.dart' to be created, which points to
// the non-existent file '[email protected]'. To avoid these dummy
// links causing the analyzer to thrash, just ignore links to
// non-existent files.
return file.exists;
}
}
return false;
}

/**
* Create and return a source representing the given [file] within the given
* [context].
Expand Down
5 changes: 4 additions & 1 deletion pkg/analysis_server/test/analysis_server_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ import "../foo/foo.dart";
}

void setUp() {
ExtensionManager manager = new ExtensionManager();
ServerPlugin plugin = new ServerPlugin();
manager.processPlugins([plugin]);
channel = new MockServerChannel();
resourceProvider = new MemoryResourceProvider();
packageMapProvider = new MockPackageMapProvider();
Expand All @@ -139,7 +142,7 @@ import "../foo/foo.dart";
resourceProvider,
packageMapProvider,
null,
new ServerPlugin(),
plugin,
new AnalysisServerOptions(),
() => new MockSdk(),
InstrumentationService.NULL_SERVICE,
Expand Down
40 changes: 22 additions & 18 deletions pkg/analysis_server/test/context_manager_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'package:analyzer/src/generated/java_io.dart';
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/generated/source_io.dart';
import 'package:analyzer/src/services/lint.dart';
import 'package:analyzer/src/util/glob.dart';
import 'package:linter/src/plugin/linter_plugin.dart';
import 'package:linter/src/rules/avoid_as.dart';
import 'package:package_config/packages.dart';
Expand Down Expand Up @@ -92,6 +93,18 @@ class AbstractContextManagerTest {
['x']
]);

List<Glob> get analysisFilesGlobs {
List<String> patterns = <String>[
'**/*.${AnalysisEngine.SUFFIX_DART}',
'**/*.${AnalysisEngine.SUFFIX_HTML}',
'**/*.${AnalysisEngine.SUFFIX_HTM}',
'**/${AnalysisEngine.ANALYSIS_OPTIONS_FILE}'
];
return patterns
.map((pattern) => new Glob(JavaFile.pathContext.separator, pattern))
.toList();
}

List<ErrorProcessor> get errorProcessors => callbacks.currentContext
.getConfigurationData(CONFIGURED_ERROR_PROCESSORS);

Expand Down Expand Up @@ -142,6 +155,7 @@ class AbstractContextManagerTest {
providePackageResolver,
provideEmbeddedUriResolver,
packageMapProvider,
analysisFilesGlobs,
InstrumentationService.NULL_SERVICE,
new AnalysisOptionsImpl());
callbacks = new TestContextManagerCallbacks(resourceProvider);
Expand Down Expand Up @@ -404,8 +418,10 @@ linter:
// * from `.analysis_options`:
expect(context.analysisOptions.enableGenericMethods, isTrue);
// * verify tests are excluded
expect(callbacks.currentContextFilePaths[projPath].keys,
['/my/proj/sdk_ext/entry.dart']);
expect(
callbacks.currentContextFilePaths[projPath].keys,
unorderedEquals(
['/my/proj/sdk_ext/entry.dart', '/my/proj/.analysis_options']));

// Verify filter setup.
expect(errorProcessors, hasLength(2));
Expand Down Expand Up @@ -657,8 +673,10 @@ analyzer:
callbacks.currentContextFilePaths[projPath];
expect(fileTimestamps, isNotEmpty);
List<String> files = fileTimestamps.keys.toList();
expect(files.length, equals(1));
expect(files[0], equals('/my/proj/lib/main.dart'));
expect(
files,
unorderedEquals(
['/my/proj/lib/main.dart', '/my/proj/.analysis_options']));
}

test_path_filter_child_contexts_option() async {
Expand Down Expand Up @@ -2441,20 +2459,6 @@ class TestContextManagerCallbacks extends ContextManagerCallbacks {
currentContextDispositions.remove(path);
}

@override
bool shouldFileBeAnalyzed(File file) {
if (!(AnalysisEngine.isDartFileName(file.path) ||
AnalysisEngine.isHtmlFileName(file.path))) {
return false;
}
// Emacs creates dummy links to track the fact that a file is open for
// editing and has unsaved changes (e.g. having unsaved changes to
// 'foo.dart' causes a link '.#foo.dart' to be created, which points to the
// non-existent file '[email protected]'. To avoid these dummy links
// causing the analyzer to thrash, just ignore links to non-existent files.
return file.exists;
}

@override
void updateContextPackageUriResolver(
Folder contextFolder, FolderDisposition disposition) {
Expand Down

0 comments on commit 841f8e6

Please sign in to comment.