wide/static/js/lib/codemirror-4.5/mode/sass/sass.js

328 lines
7.6 KiB
JavaScript
Raw Normal View History

2014-08-18 17:45:43 +04:00
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: http://codemirror.net/LICENSE
(function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../lib/codemirror"));
else if (typeof define == "function" && define.amd) // AMD
define(["../../lib/codemirror"], mod);
else // Plain browser env
mod(CodeMirror);
})(function(CodeMirror) {
"use strict";
CodeMirror.defineMode("sass", function(config) {
2014-08-30 07:26:14 +04:00
function tokenRegexp(words) {
2014-08-18 17:45:43 +04:00
return new RegExp("^" + words.join("|"));
2014-08-30 07:26:14 +04:00
}
2014-08-18 17:45:43 +04:00
var keywords = ["true", "false", "null", "auto"];
var keywordsRegexp = new RegExp("^" + keywords.join("|"));
var operators = ["\\(", "\\)", "=", ">", "<", "==", ">=", "<=", "\\+", "-", "\\!=", "/", "\\*", "%", "and", "or", "not"];
var opRegexp = tokenRegexp(operators);
var pseudoElementsRegexp = /^::?[\w\-]+/;
2014-08-30 07:26:14 +04:00
function urlTokens(stream, state) {
2014-08-18 17:45:43 +04:00
var ch = stream.peek();
2014-08-30 07:26:14 +04:00
if (ch === ")") {
2014-08-18 17:45:43 +04:00
stream.next();
state.tokenizer = tokenBase;
return "operator";
2014-08-30 07:26:14 +04:00
} else if (ch === "(") {
2014-08-18 17:45:43 +04:00
stream.next();
stream.eatSpace();
return "operator";
2014-08-30 07:26:14 +04:00
} else if (ch === "'" || ch === '"') {
2014-08-18 17:45:43 +04:00
state.tokenizer = buildStringTokenizer(stream.next());
return "string";
2014-08-30 07:26:14 +04:00
} else {
2014-08-18 17:45:43 +04:00
state.tokenizer = buildStringTokenizer(")", false);
return "string";
}
2014-08-30 07:26:14 +04:00
}
function comment(indentation, multiLine) {
return function(stream, state) {
if (stream.sol() && stream.indentation() <= indentation) {
state.tokenizer = tokenBase;
return tokenBase(stream, state);
}
2014-08-18 17:45:43 +04:00
2014-08-30 07:26:14 +04:00
if (multiLine && stream.skipTo("*/")) {
stream.next();
stream.next();
state.tokenizer = tokenBase;
} else {
stream.next();
}
2014-08-18 17:45:43 +04:00
2014-08-30 07:26:14 +04:00
return "comment";
};
}
function buildStringTokenizer(quote, greedy) {
if(greedy == null) { greedy = true; }
2014-08-18 17:45:43 +04:00
2014-08-30 07:26:14 +04:00
function stringTokenizer(stream, state) {
2014-08-18 17:45:43 +04:00
var nextChar = stream.next();
var peekChar = stream.peek();
var previousChar = stream.string.charAt(stream.pos-2);
var endingString = ((nextChar !== "\\" && peekChar === quote) || (nextChar === quote && previousChar !== "\\"));
2014-08-30 07:26:14 +04:00
if (endingString) {
2014-08-18 17:45:43 +04:00
if (nextChar !== quote && greedy) { stream.next(); }
state.tokenizer = tokenBase;
return "string";
2014-08-30 07:26:14 +04:00
} else if (nextChar === "#" && peekChar === "{") {
2014-08-18 17:45:43 +04:00
state.tokenizer = buildInterpolationTokenizer(stringTokenizer);
stream.next();
return "operator";
2014-08-30 07:26:14 +04:00
} else {
2014-08-18 17:45:43 +04:00
return "string";
}
}
return stringTokenizer;
2014-08-30 07:26:14 +04:00
}
2014-08-18 17:45:43 +04:00
2014-08-30 07:26:14 +04:00
function buildInterpolationTokenizer(currentTokenizer) {
return function(stream, state) {
if (stream.peek() === "}") {
2014-08-18 17:45:43 +04:00
stream.next();
state.tokenizer = currentTokenizer;
return "operator";
2014-08-30 07:26:14 +04:00
} else {
2014-08-18 17:45:43 +04:00
return tokenBase(stream, state);
}
};
2014-08-30 07:26:14 +04:00
}
2014-08-18 17:45:43 +04:00
2014-08-30 07:26:14 +04:00
function indent(state) {
if (state.indentCount == 0) {
2014-08-18 17:45:43 +04:00
state.indentCount++;
var lastScopeOffset = state.scopes[0].offset;
var currentOffset = lastScopeOffset + config.indentUnit;
state.scopes.unshift({ offset:currentOffset });
}
2014-08-30 07:26:14 +04:00
}
2014-08-18 17:45:43 +04:00
2014-08-30 07:26:14 +04:00
function dedent(state) {
if (state.scopes.length == 1) return;
2014-08-18 17:45:43 +04:00
state.scopes.shift();
2014-08-30 07:26:14 +04:00
}
2014-08-18 17:45:43 +04:00
2014-08-30 07:26:14 +04:00
function tokenBase(stream, state) {
2014-08-18 17:45:43 +04:00
var ch = stream.peek();
2014-08-30 07:26:14 +04:00
// Comment
if (stream.match("/*")) {
state.tokenizer = comment(stream.indentation(), true);
return state.tokenizer(stream, state);
2014-08-18 17:45:43 +04:00
}
2014-08-30 07:26:14 +04:00
if (stream.match("//")) {
state.tokenizer = comment(stream.indentation(), false);
2014-08-18 17:45:43 +04:00
return state.tokenizer(stream, state);
}
// Interpolation
2014-08-30 07:26:14 +04:00
if (stream.match("#{")) {
state.tokenizer = buildInterpolationTokenizer(tokenBase);
2014-08-18 17:45:43 +04:00
return "operator";
}
2014-08-30 07:26:14 +04:00
if (ch === ".") {
2014-08-18 17:45:43 +04:00
stream.next();
// Match class selectors
2014-08-30 07:26:14 +04:00
if (stream.match(/^[\w-]+/)) {
2014-08-18 17:45:43 +04:00
indent(state);
return "atom";
2014-08-30 07:26:14 +04:00
} else if (stream.peek() === "#") {
2014-08-18 17:45:43 +04:00
indent(state);
return "atom";
2014-08-30 07:26:14 +04:00
} else {
2014-08-18 17:45:43 +04:00
return "operator";
}
}
2014-08-30 07:26:14 +04:00
if (ch === "#") {
2014-08-18 17:45:43 +04:00
stream.next();
// Hex numbers
2014-08-30 07:26:14 +04:00
if (stream.match(/[0-9a-fA-F]{6}|[0-9a-fA-F]{3}/))
2014-08-18 17:45:43 +04:00
return "number";
// ID selectors
2014-08-30 07:26:14 +04:00
if (stream.match(/^[\w-]+/)) {
2014-08-18 17:45:43 +04:00
indent(state);
return "atom";
}
2014-08-30 07:26:14 +04:00
if (stream.peek() === "#") {
2014-08-18 17:45:43 +04:00
indent(state);
return "atom";
}
}
// Numbers
2014-08-30 07:26:14 +04:00
if (stream.match(/^-?[0-9\.]+/))
2014-08-18 17:45:43 +04:00
return "number";
// Units
2014-08-30 07:26:14 +04:00
if (stream.match(/^(px|em|in)\b/))
2014-08-18 17:45:43 +04:00
return "unit";
2014-08-30 07:26:14 +04:00
if (stream.match(keywordsRegexp))
2014-08-18 17:45:43 +04:00
return "keyword";
2014-08-30 07:26:14 +04:00
if (stream.match(/^url/) && stream.peek() === "(") {
2014-08-18 17:45:43 +04:00
state.tokenizer = urlTokens;
return "atom";
}
// Variables
2014-08-30 07:26:14 +04:00
if (ch === "$") {
2014-08-18 17:45:43 +04:00
stream.next();
stream.eatWhile(/[\w-]/);
2014-08-30 07:26:14 +04:00
if (stream.peek() === ":") {
2014-08-18 17:45:43 +04:00
stream.next();
return "variable-2";
2014-08-30 07:26:14 +04:00
} else {
2014-08-18 17:45:43 +04:00
return "variable-3";
}
}
2014-08-30 07:26:14 +04:00
if (ch === "!") {
2014-08-18 17:45:43 +04:00
stream.next();
2014-08-30 07:26:14 +04:00
return stream.match(/^[\w]+/) ? "keyword": "operator";
2014-08-18 17:45:43 +04:00
}
2014-08-30 07:26:14 +04:00
if (ch === "=") {
2014-08-18 17:45:43 +04:00
stream.next();
// Match shortcut mixin definition
2014-08-30 07:26:14 +04:00
if (stream.match(/^[\w-]+/)) {
2014-08-18 17:45:43 +04:00
indent(state);
return "meta";
2014-08-30 07:26:14 +04:00
} else {
2014-08-18 17:45:43 +04:00
return "operator";
}
}
2014-08-30 07:26:14 +04:00
if (ch === "+") {
2014-08-18 17:45:43 +04:00
stream.next();
// Match shortcut mixin definition
2014-08-30 07:26:14 +04:00
if (stream.match(/^[\w-]+/))
2014-08-18 17:45:43 +04:00
return "variable-3";
2014-08-30 07:26:14 +04:00
else
2014-08-18 17:45:43 +04:00
return "operator";
}
// Indent Directives
2014-08-30 07:26:14 +04:00
if (stream.match(/^@(else if|if|media|else|for|each|while|mixin|function)/)) {
2014-08-18 17:45:43 +04:00
indent(state);
return "meta";
}
// Other Directives
2014-08-30 07:26:14 +04:00
if (ch === "@") {
2014-08-18 17:45:43 +04:00
stream.next();
stream.eatWhile(/[\w-]/);
return "meta";
}
// Strings
2014-08-30 07:26:14 +04:00
if (ch === '"' || ch === "'") {
2014-08-18 17:45:43 +04:00
stream.next();
state.tokenizer = buildStringTokenizer(ch);
return "string";
}
// Pseudo element selectors
2014-08-30 07:26:14 +04:00
if (ch == ":" && stream.match(pseudoElementsRegexp))
2014-08-18 17:45:43 +04:00
return "keyword";
// atoms
2014-08-30 07:26:14 +04:00
if (stream.eatWhile(/[\w-&]/)) {
2014-08-18 17:45:43 +04:00
// matches a property definition
if (stream.peek() === ":" && !stream.match(pseudoElementsRegexp, false))
return "property";
else
return "atom";
}
2014-08-30 07:26:14 +04:00
if (stream.match(opRegexp))
2014-08-18 17:45:43 +04:00
return "operator";
// If we haven't returned by now, we move 1 character
// and return an error
stream.next();
return null;
2014-08-30 07:26:14 +04:00
}
2014-08-18 17:45:43 +04:00
2014-08-30 07:26:14 +04:00
function tokenLexer(stream, state) {
if (stream.sol()) state.indentCount = 0;
2014-08-18 17:45:43 +04:00
var style = state.tokenizer(stream, state);
var current = stream.current();
2014-08-30 07:26:14 +04:00
if (current === "@return")
2014-08-18 17:45:43 +04:00
dedent(state);
2014-08-30 07:26:14 +04:00
if (style === "atom")
2014-08-18 17:45:43 +04:00
indent(state);
2014-08-30 07:26:14 +04:00
if (style !== null) {
2014-08-18 17:45:43 +04:00
var startOfToken = stream.pos - current.length;
var withCurrentIndent = startOfToken + (config.indentUnit * state.indentCount);
var newScopes = [];
2014-08-30 07:26:14 +04:00
for (var i = 0; i < state.scopes.length; i++) {
2014-08-18 17:45:43 +04:00
var scope = state.scopes[i];
2014-08-30 07:26:14 +04:00
if (scope.offset <= withCurrentIndent)
2014-08-18 17:45:43 +04:00
newScopes.push(scope);
}
state.scopes = newScopes;
}
return style;
2014-08-30 07:26:14 +04:00
}
2014-08-18 17:45:43 +04:00
return {
startState: function() {
return {
tokenizer: tokenBase,
2014-08-30 07:26:14 +04:00
scopes: [{offset: 0, type: "sass"}],
2014-10-27 10:49:58 +03:00
indentCount: 0,
2014-08-18 17:45:43 +04:00
definedVars: [],
definedMixins: []
};
},
token: function(stream, state) {
var style = tokenLexer(stream, state);
state.lastToken = { style: style, content: stream.current() };
return style;
},
indent: function(state) {
return state.scopes[0].offset;
}
};
});
CodeMirror.defineMIME("text/x-sass", "sass");
});