Skip to content

Commit 2b004ee

Browse files
vderPiotr Fałdrowicz
andauthored
Draft: additional completions for using clause (#23647)
fix for #22939 --------- Co-authored-by: Piotr Fałdrowicz <[email protected]>
1 parent 8d932d0 commit 2b004ee

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

presentation-compiler/src/main/dotty/tools/pc/completions/CompletionAffix.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ case class CompletionAffix(
5656
private def loopPrefix(prefixes: List[PrefixKind]): String =
5757
prefixes match
5858
case PrefixKind.New :: tail => "new " + loopPrefix(tail)
59+
case PrefixKind.Using :: tail => "using " + loopPrefix(tail)
5960
case _ => ""
6061

6162
/**
@@ -87,7 +88,7 @@ enum SuffixKind:
8788
case Brace, Bracket, Template, NoSuffix
8889

8990
enum PrefixKind:
90-
case New
91+
case New, Using
9192

9293
type Suffix = Affix[SuffixKind]
9394
type Prefix = Affix[PrefixKind]

presentation-compiler/src/main/dotty/tools/pc/completions/Completions.scala

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,24 @@ class Completions(
193193
)
194194
end isAbstractType
195195

196-
private def findSuffix(symbol: Symbol): CompletionAffix =
196+
private def findSuffix(symbol: Symbol, adjustedPath: List[untpd.Tree]): CompletionAffix =
197197
CompletionAffix.empty
198198
.chain { suffix => // for [] suffix
199199
if shouldAddSuffix && symbol.info.typeParams.nonEmpty then
200200
suffix.withNewSuffixSnippet(Affix(SuffixKind.Bracket))
201201
else suffix
202202
}
203+
.chain{ suffix =>
204+
adjustedPath match
205+
case (ident: Ident) :: (app@Apply(_, List(arg))) :: _ =>
206+
app.symbol.info match
207+
case mt@MethodType(termNames) if app.symbol.paramSymss.last.exists(_.is(Given)) &&
208+
!text.substring(app.fun.span.start, arg.span.end).contains("using") =>
209+
suffix.withNewPrefix(Affix(PrefixKind.Using))
210+
case _ => suffix
211+
case _ => suffix
212+
213+
}
203214
.chain { suffix => // for () suffix
204215
if shouldAddSuffix && symbol.is(Flags.Method) then
205216
val paramss = getParams(symbol)
@@ -271,7 +282,7 @@ class Completions(
271282
val existsApply = extraMethodDenots.exists(_.symbol.name == nme.apply)
272283

273284
extraMethodDenots.map { methodDenot =>
274-
val suffix = findSuffix(methodDenot.symbol)
285+
val suffix = findSuffix(methodDenot.symbol, adjustedPath)
275286
val affix = if methodDenot.symbol.isConstructor && existsApply then
276287
adjustedPath match
277288
case (select @ Select(qual, _)) :: _ =>
@@ -293,7 +304,7 @@ class Completions(
293304

294305
if skipOriginalDenot then extraCompletionValues
295306
else
296-
val suffix = findSuffix(denot.symbol)
307+
val suffix = findSuffix(denot.symbol, adjustedPath)
297308
val name = undoBacktick(label)
298309
val denotCompletionValue = toCompletionValue(name, denot, suffix)
299310
denotCompletionValue :: extraCompletionValues

presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionArgSuite.scala

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,65 @@ class CompletionArgSuite extends BaseCompletionSuite:
238238
""
239239
)
240240

241+
@Test def `using` =
242+
checkEdit(
243+
s"""|def hello(using String): Unit = ???
244+
|@main def main1(): Unit =
245+
| val str = "hello"
246+
| hello(st@@)
247+
|""".stripMargin,
248+
s"""|def hello(using String): Unit = ???
249+
|@main def main1(): Unit =
250+
| val str = "hello"
251+
| hello(using str)
252+
|""".stripMargin,
253+
assertSingleItem = false)
254+
255+
@Test def `using2` =
256+
checkEdit(
257+
s"""|def hello(using String): Unit = ???
258+
|@main def main1(): Unit =
259+
| val str = "hello"
260+
| hello(using st@@)
261+
|""".stripMargin,
262+
s"""|def hello(using String): Unit = ???
263+
|@main def main1(): Unit =
264+
| val str = "hello"
265+
| hello(using str)
266+
|""".stripMargin,
267+
assertSingleItem = false)
268+
269+
@Test def `using3` =
270+
checkEdit(
271+
s"""|def hello(using String, Int): Unit = ???
272+
|@main def main1(): Unit =
273+
| val str = "hello"
274+
| val int = 4
275+
| hello(str, in@@)
276+
|""".stripMargin,
277+
s"""|def hello(using String, Int): Unit = ???
278+
|@main def main1(): Unit =
279+
| val str = "hello"
280+
| val int = 4
281+
| hello(str, int)
282+
|""".stripMargin,
283+
assertSingleItem = false)
284+
285+
@Test def `using4` =
286+
checkEdit(
287+
s"""|def hello(name: String)(using String): Unit = ???
288+
|@main def main1(): Unit =
289+
| val str = "hello"
290+
| hello("name")(str@@)
291+
|""".stripMargin,
292+
s"""|def hello(name: String)(using String): Unit = ???
293+
|@main def main1(): Unit =
294+
| val str = "hello"
295+
| hello("name")(using str)
296+
|""".stripMargin,
297+
assertSingleItem = false
298+
)
299+
241300
@Test def `default-args` =
242301
check(
243302
s"""|object Main {

0 commit comments

Comments
 (0)