Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(354)

Unified Diff: pkg/analysis_server/lib/src/protocol_server.dart

Issue 2692383003: Use CompilationUnitElement.lineInfo instead of AnalysisContext. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analysis_server/lib/src/protocol_server.dart
diff --git a/pkg/analysis_server/lib/src/protocol_server.dart b/pkg/analysis_server/lib/src/protocol_server.dart
index 596b8732cd7cc59db3ba5058622e192ce7da3125..277c5b5162d97ac88825b467677231a57dac4104 100644
--- a/pkg/analysis_server/lib/src/protocol_server.dart
+++ b/pkg/analysis_server/lib/src/protocol_server.dart
@@ -133,9 +133,7 @@ AnalysisError newAnalysisError_fromEngine(
* Create a Location based on an [engine.Element].
*/
Location newLocation_fromElement(engine.Element element) {
- engine.AnalysisContext context = element.context;
- engine.Source source = element.source;
- if (context == null || source == null) {
+ if (element == null || element.source == null) {
return null;
}
int offset = element.nameOffset;
@@ -145,17 +143,17 @@ Location newLocation_fromElement(engine.Element element) {
offset = 0;
length = 0;
}
+ engine.CompilationUnitElement unitElement = _getUnitElement(element);
engine.SourceRange range = new engine.SourceRange(offset, length);
- return _locationForArgs(context, source, range);
+ return _locationForArgs(unitElement, range);
}
/**
* Create a Location based on an [engine.SearchMatch].
*/
Location newLocation_fromMatch(engine.SearchMatch match) {
- engine.Element enclosingElement = match.element;
- return _locationForArgs(
- enclosingElement.context, enclosingElement.source, match.sourceRange);
+ engine.CompilationUnitElement unitElement = _getUnitElement(match.element);
+ return _locationForArgs(unitElement, match.sourceRange);
}
/**
@@ -165,10 +163,8 @@ Location newLocation_fromNode(engine.AstNode node) {
engine.CompilationUnit unit =
node.getAncestor((node) => node is engine.CompilationUnit);
engine.CompilationUnitElement unitElement = unit.element;
- engine.AnalysisContext context = unitElement.context;
- engine.Source source = unitElement.source;
engine.SourceRange range = new engine.SourceRange(node.offset, node.length);
- return _locationForArgs(context, source, range);
+ return _locationForArgs(unitElement, range);
}
/**
@@ -176,10 +172,7 @@ Location newLocation_fromNode(engine.AstNode node) {
*/
Location newLocation_fromUnit(
engine.CompilationUnit unit, engine.SourceRange range) {
- engine.CompilationUnitElement unitElement = unit.element;
- engine.AnalysisContext context = unitElement.context;
- engine.Source source = unitElement.source;
- return _locationForArgs(context, source, range);
+ return _locationForArgs(unit.element, range);
}
/**
@@ -250,15 +243,33 @@ List<Element> _computePath(engine.Element element) {
return path;
}
+engine.CompilationUnitElement _getUnitElement(engine.Element element) {
+ if (element is engine.CompilationUnitElement) {
+ return element;
+ }
+ if (element?.enclosingElement is engine.LibraryElement) {
+ element = element.enclosingElement;
+ }
+ if (element is engine.LibraryElement) {
+ return element.definingCompilationUnit;
+ }
+ for (; element != null; element = element.enclosingElement) {
+ if (element is engine.CompilationUnitElement) {
+ return element;
+ }
+ }
+ return null;
+}
+
/**
* Creates a new [Location].
*/
-Location _locationForArgs(engine.AnalysisContext context, engine.Source source,
- engine.SourceRange range) {
+Location _locationForArgs(
+ engine.CompilationUnitElement unitElement, engine.SourceRange range) {
int startLine = 0;
int startColumn = 0;
try {
- engine.LineInfo lineInfo = context.computeLineInfo(source);
+ engine.LineInfo lineInfo = unitElement.lineInfo;
if (lineInfo != null) {
engine.LineInfo_Location offsetLocation =
lineInfo.getLocation(range.offset);
@@ -266,6 +277,6 @@ Location _locationForArgs(engine.AnalysisContext context, engine.Source source,
startColumn = offsetLocation.columnNumber;
}
} on AnalysisException {}
- return new Location(
- source.fullName, range.offset, range.length, startLine, startColumn);
+ return new Location(unitElement.source.fullName, range.offset, range.length,
+ startLine, startColumn);
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698