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

Side by Side Diff: pkg/analysis_server/lib/src/services/completion/postfix/postfix_completion.dart

Issue 2969833002: Convert DartChangeBuilder to use AnalysisSession (Closed)
Patch Set: Created 3 years, 5 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
OLDNEW
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2017, 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 import 'dart:async'; 5 import 'dart:async';
6 6
7 import 'package:analysis_server/src/protocol_server.dart' hide Element; 7 import 'package:analysis_server/src/protocol_server.dart' hide Element;
8 import 'package:analysis_server/src/services/correction/util.dart'; 8 import 'package:analysis_server/src/services/correction/util.dart';
9 import 'package:analyzer/dart/analysis/session.dart';
9 import 'package:analyzer/dart/ast/ast.dart'; 10 import 'package:analyzer/dart/ast/ast.dart';
10 import 'package:analyzer/dart/element/element.dart'; 11 import 'package:analyzer/dart/element/element.dart';
11 import 'package:analyzer/dart/element/type.dart'; 12 import 'package:analyzer/dart/element/type.dart';
12 import 'package:analyzer/error/error.dart' as engine; 13 import 'package:analyzer/error/error.dart' as engine;
13 import 'package:analyzer/src/dart/analysis/driver.dart'; 14 import 'package:analyzer/src/dart/analysis/driver.dart';
14 import 'package:analyzer/src/dart/ast/utilities.dart'; 15 import 'package:analyzer/src/dart/ast/utilities.dart';
15 import 'package:analyzer/src/generated/engine.dart'; 16 import 'package:analyzer/src/generated/engine.dart';
16 import 'package:analyzer/src/generated/java_core.dart'; 17 import 'package:analyzer/src/generated/java_core.dart';
17 import 'package:analyzer/src/generated/resolver.dart'; 18 import 'package:analyzer/src/generated/resolver.dart';
18 import 'package:analyzer/src/generated/source.dart'; 19 import 'package:analyzer/src/generated/source.dart';
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 String get file => completionContext.file; 300 String get file => completionContext.file;
300 301
301 String get key => completionContext.key; 302 String get key => completionContext.key;
302 303
303 LineInfo get lineInfo => completionContext.lineInfo; 304 LineInfo get lineInfo => completionContext.lineInfo;
304 305
305 int get requestLine => lineInfo.getLocation(selectionOffset).lineNumber; 306 int get requestLine => lineInfo.getLocation(selectionOffset).lineNumber;
306 307
307 int get selectionOffset => completionContext.selectionOffset; 308 int get selectionOffset => completionContext.selectionOffset;
308 309
310 /**
311 * Return the analysis session to be used to create the change builder.
312 */
313 AnalysisSession get session => driver.currentSession;
314
309 Source get source => completionContext.unitElement.source; 315 Source get source => completionContext.unitElement.source;
310 316
311 TypeProvider get typeProvider { 317 TypeProvider get typeProvider {
312 return _typeProvider ??= unitElement.context.typeProvider; 318 return _typeProvider ??= unitElement.context.typeProvider;
313 } 319 }
314 320
315 CompilationUnit get unit => completionContext.unit; 321 CompilationUnit get unit => completionContext.unit;
316 322
317 CompilationUnitElement get unitElement => completionContext.unitElement; 323 CompilationUnitElement get unitElement => completionContext.unitElement;
318 324
319 Future<PostfixCompletion> compute() async { 325 Future<PostfixCompletion> compute() async {
320 node = _selectedNode(); 326 node = _selectedNode();
321 if (node == null) { 327 if (node == null) {
322 return NO_COMPLETION; 328 return NO_COMPLETION;
323 } 329 }
324 PostfixCompletionKind completer = DartPostfixCompletion.forKey(key); 330 PostfixCompletionKind completer = DartPostfixCompletion.forKey(key);
325 return completer?.computer(this, completer) ?? NO_COMPLETION; 331 return completer?.computer(this, completer) ?? NO_COMPLETION;
326 } 332 }
327 333
328 Future<PostfixCompletion> expand( 334 Future<PostfixCompletion> expand(
329 PostfixCompletionKind kind, Function contexter, Function sourcer, 335 PostfixCompletionKind kind, Function contexter, Function sourcer,
330 {bool withBraces: true}) async { 336 {bool withBraces: true}) async {
331 AstNode expr = contexter(); 337 AstNode expr = contexter();
332 if (expr == null) { 338 if (expr == null) {
333 return null; 339 return null;
334 } 340 }
335 341
336 DartChangeBuilder changeBuilder = new DartChangeBuilder(driver); 342 DartChangeBuilder changeBuilder = new DartChangeBuilder(session);
337 await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) { 343 await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
338 builder.addReplacement(range.node(expr), (DartEditBuilder builder) { 344 builder.addReplacement(range.node(expr), (DartEditBuilder builder) {
339 String newSrc = sourcer(expr); 345 String newSrc = sourcer(expr);
340 if (newSrc == null) { 346 if (newSrc == null) {
341 return null; 347 return null;
342 } 348 }
343 builder.write(newSrc); 349 builder.write(newSrc);
344 if (withBraces) { 350 if (withBraces) {
345 builder.write(" {"); 351 builder.write(" {");
346 builder.write(eol); 352 builder.write(eol);
(...skipping 13 matching lines...) Expand all
360 return completion; 366 return completion;
361 } 367 }
362 368
363 Future<PostfixCompletion> expandTry( 369 Future<PostfixCompletion> expandTry(
364 PostfixCompletionKind kind, Function contexter, 370 PostfixCompletionKind kind, Function contexter,
365 {bool withOn: false}) async { 371 {bool withOn: false}) async {
366 AstNode stmt = contexter(); 372 AstNode stmt = contexter();
367 if (stmt == null) { 373 if (stmt == null) {
368 return null; 374 return null;
369 } 375 }
370 DartChangeBuilder changeBuilder = new DartChangeBuilder(driver); 376 DartChangeBuilder changeBuilder = new DartChangeBuilder(session);
371 await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) { 377 await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
372 // Embed the full line(s) of the statement in the try block. 378 // Embed the full line(s) of the statement in the try block.
373 var startLine = lineInfo.getLocation(stmt.offset).lineNumber - 1; 379 var startLine = lineInfo.getLocation(stmt.offset).lineNumber - 1;
374 var endLine = lineInfo.getLocation(stmt.end).lineNumber - 1; 380 var endLine = lineInfo.getLocation(stmt.end).lineNumber - 1;
375 if (stmt is ExpressionStatement && !stmt.semicolon.isSynthetic) { 381 if (stmt is ExpressionStatement && !stmt.semicolon.isSynthetic) {
376 endLine += 1; 382 endLine += 1;
377 } 383 }
378 var startOffset = lineInfo.getOffsetOfLine(startLine); 384 var startOffset = lineInfo.getOffsetOfLine(startLine);
379 var endOffset = lineInfo.getOffsetOfLine(endLine); 385 var endOffset = lineInfo.getOffsetOfLine(endLine);
380 var src = utils.getText(startOffset, endOffset - startOffset); 386 var src = utils.getText(startOffset, endOffset - startOffset);
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
555 [List args]) { 561 [List args]) {
556 SourceChange change = builder.sourceChange; 562 SourceChange change = builder.sourceChange;
557 if (change.edits.isEmpty) { 563 if (change.edits.isEmpty) {
558 completion = null; 564 completion = null;
559 return; 565 return;
560 } 566 }
561 change.message = formatList(kind.message, args); 567 change.message = formatList(kind.message, args);
562 completion = new PostfixCompletion(kind, change); 568 completion = new PostfixCompletion(kind, change);
563 } 569 }
564 } 570 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698