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: completions when parenthesis already provided #21299

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ class Completions(

private lazy val shouldAddSnippet =
path match
case (_: (Import | Export)) :: _ => false
case _ :: (_: (Import | Export)) :: _ => false
// UnApply has patterns included in MatchCaseCompletions
case _ :: (_: UnApply) :: _ => false
case _ => true

private lazy val shouldAddSuffix = shouldAddSnippet &&
(path match
/* In case of `method@@()` we should not add snippets and the path
* will contain apply as the parent of the current tree.
*/
Expand All @@ -72,11 +80,8 @@ class Completions(
case _ :: (withcursor @ Select(fun, name)) :: (appl: GenericApply) :: _
if appl.fun == withcursor && name.decoded == Cursor.value =>
false
case (_: (Import | Export)) :: _ => false
case _ :: (_: (Import | Export)) :: _ => false
// UnApply has patterns included in MatchCaseCompletions
case _ :: (_: UnApply) :: _ => false
case _ => true
case _ => true)


private lazy val isNew: Boolean = Completion.isInNewContext(adjustedPath)

Expand Down Expand Up @@ -198,12 +203,12 @@ class Completions(
private def findSuffix(symbol: Symbol): CompletionAffix =
CompletionAffix.empty
.chain { suffix => // for [] suffix
if shouldAddSnippet && symbol.info.typeParams.nonEmpty then
if shouldAddSuffix && symbol.info.typeParams.nonEmpty then
suffix.withNewSuffixSnippet(Affix(SuffixKind.Bracket))
else suffix
}
.chain { suffix => // for () suffix
if shouldAddSnippet && symbol.is(Flags.Method) then
if shouldAddSuffix && symbol.is(Flags.Method) then
val paramss = getParams(symbol)
paramss match
case Nil => suffix
Expand All @@ -224,7 +229,7 @@ class Completions(
else suffix
}
.chain { suffix => // for {} suffix
if shouldAddSnippet && isNew && isAbstractType(symbol) then
if shouldAddSuffix && isNew && isAbstractType(symbol) then
if suffix.hasSnippet then suffix.withNewSuffix(Affix(SuffixKind.Template))
else suffix.withNewSuffixSnippet(Affix(SuffixKind.Template))
else suffix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ class CompletionSnippetSuite extends BaseCompletionSuite:
|}
|""".stripMargin,
"scala.util.Try@@(1)",
"scala.util.Try(1)"
"scala.util.Try(1)",
assertSingleItem = false
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need the change here, what do we get additionally?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will now both offer Try() as apply (insert text will be Try) and Try as module (insert text also Try).

)

@Test def `case-class` =
Expand All @@ -300,7 +301,8 @@ class CompletionSnippetSuite extends BaseCompletionSuite:
|""".stripMargin,
"scala.util.Tr@@(1)",
"scala.util.Try(1)",
filter = str => str.contains("Try")
filter = str => str.contains("Try"),
assertSingleItem = false
)

@Test def `case-class2` =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2031,3 +2031,14 @@ class CompletionSuite extends BaseCompletionSuite:
""".stripMargin,
filter = _.contains("name")
)

@Test def `with-parenthesis` =
check(
"""|package a
|class MyClass
|val i = MyClass@@()
|""".stripMargin,
"""|MyClass(): MyClass (Constructor)
|""".stripMargin,
includeCompletionKind = true
)
Loading