Skip to content

Commit

Permalink
handlebars: error filter for html tags WEB-12338
Browse files Browse the repository at this point in the history
  • Loading branch information
anstarovoyt authored and dmarcotte committed Jul 26, 2015
1 parent 31edac7 commit 24127aa
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
enabledByDefault="true"
level="WARNING"
implementationClass="com.dmarcotte.handlebars.inspections.HbEmptyBlockInspection"/>
<highlightErrorFilter implementation="com.dmarcotte.handlebars.inspections.HbErrorFilter"/>
</extensions>

<extensions defaultExtensionNs="JavaScript">
Expand Down
38 changes: 38 additions & 0 deletions src/com/dmarcotte/handlebars/inspections/HbErrorFilter.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
3 changes: 3 additions & 0 deletions test/data/highlighting/uncompletedTag.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#each}}
<input<EOLError></EOLError>
{{/each}}
5 changes: 5 additions & 0 deletions test/data/highlighting/uncompletedTagInHandlebars.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class=""
{{#each input.attributes}}
{{@key}}="{{this}}"
{{/each}} />

Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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() {
Expand All @@ -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);
}
}

0 comments on commit 24127aa

Please sign in to comment.