@@ -727,26 +727,13 @@ object Parsers {
727
727
t
728
728
end indentedToBraces
729
729
730
- /** The region to eliminate when replacing an opening `(` or `{` that ends a line.
731
- * The `(` or `{` is at in.offset.
732
- */
733
- def startingElimRegion (colonRequired : Boolean ): (Offset , Offset ) =
734
- val (start, end) = blankLinesAround(in.offset, in.offset + 1 )
735
- if testChar(end, Chars .LF ) then
736
- if testChar(start - 1 , Chars .LF ) && ! colonRequired then
737
- (start, end + 1 ) // skip the whole line
738
- else (start, end) // skip the end of line
739
- else (start, in.offset + 1 ) // skip from last to end of token
740
-
741
- /** The region to eliminate when replacing a closing `)` or `}` that starts a new line
742
- * The `)` or `}` precedes in.lastOffset.
743
- */
744
- def closingElimRegion (): (Offset , Offset ) =
745
- val (start, end) = blankLinesAround(in.lastOffset - 1 , in.lastOffset)
730
+ /** The region to eliminate when replacing a brace or parenthesis that ends a line */
731
+ def elimRegion (offset : Offset ): (Offset , Offset ) =
732
+ val (start, end) = blankLinesAround(offset, offset + 1 )
746
733
if testChar(end, Chars .LF ) then
747
734
if testChar(start - 1 , Chars .LF ) then (start, end + 1 ) // skip the whole line
748
735
else (start, end) // skip the end of line
749
- else (in.lastOffset - 1 , end) // skip from token to next
736
+ else (offset , end) // skip from last to end of token
750
737
751
738
/** Expand the current span to the surrounding blank lines */
752
739
def blankLinesAround (start : Offset , end : Offset ): (Offset , Offset ) =
@@ -763,21 +750,25 @@ object Parsers {
763
750
* 6. the opening brace does not follow a closing `}`
764
751
* 7. last token is not a leading operator
765
752
* 8. not a block in a sequence of statements
753
+ * 9. cannot rewrite to colon after a NEWLINE, e.g.
754
+ * true ||
755
+ * { // NEWLINE inserted between || and {
756
+ * false
757
+ * }
766
758
*/
767
759
def bracesToIndented [T ](body : => T , inStatSeq : Boolean , rewriteWithColon : Boolean ): T = {
768
760
val lastSaved = in.last.saveCopy
761
+ val lastOffsetSaved = in.lastOffset
769
762
val underColonSyntax = possibleColonOffset == in.lastOffset
770
763
val colonRequired = rewriteWithColon || underColonSyntax
771
- val (startOpening, endOpening) = startingElimRegion(colonRequired)
772
- val isOutermost = in.currentRegion.isOutermost
764
+ val (startOpening, endOpening) = elimRegion(in.offset)
773
765
def isBracesOrIndented (r : Region ): Boolean = r match
774
766
case r : Indented => true
775
767
case r : InBraces => true
776
768
case _ => false
777
- val followsLeadingOperator = lastSaved.isOperator && lastSaved.isAfterLineEnd
778
769
var canRewrite = isBracesOrIndented(in.currentRegion) && // test (1)
779
770
lastSaved.token != RBRACE && // test (6)
780
- ! followsLeadingOperator && // test (7)
771
+ ! (lastSaved.isOperator && lastSaved.isAfterLineEnd) && // test (7)
781
772
! inStatSeq // test (8)
782
773
var innerIndent : Option [IndentWidth ] = None
783
774
val t = enclosed(LBRACE , {
@@ -791,23 +782,18 @@ object Parsers {
791
782
canRewrite = false
792
783
body
793
784
})
794
- canRewrite &= (in.isAfterLineEnd || in.token == EOF || statCtdTokens.contains(in.token)) // test (5)
795
- def isOperatorFollowedByColon =
796
- lastSaved.name != null &&
797
- startOpening == lastSaved.offset + lastSaved.name.length &&
798
- Chars .isOperatorPart(lastSaved.name.last)
785
+ canRewrite &= (in.isAfterLineEnd || in.token == EOF || statCtdTokens.contains(in.token)) && // test (5)
786
+ (! colonRequired || ! lastSaved.isNewLine) // test (9)
799
787
if canRewrite && (! underColonSyntax || Feature .fewerBracesEnabled) then
800
- val openingPatchStr =
801
- if ! colonRequired then " "
802
- else if isOperatorFollowedByColon then
803
- // surround previous ident with backticks
804
- patch(Span (lastSaved.offset, lastSaved.offset + lastSaved.name.length), s " ` ${lastSaved.name}` " )
805
- " :"
806
- else " :"
807
- val (startClosing, endClosing) = closingElimRegion()
788
+ val (startClosing, endClosing) = elimRegion(in.last.offset)
808
789
// patch over the added indentation to remove braces
809
- patchOver(source, Span (startOpening, endOpening), openingPatchStr )
790
+ patchOver(source, Span (startOpening, endOpening), " " )
810
791
patchOver(source, Span (startClosing, endClosing), " " )
792
+ if colonRequired then
793
+ // add backticks around operator and add colon
794
+ if lastSaved.isOperator then
795
+ patch(Span (lastSaved.offset, lastSaved.offset + lastSaved.name.length), s " ` ${lastSaved.name}` : " )
796
+ else patch(Span (lastOffsetSaved), " :" )
811
797
// force outdentation of token after }
812
798
maximumIndent = innerIndent
813
799
else
@@ -883,7 +869,7 @@ object Parsers {
883
869
val preFill = if (closingStartsLine || endStr.isEmpty) " " else " "
884
870
val postFill = if (in.lastOffset == in.offset) " " else " "
885
871
val (startClosing, endClosing) =
886
- if (closingStartsLine && endStr.isEmpty) closingElimRegion( )
872
+ if (closingStartsLine && endStr.isEmpty) elimRegion(in.last.offset )
887
873
else (in.lastOffset - 1 , in.lastOffset)
888
874
patch(source, Span (startClosing, endClosing), s " $preFill$endStr$postFill" )
889
875
}
0 commit comments