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

fix: Regions based on indent #5260

Merged
merged 3 commits into from Jun 5, 2023

Conversation

jkciesluk
Copy link
Member

solves #5241
The problem was not connected to packages being in the same file, but to extensions being defined in package at the toplevel. This PR contains multiple fixes:

  1. For val/def/extension/... defined inside package, their owner is module class <filename>$package$, not the package directly, so in inverseSemanticdbSymbol, we have to look inside module classes too.
  2. Previously regions based on indent were working incorrectly if colon was not followed directly by a newline.
  3. We were not always using correct region after escaping from indented region.

Previously, we were not always changing region based on indent correctly.
Also, for vals/defs/... defined directly in package, we have to look inside module class.
@@ -64,7 +68,7 @@ object SemanticdbSymbols:
.alternatives
.iterator
.map(_.symbol)
.filter(sym => symbolName(sym) == s)
// .filter(sym => symbolName(sym) == s)
Copy link
Member Author

Choose a reason for hiding this comment

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

Turns out this line was actually needed for overloaded methods 😅
Basically, the problem is: For code like

// Filename: Main.scala
package a:
  def fooBar(x: Int) = x + 1
  def foobar(x: String) = x.length

Semanticdb contains names

a/Main$package.fooBar().
a/Main$package.fooBar(+1).

But MtagsIndexer creates names

a/fooBar().
a/fooBar(+1).

So I have to fix it in the indexer

@jkciesluk jkciesluk force-pushed the toplevel-defs-completion branch 3 times, most recently from 98fd8f7 to ae15600 Compare May 31, 2023 08:19
Copy link
Member Author

@jkciesluk jkciesluk 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 comments explaining changes

@@ -128,7 +135,8 @@ class ScalaToplevelMtags(
if (dialect.allowSignificantIndentation) {
data.token match {
case WHITESPACE | COMMENT => region
case _ => exitIndented(region, indent)
case _ =>
Copy link
Member Author

Choose a reason for hiding this comment

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

We were not always changing owner correctly when escaping indent

@@ -185,7 +192,7 @@ class ScalaToplevelMtags(
withOwner(currRegion.owner) {
term(name.name, name.pos, Kind.METHOD, EXTENSION)
}
loop(indent, isAfterNewline = false, region, newExpectIgnoreBody)
loop(indent, isAfterNewline = false, currRegion, newExpectIgnoreBody)
Copy link
Member Author

Choose a reason for hiding this comment

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

We should always use currRegion instead of region, to account for regions based on indent

override def isExtension = extension

override val withTermOwner: String => InBrace = termOwner =>
Copy link
Member Author

Choose a reason for hiding this comment

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

Previously, withTermOwner(newOwner) had no effect, which was very confusing

def acceptMembers: Boolean =
owner.endsWith("/")
val produceSourceToplevel: Boolean = false
val produceSourceToplevel: Boolean = termOwner.isPackage
Copy link
Member Author

Choose a reason for hiding this comment

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

After adding <filename>$package, termOwner is no longer a package, so this suffix is not added again, and its being removed when leaving this package. Previously this logic was based on global mutable variable, which wasn't changed after escaping a package

@@ -87,8 +87,8 @@ trait PCSuite {

private def addSourceToIndex(filename: String, code2: String): Unit = {
val file = tmp.resolve(filename)
Files.createDirectories(file.toNIO.getParent)
Copy link
Member Author

Choose a reason for hiding this comment

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

For test CompletionFilenameSuite.paths

Copy link
Contributor

@kasiaMarek kasiaMarek 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 awesome, it seems you caught a few issues there.

I think that it could be also useful to add an analogue of ToplevelSuite for Mtags.toplevels (not in this PR but in general), to see if any of those cases are not handled properly.

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.

Great work! 🚀

|package b:
| def main: Unit = incre@@
|""".stripMargin,
"""|increment3: Int
Copy link
Contributor

Choose a reason for hiding this comment

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

Any dea why this isn't ordered? Not important for this PR though

Copy link
Member Author

Choose a reason for hiding this comment

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

increment3 is a val, the other two are defs, I think this causes this ordering

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.

Great work! 🚀

@tgodzik tgodzik merged commit 2cd41a6 into scalameta:main Jun 5, 2023
21 of 23 checks passed
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.

None yet

3 participants