From 24127aa565d8b708aee04c6bd8b264507b744ad2 Mon Sep 17 00:00:00 2001 From: Andrey Starovoyt Date: Sun, 26 Jul 2015 12:59:30 -0700 Subject: [PATCH] handlebars: error filter for html tags WEB-12338 --- src/META-INF/plugin.xml | 1 + .../handlebars/inspections/HbErrorFilter.java | 38 +++++++++++++++++++ test/data/highlighting/uncompletedTag.hbs | 3 ++ .../uncompletedTagInHandlebars.hbs | 5 +++ .../highlighting/HbHighlightingTest.java | 25 +++++++++++- 5 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/com/dmarcotte/handlebars/inspections/HbErrorFilter.java create mode 100644 test/data/highlighting/uncompletedTag.hbs create mode 100644 test/data/highlighting/uncompletedTagInHandlebars.hbs diff --git a/src/META-INF/plugin.xml b/src/META-INF/plugin.xml index 111bc0f..e22dacc 100644 --- a/src/META-INF/plugin.xml +++ b/src/META-INF/plugin.xml @@ -55,6 +55,7 @@ enabledByDefault="true" level="WARNING" implementationClass="com.dmarcotte.handlebars.inspections.HbEmptyBlockInspection"/> + diff --git a/src/com/dmarcotte/handlebars/inspections/HbErrorFilter.java b/src/com/dmarcotte/handlebars/inspections/HbErrorFilter.java new file mode 100644 index 0000000..90ece22 --- /dev/null +++ b/src/com/dmarcotte/handlebars/inspections/HbErrorFilter.java @@ -0,0 +1,38 @@ +package com.dmarcotte.handlebars.inspections; + + +import com.dmarcotte.handlebars.file.HbFileViewProvider; +import com.intellij.codeInsight.highlighting.TemplateLanguageErrorFilter; +import com.intellij.psi.FileViewProvider; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiWhiteSpace; +import com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider; +import com.intellij.psi.tree.TokenSet; +import org.jetbrains.annotations.NotNull; + +import static com.dmarcotte.handlebars.parsing.HbTokenTypes.*; + +public class HbErrorFilter extends TemplateLanguageErrorFilter { + + private static final TokenSet START_TEMPLATE_TOKENS = TokenSet.create(OPEN, OPEN_PARTIAL, OPEN_BLOCK, OPEN_INVERSE); + + protected HbErrorFilter() { + super(START_TEMPLATE_TOKENS, HbFileViewProvider.class, "HTML"); + } + + + protected boolean shouldIgnoreErrorAt(@NotNull FileViewProvider viewProvider, int offset) { + if (super.shouldIgnoreErrorAt(viewProvider, offset)) return true; + + return hasWhitespacesInHtmlBetweenErrorAndOpenTokens(offset, (TemplateLanguageFileViewProvider)viewProvider); + } + + private static boolean hasWhitespacesInHtmlBetweenErrorAndOpenTokens(int offset, TemplateLanguageFileViewProvider viewProvider) { + PsiElement at = viewProvider.findElementAt(offset, viewProvider.getTemplateDataLanguage()); + if (!(at instanceof PsiWhiteSpace)) return false; + PsiElement elementAt = viewProvider.findElementAt(at.getTextRange().getEndOffset() + 1, viewProvider.getBaseLanguage()); + if (elementAt != null && START_TEMPLATE_TOKENS.contains(elementAt.getNode().getElementType())) return true; + + return false; + } +} diff --git a/test/data/highlighting/uncompletedTag.hbs b/test/data/highlighting/uncompletedTag.hbs new file mode 100644 index 0000000..0655337 --- /dev/null +++ b/test/data/highlighting/uncompletedTag.hbs @@ -0,0 +1,3 @@ +{{#each}} + +{{/each}} \ No newline at end of file diff --git a/test/data/highlighting/uncompletedTagInHandlebars.hbs b/test/data/highlighting/uncompletedTagInHandlebars.hbs new file mode 100644 index 0000000..bf82160 --- /dev/null +++ b/test/data/highlighting/uncompletedTagInHandlebars.hbs @@ -0,0 +1,5 @@ +
+ diff --git a/test/src/com/dmarcotte/handlebars/highlighting/HbHighlightingTest.java b/test/src/com/dmarcotte/handlebars/highlighting/HbHighlightingTest.java index c62b902..001075c 100644 --- a/test/src/com/dmarcotte/handlebars/highlighting/HbHighlightingTest.java +++ b/test/src/com/dmarcotte/handlebars/highlighting/HbHighlightingTest.java @@ -2,6 +2,7 @@ import com.dmarcotte.handlebars.util.HbTestUtils; import com.intellij.codeInspection.htmlInspections.HtmlUnknownTagInspection; +import com.intellij.testFramework.PlatformTestCase; import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase; import org.jetbrains.annotations.NotNull; @@ -11,6 +12,17 @@ protected String getBasePath() { return "/highlighting"; } + @SuppressWarnings("JUnitTestCaseWithNonTrivialConstructors") + public HbHighlightingTest() { + PlatformTestCase.initPlatformLangPrefix(); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + enableInspections(); + } + @NotNull @Override protected String getTestDataPath() { @@ -28,7 +40,18 @@ private void doTest(String extension) { } public void testScriptTag() { - myFixture.enableInspections(HtmlUnknownTagInspection.class); doTest("html"); } + + public void testUncompletedTag() { + doTest("hbs"); + } + + public void testUncompletedTagInHandlebars() { + doTest("hbs"); + } + + private void enableInspections() { + myFixture.enableInspections(HtmlUnknownTagInspection.class); + } }