| 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:core'; | 9 import 'dart:core'; |
| 10 import 'dart:io' as io; | 10 import 'dart:io' as io; |
| (...skipping 688 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 699 Source source = sources.first; | 699 Source source = sources.first; |
| 700 return new ContextSourcePair(context, source); | 700 return new ContextSourcePair(context, source); |
| 701 } | 701 } |
| 702 } | 702 } |
| 703 // file-based source | 703 // file-based source |
| 704 Source fileSource = file.createSource(); | 704 Source fileSource = file.createSource(); |
| 705 return new ContextSourcePair(null, fileSource); | 705 return new ContextSourcePair(null, fileSource); |
| 706 } | 706 } |
| 707 | 707 |
| 708 /** | 708 /** |
| 709 * Returns [Element]s at the given [offset] of the given [file]. | 709 * Return the [Element] at the given [offset] of the given [file], or `null` |
| 710 * | 710 * if there is no node at the [offset] or the node does not have an element. |
| 711 * May be empty if cannot be resolved, but not `null`. | |
| 712 */ | 711 */ |
| 713 List<Element> getElementsAtOffset(String file, int offset) { | 712 Element getElementAtOffset(String file, int offset) { |
| 714 List<AstNode> nodes = getNodesAtOffset(file, offset); | 713 AstNode node = getNodeAtOffset(file, offset); |
| 715 return getElementsOfNodes(nodes); | 714 return getElementOfNode(node); |
| 716 } | 715 } |
| 717 | 716 |
| 718 /** | 717 /** |
| 719 * Returns [Element]s of the given [nodes]. | 718 * Return the [Element] of the given [node], or `null` if [node] is `null` or |
| 720 * | 719 * does not have an element. |
| 721 * May be empty if not resolved, but not `null`. | |
| 722 */ | 720 */ |
| 723 List<Element> getElementsOfNodes(List<AstNode> nodes) { | 721 Element getElementOfNode(AstNode node) { |
| 724 List<Element> elements = <Element>[]; | 722 if (node == null) { |
| 725 for (AstNode node in nodes) { | 723 return null; |
| 726 if (node is SimpleIdentifier && node.parent is LibraryIdentifier) { | |
| 727 node = node.parent; | |
| 728 } | |
| 729 if (node is LibraryIdentifier) { | |
| 730 node = node.parent; | |
| 731 } | |
| 732 if (node is StringLiteral && node.parent is UriBasedDirective) { | |
| 733 continue; | |
| 734 } | |
| 735 Element element = ElementLocator.locate(node); | |
| 736 if (node is SimpleIdentifier && element is PrefixElement) { | |
| 737 element = getImportElement(node); | |
| 738 } | |
| 739 if (element != null) { | |
| 740 elements.add(element); | |
| 741 } | |
| 742 } | 724 } |
| 743 return elements; | 725 if (node is SimpleIdentifier && node.parent is LibraryIdentifier) { |
| 726 node = node.parent; |
| 727 } |
| 728 if (node is LibraryIdentifier) { |
| 729 node = node.parent; |
| 730 } |
| 731 if (node is StringLiteral && node.parent is UriBasedDirective) { |
| 732 return null; |
| 733 } |
| 734 Element element = ElementLocator.locate(node); |
| 735 if (node is SimpleIdentifier && element is PrefixElement) { |
| 736 element = getImportElement(node); |
| 737 } |
| 738 return element; |
| 744 } | 739 } |
| 745 | 740 |
| 746 /** | 741 /** |
| 747 * Return an analysis error info containing the array of all of the errors and | 742 * Return an analysis error info containing the array of all of the errors and |
| 748 * the line info associated with [file]. | 743 * the line info associated with [file]. |
| 749 * | 744 * |
| 750 * Returns `null` if [file] does not belong to any [AnalysisContext], or the | 745 * Returns `null` if [file] does not belong to any [AnalysisContext], or the |
| 751 * file does not exist. | 746 * file does not exist. |
| 752 * | 747 * |
| 753 * The array of errors will be empty if there are no errors in [file]. The | 748 * The array of errors will be empty if there are no errors in [file]. The |
| 754 * errors contained in the array can be incomplete. | 749 * errors contained in the array can be incomplete. |
| 755 * | 750 * |
| 756 * This method does not wait for all errors to be computed, and returns just | 751 * This method does not wait for all errors to be computed, and returns just |
| 757 * the current state. | 752 * the current state. |
| 758 */ | 753 */ |
| 759 AnalysisErrorInfo getErrors(String file) { | 754 AnalysisErrorInfo getErrors(String file) { |
| 760 ContextSourcePair contextSource = getContextSourcePair(file); | 755 ContextSourcePair contextSource = getContextSourcePair(file); |
| 761 AnalysisContext context = contextSource.context; | 756 AnalysisContext context = contextSource.context; |
| 762 Source source = contextSource.source; | 757 Source source = contextSource.source; |
| 763 if (context == null) { | 758 if (context == null) { |
| 764 return null; | 759 return null; |
| 765 } | 760 } |
| 766 if (!context.exists(source)) { | 761 if (!context.exists(source)) { |
| 767 return null; | 762 return null; |
| 768 } | 763 } |
| 769 return context.getErrors(source); | 764 return context.getErrors(source); |
| 770 } | 765 } |
| 771 | 766 |
| 772 /** | 767 /** |
| 773 * Returns resolved [AstNode]s at the given [offset] of the given [file]. | 768 * Return the resolved [AstNode]s at the given [offset] of the given [file], |
| 774 * | 769 * or `null` if there is no node as the [offset]. |
| 775 * May be empty, but not `null`. | |
| 776 */ | 770 */ |
| 777 List<AstNode> getNodesAtOffset(String file, int offset) { | 771 AstNode getNodeAtOffset(String file, int offset) { |
| 778 List<CompilationUnit> units = getResolvedCompilationUnits(file); | 772 CompilationUnit unit = getResolvedCompilationUnit(file); |
| 779 List<AstNode> nodes = <AstNode>[]; | 773 if (unit != null) { |
| 780 for (CompilationUnit unit in units) { | 774 return new NodeLocator(offset).searchWithin(unit); |
| 781 AstNode node = new NodeLocator(offset).searchWithin(unit); | |
| 782 if (node != null) { | |
| 783 nodes.add(node); | |
| 784 } | |
| 785 } | 775 } |
| 786 return nodes; | 776 return null; |
| 787 } | 777 } |
| 788 | 778 |
| 789 /** | 779 /** |
| 790 * Returns resolved [CompilationUnit]s of the Dart file with the given [path]. | 780 * Return the resolved [CompilationUnit] for the Dart file with the given |
| 791 * | 781 * [path], or `null` if the file is not a Dart file or cannot be resolved. |
| 792 * May be empty, but not `null`. | |
| 793 */ | 782 */ |
| 794 List<CompilationUnit> getResolvedCompilationUnits(String path) { | 783 CompilationUnit getResolvedCompilationUnit(String path) { |
| 795 List<CompilationUnit> units = <CompilationUnit>[]; | |
| 796 ContextSourcePair contextSource = getContextSourcePair(path); | 784 ContextSourcePair contextSource = getContextSourcePair(path); |
| 797 // prepare AnalysisContext | |
| 798 AnalysisContext context = contextSource.context; | 785 AnalysisContext context = contextSource.context; |
| 799 if (context == null) { | 786 if (context == null) { |
| 800 return units; | 787 return null; |
| 801 } | 788 } |
| 802 // add a unit for each unit/library combination | 789 return runWithActiveContext(context, () { |
| 803 runWithActiveContext(context, () { | |
| 804 Source unitSource = contextSource.source; | 790 Source unitSource = contextSource.source; |
| 805 List<Source> librarySources = context.getLibrariesContaining(unitSource); | 791 List<Source> librarySources = context.getLibrariesContaining(unitSource); |
| 806 for (Source librarySource in librarySources) { | 792 for (Source librarySource in librarySources) { |
| 807 CompilationUnit unit = | 793 return context.resolveCompilationUnit2(unitSource, librarySource); |
| 808 context.resolveCompilationUnit2(unitSource, librarySource); | |
| 809 if (unit != null) { | |
| 810 units.add(unit); | |
| 811 } | |
| 812 } | 794 } |
| 795 return null; |
| 813 }); | 796 }); |
| 814 // done | |
| 815 return units; | |
| 816 } | 797 } |
| 817 | 798 |
| 818 // TODO(brianwilkerson) Add the following method after 'prioritySources' has | 799 // TODO(brianwilkerson) Add the following method after 'prioritySources' has |
| 819 // been added to InternalAnalysisContext. | 800 // been added to InternalAnalysisContext. |
| 820 // /** | 801 // /** |
| 821 // * Return a list containing the full names of all of the sources that are | 802 // * Return a list containing the full names of all of the sources that are |
| 822 // * priority sources. | 803 // * priority sources. |
| 823 // */ | 804 // */ |
| 824 // List<String> getPriorityFiles() { | 805 // List<String> getPriorityFiles() { |
| 825 // List<String> priorityFiles = new List<String>(); | 806 // List<String> priorityFiles = new List<String>(); |
| (...skipping 1230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2056 /** | 2037 /** |
| 2057 * The [PerformanceTag] for time spent in server request handlers. | 2038 * The [PerformanceTag] for time spent in server request handlers. |
| 2058 */ | 2039 */ |
| 2059 static PerformanceTag serverRequests = new PerformanceTag('serverRequests'); | 2040 static PerformanceTag serverRequests = new PerformanceTag('serverRequests'); |
| 2060 | 2041 |
| 2061 /** | 2042 /** |
| 2062 * The [PerformanceTag] for time spent in split store microtasks. | 2043 * The [PerformanceTag] for time spent in split store microtasks. |
| 2063 */ | 2044 */ |
| 2064 static PerformanceTag splitStore = new PerformanceTag('splitStore'); | 2045 static PerformanceTag splitStore = new PerformanceTag('splitStore'); |
| 2065 } | 2046 } |
| OLD | NEW |