Skip to content
Snippets Groups Projects
Commit 26c9bd8a authored by Marijn Haverbeke's avatar Marijn Haverbeke
Browse files

less chaotic indentation for CSS mode

parent 273180b7
No related branches found
No related tags found
No related merge requests found
......@@ -4,7 +4,7 @@ CodeMirror.defineMode("css", function(config) {
function tokenBase(stream, state) {
var ch = stream.next();
if (ch == "@") return ret("css-at", ch + stream.eatWhile(/\w/));
if (ch == "@") {stream.eatWhile(/\w/); return ret("css-at", stream.current());}
else if (ch == "/" && stream.eat("*")) {
state.tokenize = tokenCComment;
return tokenCComment(stream, state);
......@@ -21,7 +21,7 @@ CodeMirror.defineMode("css", function(config) {
}
else if (ch == "#") {
stream.eatWhile(/\w/);
return ret("css-hash", "hash");
return ret("css-selector", "hash");
}
else if (ch == "!") {
stream.match(/^\s*\w*/);
......@@ -84,40 +84,37 @@ CodeMirror.defineMode("css", function(config) {
startState: function(base) {
return {tokenize: tokenBase,
baseIndent: base || 0,
inBraces: false, inRule: false, inDecl: false};
stack: []};
},
token: function(stream, state) {
if (stream.eatSpace()) return null;
var style = state.tokenize(stream, state);
if (type == "hash")
style = state.inRule ? "css-colorcode" : "css-identifier";
if (style == "css-identifier") {
if (state.inRule) style = "css-value";
else if (!state.inBraces && !state.inDecl) style = "css-selector";
var context = state.stack[state.stack.length-1];
if (type == "hash" && context == "rule") style = "css-colorcode";
else if (style == "css-identifier") {
if (context == "rule") style = "css-value";
else if (!context || context == "@media{") style = "css-selector";
}
if (type == "{" && state.inDecl == "@media")
state.inDecl = false;
else if (type == "{")
state.inBraces = true;
else if (type == "}")
state.inBraces = state.inRule = state.inDecl = false;
else if (type == ";")
state.inRule = state.inDecl = false;
else if (state.inBraces && type != "comment")
state.inRule = true;
else if (!state.inBraces && style == "css-at")
state.inDecl = type;
if (context == "rule" && /^[\{\};]$/.test(type))
state.stack.pop();
if (type == "{") {
if (context == "@media") state.stack[state.stack.length-1] = "@media{";
else state.stack.push("{");
}
else if (type == "}") state.stack.pop();
else if (type == "@media") state.stack.push("@media");
else if (context != "rule" && context != "@media" && type != "comment") state.stack.push("rule");
return style;
},
indent: function(state, textAfter) {
if (!state.inBraces || /^\}/.test(textAfter)) return state.baseIndent;
else if (state.inRule) return state.baseIndent + indentUnit * 2;
else return state.baseIndent + indentUnit;
var n = state.stack.length;
if (/^\}/.test(textAfter))
n -= state.stack[state.stack.length-1] == "rule" ? 2 : 1;
return state.baseIndent + n * indentUnit;
}
};
});
......
......@@ -6,7 +6,7 @@
<script src="../../lib/codemirror.js"></script>
<script src="css.js"></script>
<link rel="stylesheet" href="css.css">
<style>.CodeMirror {background: #f6f8ff;}</style>
<style>.CodeMirror {background: #f8f8f8;}</style>
</head>
<body>
<h1>CodeMirror 2: CSS mode</h1>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment