insomnia/app/ui/components/codemirror/modes/nunjucks.js
Gregory Schier e2c72987e8 Autocomplete for variables and tags (#112)
* Fixed duplication kve bug

* Autocomplete sort of works now

* Even better

* Styled autocomplete dropdown

* Minor tweaks

* Autocomplete looking pretty spiffy

* Bug fixes

* Apply dropdown to all editors

* Fixed key propagation when autocomplete open

* Fixed some modals

* Split up editor less

* Some css improvements

* Move filter help out of editor

* Fixed drag-n-drop

* Perfected autocomplete theme

* "fixed" one-line-editor hint click bug

* Better autocomplete and switch single line input on drag enter

* Don't autocomplete on Tab

* Better tag dnd

* Add constants completion API

* Autocomplete headers

* Fixed tests
2017-03-11 16:31:23 -08:00

64 lines
1.6 KiB
JavaScript

import CodeMirror from 'codemirror';
CodeMirror.defineMode('nunjucks', (config, parserConfig) => {
const baseMode = CodeMirror.getMode(config, parserConfig.baseMode || 'text/plain');
const nunjucksMode = _nunjucksMode();
return CodeMirror.overlayMode(baseMode, nunjucksMode, false);
});
function _nunjucksMode () {
const regexVariable = /^{{\s*([^ ]+)\s*[^}]*\s*}}/; // TODO: This breaks when "{{foo}}{{bar}}"
const regexTag = /^{%\s*([^ ]+)\s*[^%]*\s*%}/;
const regexComment = /^{#\s*[^#]+\s*#}/;
let ticker = 1;
return {
startState () {
return {inRaw: false};
},
token (stream, state) {
let m;
// This makes sure that adjacent tags still have unique types
ticker *= -1;
m = stream.match(regexTag, true);
if (m) {
const name = m[1];
if (state.inRaw && name === 'endraw') {
state.inRaw = false;
} else if (!state.inRaw && name === 'raw') {
state.inRaw = true;
} else if (state.inRaw) {
// Inside raw tag so do nothing
return null;
}
return `nunjucks-tag ${ticker}`;
}
if (!state.inRaw) {
m = stream.match(regexVariable, true);
if (m) {
return `nunjucks-variable ${ticker}`;
}
}
if (!state.inRaw) {
m = stream.match(regexComment, true);
if (m) {
return `nunjucks-comment ${ticker}`;
}
}
while (stream.next() != null) {
if (stream.match(regexVariable, false)) break;
if (stream.match(regexTag, false)) break;
if (stream.match(regexComment, false)) break;
}
return null;
}
};
}