-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add save action that adds missing override keywords
Only works with defs and types, but not yet with vals.
- Loading branch information
Showing
3 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
...ore/src/org/scalaide/core/internal/extensions/saveactions/AddMissingOverrideCreator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.scalaide.core.internal.extensions.saveactions | ||
|
||
import org.scalaide.core.compiler.ScalaPresentationCompiler | ||
import scala.reflect.internal.util.SourceFile | ||
import org.scalaide.extensions.CompilerSupport | ||
import org.scalaide.extensions.saveactions.AddMissingOverride | ||
|
||
object AddMissingOverrideCreator { | ||
def create( | ||
c: ScalaPresentationCompiler, | ||
t: ScalaPresentationCompiler#Tree, | ||
sf: SourceFile, | ||
selectionStart: Int, | ||
selectionEnd: Int): AddMissingOverride = | ||
new AddMissingOverride { | ||
override val global = c | ||
override val sourceFile = sf | ||
override val selection = new FileSelection( | ||
sf.file, t.asInstanceOf[global.Tree], selectionStart, selectionEnd) | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
org.scala-ide.sdt.core/src/org/scalaide/extensions/saveactions/AddMissingOverride.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.scalaide.extensions | ||
package saveactions | ||
|
||
object AddMissingOverrideSetting extends SaveActionSetting( | ||
id = ExtensionSetting.fullyQualifiedName[AddMissingOverride], | ||
name = "Add missing override keyword", | ||
description = "Adds the override keyword to all symbols that override another symbol.", | ||
codeExample = """|trait T { | ||
| def method: Int | ||
| def value: Int | ||
| type Type | ||
|} | ||
|class C extends T { | ||
| def method = 0 | ||
| def value = 0 | ||
| type Type = Int | ||
|} | ||
|""".stripMargin | ||
) | ||
|
||
/** | ||
* This save action is not yet completed. It does't add the `override` keyword | ||
* to vals, only to defs and types. | ||
*/ | ||
trait AddMissingOverride extends SaveAction with CompilerSupport { | ||
import global._ | ||
|
||
def setting = AddMissingOverrideSetting | ||
|
||
def perform() = { | ||
val symbolWithoutOverride = filter { | ||
case d @ (_: DefDef | _: TypeDef) => | ||
d.symbol.isOverridingSymbol && !d.symbol.isOverride | ||
} | ||
|
||
/** | ||
* Alternative (preferred) implementation: | ||
* {{{ | ||
* mods.copy().withPosition(Flag.OVERRIDE, pos) | ||
* }}} | ||
* The above does not work due to a limitation in scala-refactoring. | ||
*/ | ||
def modsWithOverrideKeyword(mods: Modifiers, pos: Position): Modifiers = | ||
mods.copy() setPositions Map(Flag.OVERRIDE -> pos) ++ mods.positions | ||
|
||
val addOverrideKeyword = transform { | ||
case d: DefDef => | ||
d.mods.copy() withPosition(Flag.OVERRIDE, d.pos) | ||
val mods = modsWithOverrideKeyword(d.mods, d.pos) | ||
d.copy(mods = mods | Flag.OVERRIDE) replaces d | ||
|
||
case d: TypeDef => | ||
val mods = modsWithOverrideKeyword(d.mods, d.pos) | ||
d.copy(mods = mods | Flag.OVERRIDE) replaces d | ||
} | ||
|
||
val refactoring = topdown { | ||
matchingChildren { | ||
symbolWithoutOverride &> addOverrideKeyword | ||
} | ||
} | ||
transformFile(refactoring) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters