Skip to content

Commit 79faded

Browse files
srawlinsCommit Queue
authored and
Commit Queue
committed
analyzer: Touch up docs and impl of CorrectionUtils and EditBuilders
In this CL, I do not change the signature of any EditBuilder code, as it is public analyzer_plugin API. I only change doc comments and a few impl nits here and there, to make more modern or idiomatic. This CL is isolated this way so that when I do a copy to the analysis_server_plugin package, the moves will be smaller, readable diffs (almost zero diff, just imports and things like that). Change-Id: Ibc8a5ddb9a679278dd7223a3e2d868fe21c4acef Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/363881 Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 326cf87 commit 79faded

File tree

4 files changed

+227
-224
lines changed

4 files changed

+227
-224
lines changed

pkg/analysis_server/lib/src/services/correction/util.dart

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -425,39 +425,39 @@ final class CorrectionUtils {
425425

426426
String get twoIndents => _twoIndents;
427427

428-
/// Returns the [AstNode] that encloses the given offset.
428+
/// Returns the [AstNode] that encloses the given [offset].
429429
AstNode? findNode(int offset) => NodeLocator(offset).searchWithin(_unit);
430430

431431
/// Skips whitespace characters and single EOL on the right from [index].
432432
///
433-
/// If [index] the end of a statement or method, then in the most cases it is
434-
/// a start of the next line.
433+
/// If [index] the end of a statement or method, then in most cases this
434+
/// returns the start of the next line.
435435
int getLineContentEnd(int index) {
436436
var length = _buffer.length;
437-
// skip whitespace characters
437+
// Skip whitespace characters.
438438
while (index < length) {
439439
var c = _buffer.codeUnitAt(index);
440440
if (!c.isWhitespace || c.isEOL) {
441441
break;
442442
}
443443
index++;
444444
}
445-
// skip single \r
445+
// Skip a single '\r' character.
446446
if (index < length && _buffer.codeUnitAt(index) == 0x0D) {
447447
index++;
448448
}
449-
// skip single \n
449+
// Skip a single '\n' character.
450450
if (index < length && _buffer.codeUnitAt(index) == 0x0A) {
451451
index++;
452452
}
453-
// done
453+
// Done.
454454
return index;
455455
}
456456

457457
/// Skips spaces and tabs on the left from [index].
458458
///
459-
/// If [index] is the start or a statement, then in the most cases it is a
460-
/// start on its line.
459+
/// If [index] is the start or a statement, then in most cases this returns
460+
/// the offset of the line in which [index] is found.
461461
int getLineContentStart(int index) {
462462
while (index > 0) {
463463
var c = _buffer.codeUnitAt(index - 1);
@@ -469,8 +469,8 @@ final class CorrectionUtils {
469469
return index;
470470
}
471471

472-
/// Returns a start index of the next line after the line which contains the
473-
/// given index.
472+
/// Returns the index of the start of the line following the line which
473+
/// contains the given [index].
474474
int getLineNext(int index) {
475475
var length = _buffer.length;
476476
// skip to the end of the line
@@ -481,19 +481,19 @@ final class CorrectionUtils {
481481
}
482482
index++;
483483
}
484-
// skip single \r
484+
// Skip a single '\r'.
485485
if (index < length && _buffer.codeUnitAt(index) == 0xD) {
486486
index++;
487487
}
488-
// skip single \n
488+
// Skip a single '\n'.
489489
if (index < length && _buffer.codeUnitAt(index) == 0xA) {
490490
index++;
491491
}
492-
// done
492+
// Done.
493493
return index;
494494
}
495495

496-
/// Returns the whitespace prefix of the line which contains given offset.
496+
/// Returns the whitespace prefix of the line which contains given [index].
497497
String getLinePrefix(int index) {
498498
var lineStart = getLineThis(index);
499499
var length = _buffer.length;
@@ -541,7 +541,7 @@ final class CorrectionUtils {
541541
return getLinesRange(range.nodes(statements));
542542
}
543543

544-
/// Returns the start index of the line which contains given index.
544+
/// Returns the start index of the line which contains the given [index].
545545
int getLineThis(int index) {
546546
while (index > 0) {
547547
var c = _buffer.codeUnitAt(index - 1);
@@ -553,8 +553,7 @@ final class CorrectionUtils {
553553
return index;
554554
}
555555

556-
/// Returns the line prefix consisting of spaces and tabs on the left from the
557-
/// given [AstNode].
556+
/// Returns the whitespace prefix of the line which contains given [node].
558557
String getNodePrefix(AstNode node) {
559558
var offset = node.offset;
560559
// function literal is special, it uses offset of enclosing line
@@ -565,7 +564,8 @@ final class CorrectionUtils {
565564
return getPrefix(offset);
566565
}
567566

568-
/// Returns the text of the given [AstNode] in the unit.
567+
/// Returns the text of the given [AstNode] in the unit, including preceding
568+
/// comments.
569569
String getNodeText(
570570
AstNode node, {
571571
bool withLeadingComments = false,
@@ -579,8 +579,7 @@ final class CorrectionUtils {
579579
return getText(offset, length);
580580
}
581581

582-
/// Returns the line prefix consisting of spaces and tabs on the left from the
583-
/// given offset.
582+
/// Returns the whitespace prefix to the left of the given [endIndex].
584583
String getPrefix(int endIndex) {
585584
var startIndex = getLineContentStart(endIndex);
586585
return _buffer.substring(startIndex, endIndex);
@@ -593,7 +592,7 @@ final class CorrectionUtils {
593592
String getText(int offset, int length) =>
594593
_buffer.substring(offset, offset + length);
595594

596-
/// Indents given source left or right.
595+
/// Indents the given [source] left or right.
597596
String indentSourceLeftRight(String source, {bool indentLeft = true}) {
598597
var sb = StringBuffer();
599598
var indent = oneIndent;
@@ -632,10 +631,9 @@ final class CorrectionUtils {
632631
/// If [ensureTrailingNewline] is `true`, a newline will be added to
633632
/// the end of the returned code if it does not already have one.
634633
///
635-
/// Usually [includeLeading] and [ensureTrailingNewline] will both be set
636-
/// together when indenting a set of statements to go inside a block (as
637-
/// opposed to just wrapping a nested expression that might span multiple
638-
/// lines).
634+
/// Usually [includeLeading] and [ensureTrailingNewline] are set together,
635+
/// when indenting a set of statements to go inside a block (as opposed to
636+
/// just wrapping a nested expression that might span multiple lines).
639637
String replaceSourceIndent(String source, String oldIndent, String newIndent,
640638
{bool includeLeading = false, bool ensureTrailingNewline = false}) {
641639
// Prepare token ranges.
@@ -700,10 +698,9 @@ final class CorrectionUtils {
700698
/// If [ensureTrailingNewline] is `true`, a newline will be added to
701699
/// the end of the returned code if it does not already have one.
702700
///
703-
/// Usually [includeLeading] and [ensureTrailingNewline] will both be set
704-
/// together when indenting a set of statements to go inside a block (as
705-
/// opposed to just wrapping a nested expression that might span multiple
706-
/// lines).
701+
/// Usually [includeLeading] and [ensureTrailingNewline] are set together,
702+
/// when indenting a set of statements to go inside a block (as opposed to
703+
/// just wrapping a nested expression that might span multiple lines).
707704
String replaceSourceRangeIndent(
708705
SourceRange range, String oldIndent, String newIndent,
709706
{bool includeLeading = false, bool ensureTrailingNewline = false}) {
@@ -781,10 +778,10 @@ final class CorrectionUtils {
781778
return _InvertedCondition._simple(getNodeText(expression));
782779
}
783780

784-
/// Skip spaces, tabs and EOLs on the left from [index].
781+
/// Skips whitespace and EOLs to the left of [index].
785782
///
786-
/// If [index] is the start of a method, then in the most cases return the end
787-
/// of the previous not-whitespace line.
783+
/// If [index] is the start of a method declaration, then in most cases, this
784+
/// returns the end of the previous non-whitespace line.
788785
int _skipEmptyLinesLeft(int index) {
789786
var lastLine = index;
790787
while (index > 0) {

0 commit comments

Comments
 (0)