Skip to content

Commit c533443

Browse files
authored
Print parens for single method argument only if a direct tuple type (#21510)
A type alias of a tuple type should be printed without parenthesis. Closes scalameta/metals#6613
2 parents afb4806 + 57e2ef0 commit c533443

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

Diff for: compiler/src/dotty/tools/dotc/core/Definitions.scala

+14-8
Original file line numberDiff line numberDiff line change
@@ -1806,18 +1806,24 @@ class Definitions {
18061806
|| (sym eq Any_typeTest)
18071807
|| (sym eq Any_typeCast)
18081808

1809-
/** Is this type a `TupleN` type?
1809+
/** Is `tp` a `TupleN` type?
1810+
*
1811+
* @return true if the type of `tp` is `TupleN[T1, T2, ..., Tn]`
1812+
*/
1813+
def isDirectTupleNType(tp: Type)(using Context): Boolean =
1814+
val arity = tp.argInfos.length
1815+
arity <= MaxTupleArity && {
1816+
val tupletp = TupleType(arity)
1817+
tupletp != null && tp.isRef(tupletp.symbol)
1818+
}
1819+
1820+
/** Is `tp` (an alias of) a `TupleN` type?
18101821
*
18111822
* @return true if the dealiased type of `tp` is `TupleN[T1, T2, ..., Tn]`
18121823
*/
1813-
def isTupleNType(tp: Type)(using Context): Boolean = {
1824+
def isTupleNType(tp: Type)(using Context): Boolean =
18141825
val tp1 = tp.dealias
1815-
val arity = tp1.argInfos.length
1816-
arity <= MaxTupleArity && {
1817-
val tupletp = TupleType(arity)
1818-
tupletp != null && tp1.isRef(tupletp.symbol)
1819-
}
1820-
}
1826+
isDirectTupleNType(tp1)
18211827

18221828
def tupleType(elems: List[Type]): Type = {
18231829
val arity = elems.length

Diff for: compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
168168
changePrec(GlobalPrec) {
169169
val argStr: Text =
170170
if args.length == 2
171-
&& !defn.isTupleNType(args.head)
171+
&& !defn.isDirectTupleNType(args.head)
172172
&& !isGiven
173173
then
174174
atPrec(InfixPrec) { argText(args.head) }

Diff for: presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala

+44
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,50 @@ class CompletionSuite extends BaseCompletionSuite:
147147
"XtensionMethod(a: Int): XtensionMethod"
148148
)
149149

150+
@Test def tupleDirect =
151+
check(
152+
"""
153+
|trait Foo {
154+
| def setup: List[(String, String)]
155+
|}
156+
|object Foo {
157+
| val foo: Foo = ???
158+
| foo.setup.exist@@
159+
|}""".stripMargin,
160+
"""|exists(p: ((String, String)) => Boolean): Boolean
161+
|""".stripMargin
162+
)
163+
164+
@Test def tupleAlias =
165+
check(
166+
"""
167+
|trait Foo {
168+
| def setup: List[Foo.TupleAliasResult]
169+
|}
170+
|object Foo {
171+
| type TupleAliasResult = (String, String)
172+
| val foo: Foo = ???
173+
| foo.setup.exist@@
174+
|}""".stripMargin,
175+
"""|exists(p: TupleAliasResult => Boolean): Boolean
176+
|""".stripMargin
177+
)
178+
179+
@Test def listAlias =
180+
check(
181+
"""
182+
|trait Foo {
183+
| def setup: List[Foo.ListAliasResult]
184+
|}
185+
|object Foo {
186+
| type ListAliasResult = List[String]
187+
| val foo: Foo = ???
188+
| foo.setup.exist@@
189+
|}""".stripMargin,
190+
"""|exists(p: ListAliasResult => Boolean): Boolean
191+
|""".stripMargin
192+
)
193+
150194
@Ignore("This test should be handled by compiler fuzzy search")
151195
@Test def fuzzy =
152196
check(

0 commit comments

Comments
 (0)