Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Feature/plug existing refactoring actions to quickfix proposals #86

Merged
merged 5 commits into from

4 participants

@dankilman

This pull request includes the following refactoring actions: extract method, inline local, extract local and rename
and makes them available using quickfix (CTRL+1).
It does some checking before inserting each proposal to verify that only valid refactoring options are presented.

@tonit

Cool! Much appreciated addition!

@dragos
Owner

Looks great!

...ala/tools/eclipse/quickfix/ExtractLocalProposal.scala
@@ -0,0 +1,12 @@
+package scala.tools.eclipse.quickfix
+
+import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal
+import org.eclipse.jface.text.IDocument
+import org.eclipse.jface.text.contentassist.IContextInformation
+import org.eclipse.swt.graphics.Image
+import org.eclipse.swt.graphics.Point
+import scala.tools.eclipse.refactoring.ExtractLocalAction
+
+object ExtractLocalProposal
+ extends ProposalRefactoringActionAdapter(
+ new ExtractLocalAction, 100, "Extract to local value")
@misto Owner
misto added a note

Why not put that 100 into the ProposalRefactoringActionAdapter as the default value for the relevance?

good idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...lipse/quickfix/ProposalRefactoringActionAdapter.scala
@@ -0,0 +1,45 @@
+package scala.tools.eclipse.quickfix
+
+import scala.tools.eclipse.refactoring.RefactoringAction
+import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal
+import org.eclipse.jface.text.IDocument
+import org.eclipse.jface.text.contentassist.IContextInformation
+import org.eclipse.swt.graphics.Image
+import org.eclipse.swt.graphics.Point
+import scala.tools.eclipse.refactoring.ActionAdapter
+import scala.tools.eclipse.refactoring.rename.RenameAction
+import scala.tools.eclipse.refactoring.ScalaIdeRefactoring
+import scala.tools.eclipse.logging.HasLogger
+
+abstract class ProposalRefactoringActionAdapter(
+ val action: ActionAdapter,
@misto Owner
misto added a note

Is is necessary that action has a getter?

no, initially I though each subclass would implement the isValidProposal differently and forget to remove this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dankilman

I signed the CLA, so feel free to merge :)

@dragos dragos merged commit ef0c9d0 into scala-ide:master
@dragos
Owner

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
12 org.scala-ide.sdt.core/src/scala/tools/eclipse/quickfix/ExtractLocalProposal.scala
@@ -0,0 +1,12 @@
+package scala.tools.eclipse.quickfix
+
+import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal
+import org.eclipse.jface.text.IDocument
+import org.eclipse.jface.text.contentassist.IContextInformation
+import org.eclipse.swt.graphics.Image
+import org.eclipse.swt.graphics.Point
+import scala.tools.eclipse.refactoring.ExtractLocalAction
+
+object ExtractLocalProposal
+ extends ProposalRefactoringActionAdapter(
+ new ExtractLocalAction, "Extract to local value")
View
12 org.scala-ide.sdt.core/src/scala/tools/eclipse/quickfix/ExtractMethodProposal.scala
@@ -0,0 +1,12 @@
+package scala.tools.eclipse.quickfix
+
+import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal
+import org.eclipse.jface.text.IDocument
+import org.eclipse.jface.text.contentassist.IContextInformation
+import org.eclipse.swt.graphics.Image
+import org.eclipse.swt.graphics.Point
+import scala.tools.eclipse.refactoring.ExtractMethodAction
+
+object ExtractMethodProposal
+ extends ProposalRefactoringActionAdapter(
+ new ExtractMethodAction, "Extract method")
View
13 org.scala-ide.sdt.core/src/scala/tools/eclipse/quickfix/InlineLocalProposal.scala
@@ -0,0 +1,13 @@
+package scala.tools.eclipse.quickfix
+
+import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal
+import org.eclipse.jface.text.IDocument
+import org.eclipse.jface.text.contentassist.IContextInformation
+import org.eclipse.swt.graphics.Image
+import org.eclipse.swt.graphics.Point
+import scala.tools.eclipse.refactoring.ExtractLocalAction
+import scala.tools.eclipse.refactoring.InlineLocalAction
+
+object InlineLocalProposal
+ extends ProposalRefactoringActionAdapter(
+ new InlineLocalAction, "Inline local value")
View
45 ...cala-ide.sdt.core/src/scala/tools/eclipse/quickfix/ProposalRefactoringActionAdapter.scala
@@ -0,0 +1,45 @@
+package scala.tools.eclipse.quickfix
+
+import scala.tools.eclipse.refactoring.RefactoringAction
+import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal
+import org.eclipse.jface.text.IDocument
+import org.eclipse.jface.text.contentassist.IContextInformation
+import org.eclipse.swt.graphics.Image
+import org.eclipse.swt.graphics.Point
+import scala.tools.eclipse.refactoring.ActionAdapter
+import scala.tools.eclipse.refactoring.rename.RenameAction
+import scala.tools.eclipse.refactoring.ScalaIdeRefactoring
+import scala.tools.eclipse.logging.HasLogger
+
+abstract class ProposalRefactoringActionAdapter(
+ action: ActionAdapter,
+ displayString: String,
+ relevance: Int = 100)
+ extends IJavaCompletionProposal {
+
+ override def apply(document: IDocument): Unit = {
+ // document is not used because the refactoring actions use the current editor
+ // TODO not sure if this null here is very safe
+ action.run(null)
+ }
+
+ override def getRelevance = relevance
+ override def getDisplayString(): String = displayString
+ override def getSelection(document: IDocument): Point = null
+ override def getAdditionalProposalInfo(): String = null
+ override def getImage(): Image = null
+ override def getContextInformation: IContextInformation = null
+
+ def isValidProposal : Boolean = {
+ val ra = action match {
+ case refactoringAction: RefactoringAction => refactoringAction
+ case renameAction : RenameAction => renameAction.getRenameAction
+ }
+ ra.createScalaIdeRefactoringForCurrentEditorAndSelection match {
+ // TODO not sure if this null here is very safe
+ case Some(refactoring) => !refactoring.checkInitialConditions(null).hasWarning
+ case None => false
+ }
+ }
+
+}
View
7 org.scala-ide.sdt.core/src/scala/tools/eclipse/quickfix/RenameProposal.scala
@@ -0,0 +1,7 @@
+package scala.tools.eclipse.quickfix
+
+import scala.tools.eclipse.refactoring.rename.RenameAction
+
+object RenameProposal
+ extends ProposalRefactoringActionAdapter(
+ new RenameAction, "Rename value")
View
11 org.scala-ide.sdt.core/src/scala/tools/eclipse/quickfix/ScalaQuickAssistProcessor.scala
@@ -57,12 +57,17 @@ class ScalaQuickAssistProcessor extends org.eclipse.jdt.ui.text.java.IQuickAssis
}
private def suggestAssist(compilationUnit: ICompilationUnit, problemMessage: String, location: Position): List[IJavaCompletionProposal] = {
-
- problemMessage match {
+ List(
+ ExtractLocalProposal,
+ InlineLocalProposal,
+ RenameProposal,
+ ExtractMethodProposal
+ ).filter(_.isValidProposal) :::
+ (problemMessage match {
case ImplicitConversionFound(s) => List(new ImplicitConversionExpandingProposal(s, location))
case ImplicitArgFound(s) => List(new ImplicitArgumentExpandingProposal(s, location))
case _ => Nil
- }
+ })
}
}
View
6 org.scala-ide.sdt.core/src/scala/tools/eclipse/refactoring/rename/RenameAction.scala
@@ -29,12 +29,12 @@ import scala.tools.refactoring.common.{ ConsoleTracing, InteractiveScalaCompiler
class RenameAction extends ActionAdapter {
override def run(action: IAction) {
- val renameAction = {
- if (isLocalRename) new LocalRenameAction else new GlobalRenameAction
- }
+ val renameAction = getRenameAction
renameAction.run(action)
}
+ def getRenameAction = if (isLocalRename) new LocalRenameAction else new GlobalRenameAction
+
/**
* Using the currently opened file and selection, determines whether the
* selected SymbolTree is only locally visible or not.
Something went wrong with that request. Please try again.