|
| 1 | +import com.intellij.codeInsight.intention.IntentionAction |
| 2 | +import com.intellij.codeInsight.intention.impl.BaseIntentionAction |
| 3 | +import com.intellij.lang.javascript.linter.JSLinterError |
| 4 | +import com.intellij.lang.javascript.linter.JSLinterErrorBase |
| 5 | +import com.intellij.openapi.application.ApplicationManager |
| 6 | +import com.intellij.openapi.command.WriteCommandAction |
| 7 | +import com.intellij.openapi.editor.Editor |
| 8 | +import com.intellij.openapi.project.Project |
| 9 | +import com.intellij.psi.PsiDocumentManager |
| 10 | + |
| 11 | +object TemplateLintSuppressionUtil { |
| 12 | + fun getSuppressForLineAction(error: JSLinterErrorBase, modificationStamp: Long): IntentionAction? { |
| 13 | + if (error !is JSLinterError) return null |
| 14 | + val rule = error.code ?: return null |
| 15 | + |
| 16 | + return object : BaseIntentionAction() { |
| 17 | + override fun getFamilyName(): String { |
| 18 | + return "Disable template-lint rule" |
| 19 | + } |
| 20 | + |
| 21 | + override fun getText(): String { |
| 22 | + return "Disable template-lint rule $rule" |
| 23 | + } |
| 24 | + |
| 25 | + override fun isAvailable(project: Project, editor: Editor?, file: com.intellij.psi.PsiFile?): Boolean { |
| 26 | + return editor != null && editor.document.modificationStamp == modificationStamp |
| 27 | + } |
| 28 | + |
| 29 | + override fun invoke(project: Project, editor: Editor?, file: com.intellij.psi.PsiFile?) { |
| 30 | + if (editor == null) return |
| 31 | + val document = editor.document |
| 32 | + val line = Math.max(0, error.line - 1) |
| 33 | + try { |
| 34 | + val lineStart = document.getLineStartOffset(line) |
| 35 | + val comment = "{{!-- template-lint-disable $rule --}}\n" |
| 36 | + WriteCommandAction.runWriteCommandAction(project) { |
| 37 | + document.insertString(lineStart, comment) |
| 38 | + PsiDocumentManager.getInstance(project).commitDocument(document) |
| 39 | + } |
| 40 | + } catch (e: Exception) { |
| 41 | + // ignore |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + override fun startInWriteAction(): Boolean { |
| 46 | + return true |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + fun getSuppressionsForError(error: JSLinterError, modificationStamp: Long): List<IntentionAction> { |
| 52 | + val a = getSuppressForLineAction(error, modificationStamp) |
| 53 | + return if (a != null) listOf(a) else emptyList() |
| 54 | + } |
| 55 | +} |
0 commit comments