-
Notifications
You must be signed in to change notification settings - Fork 278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: internal/FormatWriter simplify two regexen; fix pipe char bug #1924
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -368,7 +368,9 @@ class FormatWriter(formatOps: FormatOps) { | |
tupleOpt.fold(text) { | ||
case (pipe, indent) => | ||
val spaces = getIndentation(indent) | ||
getStripMarginPattern(pipe).matcher(text).replaceAll(spaces) | ||
getStripMarginPattern(pipe) | ||
.matcher(text) | ||
.replaceAll(s"\n${spaces}$$1") | ||
} | ||
} | ||
|
||
|
@@ -729,13 +731,30 @@ object FormatWriter { | |
} | ||
} | ||
|
||
private val trailingSpace = Pattern.compile("\\h+$", Pattern.MULTILINE) | ||
/** | ||
* No Unicode is necessary in the three (3) Pattern.compile() below. | ||
* scalafmt parser will have already have objected. | ||
* https://github.com/scala/scala/blob/2.11.x/spec/01-lexical-syntax.md | ||
* gives the recognized scala whitespace characters. | ||
* "Whitespace characters. \U0020 | \U0009 | \U000D | \U000A" | ||
* (original text has lowercase u. U substituted here to allow scalafmt | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this comment relevant to the code? :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had thought so, but that section is superseded now. After I commit, let me know if |
||
* to format this file.) | ||
* | ||
* Of the characters which the parser will pass, only space (\u0020) | ||
* and tab (\u0009) are horizontal whitespace. | ||
* Save execution time by not checking what the character could not be. | ||
* | ||
* */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Being a visitor and it not being my usual coding style, for consistency I did a Yes, the trailing "* */" is not a variant I have seen in recent memory, That comment is re-worked in the commit I am about to submit, using my |
||
|
||
private val trailingSpace = | ||
Pattern.compile("[\\x20\\t]+$", Pattern.MULTILINE) | ||
|
||
private def removeTrailingWhiteSpace(str: String): String = { | ||
trailingSpace.matcher(str).replaceAll("") | ||
} | ||
|
||
private val leadingAsteriskSpace = | ||
Pattern.compile("(?<=\\n)\\h*(?=[*][^*])") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explain why allowing match on multiple asterisks is equivalent? I tried yesterday, there was a test which failed but looks like for you it didn't. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I took the requirement that " *X" gets replaced and " **" (double asterisk) does not as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, was looking at the code on my phone, didn't see the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
thanks for adding tests. this question above is the last one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. re: Pardon me for being concrete, no disrespect intended. The concrete answer It is my understanding that for 'normal' strings In s-interpolated strings (and possibly/probably other interpolations) things get So, both work in interpolated strings, unless you have evidence or experience to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Re: tests I found StripMargin.stat and added tests for only the I had expected to find tests for leading and trailing spaces. I could not find anything relevant I did find a Unicode.stat and could slip in a case for leading and trailing horizontal Off the top of your head, do you have any clues or suggestions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I added one tab in the https://github.com/scalameta/scalafmt/pull/1911/files you can create a dedicated whitespace test but i considered it part of handling comments and strings. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kitbellew The info you gave helped me to get to the next round. Thank you. All tests, new & previous, pass on my system. I will check CI later. The tests are still sensitive to some future devo removing the now invisible whitespace I think the three regex expressions are as simple & fast as the requirements and reasonable I suspect that there are a couple of bugs remaining in scalafmt relating to tabs. I will see in Unit tests prove their worth, much as they are a pain to write. The hard time I had If you like the current work, I can update & buff the commit message and change the |
||
private val leadingAsteriskSpace = Pattern.compile("\n[\\x20\\t]*\\*([^*])") | ||
|
||
private def formatComment( | ||
comment: T.Comment, | ||
indent: Int | ||
|
@@ -746,7 +765,9 @@ object FormatWriter { | |
val isDocstring = comment.syntax.startsWith("/**") && style.scalaDocs | ||
val spaces: String = | ||
getIndentation(if (isDocstring) (indent + 2) else (indent + 1)) | ||
leadingAsteriskSpace.matcher(comment.syntax).replaceAll(spaces) | ||
leadingAsteriskSpace | ||
.matcher(comment.syntax) | ||
.replaceAll(s"\n${spaces}*$$1") | ||
} else { | ||
comment.syntax | ||
} | ||
|
@@ -756,9 +777,11 @@ object FormatWriter { | |
@inline | ||
private def getStripMarginPattern(pipe: Char) = | ||
if (pipe == '|') leadingPipeSpace else compileStripMarginPattern(pipe) | ||
|
||
@inline | ||
private def compileStripMarginPattern(pipe: Char) = | ||
Pattern.compile(s"(?<=\\n)\\h*(?=[$pipe])") | ||
Pattern.compile(s"\n[\\x20\\t]*(\\${pipe})") | ||
|
||
private val leadingPipeSpace = compileStripMarginPattern('|') | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,7 +125,7 @@ object a { | |
/* | ||
|
||
/** | ||
* comment | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. did you remove this because you create a separate test case for that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly. The case seemed to be multiplexed in a non-obvious (to me way). Creating a second test with a title which described its primary (and sole) concerned |
||
* comment | ||
*/ | ||
|
||
def undefined = ??? | ||
|
@@ -142,3 +142,15 @@ object a { | |
def undefined = ??? | ||
*/ | ||
} | ||
<<< remove spaces and tabs in leading horizontal whitespace | ||
object A { | ||
/* | ||
* remove spaces & 2 tabs (\t) in leading horizontal whitespace | ||
*/ | ||
} | ||
>>> | ||
object A { | ||
/* | ||
* remove spaces & 2 tabs (\t) in leading horizontal whitespace | ||
*/ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
maxColumn = 80 | ||
# Devos: be careful this file contains essential but invisible spaces. | ||
<<< Remove trailing spaces from variables | ||
object A { | ||
val a = 1 | ||
val b = 2 | ||
} | ||
>>> | ||
object A { | ||
val a = 1 | ||
val b = 2 | ||
} | ||
<<< Remove trailing tab-blank-tab combinations from variables | ||
object B { | ||
val a = 1 | ||
val b = 2 | ||
} | ||
>>> | ||
object B { | ||
val a = 1 | ||
val b = 2 | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would you mind remove the 3 blank lines at the end? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, thank you. So you have something against trailing whitespace ;-) |
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are you saying that nothing other than space and tab are allowed by scalac or scalameta parser that precedes formatting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It shouldn't be allowed according to specs.
A cursory examination of the scalameta sources led to this. So, it matches.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After another long day of creating test cases and trying to be perverse, it turns out
that trailing whitespace with Unicode whitespace is allowed in comments. As cited,
it is rejected in non-comment code.
I am about to do a commit which handles, tests, & succinctly (I hope) documents.
The discussion here helps.