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

Unify completion pos usage, fix presentation compiler crash in interpolation #19614

Merged
merged 9 commits into from
Feb 29, 2024

Conversation

rochala
Copy link
Contributor

@rochala rochala commented Feb 5, 2024

Fixes crash for added tests in CompletionInterpolatorSuite along with unification and better naming for completion pos, source pos, and span usage.

offsetParams: OffsetParams,
adjustedPath: List[Tree]
)(using Context): CompletionPos =
infer(cursorPos, offsetParams.uri().nn, offsetParams.text().nn, adjustedPath)
infer(sourcePosition, offsetParams.uri().nn, String(sourcePosition.source.content()), adjustedPath)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sourcePosition contains artificial "CURSOR", offsetparams.text() is plain sourcecode

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually looks like text is unused and we should remove it

infer(cursorPos, offsetParams.uri().nn, offsetParams.text().nn, adjustedPath)
val identEnd = adjustedPath match
case (ident: Ident) :: _ if ident.toString.contains(Cursor.value) =>
ident.span.end - Cursor.value.length
Copy link
Contributor

Choose a reason for hiding this comment

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

What about e.g. Select? Wouldn't you have to consider all possible trees for this to work this way?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

RefTree.name should handle all the cases

Copy link
Contributor

Choose a reason for hiding this comment

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

including import, export?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually yes. I think that it is impossible for pathTo to return ImportSelector or ExportSelector as head as there are only 2 cases:

  • we do normal import, and this Identifier has the same span as this ImportSelector
  • we do a rename, which has another Identifier that will be matched in pathTo if we are inside it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unless it is empty ? Maybe a test like this will ensure its correctness.

@@ -107,7 +106,7 @@ class Completions(
end includeSymbol

def completions(): (List[CompletionValue], SymbolSearch.Result) =
val (advanced, exclusive) = advancedCompletions(path, pos, completionPos)
val (advanced, exclusive) = advancedCompletions(path, completionPos.originalCursorPosition, completionPos)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
val (advanced, exclusive) = advancedCompletions(path, completionPos.originalCursorPosition, completionPos)
val (advanced, exclusive) = advancedCompletions(path, completionPos)

And to avoid too many changes in advancedCompletions just do:

val pos = completionPos.originalCursorPosition

case untpd.Ident(_) :: untpd.Import(_, _) :: _ => Mode.ImportOrExport
case untpd.Ident(_) :: (_: untpd.ImportSelector) :: _ => Mode.ImportOrExport
case untpd.Literal(Constants.Constant(_: String)) :: _ => Mode.Term // literal completions
case untpd.Ident(_) :: (_: untpd.ImportSelector) :: _ => Mode.ImportOrExport // import scala.@@
Copy link
Contributor

Choose a reason for hiding this comment

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

possibly also should be member?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I know member completions are fetched from symbol search. They are also source of extension methods outside the scope. ain this scenario we can only complete with direct members

@@ -211,7 +194,6 @@ object Completion:
case tpd.Select(qual, _) :: _ if qual.typeOpt.hasSimpleKind => completer.selectionCompletions(qual)
case tpd.Select(qual, _) :: _ => Map.empty
case (tree: tpd.ImportOrExport) :: _ => completer.directMemberCompletions(tree.expr)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this case needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, those are typed Trees, and this is the head of the path. We need it to find package members

|unshared - scala.annotation.internal
|unspecialized - scala.annotation""".stripMargin,
topLines = Some(5)
"",
Copy link
Contributor

Choose a reason for hiding this comment

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

why did this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

these are illegal completions at this position, it expects the name. On the other hand you can add annotations in this position. I'll add tests that ensure that they are present if we start a query with @

@rochala
Copy link
Contributor Author

rochala commented Feb 15, 2024

I also removed 2 files: Custom completions and its Tests. They were added to the compiler for ammonite, but after all they were also never used. They failed the tests, and the reason behind it that the file hasn't been updated for 3 years, and getting it up to date is too much costs / not worth.

Copy link
Contributor

@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.

They were added to the compiler for ammonite, but after all they were also never used. They failed the tests, and the reason behind it that the file hasn't been updated for 3 years, and getting it up to date is too much costs / not worth.

We also have AmmoniteIvyCompletions now, which should do the same thing.


case (sel: untpd.ImportSelector) :: _ =>
if sel.imported.span.contains(pos.span) then Mode.ImportOrExport
Copy link
Contributor

Choose a reason for hiding this comment

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

Why remove this check?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually I have to revert it. I was tricked by our tests, as they don't enter completions in this position for some reason. We should probably also add check to not compute any completions when mode is None...

@rochala
Copy link
Contributor Author

rochala commented Feb 19, 2024

They were added to the compiler for ammonite, but after all they were also never used. They failed the tests, and the reason behind it that the file hasn't been updated for 3 years, and getting it up to date is too much costs / not worth.

We also have AmmoniteIvyCompletions now, which should do the same thing.

This is actually a bit different. They planned to use this in repl completions.

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.

lgtm

@rochala rochala merged commit 6b52789 into scala:main Feb 29, 2024
19 checks passed
@Kordyjan Kordyjan added this to the 3.4.2 milestone Mar 28, 2024
@tgodzik tgodzik added the area:presentation-compiler Related to the presentation compiler module used by Metals and possibly other tools label May 8, 2024
WojciechMazur pushed a commit that referenced this pull request Jul 2, 2024
…olation (#19614)

Fixes crash for added tests in `CompletionInterpolatorSuite` along with
unification and better naming for completion pos, source pos, and span
usage.
[Cherry-picked 6b52789]
WojciechMazur added a commit that referenced this pull request Jul 3, 2024
… in interpolation" to LTS (#20964)

Backports #19614 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:presentation-compiler Related to the presentation compiler module used by Metals and possibly other tools
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants