css-hint.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. (function () {
  2. "use strict";
  3. function getHints(cm) {
  4. var cur = cm.getCursor(), token = cm.getTokenAt(cur);
  5. var inner = CodeMirror.innerMode(cm.getMode(), token.state);
  6. if (inner.mode.name != "css") return;
  7. // If it's not a 'word-style' token, ignore the token.
  8. if (!/^[\w$_-]*$/.test(token.string)) {
  9. token = {
  10. start: cur.ch, end: cur.ch, string: "", state: token.state,
  11. type: null
  12. };
  13. var stack = token.state.stack;
  14. var lastToken = stack && stack.length > 0 ? stack[stack.length - 1] : "";
  15. if (token.string == ":" || lastToken.indexOf("property") == 0)
  16. token.type = "variable";
  17. else if (token.string == "{" || lastToken.indexOf("rule") == 0)
  18. token.type = "property";
  19. }
  20. if (!token.type)
  21. return;
  22. var spec = CodeMirror.resolveMode("text/css");
  23. var keywords = null;
  24. if (token.type.indexOf("property") == 0)
  25. keywords = spec.propertyKeywords;
  26. else if (token.type.indexOf("variable") == 0)
  27. keywords = spec.valueKeywords;
  28. if (!keywords)
  29. return;
  30. var result = [];
  31. for (var name in keywords) {
  32. if (name.indexOf(token.string) == 0 /* > -1 */)
  33. result.push(name);
  34. }
  35. return {
  36. list: result,
  37. from: CodeMirror.Pos(cur.line, token.start),
  38. to: CodeMirror.Pos(cur.line, token.end)
  39. };
  40. }
  41. CodeMirror.registerHelper("hint", "css", getHints);
  42. })();