-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #547 from alex999990009/alex99999/fixes
Fix {?} and the Infer parameter quick fix and add the Intention to generate pattern matching clauses
- Loading branch information
Showing
22 changed files
with
302 additions
and
11 deletions.
There are no files selected for viewing
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
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
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
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
51 changes: 51 additions & 0 deletions
51
src/main/kotlin/org/arend/intention/generating/GenerateElimMissingClausesIntention.kt
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,51 @@ | ||
package org.arend.intention.generating | ||
|
||
import com.intellij.codeInsight.template.Template | ||
import com.intellij.codeInsight.template.TemplateBuilderImpl | ||
import com.intellij.codeInsight.template.TemplateEditingAdapter | ||
import com.intellij.codeInsight.template.TemplateManager | ||
import com.intellij.codeInsight.template.impl.TemplateState | ||
import com.intellij.openapi.application.runWriteAction | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.refactoring.suggested.endOffset | ||
import org.arend.intention.SelfTargetingIntention | ||
import org.arend.psi.ArendFile | ||
import org.arend.psi.ArendPsiFactory | ||
import org.arend.util.ArendBundle | ||
|
||
class GenerateElimMissingClausesIntention : SelfTargetingIntention<PsiElement>(PsiElement::class.java, ArendBundle.message("arend.generateElimPatternMatchingClauses")) { | ||
override fun isApplicableTo(element: PsiElement, caretOffset: Int, editor: Editor): Boolean { | ||
return checkMissingClauses(element, editor) | ||
} | ||
|
||
override fun applyTo(element: PsiElement, project: Project, editor: Editor) { | ||
val file = element.containingFile as ArendFile | ||
val (group, _) = deleteFunctionBody(element) ?: return | ||
|
||
val psiFactory = ArendPsiFactory(project) | ||
val elim = psiFactory.createElim(emptyList()) | ||
runWriteAction { | ||
group.add(psiFactory.createWhitespace(" ")) | ||
group.add(elim) | ||
} | ||
|
||
val offset = group.endOffset | ||
runWriteAction { | ||
editor.document.insertString(offset, " ") | ||
} | ||
editor.caretModel.moveToOffset(offset + 1) | ||
|
||
val builder = TemplateBuilderImpl(elim) | ||
builder.replaceElement(elim, "") | ||
val template = builder.buildTemplate() | ||
TemplateManager.getInstance(project).startTemplate(editor, template, object : TemplateEditingAdapter() { | ||
override fun beforeTemplateFinished(state: TemplateState, template: Template?) { | ||
fixMissingClausesError(project, file, editor, group, state.currentVariableRange?.endOffset ?: return) | ||
} | ||
}) | ||
} | ||
|
||
override fun startInWriteAction(): Boolean = false | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/kotlin/org/arend/intention/generating/GenerateMissingClausesIntention.kt
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,23 @@ | ||
package org.arend.intention.generating | ||
|
||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiElement | ||
import org.arend.intention.SelfTargetingIntention | ||
import org.arend.psi.ArendFile | ||
import org.arend.util.ArendBundle | ||
|
||
class GenerateMissingClausesIntention : SelfTargetingIntention<PsiElement>(PsiElement::class.java, ArendBundle.message("arend.generatePatternMatchingClauses")) { | ||
override fun isApplicableTo(element: PsiElement, caretOffset: Int, editor: Editor): Boolean { | ||
return checkMissingClauses(element, editor) | ||
} | ||
|
||
override fun applyTo(element: PsiElement, project: Project, editor: Editor) { | ||
val file = element.containingFile as ArendFile | ||
val (group, startOffsetParent) = deleteFunctionBody(element) ?: return | ||
|
||
fixMissingClausesError(project, file, editor, group, startOffsetParent - 1) | ||
} | ||
|
||
override fun startInWriteAction(): Boolean = false | ||
} |
88 changes: 88 additions & 0 deletions
88
src/main/kotlin/org/arend/intention/generating/GenerateMissingClausesUtils.kt
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,88 @@ | ||
package org.arend.intention.generating | ||
|
||
import com.intellij.openapi.application.runWriteAction | ||
import com.intellij.openapi.components.service | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.SmartPointerManager | ||
import com.intellij.psi.util.elementType | ||
import com.intellij.refactoring.suggested.endOffset | ||
import com.intellij.refactoring.suggested.startOffset | ||
import org.arend.ext.error.MissingClausesError | ||
import org.arend.psi.ArendElementTypes | ||
import org.arend.psi.ArendFile | ||
import org.arend.psi.ArendPsiFactory | ||
import org.arend.psi.ext.* | ||
import org.arend.quickfix.ImplementMissingClausesQuickFix | ||
import org.arend.typechecking.ArendTypechecking | ||
import org.arend.typechecking.error.ErrorService | ||
|
||
internal fun checkMissingClauses(element: PsiElement, editor: Editor): Boolean { | ||
if (element.elementType != ArendElementTypes.TGOAL) { | ||
return false | ||
} | ||
var fileText = (element.containingFile as ArendFile).text | ||
|
||
var parent: PsiElement? = element | ||
while (parent !is ArendFunctionBody) { | ||
parent = parent?.parent | ||
if (parent == null) { | ||
return false | ||
} | ||
} | ||
fileText = fileText.removeRange(parent.startOffset, parent.endOffset) | ||
|
||
val project = editor.project ?: return false | ||
val psiFactory = ArendPsiFactory(project) | ||
val newFile = psiFactory.createFromText(fileText) ?: return false | ||
|
||
ArendTypechecking.create(project).typecheckModules(listOf(newFile), null) | ||
|
||
val errorService = project.service<ErrorService>() | ||
val error = errorService.getErrors(newFile).filter { it.error is MissingClausesError }.find { | ||
it.definition?.endOffset?.plus(1) == parent.startOffset | ||
} | ||
|
||
errorService.clearNameResolverErrors(newFile) | ||
errorService.clearTypecheckingErrors(newFile) | ||
return error != null | ||
} | ||
|
||
internal fun deleteFunctionBody(element: PsiElement): Pair<ArendGroup, Int>? { | ||
var parent: PsiElement? = element | ||
while (parent !is ArendFunctionBody) { | ||
parent = parent?.parent | ||
if (parent == null) { | ||
return null | ||
} | ||
} | ||
|
||
val group = parent.parent as? ArendGroup? ?: return null | ||
val startOffsetParent = parent.startOffset | ||
runWriteAction { | ||
parent.delete() | ||
} | ||
return Pair(group, startOffsetParent) | ||
} | ||
|
||
internal fun fixMissingClausesError(project: Project, file: ArendFile, editor: Editor, group: ArendGroup, offset: Int) { | ||
val errorService = project.service<ErrorService>() | ||
(group as? ArendDefFunction?)?.let { errorService.clearTypecheckingErrors(it) } | ||
|
||
group.dropTypechecked() | ||
ArendTypechecking.create(project).typecheckModules(listOf(group), null) | ||
|
||
val error = errorService.getErrors(file).filter { it.error is MissingClausesError }.find { | ||
it.definition?.endOffset == offset | ||
} ?: return | ||
(error.definition as? TCDefinition?)?.let { | ||
errorService.clearTypecheckingErrors(it) | ||
} | ||
runWriteAction { | ||
ImplementMissingClausesQuickFix( | ||
error.error as MissingClausesError, | ||
SmartPointerManager.createPointer(error.cause as ArendCompositeElement) | ||
).invoke(project, editor, file) | ||
} | ||
} |
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
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
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
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
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
5 changes: 5 additions & 0 deletions
5
...in/resources/intentionDescriptions/GenerateElimMissingClausesIntention/after.ard.template
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,5 @@ | ||
\import Data.Bool | ||
|
||
\func foo (n m : Nat) : Bool <spot>\elim n | ||
| 0 => {?} | ||
| suc n => {?}</spot> |
3 changes: 3 additions & 0 deletions
3
...n/resources/intentionDescriptions/GenerateElimMissingClausesIntention/before.ard.template
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,3 @@ | ||
\import Data.Bool | ||
|
||
\func foo (n m : Nat) : Bool => <spot>{?}</spot> |
5 changes: 5 additions & 0 deletions
5
...main/resources/intentionDescriptions/GenerateElimMissingClausesIntention/description.html
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,5 @@ | ||
<html> | ||
<body> | ||
Generates pattern matching clauses using \\elim | ||
</body> | ||
</html> |
7 changes: 7 additions & 0 deletions
7
src/main/resources/intentionDescriptions/GenerateMissingClausesIntention/after.ard.template
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,7 @@ | ||
\import Data.Bool | ||
|
||
\func foo (n m : Nat) : Bool<spot> | ||
| 0, 0 => {?} | ||
| 0, suc m => {?} | ||
| suc n, 0 => {?} | ||
| suc n, suc m => {?}</spot> |
3 changes: 3 additions & 0 deletions
3
src/main/resources/intentionDescriptions/GenerateMissingClausesIntention/before.ard.template
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,3 @@ | ||
\import Data.Bool | ||
|
||
\func foo (n m : Nat) : Bool => <spot>{?}</spot> |
5 changes: 5 additions & 0 deletions
5
src/main/resources/intentionDescriptions/GenerateMissingClausesIntention/description.html
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,5 @@ | ||
<html> | ||
<body> | ||
Generates pattern matching clauses | ||
</body> | ||
</html> |
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
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
Oops, something went wrong.