Skip to content

Commit

Permalink
Define xml state inspection methods for html/angular modes
Browse files Browse the repository at this point in the history
Issue #266
  • Loading branch information
marijnh committed Sep 11, 2019
1 parent ca49b24 commit 47fe841
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/angulartemplate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as CodeMirror from "codemirror"
import "codemirror-grammar-mode"
import * as grammar from "./angulartemplate.mode"
import {predicates, indent} from "./html_util"
import {predicates, indent, contextInfo} from "./html_util"

class AngularTemplateMode extends CodeMirror.GrammarMode {
constructor(conf, modeConf) {
Expand All @@ -19,5 +19,6 @@ proto.electricInput = /^\s*<\/.*?>/
proto.blockCommentStart = "<!--"
proto.blockCommentEnd = "-->"
proto.fold = "xml"
contextInfo(proto)

CodeMirror.defineMode("google-angular-template", (conf, modeConf) => new AngularTemplateMode(conf, modeConf))
6 changes: 5 additions & 1 deletion src/html.grammar
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ context tag {
openTag
(asTag("/>") |
&selfClosing asTag(">") |
asTag(">") element* (~"</" (&matchingTag asTag("</") spaceChar* tagName asTag(">"))?))
asTag(">") element* (~"</" (&matchingTag closeTag)?))
}

context openTag {
asTag("<") !(spaceChar* "/") spaceChar* tagName spaceChar* attribute*
}

context closeTag {
asTag("</") spaceChar* tagName asTag(">")
}

asTag(value)="tag" { value }

attribute {
Expand Down
3 changes: 2 additions & 1 deletion src/html.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as CodeMirror from "codemirror"
import "codemirror-grammar-mode"
import * as grammar from "./html.mode"
import {predicates, indent} from "./html_util"
import {predicates, indent, contextInfo} from "./html_util"

class HTMLMode extends CodeMirror.GrammarMode {
constructor(conf, modeConf) {
Expand All @@ -19,5 +19,6 @@ proto.electricInput = /^\s*<\/.*?>/
proto.blockCommentStart = "<!--"
proto.blockCommentEnd = "-->"
proto.fold = "xml"
contextInfo(proto)

CodeMirror.defineMode("google-html", (conf, modeConf) => new HTMLMode(conf, modeConf))
19 changes: 19 additions & 0 deletions src/html_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,22 @@ export function indent(state, textAfter, line, config) {
}
return 0
}

export function contextInfo(proto) {
proto.xmlCurrentTag = state => {
let cx = state.context
if (!cx || (cx.name != "openTag" || cx.name != "closeTag")) return null
let match = /<\/?\s*([\w\-\.]+)/.exec(cx.startLine.slice(cx.startPos))
return match ? {name: match[1], close: cx.name == "closeTag"} : null
}
proto.xmlCurrentContext = state => {
let context = []
for (let cx = state.context; cx; cx = cx.parent) {
if (cx.name == "tag") {
let match = /<\/?\s*([\w\-\.]+)/.exec(cx.startLine.slice(cx.startPos))
if (match) context.push(match[1])
}
}
return context.reverse()
}
}

0 comments on commit 47fe841

Please sign in to comment.