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

Unified Diff: lib/src/line_splitter.dart

Issue 822273004: Add API to provide a selection range, and return the updated selection after formatting. (Closed) Base URL: https://github.com/dart-lang/dart_style.git@master
Patch Set: Remove redundant argument check. Created 5 years, 11 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 | « lib/src/formatter_exception.dart ('k') | lib/src/line_writer.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/line_splitter.dart
diff --git a/lib/src/line_splitter.dart b/lib/src/line_splitter.dart
index 205fdfcadfb1333dcf3a65ccaa3bbb4b25ca5567..476d2119170a138a935c3c35b23331fe19b058c9 100644
--- a/lib/src/line_splitter.dart
+++ b/lib/src/line_splitter.dart
@@ -9,6 +9,7 @@ import 'dart:math' as math;
import 'chunk.dart';
import 'debug.dart';
import 'line_prefix.dart';
+import 'line_writer.dart';
/// The number of spaces in a single level of indentation.
const spacesPerIndent = 2;
@@ -94,19 +95,38 @@ class LineSplitter {
///
/// It will determine how best to split it into multiple lines of output and
/// return a single string that may contain one or more newline characters.
- void apply(StringBuffer buffer) {
+ ///
+ /// Returns a two-element list. The first element will be an [int] indicating
+ /// where in [buffer] the selection start point should appear if it was
+ /// contained in the formatted list of chunks. Otherwise it will be `null`.
+ /// Likewise, the second element will be non-`null` if the selection endpoint
+ /// is within the list of chunks.
+ List<int> apply(StringBuffer buffer) {
if (debugFormatter) dumpLine(_chunks, _indent);
var splits = _findBestSplits(new LinePrefix());
+ var selection = [null, null];
// Write each chunk and the split after it.
buffer.write(" " * (_indent * spacesPerIndent));
- for (var i = 0; i < _chunks.length - 1; i++) {
+ for (var i = 0; i < _chunks.length; i++) {
var chunk = _chunks[i];
+ // If this chunk contains one of the selection markers, tell the writer
+ // where it ended up in the final output.
+ if (chunk.selectionStart != null) {
+ selection[0] = buffer.length + chunk.selectionStart;
+ }
+
+ if (chunk.selectionEnd != null) {
+ selection[1] = buffer.length + chunk.selectionEnd;
+ }
+
buffer.write(chunk.text);
- if (splits.shouldSplitAt(i)) {
+ if (i == _chunks.length - 1) {
+ // Don't write trailing whitespace after the last chunk.
+ } else if (splits.shouldSplitAt(i)) {
buffer.write(_lineEnding);
if (chunk.isDouble) buffer.write(_lineEnding);
@@ -120,8 +140,7 @@ class LineSplitter {
}
}
- // Write the final chunk without any trailing newlines.
- buffer.write(_chunks.last.text);
+ return selection;
}
/// Finds the best set of splits to apply to the remainder of the line
« no previous file with comments | « lib/src/formatter_exception.dart ('k') | lib/src/line_writer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698