Skip to content

Commit 0a1665f

Browse files
committed
feat: add in an action to remove duplicated modifiers
1 parent 0de1f0a commit 0a1665f

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3028,7 +3028,7 @@ object Parsers {
30283028
val name = in.name
30293029
val mod = atSpan(in.skipToken()) { modOfToken(tok, name) }
30303030

3031-
if (mods.isOneOf(mod.flags)) syntaxError(RepeatedModifier(mod.flags.flagsString))
3031+
if (mods.isOneOf(mod.flags)) syntaxError(RepeatedModifier(mod.flags.flagsString, source, mod.span))
30323032
addMod(mods, mod)
30333033
}
30343034

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import dotty.tools.dotc.rewrites.Rewrites.ActionPatch
3333
import dotty.tools.dotc.util.Spans.Span
3434
import dotty.tools.dotc.util.SourcePosition
3535
import scala.jdk.CollectionConverters.*
36+
import dotty.tools.dotc.util.SourceFile
3637

3738
/** Messages
3839
* ========
@@ -497,7 +498,7 @@ extends SyntaxMsg(ObjectMayNotHaveSelfTypeID) {
497498
}
498499
}
499500

500-
class RepeatedModifier(modifier: String)(implicit ctx:Context)
501+
class RepeatedModifier(modifier: String, source: SourceFile, span: Span)(implicit ctx:Context)
501502
extends SyntaxMsg(RepeatedModifierID) {
502503
def msg(using Context) = i"""Repeated modifier $modifier"""
503504

@@ -516,6 +517,16 @@ extends SyntaxMsg(RepeatedModifierID) {
516517
|
517518
|"""
518519
}
520+
521+
override def actions(using Context) =
522+
List(
523+
CodeAction(title = s"""Remove repeated modifier: "$modifier"""",
524+
description = java.util.Optional.empty(),
525+
patches = List(
526+
ActionPatch(SourcePosition(source, span), "")
527+
).asJava
528+
)
529+
).asJava
519530
}
520531

521532
class InterpolatedStringError()(implicit ctx:Context)

compiler/test/dotty/tools/dotc/reporting/CodeActionTest.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ class CodeActionTest extends DottyTest:
4444

4545
)
4646

47+
@Test def removeRepeatModifier =
48+
checkCodeAction(
49+
"""|final final class Test
50+
|""".stripMargin,
51+
"""Remove repeated modifier: "final"""",
52+
// TODO look into trying to remove the extra space that is left behind
53+
"""|final class Test
54+
|""".stripMargin
55+
56+
)
57+
4758
// Make sure we're not using the default reporter, which is the ConsoleReporter,
4859
// meaning they will get reported in the test run and that's it.
4960
private def newContext =

0 commit comments

Comments
 (0)