-
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 "AddReturnTypeToPublicSymbols" save action
- Loading branch information
Showing
5 changed files
with
259 additions
and
3 deletions.
There are no files selected for viewing
175 changes: 175 additions & 0 deletions
175
...core.tests/src/org/scalaide/extensions/saveactions/AddReturnTypeToPublicSymbolsTest.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,175 @@ | ||
package org.scalaide.extensions.saveactions | ||
|
||
import org.scalaide.core.compiler.ScalaPresentationCompiler | ||
import scala.reflect.internal.util.SourceFile | ||
import org.junit.Test | ||
|
||
object AddReturnTypeToPublicSymbolsTest extends CompilerSaveActionTests { | ||
override def saveAction(spc: ScalaPresentationCompiler, tree: ScalaPresentationCompiler#Tree, sf: SourceFile, selectionStart: Int, selectionEnd: Int) = | ||
new AddReturnTypeToPublicSymbols { | ||
override val global = spc | ||
override val sourceFile = sf | ||
override val selection = new FileSelection( | ||
sf.file, tree.asInstanceOf[global.Tree], selectionStart, selectionEnd) | ||
} | ||
} | ||
|
||
class AddReturnTypeToPublicSymbolsTest { | ||
import AddReturnTypeToPublicSymbolsTest._ | ||
|
||
@Test | ||
def add_no_return_type_if_it_already_exists() = """^ | ||
class X { | ||
def meth: java.io.File = new java.io.File("") | ||
val value: java.io.File = new java.io.File("") | ||
var value: java.io.File = new java.io.File("") | ||
} | ||
""" becomes """^ | ||
class X { | ||
def meth: java.io.File = new java.io.File("") | ||
val value: java.io.File = new java.io.File("") | ||
var value: java.io.File = new java.io.File("") | ||
} | ||
""" after SaveEvent | ||
|
||
@Test | ||
def add_no_return_type_to_private_def() = """^ | ||
class X { | ||
private def meth = new java.io.File("") | ||
} | ||
""" becomes """^ | ||
class X { | ||
private def meth = new java.io.File("") | ||
} | ||
""" after SaveEvent | ||
|
||
@Test | ||
def add_no_return_type_to_private_val() = """^ | ||
class X { | ||
private val value = new java.io.File("") | ||
} | ||
""" becomes """^ | ||
class X { | ||
private val value = new java.io.File("") | ||
} | ||
""" after SaveEvent | ||
|
||
@Test | ||
def add_no_return_type_to_private_var() = """^ | ||
class X { | ||
private var variable = new java.io.File("") | ||
} | ||
""" becomes """^ | ||
class X { | ||
private var variable = new java.io.File("") | ||
} | ||
""" after SaveEvent | ||
|
||
@Test | ||
def add_no_return_type_to_nested_def() = """^ | ||
class X { | ||
def meth: java.io.File = { | ||
def m = new java.io.File("") | ||
m | ||
} | ||
val value: java.io.File = { | ||
def m = new java.io.File("") | ||
m | ||
} | ||
} | ||
""" becomes """^ | ||
class X { | ||
def meth: java.io.File = { | ||
def m = new java.io.File("") | ||
m | ||
} | ||
val value: java.io.File = { | ||
def m = new java.io.File("") | ||
m | ||
} | ||
} | ||
""" after SaveEvent | ||
|
||
@Test | ||
def add_no_return_type_to_nested_val() = """^ | ||
class X { | ||
val value: java.io.File = { | ||
val m = new java.io.File("") | ||
m | ||
} | ||
def meth: java.io.File = { | ||
def m = new java.io.File("") | ||
m | ||
} | ||
} | ||
""" becomes """^ | ||
class X { | ||
val value: java.io.File = { | ||
val m = new java.io.File("") | ||
m | ||
} | ||
def meth: java.io.File = { | ||
def m = new java.io.File("") | ||
m | ||
} | ||
} | ||
""" after SaveEvent | ||
|
||
@Test | ||
def add_no_return_type_to_nested_var() = """^ | ||
class X { | ||
var variable: java.io.File = { | ||
var m = new java.io.File("") | ||
m | ||
} | ||
def meth: java.io.File = { | ||
def m = new java.io.File("") | ||
m | ||
} | ||
} | ||
""" becomes """^ | ||
class X { | ||
var variable: java.io.File = { | ||
var m = new java.io.File("") | ||
m | ||
} | ||
def meth: java.io.File = { | ||
def m = new java.io.File("") | ||
m | ||
} | ||
} | ||
""" after SaveEvent | ||
|
||
@Test | ||
def add_return_type_to_public_def() = """^ | ||
class X { | ||
def meth = new java.io.File("") | ||
} | ||
""" becomes """^ | ||
class X { | ||
def meth: java.io.File = new java.io.File("") | ||
} | ||
""" after SaveEvent | ||
|
||
@Test | ||
def add_return_type_to_public_val() = """^ | ||
class X { | ||
val value = new java.io.File("") | ||
} | ||
""" becomes """^ | ||
class X { | ||
val value: java.io.File = new java.io.File("") | ||
} | ||
""" after SaveEvent | ||
|
||
@Test | ||
def add_return_type_to_public_var() = """^ | ||
class X { | ||
val variable = new java.io.File("") | ||
} | ||
""" becomes """^ | ||
class X { | ||
val variable: java.io.File = new java.io.File("") | ||
} | ||
""" after SaveEvent | ||
} |
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
21 changes: 21 additions & 0 deletions
21
...g/scalaide/core/internal/extensions/saveactions/AddReturnTypeToPublicSymbolsCreator.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 scala.reflect.internal.util.SourceFile | ||
|
||
import org.scalaide.core.compiler.ScalaPresentationCompiler | ||
import org.scalaide.extensions.saveactions.AddReturnTypeToPublicSymbols | ||
|
||
object AddReturnTypeToPublicSymbolsCreator { | ||
def create( | ||
c: ScalaPresentationCompiler, | ||
t: ScalaPresentationCompiler#Tree, | ||
sf: SourceFile, | ||
selectionStart: Int, | ||
selectionEnd: Int): AddReturnTypeToPublicSymbols = | ||
new AddReturnTypeToPublicSymbols { | ||
override val global = c | ||
override val sourceFile = sf | ||
override val selection = new FileSelection( | ||
sf.file, t.asInstanceOf[global.Tree], selectionStart, selectionEnd) | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...a-ide.sdt.core/src/org/scalaide/extensions/saveactions/AddReturnTypeToPublicSymbols.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,55 @@ | ||
package org.scalaide.extensions | ||
package saveactions | ||
|
||
object AddReturnTypeToPublicSymbolsSetting extends SaveActionSetting( | ||
id = ExtensionSetting.fullyQualifiedName[AddReturnTypeToPublicSymbols], | ||
name = "Add return type to public symbols", | ||
description = | ||
"Adds the return type to all public symbols when they not yet exist." + | ||
" The symbols that can be public and can have types are defs, vars and vals.", | ||
codeExample = """|class X { | ||
| def meth = new java.io.File("") | ||
| val value = new java.io.File("") | ||
| var value = new java.io.File("") | ||
|} | ||
|""".stripMargin | ||
) | ||
|
||
trait AddReturnTypeToPublicSymbols extends SaveAction with CompilerSupport { | ||
import global._ | ||
|
||
override def setting = AddReturnTypeToPublicSymbolsSetting | ||
|
||
override def perform() = { | ||
val symbolWithoutReturnType = filter { | ||
case d @ ValOrDefDef(_, _, tpt: TypeTree, _) => | ||
val o = d.symbol.owner | ||
if (o.isMethod || o.isValue) | ||
false | ||
else | ||
d match { | ||
case d: DefDef => | ||
d.symbol.isPublic && !d.symbol.isSynthetic && !d.symbol.isAccessor && tpt.original == null | ||
case d: ValDef => | ||
val getter = d.symbol.getterIn(o) | ||
getter.isPublic && tpt.original == null | ||
} | ||
} | ||
|
||
val addReturnType = transform { | ||
case d @ ValOrDefDef(_, _, tpt: TypeTree, _) => | ||
val newTpt = tpt setOriginal mkReturn(List(tpt.tpe.typeSymbol)) | ||
d match { | ||
case d: DefDef => d.copy(tpt = newTpt) replaces d | ||
case d: ValDef => d.copy(tpt = newTpt) replaces d | ||
} | ||
} | ||
|
||
val refactoring = topdown { | ||
matchingChildren { | ||
symbolWithoutReturnType &> addReturnType | ||
} | ||
} | ||
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