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

Side by Side Diff: pkg/analysis_server/lib/src/services/correction/util.dart

Issue 1009833002: Quick Assist for creating a constructor for selected final fields. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 9 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 | Annotate | Revision Log
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 services.src.correction.util; 5 library services.src.correction.util;
6 6
7 import 'dart:math'; 7 import 'dart:math';
8 8
9 import 'package:analysis_server/src/protocol.dart' 9 import 'package:analysis_server/src/protocol.dart'
10 show SourceChange, SourceEdit; 10 show SourceChange, SourceEdit;
(...skipping 1043 matching lines...) Expand 10 before | Expand all | Expand 10 after
1054 String trimmedText = getRangeText(range).trim(); 1054 String trimmedText = getRangeText(range).trim();
1055 // may be whitespace 1055 // may be whitespace
1056 if (trimmedText.isEmpty) { 1056 if (trimmedText.isEmpty) {
1057 return true; 1057 return true;
1058 } 1058 }
1059 // may be comment 1059 // may be comment
1060 return TokenUtils.getTokens(trimmedText).isEmpty; 1060 return TokenUtils.getTokens(trimmedText).isEmpty;
1061 } 1061 }
1062 1062
1063 /** 1063 /**
1064 * Return the best locations for a new constructor in the given
1065 * [classDeclaration].
1066 */
1067 ConstructorLocation findNewConstructorLocation(
1068 ClassDeclaration classDeclaration) {
1069 String eol = endOfLine;
1070 List<ClassMember> members = classDeclaration.members;
1071 // find the last field/constructor
1072 ClassMember lastFieldOrConstructor = null;
1073 for (ClassMember member in members) {
1074 if (member is FieldDeclaration || member is ConstructorDeclaration) {
1075 lastFieldOrConstructor = member;
1076 } else {
1077 break;
1078 }
1079 }
1080 // after the last field/constructor
1081 if (lastFieldOrConstructor != null) {
1082 return new ConstructorLocation(eol + eol, lastFieldOrConstructor.end, '');
1083 }
1084 // at the beginning of the class
1085 String suffix = members.isEmpty ? '' : eol;
1086 return new ConstructorLocation(
1087 eol, classDeclaration.leftBracket.end, suffix);
1088 }
1089
1090 /**
1064 * Returns the source with indentation changed from [oldIndent] to 1091 * Returns the source with indentation changed from [oldIndent] to
1065 * [newIndent], keeping indentation of lines relative to each other. 1092 * [newIndent], keeping indentation of lines relative to each other.
1066 */ 1093 */
1067 String replaceSourceIndent( 1094 String replaceSourceIndent(
1068 String source, String oldIndent, String newIndent) { 1095 String source, String oldIndent, String newIndent) {
1069 // prepare STRING token ranges 1096 // prepare STRING token ranges
1070 List<SourceRange> lineRanges = []; 1097 List<SourceRange> lineRanges = [];
1071 { 1098 {
1072 List<Token> tokens = TokenUtils.getTokens(source); 1099 List<Token> tokens = TokenUtils.getTokens(source);
1073 for (Token token in tokens) { 1100 for (Token token in tokens) {
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after
1473 1500
1474 @override 1501 @override
1475 Object visitExpression(Expression node) { 1502 Object visitExpression(Expression node) {
1476 if (node is BinaryExpression && node.operator.type == groupOperatorType) { 1503 if (node is BinaryExpression && node.operator.type == groupOperatorType) {
1477 return super.visitNode(node); 1504 return super.visitNode(node);
1478 } 1505 }
1479 operands.add(node); 1506 operands.add(node);
1480 return null; 1507 return null;
1481 } 1508 }
1482 } 1509 }
1510
1511 /**
1512 * Describes the location for a newly created [ConstructorDeclaration].
1513 */
1514 class ConstructorLocation {
1515 final String prefix;
1516 final int offset;
1517 final String suffix;
1518
1519 ConstructorLocation(this.prefix, this.offset, this.suffix);
1520 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698