Permalink
Browse files

Enable highlighting on open/close mustache pairs

  • Loading branch information...
1 parent 1ac35cd commit cf7be85327af701608cc1109115523d7e590e4e8 Daniel Marcotte committed Feb 21, 2012
View
@@ -21,8 +21,9 @@
<fileTypeFactory implementation="com.dmarcotte.handlebars.file.HbFileTypeFactory" />
<lang.parserDefinition language="Handlebars" implementationClass="com.dmarcotte.handlebars.parsing.HbParseDefinition" />
<lang.formatter language="Handlebars" implementationClass="com.dmarcotte.handlebars.format.HbFormattingModelBuilder"/>
- <colorSettingsPage implementation="com.dmarcotte.handlebars.pages.HbColorsPage" />
- <lang.fileViewProviderFactory language="Handlebars" implementationClass="com.dmarcotte.handlebars.file.HbFileViewProviderFactory" />
+ <colorSettingsPage implementation="com.dmarcotte.handlebars.pages.HbColorsPage"/>
+ <lang.fileViewProviderFactory language="Handlebars" implementationClass="com.dmarcotte.handlebars.file.HbFileViewProviderFactory"/>
<lang.commenter language="Handlebars" implementationClass="com.dmarcotte.handlebars.HandlebarsCommenter"/>
+ <lang.braceMatcher language="Handlebars" implementationClass="com.dmarcotte.handlebars.HbBraceMatcher"/>
</extensions>
</idea-plugin>
View
@@ -1,22 +1,22 @@
-## IDEA-Handlebars: An Intellij IDEA plug for [Handlebars](http://handlebarsjs.com/) templates
+# IDEA-Handlebars: An Intellij IDEA plug for [Handlebars](http://handlebarsjs.com/) templates
-IDEA-Handlebars adds support for Handlebars templates (by default files with the ".handlebars" extension) to IDEs based
+IDEA-Handlebars adds support for Handlebars templates (by default, files with the ".handlebars" extension) to IDEs based
on the Intellij IDEA platform (IDEA, RubyMine, PhpStorm, and others).
-# Features
+## Features
* Configurable syntax highlighting for Handlebars code
* Full HTML highlighting, code-completion, inspections, formatting and commenting for the HTML content in your Handlebars templates
-# Not yet implemented
+## Not yet implemented
* Find usages, Go-to declaration, etc. for references
* Support for Handlebars templates embedded in script tags in html files
-# Compatibility
+## Compatibility
* Confirmed to work with IDEA 10.5, IDEA 11, RubyMine 4 and PhpStorm 3 DM TODO check phpStorm and RubyMine compatibility
* Should work with any other IDE based on the IDEA platform, build 107.587 or higher
-# Special thanks
+## Special thanks
* Extra special thanks to the [intellij-latte](https://github.com/juzna/intellij-latte project) project
(for having a clean code-base to learn from, and taking the time to make posts like [this gem](http://devnet.jetbrains.net/message/5450284?tstart=0)
* Yehuda Katz for creating and maintaining [Handlebars](http://handlebarsjs.com/)
@@ -0,0 +1,33 @@
+package com.dmarcotte.handlebars;
+
+import com.dmarcotte.handlebars.parsing.HbTokenTypes;
+import com.intellij.lang.BracePair;
+import com.intellij.lang.PairedBraceMatcher;
+import com.intellij.psi.PsiFile;
+import com.intellij.psi.tree.IElementType;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+public class HbBraceMatcher implements PairedBraceMatcher {
+
+ private static final BracePair[] PAIRS = {
+ new BracePair(HbTokenTypes.OPEN, HbTokenTypes.CLOSE, false),
+ new BracePair(HbTokenTypes.OPEN_BLOCK, HbTokenTypes.CLOSE, false),
+ new BracePair(HbTokenTypes.OPEN_PARTIAL, HbTokenTypes.CLOSE, false),
+ new BracePair(HbTokenTypes.OPEN_ENDBLOCK, HbTokenTypes.CLOSE, false),
+ new BracePair(HbTokenTypes.OPEN_INVERSE, HbTokenTypes.CLOSE, false),
+ new BracePair(HbTokenTypes.OPEN_UNESCAPED, HbTokenTypes.CLOSE, false)
+ };
+
+ public BracePair[] getPairs() {
+ return PAIRS;
+ }
+
+ public boolean isPairedBracesAllowedBeforeType(@NotNull IElementType lbraceType, @Nullable IElementType contextType) {
+ return false; // never allow IDEA to automatically insert braces since it does not do the multiple character braces properly
+ }
+
+ public int getCodeConstructStart(PsiFile file, int openingBraceOffset) {
+ return openingBraceOffset;
+ }
+}
@@ -78,6 +78,7 @@ public Lexer getHighlightingLexer() {
keys1.put(HbTokenTypes.OPEN, MUSTACHES);
keys1.put(HbTokenTypes.OPEN_BLOCK, MUSTACHES);
+ keys1.put(HbTokenTypes.OPEN_PARTIAL, MUSTACHES);
keys1.put(HbTokenTypes.OPEN_ENDBLOCK, MUSTACHES);
keys1.put(HbTokenTypes.OPEN_INVERSE, MUSTACHES);
keys1.put(HbTokenTypes.OPEN_UNESCAPED, MUSTACHES);
@@ -22,12 +22,12 @@ private HbTokenTypes() {}
public static final IElementType WHITE_SPACE = new HbElementType("WHITE_SPACE");
public static final IElementType COMMENT = new HbElementType("COMMENT");
- public static final IElementType OPEN_PARTIAL = new HbElementType("OPEN_PARTIAL");
+ public static final IElementType OPEN = new HbElementType("OPEN");
public static final IElementType OPEN_BLOCK = new HbElementType("OPEN_BLOCK");
+ public static final IElementType OPEN_PARTIAL = new HbElementType("OPEN_PARTIAL");
public static final IElementType OPEN_ENDBLOCK = new HbElementType("OPEN_ENDBLOCK");
public static final IElementType OPEN_INVERSE = new HbElementType("OPEN_INVERSE");
public static final IElementType OPEN_UNESCAPED = new HbElementType("OPEN_UNESCAPED");
- public static final IElementType OPEN = new HbElementType("OPEN");
public static final IElementType EQUALS = new HbElementType("EQUALS");
public static final IElementType ID = new HbElementType("ID");
public static final IElementType SEP = new HbElementType("SEP");
@@ -56,4 +56,16 @@ public void testSeparator() {
result.shouldMatchTokenTypes(OPEN, ID, SEP, ID, CLOSE);
result.shouldMatchTokenContent("{{", "dis", "/", "connected", "}}");
}
+
+ public void testSimplePartial() {
+ TokenizerResult result = tokenize("{{>partialId}}");
+ result.shouldMatchTokenTypes(OPEN_PARTIAL, ID, CLOSE);
+ result.shouldMatchTokenContent("{{>", "partialId", "}}");
+ }
+
+ public void testSimpleUnescaped() {
+ TokenizerResult result = tokenize("{{{partialId}}}");
+ result.shouldMatchTokenTypes(OPEN_UNESCAPED, ID, CLOSE);
+ result.shouldMatchTokenContent("{{{", "partialId", "}}}");
+ }
}

0 comments on commit cf7be85

Please sign in to comment.