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

415 lines
9.8 KiB
JavaScript
Raw Permalink 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("|"));
2015-03-24 12:27:39 +03:00
var operators = ["\\(", "\\)", "=", ">", "<", "==", ">=", "<=", "\\+", "-",
"\\!=", "/", "\\*", "%", "and", "or", "not", ";","\\{","\\}",":"];
2014-08-18 17:45:43 +04:00
var opRegexp = tokenRegexp(operators);
2015-03-24 12:27:39 +03:00
var pseudoElementsRegexp = /^::?[a-zA-Z_][\w\-]*/;
2014-08-18 17:45:43 +04:00
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 {
2015-03-24 12:27:39 +03:00
stream.skipToEnd();
2014-08-30 07:26:14 +04:00
}
2014-08-18 17:45:43 +04:00
2014-08-30 07:26:14 +04:00
return "comment";
};
}
function buildStringTokenizer(quote, greedy) {
2015-03-24 12:27:39 +03:00
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";
}
2015-03-24 12:27:39 +03:00
// Strings
if (ch === '"' || ch === "'") {
2014-08-18 17:45:43 +04:00
stream.next();
2015-03-24 12:27:39 +03:00
state.tokenizer = buildStringTokenizer(ch);
return "string";
}
2014-08-18 17:45:43 +04:00
2015-03-24 12:27:39 +03:00
if(!state.cursorHalf){// state.cursorHalf === 0
// first half i.e. before : for key-value pairs
// including selectors
if (ch === ".") {
stream.next();
if (stream.match(/^[\w-]+/)) {
indent(state);
return "atom";
} else if (stream.peek() === "#") {
indent(state);
return "atom";
}
2014-08-18 17:45:43 +04:00
}
2015-03-24 12:27:39 +03:00
if (ch === "#") {
stream.next();
// ID selectors
if (stream.match(/^[\w-]+/)) {
indent(state);
return "atom";
}
if (stream.peek() === "#") {
indent(state);
return "atom";
}
}
// Variables
if (ch === "$") {
stream.next();
stream.eatWhile(/[\w-]/);
return "variable-2";
}
2014-08-18 17:45:43 +04:00
2015-03-24 12:27:39 +03:00
// Numbers
if (stream.match(/^-?[0-9\.]+/))
2014-08-18 17:45:43 +04:00
return "number";
2015-03-24 12:27:39 +03:00
// Units
if (stream.match(/^(px|em|in)\b/))
return "unit";
if (stream.match(keywordsRegexp))
return "keyword";
if (stream.match(/^url/) && stream.peek() === "(") {
state.tokenizer = urlTokens;
2014-08-18 17:45:43 +04:00
return "atom";
}
2015-03-24 12:27:39 +03:00
if (ch === "=") {
// Match shortcut mixin definition
if (stream.match(/^=[\w-]+/)) {
indent(state);
return "meta";
}
2014-08-18 17:45:43 +04:00
}
2015-03-24 12:27:39 +03:00
if (ch === "+") {
// Match shortcut mixin definition
if (stream.match(/^\+[\w-]+/)){
return "variable-3";
}
}
2014-08-18 17:45:43 +04:00
2015-03-24 12:27:39 +03:00
if(ch === "@"){
if(stream.match(/@extend/)){
if(!stream.match(/\s*[\w]/))
dedent(state);
}
}
2014-08-18 17:45:43 +04:00
2015-03-24 12:27:39 +03:00
// Indent Directives
if (stream.match(/^@(else if|if|media|else|for|each|while|mixin|function)/)) {
indent(state);
return "meta";
}
2014-08-18 17:45:43 +04:00
2015-03-24 12:27:39 +03:00
// Other Directives
if (ch === "@") {
stream.next();
stream.eatWhile(/[\w-]/);
return "meta";
}
2014-08-18 17:45:43 +04:00
2015-03-24 12:27:39 +03:00
if (stream.eatWhile(/[\w-]/)){
if(stream.match(/ *: *[\w-\+\$#!\("']/,false)){
return "property";
}
else if(stream.match(/ *:/,false)){
indent(state);
state.cursorHalf = 1;
return "atom";
}
else if(stream.match(/ *,/,false)){
return "atom";
}
else{
indent(state);
return "atom";
}
}
if(ch === ":"){
if (stream.match(pseudoElementsRegexp)){ // could be a pseudo-element
return "keyword";
}
2014-08-18 17:45:43 +04:00
stream.next();
2015-03-24 12:27:39 +03:00
state.cursorHalf=1;
return "operator";
2014-08-18 17:45:43 +04:00
}
2015-03-24 12:27:39 +03:00
} // cursorHalf===0 ends here
else{
2014-08-18 17:45:43 +04:00
2015-03-24 12:27:39 +03:00
if (ch === "#") {
stream.next();
// Hex numbers
if (stream.match(/[0-9a-fA-F]{6}|[0-9a-fA-F]{3}/)){
if(!stream.peek()){
state.cursorHalf = 0;
}
return "number";
}
}
2014-08-18 17:45:43 +04:00
2015-03-24 12:27:39 +03:00
// Numbers
if (stream.match(/^-?[0-9\.]+/)){
if(!stream.peek()){
state.cursorHalf = 0;
}
return "number";
2014-08-18 17:45:43 +04:00
}
2015-03-24 12:27:39 +03:00
// Units
if (stream.match(/^(px|em|in)\b/)){
if(!stream.peek()){
state.cursorHalf = 0;
}
return "unit";
}
2014-08-18 17:45:43 +04:00
2015-03-24 12:27:39 +03:00
if (stream.match(keywordsRegexp)){
if(!stream.peek()){
state.cursorHalf = 0;
}
return "keyword";
}
if (stream.match(/^url/) && stream.peek() === "(") {
state.tokenizer = urlTokens;
if(!stream.peek()){
state.cursorHalf = 0;
}
return "atom";
}
// Variables
if (ch === "$") {
stream.next();
stream.eatWhile(/[\w-]/);
if(!stream.peek()){
state.cursorHalf = 0;
}
2014-08-18 17:45:43 +04:00
return "variable-3";
2015-03-24 12:27:39 +03:00
}
2014-08-18 17:45:43 +04:00
2015-03-24 12:27:39 +03:00
// bang character for !important, !default, etc.
if (ch === "!") {
stream.next();
if(!stream.peek()){
state.cursorHalf = 0;
}
return stream.match(/^[\w]+/) ? "keyword": "operator";
}
2014-08-18 17:45:43 +04:00
2015-03-24 12:27:39 +03:00
if (stream.match(opRegexp)){
if(!stream.peek()){
state.cursorHalf = 0;
}
return "operator";
}
2014-08-18 17:45:43 +04:00
2015-03-24 12:27:39 +03:00
// attributes
if (stream.eatWhile(/[\w-]/)) {
if(!stream.peek()){
state.cursorHalf = 0;
}
return "attribute";
}
2014-08-18 17:45:43 +04:00
2015-03-24 12:27:39 +03:00
//stream.eatSpace();
if(!stream.peek()){
state.cursorHalf = 0;
return null;
}
2014-08-18 17:45:43 +04:00
2015-03-24 12:27:39 +03:00
} // else ends here
2014-08-18 17:45:43 +04:00
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();
2015-03-24 12:27:39 +03:00
if (current === "@return" || current === "}"){
2014-08-18 17:45:43 +04:00
dedent(state);
2015-03-24 12:27:39 +03:00
}
2014-08-18 17:45:43 +04:00
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;
2015-03-24 12:27:39 +03:00
2014-08-18 17:45:43 +04:00
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,
2015-03-24 12:27:39 +03:00
cursorHalf: 0, // cursor half tells us if cursor lies after (1)
// or before (0) colon (well... more or less)
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");
});