Skip to content
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

Find References searches also for base method occurrences #1388

Closed

Conversation

kpbochenek
Copy link
Contributor

@kpbochenek kpbochenek commented Feb 5, 2020

Fixes #1063

According to the discussion in related issue it would be good to provide references of overridden / super methods when searching for references of given method.

Example code:

object Example {

  class A { def fx(): Unit = () }
  class B extends A { override def fx(): Unit = () }
  class C extends B { override def fx(): Unit = () }
  class D extends C
  class E extends D { override def fx(): Unit = () }

  class X { def fx(): Unit = () }

  val a = new A()
  a.fx()

  val b = new B();
  b.fx()

  val c = new C();
  c.fx()

  val d = new D();
  d.fx()

  val e = new E();
  e.fx()
  
}

With current behaviour when we search for a references to 'b.fx()' we will get 2 results:
2020-02-06-090121_290x107_scrot

With a new behaviour searching references to the same function we will get same results plus references to overridden methods (from superclasses and subclasses):
2020-02-06-090224_283x264_scrot

Detailed changes:

  • fix typos (symbolOccurence -> symbolOccurrence, ineheritance -> inheritance)
  • change functions to need only position and not full Params object if operate only on position
  • get rid of using TextParams API object internally in favor of PositionInFile
  • add referencesSync (only for tests) to not have to deal with async behaviour in tests.
  • move functionality of searching for all subclasses / superclasses from RenameProvider -> ReferenceProvider and put it into allInheritanceReferences function
  • reuse this function in searching for references and renaming.
  • test checking mentioned functionality: https://github.com/scalameta/metals/pull/1388/files#diff-bd990ebb68b0756f1af9b16a82841ef8R216
  • reference results are sorted ONLY in tests to provide deterministic results.
  • fix searching for parent class when parent class exists in not currently opened file.

Here (https://github.com/scalameta/metals/pull/1388/files#diff-2c60d9818336621db1490d63594edff5R151) I had to add declaration to expected results, because in tests server.references(...) creates underneath new ReferenceContext(true) (where true is for includeDeclaration) which indicates declaration should be added to results.

Finding references is not following inheritance tree in case of functions defined in methodsSearchedWithoutInheritance (e.g. toString, equals, hashCode, clone)
2020-02-06-170525_527x179_scrot

@kpbochenek kpbochenek requested a review from tgodzik February 5, 2020 11:34
@kpbochenek kpbochenek force-pushed the references-with-base-method branch from 34e387d to 87330da Compare February 7, 2020 12:38
@kpbochenek kpbochenek marked this pull request as ready for review February 7, 2020 13:04
Copy link
Contributor

@tgodzik tgodzik left a comment

Choose a reason for hiding this comment

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

Looks to be on a good path! Thanks a lot for the great work!

I had some comments though.

@kpbochenek kpbochenek force-pushed the references-with-base-method branch from b0f9eb8 to c44f65d Compare February 11, 2020 14:50
@tgodzik
Copy link
Contributor

tgodzik commented Feb 12, 2020

@olafurpg It's ready to review, let me know what you think. :)

Copy link
Contributor

@tgodzik tgodzik left a comment

Choose a reason for hiding this comment

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

Looking closer to finished! Added some comments, mostly I think nitpicks.

Copy link
Contributor

@tgodzik tgodzik left a comment

Choose a reason for hiding this comment

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

I added some more small nitpicks and I also tested it myself on ZIO codebase. It seems to work reasonably well aside from a small issue. References results seem to get duplicated:

image

I am also worried a bit about performance of any symbols in a class extending a class from outside the workspace. First search will need to load up the classpath information, which might take a while. This might not be a big issue, but I just wanted to raise it here. I am thinking, we can just not look the entire inheritance path in case of find references. So in essence we would only look up and down the inheritance chain inside the workspace. Opinions?

@olafurpg
Copy link
Member

@tgodzik is the performance penalty for every request (even for non overridden methods) or only for requests on overridden methods?

@olafurpg
Copy link
Member

Currently the performance of references has not been an issue (it’s really fast) whereas missing results (and occasionally incorrect results) has been a bigger issue in my experience.

@tgodzik
Copy link
Contributor

tgodzik commented Feb 19, 2020

Currently the performance of references has not been an issue (it’s really fast) whereas missing results (and occasionally incorrect results) has been a bigger issue in my experience.

Ok, I think we can optimize is later if needed and rename already requires loading classpath information. However, now that I think about it. Do we actually look correctly for methods coming from a dependency? Can we add a test case?

Copy link
Member

@olafurpg olafurpg left a comment

Choose a reason for hiding this comment

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

This is a fantastic contribution @kpbochenek! LGTM 👍

Copy link
Contributor

@tgodzik tgodzik left a comment

Choose a reason for hiding this comment

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

Just a few more comments, almost ready!

@kpbochenek kpbochenek force-pushed the references-with-base-method branch from 81da8a9 to dc21706 Compare February 24, 2020 14:38
@tgodzik
Copy link
Contributor

tgodzik commented Feb 25, 2020

We need to block this PR until #1287 is solved. We don't have an easy way to detect inheritance inside the dependencies and I am afraid this will cause find references less responsive. This can be done either by using classgraph or by our custom solution similar to searching for top levels.

TLDR: We need a faster and less memory heavy way to check inheritance in dependencies.

@tgodzik tgodzik closed this Feb 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Offer to 'Find all references' on overridden method
3 participants