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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 protocol.server; 5 library protocol.server;
6 6
7 import 'package:analysis_server/plugin/protocol/protocol.dart'; 7 import 'package:analysis_server/plugin/protocol/protocol.dart';
8 import 'package:analysis_server/plugin/protocol/protocol_dart.dart'; 8 import 'package:analysis_server/plugin/protocol/protocol_dart.dart';
9 import 'package:analysis_server/src/services/correction/fix.dart'; 9 import 'package:analysis_server/src/services/correction/fix.dart';
10 import 'package:analysis_server/src/services/search/search_engine.dart' 10 import 'package:analysis_server/src/services/search/search_engine.dart'
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 String correction = error.correction; 126 String correction = error.correction;
127 bool fix = hasFix(error.errorCode); 127 bool fix = hasFix(error.errorCode);
128 return new AnalysisError(severity, type, location, message, code, 128 return new AnalysisError(severity, type, location, message, code,
129 correction: correction, hasFix: fix); 129 correction: correction, hasFix: fix);
130 } 130 }
131 131
132 /** 132 /**
133 * Create a Location based on an [engine.Element]. 133 * Create a Location based on an [engine.Element].
134 */ 134 */
135 Location newLocation_fromElement(engine.Element element) { 135 Location newLocation_fromElement(engine.Element element) {
136 engine.AnalysisContext context = element.context; 136 if (element == null || element.source == null) {
137 engine.Source source = element.source;
138 if (context == null || source == null) {
139 return null; 137 return null;
140 } 138 }
141 int offset = element.nameOffset; 139 int offset = element.nameOffset;
142 int length = element.nameLength; 140 int length = element.nameLength;
143 if (element is engine.CompilationUnitElement || 141 if (element is engine.CompilationUnitElement ||
144 (element is engine.LibraryElement && offset < 0)) { 142 (element is engine.LibraryElement && offset < 0)) {
145 offset = 0; 143 offset = 0;
146 length = 0; 144 length = 0;
147 } 145 }
146 engine.CompilationUnitElement unitElement = _getUnitElement(element);
148 engine.SourceRange range = new engine.SourceRange(offset, length); 147 engine.SourceRange range = new engine.SourceRange(offset, length);
149 return _locationForArgs(context, source, range); 148 return _locationForArgs(unitElement, range);
150 } 149 }
151 150
152 /** 151 /**
153 * Create a Location based on an [engine.SearchMatch]. 152 * Create a Location based on an [engine.SearchMatch].
154 */ 153 */
155 Location newLocation_fromMatch(engine.SearchMatch match) { 154 Location newLocation_fromMatch(engine.SearchMatch match) {
156 engine.Element enclosingElement = match.element; 155 engine.CompilationUnitElement unitElement = _getUnitElement(match.element);
157 return _locationForArgs( 156 return _locationForArgs(unitElement, match.sourceRange);
158 enclosingElement.context, enclosingElement.source, match.sourceRange);
159 } 157 }
160 158
161 /** 159 /**
162 * Create a Location based on an [engine.AstNode]. 160 * Create a Location based on an [engine.AstNode].
163 */ 161 */
164 Location newLocation_fromNode(engine.AstNode node) { 162 Location newLocation_fromNode(engine.AstNode node) {
165 engine.CompilationUnit unit = 163 engine.CompilationUnit unit =
166 node.getAncestor((node) => node is engine.CompilationUnit); 164 node.getAncestor((node) => node is engine.CompilationUnit);
167 engine.CompilationUnitElement unitElement = unit.element; 165 engine.CompilationUnitElement unitElement = unit.element;
168 engine.AnalysisContext context = unitElement.context;
169 engine.Source source = unitElement.source;
170 engine.SourceRange range = new engine.SourceRange(node.offset, node.length); 166 engine.SourceRange range = new engine.SourceRange(node.offset, node.length);
171 return _locationForArgs(context, source, range); 167 return _locationForArgs(unitElement, range);
172 } 168 }
173 169
174 /** 170 /**
175 * Create a Location based on an [engine.CompilationUnit]. 171 * Create a Location based on an [engine.CompilationUnit].
176 */ 172 */
177 Location newLocation_fromUnit( 173 Location newLocation_fromUnit(
178 engine.CompilationUnit unit, engine.SourceRange range) { 174 engine.CompilationUnit unit, engine.SourceRange range) {
179 engine.CompilationUnitElement unitElement = unit.element; 175 return _locationForArgs(unit.element, range);
180 engine.AnalysisContext context = unitElement.context;
181 engine.Source source = unitElement.source;
182 return _locationForArgs(context, source, range);
183 } 176 }
184 177
185 /** 178 /**
186 * Construct based on an element from the analyzer engine. 179 * Construct based on an element from the analyzer engine.
187 */ 180 */
188 OverriddenMember newOverriddenMember_fromEngine(engine.Element member) { 181 OverriddenMember newOverriddenMember_fromEngine(engine.Element member) {
189 Element element = convertElement(member); 182 Element element = convertElement(member);
190 String className = member.enclosingElement.displayName; 183 String className = member.enclosingElement.displayName;
191 return new OverriddenMember(element, className); 184 return new OverriddenMember(element, className);
192 } 185 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 // imports are library children, but they are physically in the unit 236 // imports are library children, but they are physically in the unit
244 engine.LibraryElement library = element.enclosingElement; 237 engine.LibraryElement library = element.enclosingElement;
245 element = library.definingCompilationUnit; 238 element = library.definingCompilationUnit;
246 } else { 239 } else {
247 element = element.enclosingElement; 240 element = element.enclosingElement;
248 } 241 }
249 } 242 }
250 return path; 243 return path;
251 } 244 }
252 245
246 engine.CompilationUnitElement _getUnitElement(engine.Element element) {
247 if (element is engine.CompilationUnitElement) {
248 return element;
249 }
250 if (element?.enclosingElement is engine.LibraryElement) {
251 element = element.enclosingElement;
252 }
253 if (element is engine.LibraryElement) {
254 return element.definingCompilationUnit;
255 }
256 for (; element != null; element = element.enclosingElement) {
257 if (element is engine.CompilationUnitElement) {
258 return element;
259 }
260 }
261 return null;
262 }
263
253 /** 264 /**
254 * Creates a new [Location]. 265 * Creates a new [Location].
255 */ 266 */
256 Location _locationForArgs(engine.AnalysisContext context, engine.Source source, 267 Location _locationForArgs(
257 engine.SourceRange range) { 268 engine.CompilationUnitElement unitElement, engine.SourceRange range) {
258 int startLine = 0; 269 int startLine = 0;
259 int startColumn = 0; 270 int startColumn = 0;
260 try { 271 try {
261 engine.LineInfo lineInfo = context.computeLineInfo(source); 272 engine.LineInfo lineInfo = unitElement.lineInfo;
262 if (lineInfo != null) { 273 if (lineInfo != null) {
263 engine.LineInfo_Location offsetLocation = 274 engine.LineInfo_Location offsetLocation =
264 lineInfo.getLocation(range.offset); 275 lineInfo.getLocation(range.offset);
265 startLine = offsetLocation.lineNumber; 276 startLine = offsetLocation.lineNumber;
266 startColumn = offsetLocation.columnNumber; 277 startColumn = offsetLocation.columnNumber;
267 } 278 }
268 } on AnalysisException {} 279 } on AnalysisException {}
269 return new Location( 280 return new Location(unitElement.source.fullName, range.offset, range.length,
270 source.fullName, range.offset, range.length, startLine, startColumn); 281 startLine, startColumn);
271 } 282 }
OLDNEW
« 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