Skip to content

Commit d053bd0

Browse files
committed
Introduce new setting UseUnicodeArrows to allow reversing arrow replacement
This is needed because starting with Scala 2.13, unicode arrows are deprecated.
1 parent da8126a commit d053bd0

File tree

8 files changed

+87
-6
lines changed

8 files changed

+87
-6
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
0.2.9 (unreleased)
2+
* Add new setting `UseUnicodeArrows` that allows to reverse the unicode arrow symbol replacement when set to false (in Scala 2.13 unicode arrows are deprecated)
3+
14
0.2.8 (29/March/19)
25

36
* Fix type mismatch between `Unit` and `TokenType` (PR #276 by @Philippus)

README.rst

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ Use with Vim
139139
While there is no specific Vim integration at present, you can use
140140
Scalariform as an external formatter for the ``gg=G`` command by adding
141141
the following to ``.vimrc`` ::
142-
142+
143143
au BufEnter *.scala setl formatprg=java\ -jar\ /home/me/bin/scalariform.jar\ -f\ -q\ +compactControlReadability\ +alignParameters\ +alignSingleLineCaseStatements\ +doubleIndentConstructorArguments\ +rewriteArrowSymbols\ +preserveSpaceBeforeArguments\ --stdin\ --stdout
144144
au BufEnter *.scala setl equalprg=java\ -jar\ /home/me/bin/scalariform.jar\ -f\ -q\ +compactControlReadability\ +alignParameters\ +alignSingleLineCaseStatements\ +doubleIndentConstructorArguments\ +rewriteArrowSymbols\ +preserveSpaceBeforeArguments\ --stdin\ --stdout
145145

@@ -727,7 +727,10 @@ rewriteArrowSymbols
727727

728728
Default: ``false``
729729

730-
Replace arrow tokens with their unicode equivalents: ``=>`` with ````, and ``<-`` with ````. For example:
730+
Replace arrow tokens uniformly, either as Unicode symbols or as ASCII, depending on the setting of
731+
``useUnicodeArrows``. Starting from Scala 2.13, unicode arrows are deprecated.
732+
733+
For example, if ``useUnicodeArrows == true``:
731734

732735
.. code:: scala
733736
@@ -866,6 +869,30 @@ If ``false``,:
866869
867870
case elem@Multi(values@_*) =>
868871
872+
useUnicodeArrows
873+
~~~~~~~~~~~~~~~~
874+
875+
Default: ``true``
876+
877+
Controls the replacement of arrows if ``rewriteArrowSymbols == true``. To use unicode arrows in your codebase
878+
set to `true`, otherwise, set to false. For example, if ``useUnicodeArrows == false`` (and ``rewriteArrowSymbols == true``):
879+
880+
.. code:: scala
881+
882+
for (n ← 1 to 10) n % 2 match {
883+
case 0 ⇒ println("even")
884+
case 1 ⇒ println("odd")
885+
}
886+
887+
is formatted as:
888+
889+
.. code:: scala
890+
891+
for (n <- 1 to 10) n % 2 match {
892+
case 0 => println("even")
893+
case 1 => println("odd")
894+
}
895+
869896
Scala Style Guide
870897
~~~~~~~~~~~~~~~~~
871898

@@ -889,6 +916,7 @@ spaceBeforeColon ``false``
889916
spaceInsideBrackets ``false``
890917
spaceInsideParentheses ``false``
891918
spacesAroundMultiImports ``false``
919+
useUnicodeArrows ``true``
892920
=========================================== ========= =========
893921

894922
Source Directives

formatterPreferences.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ danglingCloseParenthesis=Force
2828
#spaceInsideParentheses=false
2929
#spacesAroundMultiImports=true
3030
#spacesWithinPatternBinders=true
31+
#useUnicodeArrows=true

scalariform/src/main/scala/scalariform/formatter/CaseClauseFormatter.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,12 @@ trait CaseClauseFormatter { self: HasFormattingPreferences with ExprFormatter wi
8383
if (formattedCasePattern.contains('\n') || (first && !clausesAreMultiline) || (!first && !newlineBeforeClause) || clauseBodyIsMultiline)
8484
Right(caseClause) :: otherClausesGrouped
8585
else {
86-
val arrowAdjust = (if (formattingPreferences(RewriteArrowSymbols)) 1 else casePattern.arrow.length) + 1
86+
val arrowAdjust = 1 + {
87+
if (formattingPreferences(RewriteArrowSymbols))
88+
if (formattingPreferences(UseUnicodeArrows)) 1
89+
else 2
90+
else casePattern.arrow.length
91+
}
8792
val casePatternLength = formattedCasePattern.length - arrowAdjust
8893
otherClausesGrouped match {
8994
case Left(consecutiveSingleLineCaseClauses) :: otherGroups

scalariform/src/main/scala/scalariform/formatter/ScalaFormatter.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,10 @@ abstract class ScalaFormatter
285285

286286
def write(token: Token, replacementOption: Option[String] = None): Option[TextEdit] = {
287287
val rewriteArrows = formattingPreferences(RewriteArrowSymbols)
288+
val useUnicodeArrows = formattingPreferences(UseUnicodeArrows)
288289
val actualReplacementOption = replacementOption orElse condOpt(token.tokenType) {
289-
case ARROW if rewriteArrows ""
290-
case LARROW if rewriteArrows ""
290+
case ARROW if rewriteArrows if (useUnicodeArrows) "" else "=>"
291+
case LARROW if rewriteArrows if (useUnicodeArrows) "" else "<-"
291292
case EOF ""
292293
}
293294
builder.append(actualReplacementOption getOrElse token.rawText)

scalariform/src/main/scala/scalariform/formatter/preferences/PreferenceDescriptor.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,9 @@ case object SpacesWithinPatternBinders extends BooleanPreferenceDescriptor {
287287
val description = "Spaces around the @ token in pattern binders"
288288
val defaultValue = true
289289
}
290+
291+
case object UseUnicodeArrows extends BooleanPreferenceDescriptor {
292+
val key = "useUnicodeArrows"
293+
val description = "Use unicode arrows if RewriteArrowSymbols is used. If true, replace => with ⇒, and <- with ←, if false the other way round."
294+
val defaultValue = true
295+
}

scalariform/src/test/scala/scalariform/formatter/CaseClausesFormatterTest.scala

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,10 @@ class CaseClausesFormatterTest extends AbstractExpressionFormatterTest {
288288

289289
{
290290
implicit val formattingPreferences: FormattingPreferences =
291-
FormattingPreferences.setPreference(AlignSingleLineCaseStatements, true).setPreference(RewriteArrowSymbols, true)
291+
FormattingPreferences
292+
.setPreference(AlignSingleLineCaseStatements, true)
293+
.setPreference(RewriteArrowSymbols, true)
294+
.setPreference(UseUnicodeArrows, true)
292295

293296
"""a match {
294297
|case b => 42
@@ -300,6 +303,24 @@ class CaseClausesFormatterTest extends AbstractExpressionFormatterTest {
300303
|}"""
301304
}
302305

306+
{
307+
implicit val formattingPreferences: FormattingPreferences =
308+
FormattingPreferences
309+
.setPreference(AlignSingleLineCaseStatements, true)
310+
.setPreference(RewriteArrowSymbols, true)
311+
.setPreference(UseUnicodeArrows, false)
312+
313+
314+
"""a match {
315+
|case b ⇒ 42
316+
| case ccc ⇒ 24
317+
|}""" ==>
318+
"""a match {
319+
| case b => 42
320+
| case ccc => 24
321+
|}"""
322+
}
323+
303324
{
304325

305326
implicit val formattingPreferences: FormattingPreferences =

scalariform/src/test/scala/scalariform/formatter/RewriteArrowsTest.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import scalariform.formatter.preferences._
55
// format: OFF
66
class RewriteArrowsTest extends AbstractExpressionFormatterTest {
77

8+
// test replacing into unicode
89
{
910
implicit val formattingPreferences: FormattingPreferences =
1011
FormattingPreferences.setPreference(RewriteArrowSymbols, true)
12+
.setPreference(UseUnicodeArrows, true)
1113

1214
"(a: Int) => 3" ==> "(a: Int) ⇒ 3"
1315
"for (i <- 1 to 10) yield i" ==> "for (i ← 1 to 10) yield i"
@@ -16,6 +18,20 @@ class RewriteArrowsTest extends AbstractExpressionFormatterTest {
1618
"cache += k -> f(k)" ==> "cache += k -> f(k)"
1719
}
1820

21+
// test replacing from unicode
22+
{
23+
implicit val formattingPreferences: FormattingPreferences =
24+
FormattingPreferences.setPreference(RewriteArrowSymbols, true)
25+
.setPreference(UseUnicodeArrows, false)
26+
27+
"(a: Int) ⇒ 3" ==> "(a: Int) => 3"
28+
"for (i ← 1 to 10) yield i" ==> "for (i <- 1 to 10) yield i"
29+
// We don't rewrite RARROW anymore since it can be, and is, used as a
30+
// normal identifier.
31+
"cache += k -> f(k)" ==> "cache += k -> f(k)"
32+
"cache += k → f(k)" ==> "cache += k → f(k)"
33+
}
34+
1935
override val debug = false
2036

2137
}

0 commit comments

Comments
 (0)