diff --git a/src/main/scala/intellij/haskell/psi/stubs/HaskellConidStub.java b/src/main/scala/intellij/haskell/psi/stubs/HaskellConidStub.java index 1e0be7694..20501826b 100644 --- a/src/main/scala/intellij/haskell/psi/stubs/HaskellConidStub.java +++ b/src/main/scala/intellij/haskell/psi/stubs/HaskellConidStub.java @@ -27,7 +27,7 @@ public HaskellConidStub(StubElement parent, IStubElementType elementType, String super(parent, elementType, name); } - public HaskellConidStub(StubElement parent, IStubElementType elementType, String name) { + public HaskellConidStub(StubElement parent, IStubElementType elementType, String name) { super(parent, elementType, name); } } diff --git a/src/main/scala/intellij/haskell/psi/stubs/HaskellConsymStub.java b/src/main/scala/intellij/haskell/psi/stubs/HaskellConsymStub.java index fe2279a0a..eb9b45a34 100644 --- a/src/main/scala/intellij/haskell/psi/stubs/HaskellConsymStub.java +++ b/src/main/scala/intellij/haskell/psi/stubs/HaskellConsymStub.java @@ -27,7 +27,7 @@ public HaskellConsymStub(StubElement parent, IStubElementType elementType, Strin super(parent, elementType, name); } - public HaskellConsymStub(StubElement parent, IStubElementType elementType, String name) { + public HaskellConsymStub(StubElement parent, IStubElementType elementType, String name) { super(parent, elementType, name); } } diff --git a/src/main/scala/intellij/haskell/psi/stubs/HaskellModidStub.java b/src/main/scala/intellij/haskell/psi/stubs/HaskellModidStub.java index e48419c52..85f90240d 100644 --- a/src/main/scala/intellij/haskell/psi/stubs/HaskellModidStub.java +++ b/src/main/scala/intellij/haskell/psi/stubs/HaskellModidStub.java @@ -27,7 +27,7 @@ public HaskellModidStub(StubElement parent, IStubElementType elementType, String super(parent, elementType, name); } - public HaskellModidStub(StubElement parent, IStubElementType elementType, String name) { + public HaskellModidStub(StubElement parent, IStubElementType elementType, String name) { super(parent, elementType, name); } } \ No newline at end of file diff --git a/src/main/scala/intellij/haskell/psi/stubs/HaskellVaridStub.java b/src/main/scala/intellij/haskell/psi/stubs/HaskellVaridStub.java index 2bfd67c36..f8b0a4a03 100644 --- a/src/main/scala/intellij/haskell/psi/stubs/HaskellVaridStub.java +++ b/src/main/scala/intellij/haskell/psi/stubs/HaskellVaridStub.java @@ -27,7 +27,7 @@ public HaskellVaridStub(StubElement parent, IStubElementType elementType, String super(parent, elementType, name); } - public HaskellVaridStub(StubElement parent, IStubElementType elementType, String name) { + public HaskellVaridStub(StubElement parent, IStubElementType elementType, String name) { super(parent, elementType, name); } } diff --git a/src/main/scala/intellij/haskell/psi/stubs/HaskellVarsymStub.java b/src/main/scala/intellij/haskell/psi/stubs/HaskellVarsymStub.java index eb4d6e734..a0a53bddf 100644 --- a/src/main/scala/intellij/haskell/psi/stubs/HaskellVarsymStub.java +++ b/src/main/scala/intellij/haskell/psi/stubs/HaskellVarsymStub.java @@ -27,7 +27,7 @@ public HaskellVarsymStub(StubElement parent, IStubElementType elementType, Strin super(parent, elementType, name); } - public HaskellVarsymStub(StubElement parent, IStubElementType elementType, String name) { + public HaskellVarsymStub(StubElement parent, IStubElementType elementType, String name) { super(parent, elementType, name); } } diff --git a/src/main/scala/intellij/haskell/psi/stubs/types/HaskellConidStubElementType.scala b/src/main/scala/intellij/haskell/psi/stubs/types/HaskellConidStubElementType.scala index 16a329512..45984483b 100644 --- a/src/main/scala/intellij/haskell/psi/stubs/types/HaskellConidStubElementType.scala +++ b/src/main/scala/intellij/haskell/psi/stubs/types/HaskellConidStubElementType.scala @@ -27,7 +27,7 @@ class HaskellConidStubElementType(debugName: String) extends HaskellNamedStubEle new HaskellConidImpl(stub, this) } - def createStub(psi: HaskellConid, parentStub: StubElement[_ <: PsiElement]): HaskellConidStub = { + def createStub(psi: HaskellConid, parentStub: StubElement[_]): HaskellConidStub = { new HaskellConidStub(parentStub, this, psi.getName) } diff --git a/src/main/scala/intellij/haskell/psi/stubs/types/HaskellConsymStubElementType.scala b/src/main/scala/intellij/haskell/psi/stubs/types/HaskellConsymStubElementType.scala index 6aebaabcd..8f2c2de03 100644 --- a/src/main/scala/intellij/haskell/psi/stubs/types/HaskellConsymStubElementType.scala +++ b/src/main/scala/intellij/haskell/psi/stubs/types/HaskellConsymStubElementType.scala @@ -27,7 +27,7 @@ class HaskellConsymStubElementType(debugName: String) extends HaskellNamedStubEl new HaskellConsymImpl(stub, this) } - def createStub(psi: HaskellConsym, parentStub: StubElement[_ <: PsiElement]): HaskellConsymStub = { + def createStub(psi: HaskellConsym, parentStub: StubElement[_]): HaskellConsymStub = { new HaskellConsymStub(parentStub, this, psi.getName) } diff --git a/src/main/scala/intellij/haskell/psi/stubs/types/HaskellModidStubElementType.scala b/src/main/scala/intellij/haskell/psi/stubs/types/HaskellModidStubElementType.scala index 30efe9097..703c7d019 100644 --- a/src/main/scala/intellij/haskell/psi/stubs/types/HaskellModidStubElementType.scala +++ b/src/main/scala/intellij/haskell/psi/stubs/types/HaskellModidStubElementType.scala @@ -27,7 +27,7 @@ class HaskellModidStubElementType(debugName: String) extends HaskellNamedStubEle new HaskellModidImpl(stub, this) } - def createStub(psi: HaskellModid, parentStub: StubElement[_ <: PsiElement]): HaskellModidStub = { + def createStub(psi: HaskellModid, parentStub: StubElement[_]): HaskellModidStub = { new HaskellModidStub(parentStub, this, psi.getName) } diff --git a/src/main/scala/intellij/haskell/psi/stubs/types/HaskellVaridStubElementType.scala b/src/main/scala/intellij/haskell/psi/stubs/types/HaskellVaridStubElementType.scala index 819e3fe59..17fb44d36 100644 --- a/src/main/scala/intellij/haskell/psi/stubs/types/HaskellVaridStubElementType.scala +++ b/src/main/scala/intellij/haskell/psi/stubs/types/HaskellVaridStubElementType.scala @@ -27,7 +27,7 @@ class HaskellVaridStubElementType(debugName: String) extends HaskellNamedStubEle new HaskellVaridImpl(stub, this) } - def createStub(psi: HaskellVarid, parentStub: StubElement[_ <: PsiElement]): HaskellVaridStub = { + def createStub(psi: HaskellVarid, parentStub: StubElement[_]): HaskellVaridStub = { new HaskellVaridStub(parentStub, this, psi.getName) } diff --git a/src/main/scala/intellij/haskell/psi/stubs/types/HaskellVarsymStubElementType.scala b/src/main/scala/intellij/haskell/psi/stubs/types/HaskellVarsymStubElementType.scala index 52dec6d96..81fe05b16 100644 --- a/src/main/scala/intellij/haskell/psi/stubs/types/HaskellVarsymStubElementType.scala +++ b/src/main/scala/intellij/haskell/psi/stubs/types/HaskellVarsymStubElementType.scala @@ -11,7 +11,7 @@ class HaskellVarsymStubElementType(debugName: String) extends HaskellNamedStubEl new HaskellVarsymImpl(stub, this) } - def createStub(psi: HaskellVarsym, parentStub: StubElement[_ <: PsiElement]): HaskellVarsymStub = { + def createStub(psi: HaskellVarsym, parentStub: StubElement[_]): HaskellVarsymStub = { new HaskellVarsymStub(parentStub, this, psi.getName) } diff --git a/src/main/scala/intellij/haskell/refactor/HaskellRefactoringSupportProvider.scala b/src/main/scala/intellij/haskell/refactor/HaskellRefactoringSupportProvider.scala index 0e2f8b8b3..07f01a701 100644 --- a/src/main/scala/intellij/haskell/refactor/HaskellRefactoringSupportProvider.scala +++ b/src/main/scala/intellij/haskell/refactor/HaskellRefactoringSupportProvider.scala @@ -17,7 +17,14 @@ package intellij.haskell.refactor import com.intellij.lang.refactoring.RefactoringSupportProvider +import com.intellij.openapi.actionSystem.DataContext +import com.intellij.openapi.command.WriteCommandAction.writeCommandAction +import com.intellij.openapi.editor.{Editor, SelectionModel} +import com.intellij.openapi.project.Project import com.intellij.psi._ +import com.intellij.psi.util.PsiTreeUtil +import com.intellij.refactoring.RefactoringActionHandler +import intellij.haskell.psi.HaskellExpression import intellij.haskell.util.HaskellProjectUtil class HaskellRefactoringSupportProvider extends RefactoringSupportProvider { @@ -32,4 +39,34 @@ class HaskellRefactoringSupportProvider extends RefactoringSupportProvider { case _ => false } } -} \ No newline at end of file + + override def getIntroduceVariableHandler: RefactoringActionHandler = { + new RefactoringActionHandler { + override def invoke(project: Project, editor: Editor, file: PsiFile, dataContext: DataContext): Unit = { + val model: SelectionModel = editor.getSelectionModel + + if (!model.hasSelection) return + + val element1: PsiElement = file.findElementAt(model.getSelectionStart) + val element2: PsiElement = file.findElementAt(model.getSelectionEnd - 1) + + // Ideally, once we fix the parser, this would be the expression selection + var parent = PsiTreeUtil.findCommonParent(element1, element2) + parent = PsiTreeUtil.getParentOfType[HaskellExpression](parent, classOf[HaskellExpression]) + + if (!(parent != null + && PsiTreeUtil.getDeepestFirst(parent) == element1 + && PsiTreeUtil.getDeepestLast(parent) == element2)) + return + + writeCommandAction(project, file).withName("Introduce Variable").withGroupId(null).run(() => { + editor.getDocument.replaceString(model.getSelectionStart, model.getSelectionEnd, "x") + }) + } + + override def invoke(project: Project, elements: Array[PsiElement], dataContext: DataContext): Unit = { + // This does not get called + } + } + } +}