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

Gen return types for single expressions #66

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

kiritsuku
Copy link
Member

Some bugfixes that were made visible through scala-ide/scala-ide#761

Review on Reviewable

@ghprb-bot
Copy link

Test PASSed.

See Console Output in the link below for an update site containing this PR binary artefacts.

Refer to this link for build results: https://jenkins.scala-ide.org:8496/jenkins/job/ghprb-scala-refactoring-validator/41/

@dragos
Copy link
Member

dragos commented Oct 6, 2014

LGTM, but it wouldn't hurt to have @misto or @huitseeker have a look. I'm not very knowledgeable of refactoring code.

@huitseeker
Copy link
Member

See my comment on the original bug-report

@dragos
Copy link
Member

dragos commented Oct 6, 2014

@huitseeker, since you mentioned the quick fix... that one uses the Scala parser and tokenizer to find the insertion point.

@misto
Copy link
Member

misto commented Oct 7, 2014

FYI, there are some helper methods in the CommentsUtils object that strip comments from source code.

@huitseeker
Copy link
Member

@misto Comments aren't the problem. Save actions should use the quickkfix's way of dealing with this.

@misto
Copy link
Member

misto commented Oct 7, 2014

@huitseeker I know, I just wanted to have mentioned it because it's sometimes useful to be able to strip comments.

@kiritsuku
Copy link
Member Author

About the save action that is the origin of this PR: I already need scala-refactoring to find definitions where a type can be added - continue using scala-refactoring by adding the return type is just logical. And the quick fix logic is also not bulletproof - moving to it for the transformation wouldn't even be an advantage.

@huitseeker
Copy link
Member

@sschaef : did you report the holes you found against the quick fix ?

I just thought integrating the quickfix logic can be a temporary solution for the Save action if it grants better correctness than refactoring currently allows.

In case

    def foo = {
      0
    }

should be transformed to

    def foo: Int = {
      0
    }

by adding its return type, the result was

    def foo: Int = 0
    }

Fixes #1002268
I would prefer to fix this somewhere in the printing logic and not in
the construction logic of the tree but I didn't find a way to
differentiate the Ident trees correctly in the printer. For

  object X { def x = 0 }

the Ident trees of `X.type` and `X.x` seem to have the same shape at the
beginning.

Fixes #1002233
Fixing this was complicated because the compiler removes braces from the
tree when the code would compile without them. Therefore we manually
have to restore them.

To get the braces (and whitespace) back, we search for an equal sign
in the area of the DefDef (which indicates the start of the rhs). Once
it is found, all contents between the equal sign and the start and the
expression of the rhs are put back into the tree.

Because an equal sign can also occur in a comment, we have to parse the
region instead of simply looking for the equal sign. This adds some
overhead, but hopefully not too much.

Furthermore, as reviewers suggested, some variable names are renamed.

This also fixes a bug in the test suite where the source file is passed
to the refactoring logic.
@kiritsuku kiritsuku force-pushed the gen-return-types-for-single-expressions branch from 9fde188 to 98c0a55 Compare February 12, 2015 15:54
@kiritsuku
Copy link
Member Author

I finally updated this PR. Sorry for the long delay! The last commit is new, it took me only a day to create it. Please review this commit carefully, it introduces a token cache to PrintingContext and I have no idea if this is too much overhead or if it ok.

@kiritsuku
Copy link
Member Author

The last commit breaks source compatibility with the IDE and probably other repos as well (because CompilerApiExtensions is added to the self types). I guess this is a problem, but first we need to find out if adding the token cache is worth it.

@misto
Copy link
Member

misto commented Feb 12, 2015

Thank you! I'll review it over the weekend. Are you sure the additional self type is actually a problem? If they just use the SourceGenerator - which they should - then it should be fine, right?

@kiritsuku
Copy link
Member Author

In the IDE there is CompilerSupport, which uses the *Printer traits. Maybe these traits shouldn't be used outside of scala-refactoring?

@misto
Copy link
Member

misto commented Feb 15, 2015

Oh yes, CompilerSupport. I wonder if we shouldn't try to merge this back with scala-refactoring since it contains some duplicated code. But to keep the source compatible, if we just let the AbstractPrinter extend CompilerApiExtensions instead of declaring it in the self type?

@fommil
Copy link
Contributor

fommil commented Feb 15, 2016

@sschaef I just noticed you have a bunch of outstanding PRs against this repo. Are they rotten or worth saving?

@kiritsuku
Copy link
Member Author

I should invest some time to finish these changes. I think it is best to keep the PRs open for now, even though I don't have time (or better interest) currently to fix it. It is not that they do any harm.

@ghprb-bot
Copy link

Test FAILed.

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.

6 participants