@@ -515,25 +515,24 @@ final class CorrectionUtils {
515
515
/// possible) to cover whole lines.
516
516
SourceRange getLinesRange (SourceRange sourceRange,
517
517
{bool skipLeadingEmptyLines = false }) {
518
- // start
518
+ // Calculate the start:
519
519
var startOffset = sourceRange.offset;
520
520
var startLineOffset = getLineContentStart (startOffset);
521
521
if (skipLeadingEmptyLines) {
522
522
startLineOffset = _skipEmptyLinesLeft (startLineOffset);
523
523
}
524
- // end
524
+ // Calculate the end:
525
525
var endOffset = sourceRange.end;
526
526
var afterEndLineOffset = endOffset;
527
527
var lineInfo = _unit.lineInfo;
528
528
var lineStart = lineInfo
529
529
.getOffsetOfLine (lineInfo.getLocation (startLineOffset).lineNumber - 1 );
530
530
if (lineStart == startLineOffset) {
531
- // Only consume line ends after the end of the range if there is nothing
532
- // else on the line containing the beginning of the range. Otherwise this
533
- // will end up incorrectly merging two line.
531
+ // Only consume line endings after the end of the range if there is
532
+ // nothing else on the line containing the beginning of the range.
533
+ // Otherwise this will end up incorrectly merging two line.
534
534
afterEndLineOffset = getLineContentEnd (endOffset);
535
535
}
536
- // range
537
536
return range.startOffsetEndOffset (startLineOffset, afterEndLineOffset);
538
537
}
539
538
@@ -588,14 +587,11 @@ final class CorrectionUtils {
588
587
}
589
588
590
589
/// Returns the text of the given range in the unit.
591
- String getRangeText (SourceRange range) {
592
- return getText (range.offset, range.length);
593
- }
590
+ String getRangeText (SourceRange range) => getText (range.offset, range.length);
594
591
595
592
/// Returns the text of the given range in the unit.
596
- String getText (int offset, int length) {
597
- return _buffer.substring (offset, offset + length);
598
- }
593
+ String getText (int offset, int length) =>
594
+ _buffer.substring (offset, offset + length);
599
595
600
596
/// Indents given source left or right.
601
597
String indentSourceLeftRight (String source, {bool indentLeft = true }) {
@@ -622,7 +618,7 @@ final class CorrectionUtils {
622
618
return sb.toString ();
623
619
}
624
620
625
- /// Return the source of the inverted condition for the given logical
621
+ /// Returns the source of the inverted condition for the given logical
626
622
/// expression.
627
623
String invertCondition (Expression expression) =>
628
624
_invertCondition0 (expression)._source;
@@ -642,7 +638,7 @@ final class CorrectionUtils {
642
638
/// lines).
643
639
String replaceSourceIndent (String source, String oldIndent, String newIndent,
644
640
{bool includeLeading = false , bool ensureTrailingNewline = false }) {
645
- // prepare STRING token ranges
641
+ // Prepare token ranges.
646
642
var lineRanges = < SourceRange > [];
647
643
{
648
644
var tokens = TokenUtils .getTokens (source, _unit.featureSet);
@@ -652,7 +648,7 @@ final class CorrectionUtils {
652
648
}
653
649
}
654
650
}
655
- // re -indent lines
651
+ // Re -indent lines.
656
652
var sb = StringBuffer ();
657
653
var eol = endOfLine;
658
654
var lines = source.split (eol);
@@ -661,16 +657,16 @@ final class CorrectionUtils {
661
657
var line = lines[i];
662
658
// Exit early if this is the last line and it's already empty, to avoid
663
659
// inserting any whitespace or appending an additional newline if
664
- // [ ensureTrailingNewline] .
660
+ // ` ensureTrailingNewline` .
665
661
if (i == lines.length - 1 && isEmpty (line)) {
666
662
break ;
667
663
}
668
- // Don't replace whitespace on first line unless [ includeLeading] .
664
+ // Don't replace whitespace on first line unless ` includeLeading` .
669
665
var doReplaceWhitespace = i != 0 || includeLeading;
670
- // Don't add eol to last line unless [ ensureTrailingNewline] .
666
+ // Don't add eol to last line unless ` ensureTrailingNewline` .
671
667
var doAppendEol = i != lines.length - 1 || ensureTrailingNewline;
672
668
673
- // check if "offset" is in one of the String ranges
669
+ // Check if "offset" is in one of the ranges.
674
670
var inString = false ;
675
671
for (var lineRange in lineRanges) {
676
672
if (lineOffset > lineRange.offset && lineOffset < lineRange.end) {
@@ -681,11 +677,11 @@ final class CorrectionUtils {
681
677
}
682
678
}
683
679
lineOffset += line.length + eol.length;
684
- // update line indent
680
+ // Update line indent.
685
681
if (! inString && doReplaceWhitespace) {
686
682
line = '$newIndent ${removeStart (line , oldIndent )}' ;
687
683
}
688
- // append line
684
+ // Append line.
689
685
sb.write (line);
690
686
if (doAppendEol) {
691
687
sb.write (eol);
@@ -717,14 +713,6 @@ final class CorrectionUtils {
717
713
ensureTrailingNewline: ensureTrailingNewline);
718
714
}
719
715
720
- /// Return `true` if [selection] covers [node] and there are any
721
- /// non-whitespace tokens between [selection] and [node] start/end.
722
- bool selectionIncludesNonWhitespaceOutsideNode (
723
- SourceRange selection, AstNode node) {
724
- return _selectionIncludesNonWhitespaceOutsideRange (
725
- selection, range.node (node));
726
- }
727
-
728
716
/// Returns the [_InvertedCondition] for the given logical expression.
729
717
_InvertedCondition _invertCondition0 (Expression expression) {
730
718
if (expression is BooleanLiteral ) {
@@ -793,39 +781,6 @@ final class CorrectionUtils {
793
781
return _InvertedCondition ._simple (getNodeText (expression));
794
782
}
795
783
796
- /// Returns whether [range] contains only whitespace or comments.
797
- bool _isJustWhitespaceOrComment (SourceRange range) {
798
- var trimmedText = getRangeText (range).trim ();
799
- // may be whitespace
800
- if (trimmedText.isEmpty) {
801
- return true ;
802
- }
803
- // may be comment
804
- return TokenUtils .getTokens (trimmedText, _unit.featureSet).isEmpty;
805
- }
806
-
807
- /// Return `true` if [selection] covers [range] and there are any
808
- /// non-whitespace tokens between [selection] and [range] start/end.
809
- bool _selectionIncludesNonWhitespaceOutsideRange (
810
- SourceRange selection, SourceRange sourceRange) {
811
- // selection should cover range
812
- if (! selection.covers (sourceRange)) {
813
- return false ;
814
- }
815
- // non-whitespace between selection start and range start
816
- if (! _isJustWhitespaceOrComment (
817
- range.startOffsetEndOffset (selection.offset, sourceRange.offset))) {
818
- return true ;
819
- }
820
- // non-whitespace after range
821
- if (! _isJustWhitespaceOrComment (
822
- range.startOffsetEndOffset (sourceRange.end, selection.end))) {
823
- return true ;
824
- }
825
- // only whitespace in selection around range
826
- return false ;
827
- }
828
-
829
784
/// Skip spaces, tabs and EOLs on the left from [index] .
830
785
///
831
786
/// If [index] is the start of a method, then in the most cases return the end
@@ -848,19 +803,10 @@ final class CorrectionUtils {
848
803
849
804
/// Utilities to work with [Token] s.
850
805
class TokenUtils {
851
- static List <Token > getNodeTokens (AstNode node) {
852
- var result = < Token > [];
853
- for (var token = node.beginToken;; token = token.next! ) {
854
- result.add (token);
855
- if (token == node.endToken) {
856
- break ;
857
- }
858
- }
859
- return result;
860
- }
861
-
862
- /// Return the tokens of the given Dart source, not `null` , may be empty if no
863
- /// tokens or some exception happens.
806
+ /// Returns the tokens of the given Dart source, [s] .
807
+ ///
808
+ /// The returned list may be empty if there are no tokens, or some exception
809
+ /// is caught.
864
810
static List <Token > getTokens (String s, FeatureSet featureSet) {
865
811
try {
866
812
var tokens = < Token > [];
0 commit comments