Skip to content

Commit 5d2a504

Browse files
committed
Add template-lint suppression quick-fix to insert template-lint-disable comments
1 parent b7cb413 commit 5d2a504

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/main/kotlin/com/emberjs/hbs/linter/ember-template-lint/TemplateLintExternalAnnotator.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.intellij.lang.javascript.psi.JSFile
1010
import com.intellij.openapi.application.ApplicationManager
1111
import com.intellij.openapi.editor.Editor
1212
import com.intellij.openapi.project.Project
13+
import com.intellij.util.containers.ContainerUtil
1314
import com.intellij.openapi.util.text.StringUtil
1415
import com.intellij.openapi.vfs.VirtualFile
1516
import com.intellij.psi.PsiDocumentManager
@@ -69,8 +70,29 @@ class TemplateLintExternalAnnotator(onTheFly: Boolean = true) : JSLinterExternal
6970
val prefix = TemplateLintBundle.message("hbs.lint.message.prefix") + " "
7071
annotationResult.errors.removeIf { it.code?.startsWith("glint") == true }
7172
val configurable = TemplateLintConfigurable(file.project, true)
73+
val document = PsiDocumentManager.getInstance(file.project).getDocument(file)
74+
val documentModificationStamp = document?.modificationStamp ?: -1L
7275
val fixes = JSLinterStandardFixes()
7376
.setEditSettingsAction(JSLinterEditSettingsAction(configurable, EmberIcons.TEMPLATE_LINT_16))
77+
.setErrorToIntentionConverter { error: JSLinterErrorBase ->
78+
val result: MutableList<com.intellij.codeInsight.intention.IntentionAction> = mutableListOf()
79+
if (!holder.isBatchMode) {
80+
// Offer a suppression comment insertion for the specific rule
81+
ContainerUtil.addIfNotNull(result, TemplateLintSuppressionUtil.getSuppressForLineAction(error, documentModificationStamp))
82+
}
83+
result
84+
}
85+
.setProblemGroup { error: JSLinterErrorBase? ->
86+
if (holder.isBatchMode) {
87+
null
88+
} else if (error is com.intellij.lang.javascript.linter.JSLinterError) {
89+
val intentions = TemplateLintSuppressionUtil.getSuppressionsForError(error, documentModificationStamp)
90+
com.intellij.lang.javascript.validation.JSAnnotatorProblemGroup(intentions, null as String?)
91+
} else {
92+
null
93+
}
94+
}
95+
7496
JSLinterAnnotationsBuilder(file, annotationResult, holder, configurable, prefix, this.inspectionClass, fixes)
7597
.setHighlightingGranularity(HighlightingGranularity.element)
7698
.setDefaultFileLevelErrorIcon(EmberIcons.TEMPLATE_LINT_16)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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

Comments
 (0)