-
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.
- Loading branch information
1 parent
00e83ff
commit def39fa
Showing
11 changed files
with
163 additions
and
7 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
78 changes: 78 additions & 0 deletions
78
src/main/kotlin/org/arend/highlight/RedundantLetBindingPass.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,78 @@ | ||
package org.arend.highlight | ||
|
||
import com.intellij.codeInsight.daemon.impl.HighlightInfo | ||
import com.intellij.codeInsight.daemon.impl.HighlightInfoProcessor | ||
import com.intellij.codeInsight.daemon.impl.HighlightInfoType | ||
import com.intellij.codeInspection.HintAction | ||
import com.intellij.lang.annotation.HighlightSeverity | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.progress.ProgressIndicator | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.util.TextRange | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiFile | ||
import com.intellij.psi.PsiRecursiveElementVisitor | ||
import com.intellij.psi.util.descendantsOfType | ||
import com.intellij.refactoring.suggested.startOffset | ||
import org.arend.naming.reference.Referable | ||
import org.arend.psi.ArendFile | ||
import org.arend.psi.deleteWithWhitespaces | ||
import org.arend.psi.ext.ArendLetClause | ||
import org.arend.psi.ext.ArendLetExpr | ||
import org.arend.psi.findPrevSibling | ||
import org.arend.util.ArendBundle | ||
|
||
class RedundantLetBindingPass(file: ArendFile, editor: Editor, highlightInfoProcessor: HighlightInfoProcessor): | ||
BasePass(file, editor, "Arend redundant let binding", TextRange(0, editor.document.textLength), highlightInfoProcessor) { | ||
|
||
override fun collectInformationWithProgress(progress: ProgressIndicator) { | ||
file.descendantsOfType<ArendLetClause>().filter { | ||
val letExpr = it.parent as ArendLetExpr | ||
var hasElement = false | ||
letExpr.expr?.accept(object : PsiRecursiveElementVisitor() { | ||
override fun visitElement(element: PsiElement) { | ||
if (element is Referable && element.reference?.resolve() == it.referable) { | ||
hasElement = true | ||
} | ||
super.visitElement(element) | ||
} | ||
}) | ||
!hasElement | ||
}.forEach { | ||
val builder = HighlightInfo | ||
.newHighlightInfo(HighlightInfoType.WARNING) | ||
.range(it.textRange) | ||
.severity(HighlightSeverity.WARNING) | ||
.descriptionAndTooltip(ArendBundle.message("arend.inspection.remove.letBinding.message")) | ||
registerFix(builder, RemoveLetBindingHintAction(it)) | ||
addHighlightInfo(builder) | ||
} | ||
} | ||
|
||
companion object { | ||
class RemoveLetBindingHintAction(private val letClause: ArendLetClause) : HintAction { | ||
|
||
override fun startInWriteAction(): Boolean = true | ||
|
||
override fun getFamilyName(): String = text | ||
|
||
override fun getText(): String = ArendBundle.message("arend.inspection.remove.letBinding") | ||
|
||
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile?): Boolean = true | ||
|
||
override fun invoke(project: Project, editor: Editor?, file: PsiFile?) { | ||
val letExpr = letClause.parent as ArendLetExpr | ||
if (letExpr.letClauses.size > 1) { | ||
letClause.findPrevSibling { it.text == "|" }?.deleteWithWhitespaces() | ||
letClause.deleteWithWhitespaces() | ||
} else { | ||
letExpr.startOffset.let { startOffset -> letExpr.expr?.startOffset?.let { endOffset -> | ||
editor?.document?.deleteString(startOffset, endOffset) | ||
} } | ||
} | ||
} | ||
|
||
override fun showHint(editor: Editor): Boolean = true | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/kotlin/org/arend/highlight/RedundantLocalBindingPassFactory.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,27 @@ | ||
package org.arend.highlight | ||
|
||
import com.intellij.codeHighlighting.TextEditorHighlightingPass | ||
import com.intellij.codeHighlighting.TextEditorHighlightingPassFactoryRegistrar | ||
import com.intellij.codeHighlighting.TextEditorHighlightingPassRegistrar | ||
import com.intellij.codeInsight.daemon.impl.DefaultHighlightInfoProcessor | ||
import com.intellij.openapi.components.service | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.util.TextRange | ||
import org.arend.psi.ArendFile | ||
|
||
class RedundantLocalBindingPassFactory: BasePassFactory<ArendFile>(ArendFile::class.java), TextEditorHighlightingPassFactoryRegistrar { | ||
private var myPassId = -1 | ||
|
||
override fun createPass(file: ArendFile, editor: Editor, textRange: TextRange): TextEditorHighlightingPass? { | ||
return RedundantLetBindingPass(file, editor, DefaultHighlightInfoProcessor()) | ||
} | ||
|
||
override fun getPassId(): Int = myPassId | ||
|
||
override fun registerHighlightingPassFactory(registrar: TextEditorHighlightingPassRegistrar, project: Project) { | ||
val service = project.service<ArendPassFactoryService>() | ||
myPassId = registrar.registerTextEditorHighlightingPass(this, intArrayOf(service.highlightingPassId), null, false, -1) | ||
service.typecheckerPassId = myPassId | ||
} | ||
} |
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
3 changes: 2 additions & 1 deletion
3
...inspection/NameShadowingInspectionTest.kt → .../org/arend/highlight/NameShadowingTest.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
3 changes: 2 additions & 1 deletion
3
...yInfixOperatorPrefixFormInspectionTest.kt → ...t/PartiallyInfixOperatorPrefixFormTest.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
34 changes: 34 additions & 0 deletions
34
src/test/kotlin/org/arend/highlight/RedundantLetBindingTest.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,34 @@ | ||
package org.arend.highlight | ||
|
||
import org.arend.inspection.doWarningsCheck | ||
import org.arend.quickfix.QuickFixTestBase | ||
import org.arend.util.ArendBundle | ||
|
||
class RedundantLetBindingTest : QuickFixTestBase() { | ||
fun testOneLetClause() = doWarningsCheck(myFixture, """ | ||
\func f : String => \let | ${bindingLetWarning("y => 1")} \in "" | ||
""") | ||
|
||
fun testRemoveOneLetClause() = doRemoveLetClause(""" | ||
\func f : String => \let | {-caret-}y => 1 \in "" | ||
""", """ | ||
\func f : String => "" | ||
""") | ||
|
||
fun testManyLetClauses() = doWarningsCheck(myFixture, """ | ||
\func f : String => \let | ${bindingLetWarning("y => 1")} | x => "" \in x | ||
""") | ||
|
||
fun testRemoveOneFromManyLetClauses() = doRemoveLetClause(""" | ||
\func f : String => \let | {-caret-}y => 1 | x => "" \in x | ||
""", """ | ||
\func f : String => \let | x => "" \in x | ||
""") | ||
|
||
private fun doRemoveLetClause(before: String, after: String) = | ||
typedQuickFixTest(ArendBundle.message("arend.inspection.remove.letBinding"), before, after) | ||
|
||
companion object { | ||
fun bindingLetWarning(text: String) = "<warning descr=\"${ArendBundle.message("arend.inspection.remove.letBinding.message")}\" textAttributesKey=\"WARNING_ATTRIBUTES\">$text</warning>" | ||
} | ||
} |
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