Skip to content
Snippets Groups Projects
Commit 2fc14dba authored by Ankit's avatar Ankit Committed by Marijn Haverbeke
Browse files

[sass mode] Improve indentation

Fixes bug #2156 for the most part

indention was bugged because it was reaching a wrong state when
any text that was not in the known set of words or patterns was
incurred.

for eg:
	head
		| <---- cursor should be here

and lets take  color : red as another example
now 'red' is same to the current tokenizer as 'head' from the first
example. So it indents for red automatically.

It was fixed by not letting any text after colon to reach the state
where it appends an indent after it.
parent b8accafb
No related branches found
No related tags found
No related merge requests found
......@@ -22,7 +22,7 @@ CodeMirror.defineMode("sass", function(config) {
var operators = ["\\(", "\\)", "=", ">", "<", "==", ">=", "<=", "\\+", "-", "\\!=", "/", "\\*", "%", "and", "or", "not"];
var opRegexp = tokenRegexp(operators);
var pseudoElementsRegexp = /^::?[\w\-]+/;
var pseudoElementsRegexp = /^::[\w\-]+/;
function urlTokens(stream, state) {
var ch = stream.peek();
......@@ -64,7 +64,7 @@ CodeMirror.defineMode("sass", function(config) {
}
function buildStringTokenizer(quote, greedy) {
if(greedy == null) { greedy = true; }
if (greedy == null) { greedy = true; }
function stringTokenizer(stream, state) {
var nextChar = stream.next();
......@@ -189,13 +189,11 @@ CodeMirror.defineMode("sass", function(config) {
if (ch === "$") {
stream.next();
stream.eatWhile(/[\w-]/);
if (stream.peek() === ":") {
stream.next();
stream.eatSpace();
if (stream.peek() === ":")
return "variable-2";
} else {
else
return "variable-3";
}
}
if (ch === "!") {
......@@ -245,22 +243,71 @@ CodeMirror.defineMode("sass", function(config) {
return "string";
}
// Pseudo element selectors
if (ch == ":" && stream.match(pseudoElementsRegexp))
return "keyword";
// Pseudo element selectors and values after colon
if (ch === ":"){
if (stream.match(pseudoElementsRegexp))
return "keyword";
stream.next();
stream.eatSpace();
if (stream.peek() === null){
// if there is no more space after it
indent(state);
return "atom";
}
// atoms
if (stream.eatWhile(/[\w-&]/)) {
// matches a property definition
if (stream.peek() === ":" && !stream.match(pseudoElementsRegexp, false))
return "property";
else
// all posible tokens after colon
if (stream.match(/\$[\w-]+/)){
// variables
return "variable-3";
} else if (stream.match(/^url/) && stream.peek() === "(") {
//urls
state.tokenizer = urlTokens;
return "atom";
} else if (stream.match(/#[0-9a-fA-F]{6}|#[0-9a-fA-F]{3}/)){
//hexadecimal value
return "number";
} else if (stream.match(/^-?[0-9\.]+/)){
return "number";
} else if (stream.match(keywordsRegexp)){
return "keyword";
} else if (stream.match(/[\w-,\s]+/)){
// other text
return "atom";
}
}
if (stream.match(opRegexp))
return "operator";
// atoms
if (stream.eatWhile(/[\w-&]/)) {
stream.eatSpace();
// matches a property definition
if (stream.peek() === ":" ){
if (!stream.match(/:[ ]*[\d\w-\$\+#!\("']/,false)){
//for cases where line ends after colon
// eg
// font:
// | <-----cursor
indent(state);
return "property";
} else if (!stream.match(pseudoElementsRegexp, false)){
return "property";
}
} else {
stream.eatSpace();
if (stream.peek() !== "," ){
//for cases where line ends after comma
//eg
//head,
//body,
//| <-----cursor
indent(state);
}
return "atom";
}
}
// If we haven't returned by now, we move 1 character
// and return an error
stream.next();
......@@ -275,9 +322,6 @@ CodeMirror.defineMode("sass", function(config) {
if (current === "@return")
dedent(state);
if (style === "atom")
indent(state);
if (style !== null) {
var startOfToken = stream.pos - current.length;
var withCurrentIndent = startOfToken + (config.indentUnit * state.indentCount);
......
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