Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Feature: introduce variable #639

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -32,4 +39,34 @@ class HaskellRefactoringSupportProvider extends RefactoringSupportProvider {
case _ => false
}
}
}

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
}
}
}
}