Skip to content

Commit fad3f49

Browse files
committed
[css-hint addon] Rewrite to work with overhauled css mode
1 parent 6ad7706 commit fad3f49

File tree

3 files changed

+35
-37
lines changed

3 files changed

+35
-37
lines changed

addon/hint/css-hint.js

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,46 @@
11
(function () {
22
"use strict";
33

4-
function getHints(cm) {
4+
var pseudoClasses = {link: 1, visited: 1, active: 1, hover: 1, focus: 1,
5+
"first-letter": 1, "first-line": 1, "first-child": 1,
6+
before: 1, after: 1, lang: 1};
7+
8+
CodeMirror.registerHelper("hint", "css", function(cm) {
59
var cur = cm.getCursor(), token = cm.getTokenAt(cur);
610
var inner = CodeMirror.innerMode(cm.getMode(), token.state);
711
if (inner.mode.name != "css") return;
812

9-
// If it's not a 'word-style' token, ignore the token.
10-
if (!/^[\w$_-]*$/.test(token.string)) {
11-
token = {
12-
start: cur.ch, end: cur.ch, string: "", state: token.state,
13-
type: null
14-
};
15-
var stack = token.state.stack;
16-
var lastToken = stack && stack.length > 0 ? stack[stack.length - 1] : "";
17-
if (token.string == ":" || lastToken.indexOf("property") == 0)
18-
token.type = "variable";
19-
else if (token.string == "{" || lastToken.indexOf("rule") == 0)
20-
token.type = "property";
13+
var word = token.string, start = token.start, end = token.end;
14+
if (/[^\w$_-]/.test(word)) {
15+
word = ""; start = end = cur.ch;
2116
}
2217

23-
if (!token.type)
24-
return;
25-
2618
var spec = CodeMirror.resolveMode("text/css");
27-
var keywords = null;
28-
if (token.type.indexOf("property") == 0)
29-
keywords = spec.propertyKeywords;
30-
else if (token.type.indexOf("variable") == 0)
31-
keywords = spec.valueKeywords;
32-
33-
if (!keywords)
34-
return;
3519

3620
var result = [];
37-
for (var name in keywords) {
38-
if (name.indexOf(token.string) == 0 /* > -1 */)
39-
result.push(name);
21+
function add(keywords) {
22+
for (var name in keywords)
23+
if (!word || name.indexOf(word) == 0)
24+
result.push(name);
4025
}
4126

42-
return {
27+
var st = token.state.state;
28+
if (st == "pseudo" || token.type == "variable-3") {
29+
add(pseudoClasses);
30+
} else if (st == "block" || st == "maybeprop") {
31+
add(spec.propertyKeywords);
32+
} else if (st == "prop" || st == "parens" || st == "at" || st == "params") {
33+
add(spec.valueKeywords);
34+
add(spec.colorKeywords);
35+
} else if (st == "media" || st == "media_parens") {
36+
add(spec.mediaTypes);
37+
add(spec.mediaFeatures);
38+
}
39+
40+
if (result.length) return {
4341
list: result,
44-
from: CodeMirror.Pos(cur.line, token.start),
45-
to: CodeMirror.Pos(cur.line, token.end)
42+
from: CodeMirror.Pos(cur.line, start),
43+
to: CodeMirror.Pos(cur.line, end)
4644
};
47-
}
48-
49-
CodeMirror.registerHelper("hint", "css", getHints);
45+
});
5046
})();

doc/manual.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2159,7 +2159,7 @@ <h2>Addons</h2>
21592159
the <a href="../demo/html5complete.html">demo</a>.</dd>
21602160

21612161
<dt id="addon_css-hint"><a href="../addon/hint/css-hint.js"><code>hint/css-hint.js</code></a></dt>
2162-
<dd>A minimal hinting function for CSS code.
2162+
<dd>A hinting function for CSS, SCSS, or LESS code.
21632163
Defines <code>CodeMirror.hint.css</code>.</dd>
21642164

21652165
<dt id="addon_python-hint"><a href="../addon/hint/python-hint.js"><code>hint/python-hint.js</code></a></dt>

mode/css/css.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ CodeMirror.defineMode("css", function(config, parserConfig) {
206206

207207
states.pseudo = function(type, stream, state) {
208208
if (type == "word") {
209-
override = "variable-2";
209+
override = "variable-3";
210210
return state.context.type;
211211
}
212212
return pass(type, stream, state);
@@ -601,7 +601,8 @@ CodeMirror.defineMode("css", function(config, parserConfig) {
601601
return [null, "interpolation"];
602602
}
603603
},
604-
name: "css"
604+
name: "css",
605+
helperType: "scss"
605606
});
606607

607608
CodeMirror.defineMIME("text/x-less", {
@@ -634,6 +635,7 @@ CodeMirror.defineMode("css", function(config, parserConfig) {
634635
return ["atom", "atom"];
635636
}
636637
},
637-
name: "css"
638+
name: "css",
639+
helperType: "less"
638640
});
639641
})();

0 commit comments

Comments
 (0)