Skip to content
This repository has been archived by the owner on Sep 3, 2020. It is now read-only.

Refactorings might mess with parens #94

Conversation

mlangc
Copy link
Collaborator

@mlangc mlangc commented Sep 6, 2015

This PR, which is closely related to #93, deals mostly with CommonPrintUtils.balanceBrackets once more (details can be found in the commit messages). Please note that I still don't trust this function completely, as I don't understand what the "\\" in

f.asText.reverse.replaceFirst("\\" + close, ("" + close) * (opening - closing + 1)).reverse

(see https://github.com/mlangc/scala-refactoring/blob/organize-imports-adds-opening-parenthesis-1002088/src/main/scala/scala/tools/refactoring/sourcegen/CommonPrintUtils.scala#L61) is about. @misto Maybe you can clarify?

Last but not least, these changes fix Ticket #1002088.

Prior versions of `CommonPrintUtils.balanceParens` treated parens in string
constants just like parens in regular Scala code which resulted in bugs like
ticket #1002088. This commit improves the aforementioned function, that also
played a prominent role in ticket #1002166, by delegating counting parens
to a helper function, that handles comments and string constants properly.

Fixes #1002088
The term "brackets" is used for "<>", "()", "{}" and "[]", while parens usually
refers to "()" and "{}" only. As the function in question accepts the brackets
as parameters, "brackets" is more appropriate than "parens".
@@ -40,7 +40,7 @@ trait Indentations {
if(memoizedSourceWithoutComments contains tree.pos.source.path) {
memoizedSourceWithoutComments(tree.pos.source.path)
} else {
val src = CommentsUtils.stripComment(tree.pos.source.content)
val src = SourceUtils.stripComment(tree.pos.source.content)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rename breaks the IDE and maybe other clients.

@kiritsuku
Copy link
Member

Beside from the source incompatibility this code looks good (I must admit I didn't verify the correctness of this imperative monster, I hope you don't expect that ;)).

@misto
Copy link
Member

misto commented Sep 7, 2015

First, let me apolozige for that piece of code.. I have no memory of why this was introduced, but I've just deleted that part and no test fails:

-    if (opening > closing && closing > 0) {
-      f.asText.reverse.replaceFirst("\\" + close, ("" + close) * (opening - closing + 1)).reverse
-    } else if (opening > closing) {
+    if (opening > closing) {

So I think it's safe to just delete it.

@misto
Copy link
Member

misto commented Sep 7, 2015

Otherwise, looks good! But I agree with Simon that we should try to keep the source compatible.

`CommonPrintUtils.balanceBrackets` used to contain some code that was meant to
add missing closing brackets directly after the last closing bracket, instead of
just appending them to the layout. Given the fact that the code was broken anyway,
(it didn't consider comments, string constants, etc.), all tests pass without it,
and the original author thinks that it's not needed, it seems safe to remove it.
@mlangc
Copy link
Collaborator Author

mlangc commented Sep 7, 2015

Thanks for the review! I've just reintroduced CommentsUtils the way @sschaef suggested. Since the ScalaIDE projects treat warnings as errors, I've not yet deprecated it, but I'd like to do so somewhere in the near future, after the IDE is migrated to SourceUtils.

As for that mysterious piece of code: I've just removed at, as @misto suggested. Maybe we should wait a few days before merging, so that I can see if I experience any oddities.

@misto
Copy link
Member

misto commented Sep 8, 2015

I did some digging (git log -S "f.asText.reverse.replaceFirst" --source --all) and that code was introduced in 4ae1c6b, together with a test that's still part of our test suite. So I think it's safe to delete it whenever you want.

@mlangc
Copy link
Collaborator Author

mlangc commented Sep 8, 2015

Thanks for the research; the code has been deleted with 6279d92.

@misto
Copy link
Member

misto commented Sep 11, 2015

Did I just forget to merge this, or were there any issues left? :-)

@kiritsuku
Copy link
Member

I think everything is resolved. LGTM

kiritsuku added a commit that referenced this pull request Sep 11, 2015
…nthesis-1002088

Refactorings might mess with parens
@kiritsuku kiritsuku merged commit b99db3f into scala-ide:master Sep 11, 2015
@mlangc mlangc deleted the organize-imports-adds-opening-parenthesis-1002088 branch September 11, 2015 17:37
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants