-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move shouldFileBeAnalyzed into ContextManager
- Loading branch information
1 parent
e83151c
commit 841f8e6
Showing
4 changed files
with
84 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -321,6 +327,7 @@ class AnalysisServer { | |
packageResolverProvider, | ||
embeddedResolverProvider, | ||
packageMapProvider, | ||
analyzedFilesGlobs, | ||
instrumentationService, | ||
defaultContextOptions); | ||
ServerContextManagerCallbacks contextManagerCallbacks = | ||
|
@@ -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. | ||
|
@@ -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) { | ||
|
@@ -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) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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. | ||
*/ | ||
|
@@ -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; | ||
|
||
|
@@ -490,6 +491,7 @@ class ContextManagerImpl implements ContextManager { | |
this.packageResolverProvider, | ||
this.embeddedUriResolverProvider, | ||
this._packageMapProvider, | ||
this.analyzedFilesGlobs, | ||
this._instrumentationService, | ||
this.defaultContextOptions) { | ||
absolutePathContext = resourceProvider.absolutePathContext; | ||
|
@@ -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 | ||
|
@@ -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; | ||
|
@@ -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); | ||
|
@@ -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]. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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); | ||
|
||
|
@@ -142,6 +155,7 @@ class AbstractContextManagerTest { | |
providePackageResolver, | ||
provideEmbeddedUriResolver, | ||
packageMapProvider, | ||
analysisFilesGlobs, | ||
InstrumentationService.NULL_SERVICE, | ||
new AnalysisOptionsImpl()); | ||
callbacks = new TestContextManagerCallbacks(resourceProvider); | ||
|
@@ -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)); | ||
|
@@ -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 { | ||
|
@@ -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) { | ||
|