OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library analysis.server; | 5 library analysis.server; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 import 'dart:math' show max; | 9 import 'dart:math' show max; |
10 | 10 |
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 } | 367 } |
368 } | 368 } |
369 if (containingFolder != null) { | 369 if (containingFolder != null) { |
370 return folderMap[containingFolder]; | 370 return folderMap[containingFolder]; |
371 } | 371 } |
372 Resource resource = resourceProvider.getResource(path); | 372 Resource resource = resourceProvider.getResource(path); |
373 if (resource is Folder) { | 373 if (resource is Folder) { |
374 return null; | 374 return null; |
375 } | 375 } |
376 // check if there is a context that analyzed this source | 376 // check if there is a context that analyzed this source |
377 return getAnalysisContextForSource(getSource(path)); | 377 return getAnalysisContextForSource(_getSourceWithoutContext(path)); |
378 } | 378 } |
379 | 379 |
380 /** | 380 /** |
381 * Return any [AnalysisContext] that is analyzing the given [source], either | 381 * Return any [AnalysisContext] that is analyzing the given [source], either |
382 * explicitly or implicitly. Return `null` if there is no such context. | 382 * explicitly or implicitly. Return `null` if there is no such context. |
383 */ | 383 */ |
384 AnalysisContext getAnalysisContextForSource(Source source) { | 384 AnalysisContext getAnalysisContextForSource(Source source) { |
385 for (AnalysisContext context in folderMap.values) { | 385 for (AnalysisContext context in folderMap.values) { |
386 SourceKind kind = context.getKindOf(source); | 386 SourceKind kind = context.getKindOf(source); |
387 if (kind != SourceKind.UNKNOWN) { | 387 if (kind != SourceKind.UNKNOWN) { |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
490 // folderMap.values.forEach((ContextDirectory directory) { | 490 // folderMap.values.forEach((ContextDirectory directory) { |
491 // InternalAnalysisContext context = directory.context; | 491 // InternalAnalysisContext context = directory.context; |
492 // context.prioritySources.forEach((Source source) { | 492 // context.prioritySources.forEach((Source source) { |
493 // priorityFiles.add(source.fullName); | 493 // priorityFiles.add(source.fullName); |
494 // }); | 494 // }); |
495 // }); | 495 // }); |
496 // return priorityFiles; | 496 // return priorityFiles; |
497 // } | 497 // } |
498 | 498 |
499 /** | 499 /** |
500 * Returns resolved [CompilationUnit]s of the Dart file with the given [path]. | |
501 * | |
502 * May be empty, but not `null`. | |
503 */ | |
504 List<CompilationUnit> getResolvedCompilationUnits(String path) { | |
505 List<CompilationUnit> units = <CompilationUnit>[]; | |
506 // prepare AnalysisContext | |
507 AnalysisContext context = getAnalysisContext(path); | |
508 if (context == null) { | |
509 return units; | |
510 } | |
511 // add a unit for each unit/library combination | |
512 Source unitSource = getSource(path); | |
513 List<Source> librarySources = context.getLibrariesContaining(unitSource); | |
514 for (Source librarySource in librarySources) { | |
515 CompilationUnit unit = | |
516 context.resolveCompilationUnit2(unitSource, librarySource); | |
517 if (unit != null) { | |
518 units.add(unit); | |
519 } | |
520 } | |
521 // done | |
522 return units; | |
523 } | |
524 | |
525 /** | |
526 * Returns the [CompilationUnit] of the Dart file with the given [path] that | 500 * Returns the [CompilationUnit] of the Dart file with the given [path] that |
527 * should be used to resend notifications for already resolved unit. | 501 * should be used to resend notifications for already resolved unit. |
528 * Returns `null` if the file is not a part of any context, library has not | 502 * Returns `null` if the file is not a part of any context, library has not |
529 * been yet resolved, or any problem happened. | 503 * been yet resolved, or any problem happened. |
530 */ | 504 */ |
531 CompilationUnit getResolvedCompilationUnitToResendNotification(String path) { | 505 CompilationUnit getResolvedCompilationUnitToResendNotification(String path) { |
532 // prepare AnalysisContext | 506 // prepare AnalysisContext |
533 AnalysisContext context = getAnalysisContext(path); | 507 AnalysisContext context = getAnalysisContext(path); |
534 if (context == null) { | 508 if (context == null) { |
535 return null; | 509 return null; |
536 } | 510 } |
537 // prepare sources | 511 // prepare sources |
538 Source unitSource = getSource(path); | 512 Source unitSource = getSource(path); |
539 List<Source> librarySources = context.getLibrariesContaining(unitSource); | 513 List<Source> librarySources = context.getLibrariesContaining(unitSource); |
540 if (librarySources.isEmpty) { | 514 if (librarySources.isEmpty) { |
541 return null; | 515 return null; |
542 } | 516 } |
543 // if library has not been resolved yet, the unit will be resolved later | 517 // if library has not been resolved yet, the unit will be resolved later |
544 Source librarySource = librarySources[0]; | 518 Source librarySource = librarySources[0]; |
545 if (context.getLibraryElement(librarySource) == null) { | 519 if (context.getLibraryElement(librarySource) == null) { |
546 return null; | 520 return null; |
547 } | 521 } |
548 // if library has been already resolved, resolve unit | 522 // if library has been already resolved, resolve unit |
549 return context.resolveCompilationUnit2(unitSource, librarySource); | 523 return context.resolveCompilationUnit2(unitSource, librarySource); |
550 } | 524 } |
551 | 525 |
552 /** | 526 /** |
| 527 * Returns resolved [CompilationUnit]s of the Dart file with the given [path]. |
| 528 * |
| 529 * May be empty, but not `null`. |
| 530 */ |
| 531 List<CompilationUnit> getResolvedCompilationUnits(String path) { |
| 532 List<CompilationUnit> units = <CompilationUnit>[]; |
| 533 // prepare AnalysisContext |
| 534 AnalysisContext context = getAnalysisContext(path); |
| 535 if (context == null) { |
| 536 return units; |
| 537 } |
| 538 // add a unit for each unit/library combination |
| 539 Source unitSource = getSource(path); |
| 540 List<Source> librarySources = context.getLibrariesContaining(unitSource); |
| 541 for (Source librarySource in librarySources) { |
| 542 CompilationUnit unit = |
| 543 context.resolveCompilationUnit2(unitSource, librarySource); |
| 544 if (unit != null) { |
| 545 units.add(unit); |
| 546 } |
| 547 } |
| 548 // done |
| 549 return units; |
| 550 } |
| 551 |
| 552 /** |
553 * Return the [Source] of the Dart file with the given [path]. | 553 * Return the [Source] of the Dart file with the given [path]. |
554 */ | 554 */ |
555 Source getSource(String path) { | 555 Source getSource(String path) { |
556 // try SDK | 556 // try SDK |
557 { | 557 { |
558 Uri uri = resourceProvider.pathContext.toUri(path); | 558 Uri uri = resourceProvider.pathContext.toUri(path); |
559 Source sdkSource = defaultSdk.fromFileUri(uri); | 559 Source sdkSource = defaultSdk.fromFileUri(uri); |
560 if (sdkSource != null) { | 560 if (sdkSource != null) { |
561 return sdkSource; | 561 return sdkSource; |
562 } | 562 } |
563 } | 563 } |
564 // file-based source | 564 // file-based source |
565 File file = resourceProvider.getResource(path); | 565 File file = resourceProvider.getResource(path); |
566 return file.createSource(); | 566 return ContextManager.createSourceInContext(getAnalysisContext(path), file); |
567 } | 567 } |
568 | 568 |
569 /** | 569 /** |
570 * Handle a [request] that was read from the communication channel. | 570 * Handle a [request] that was read from the communication channel. |
571 */ | 571 */ |
572 void handleRequest(Request request) { | 572 void handleRequest(Request request) { |
573 _performance.logRequest(request); | 573 _performance.logRequest(request); |
574 runZoned(() { | 574 runZoned(() { |
575 int count = handlers.length; | 575 int count = handlers.length; |
576 for (int i = 0; i < count; i++) { | 576 for (int i = 0; i < count; i++) { |
(...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1065 // | 1065 // |
1066 // Update the defaults used to create new contexts. | 1066 // Update the defaults used to create new contexts. |
1067 // | 1067 // |
1068 AnalysisOptionsImpl options = contextDirectoryManager.defaultOptions; | 1068 AnalysisOptionsImpl options = contextDirectoryManager.defaultOptions; |
1069 optionUpdaters.forEach((OptionUpdater optionUpdater) { | 1069 optionUpdaters.forEach((OptionUpdater optionUpdater) { |
1070 optionUpdater(options); | 1070 optionUpdater(options); |
1071 }); | 1071 }); |
1072 } | 1072 } |
1073 | 1073 |
1074 /** | 1074 /** |
| 1075 * Return the [Source] of the Dart file with the given [path], assuming that |
| 1076 * we do not know the context in which the path should be interpreted. |
| 1077 */ |
| 1078 Source _getSourceWithoutContext(String path) { |
| 1079 // try SDK |
| 1080 { |
| 1081 Uri uri = resourceProvider.pathContext.toUri(path); |
| 1082 Source sdkSource = defaultSdk.fromFileUri(uri); |
| 1083 if (sdkSource != null) { |
| 1084 return sdkSource; |
| 1085 } |
| 1086 } |
| 1087 // file-based source |
| 1088 File file = resourceProvider.getResource(path); |
| 1089 return file.createSource(); |
| 1090 } |
| 1091 |
| 1092 /** |
1075 * Schedules [performOperation] exection. | 1093 * Schedules [performOperation] exection. |
1076 */ | 1094 */ |
1077 void _schedulePerformOperation() { | 1095 void _schedulePerformOperation() { |
1078 if (performOperationPending) { | 1096 if (performOperationPending) { |
1079 return; | 1097 return; |
1080 } | 1098 } |
1081 /* | 1099 /* |
1082 * TODO (danrubel) Rip out this workaround once the underlying problem | 1100 * TODO (danrubel) Rip out this workaround once the underlying problem |
1083 * is fixed. Currently, the VM and dart:io do not deliver content | 1101 * is fixed. Currently, the VM and dart:io do not deliver content |
1084 * on stdin in a timely manner if the event loop is busy. | 1102 * on stdin in a timely manner if the event loop is busy. |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1329 * AnalysisServer.performOperation when the server is not idle. | 1347 * AnalysisServer.performOperation when the server is not idle. |
1330 */ | 1348 */ |
1331 static PerformanceTag intertask = new PerformanceTag('intertask'); | 1349 static PerformanceTag intertask = new PerformanceTag('intertask'); |
1332 | 1350 |
1333 /** | 1351 /** |
1334 * The [PerformanceTag] for time spent between calls to | 1352 * The [PerformanceTag] for time spent between calls to |
1335 * AnalysisServer.performOperation when the server is idle. | 1353 * AnalysisServer.performOperation when the server is idle. |
1336 */ | 1354 */ |
1337 static PerformanceTag idle = new PerformanceTag('idle'); | 1355 static PerformanceTag idle = new PerformanceTag('idle'); |
1338 } | 1356 } |
OLD | NEW |