Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Support quick fix for inserting suppress annotation #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 8 additions & 4 deletions src/cc/redpen/intellij/RedPenInspection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.intellij.codeInsight.daemon.GroupNames
import com.intellij.codeInspection.InspectionManager
import com.intellij.codeInspection.LocalInspectionTool
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.codeInspection.ProblemHighlightType.GENERIC_ERROR_OR_WARNING
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiFile
Expand Down Expand Up @@ -46,9 +46,13 @@ open class RedPenInspection : LocalInspectionTool() {
return errors.map { e ->
try {
val range = toRange(e, lines)
manager.createProblemDescriptor(element, range,
e.message + " (" + e.validatorName + ")", GENERIC_ERROR_OR_WARNING, isOnTheFly,
BaseQuickFix.forValidator(e, redPen.configuration, text, range))
manager.createProblemDescriptor(
element,
range,
e.message + " (" + e.validatorName + ")",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
isOnTheFly,
*BaseQuickFix.forValidator(e, redPen.configuration, text, range))
} catch (ex: Exception) {
Logger.getInstance(javaClass.name).warn(e.message + ": " + ex.toString());
null
Expand Down
26 changes: 14 additions & 12 deletions src/cc/redpen/intellij/fixes/BaseQuickFix.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,22 @@ abstract class BaseQuickFix(var text: String) : LocalQuickFix {
override fun toString() = javaClass.simpleName + "[" + text + "]"

companion object {
fun forValidator(error: ValidationError, config: Configuration, fullText: String, range: TextRange): BaseQuickFix? {
fun forValidator(error: ValidationError, config: Configuration, fullText: String, range: TextRange): Array<BaseQuickFix> {
val text = fullText.substring(range.startOffset, range.endOffset)
return when (error.validatorName) {
"Hyphenation" -> HyphenateQuickFix(text)
"InvalidSymbol" -> InvalidSymbolQuickFix(config, fullText, range)
"SymbolWithSpace" -> SymbolWithSpaceQuickFix(config, text)
"StartWithCapitalLetter" -> StartWithCapitalLetterQuickFix(text)
"NumberFormat" -> NumberFormatQuickFix(config, text)
"SpaceBeginningOfSentence" -> SpaceBeginningOfSentenceQuickFix(text)
"EndOfSentence" -> EndOfSentenceQuickFix(text)
"SuggestExpression" -> SuggestExpressionQuickFix(text, error.message)
"ParagraphStartWith" -> ParagraphStartWithQuickFix(config, fullText, range)
else -> if (isSentenceLevelError(error)) return null else RemoveQuickFix(text)
val mutableList : MutableList<BaseQuickFix> = arrayListOf()
when (error.validatorName) {
"Hyphenation" -> mutableList.add(HyphenateQuickFix(text))
"InvalidSymbol" -> mutableList.add(InvalidSymbolQuickFix(config, fullText, range))
"SymbolWithSpace" -> mutableList.add(SymbolWithSpaceQuickFix(config, text))
"StartWithCapitalLetter" -> mutableList.add(StartWithCapitalLetterQuickFix(text))
"NumberFormat" -> mutableList.add(NumberFormatQuickFix(config, text))
"SpaceBeginningOfSentence" -> mutableList.add(SpaceBeginningOfSentenceQuickFix(text))
"EndOfSentence" -> mutableList.add(EndOfSentenceQuickFix(text))
"SuggestExpression" -> mutableList.add(SuggestExpressionQuickFix(text, error.message))
"ParagraphStartWith" -> mutableList.add(ParagraphStartWithQuickFix(config, fullText, range))
else -> if (!isSentenceLevelError(error)) mutableList.add(RemoveQuickFix(text))
}
return mutableList.toTypedArray()
}

private fun isSentenceLevelError(error: ValidationError) = !error.startPosition.isPresent
Expand Down
26 changes: 15 additions & 11 deletions test/cc/redpen/intellij/fixes/QuickFixCompanionTest.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
package cc.redpen.intellij.fixes

import cc.redpen.config.Configuration
import cc.redpen.intellij.BaseTest
import cc.redpen.model.Sentence
import cc.redpen.parser.LineOffset
import cc.redpen.validator.ValidationError
import com.intellij.openapi.util.TextRange
import com.nhaarman.mockito_kotlin.*
import org.junit.Assert.*
import com.nhaarman.mockito_kotlin.doReturn
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.spy
import com.nhaarman.mockito_kotlin.whenever
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import java.util.*

class QuickFixCompanionTest() : BaseTest() {
@Test
fun sentenceLevelErrorHaveNoQuickFixes() {
val error = ErrorGenerator.sentence(Sentence("Too long sentence", 1))
assertNull(BaseQuickFix.forValidator(error, mock(), "full text", TextRange(0, 0)))
val quickFixes = BaseQuickFix.forValidator(error, mock(), "full text", TextRange(0, 0))
assertEquals(0, quickFixes.size)
}

@Test
fun removeQuickFixByDefault() {
val error = ErrorGenerator.at(5, 9)
val quickFix = BaseQuickFix.forValidator(error, mock(), "full text", TextRange(5, 9))
val quickFixes = BaseQuickFix.forValidator(error, mock(), "full text", TextRange(5, 9))
val quickFix = quickFixes.get(0)
assertTrue(quickFix is RemoveQuickFix)
assertEquals("text", quickFix?.text)
}
Expand All @@ -30,15 +32,17 @@ class QuickFixCompanionTest() : BaseTest() {
fun validatorSpecificQuickFix() {
val error = spy(ErrorGenerator.at(5, 9))
doReturn("Hyphenation").whenever(error).validatorName
val quickFix = BaseQuickFix.forValidator(error, mock(), "full text", TextRange(5, 9))
val quickFixes = BaseQuickFix.forValidator(error, mock(), "full text", TextRange(5, 9))
val quickFix = quickFixes.get(0)
assertTrue(quickFix is HyphenateQuickFix)
}

@Test
fun supportedSentenceLevelQuickFix() {
val error = spy(ErrorGenerator.sentence(Sentence("Too long sentence", 1)))
doReturn("ParagraphStartWith").whenever(error).validatorName
val quickFix = BaseQuickFix.forValidator(error, mock(), "full text", TextRange(5, 9))
val quickFixes = BaseQuickFix.forValidator(error, mock(), "full text", TextRange(5, 9))
val quickFix = quickFixes.get(0)
assertTrue(quickFix is ParagraphStartWithQuickFix)
}
}
}