css-hint.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"), require("../../mode/css/css"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror", "../../mode/css/css"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. var pseudoClasses = {link: 1, visited: 1, active: 1, hover: 1, focus: 1,
  13. "first-letter": 1, "first-line": 1, "first-child": 1,
  14. before: 1, after: 1, lang: 1};
  15. CodeMirror.registerHelper("hint", "css", function(cm) {
  16. var cur = cm.getCursor(), token = cm.getTokenAt(cur);
  17. var inner = CodeMirror.innerMode(cm.getMode(), token.state);
  18. if (inner.mode.name != "css") return;
  19. var start = token.start, end = cur.ch, word = token.string.slice(0, end - start);
  20. if (/[^\w$_-]/.test(word)) {
  21. word = ""; start = end = cur.ch;
  22. }
  23. var spec = CodeMirror.resolveMode("text/css");
  24. var result = [];
  25. function add(keywords) {
  26. for (var name in keywords)
  27. if (!word || name.lastIndexOf(word, 0) == 0)
  28. result.push(name);
  29. }
  30. var st = inner.state.state;
  31. if (st == "pseudo" || token.type == "variable-3") {
  32. add(pseudoClasses);
  33. } else if (st == "block" || st == "maybeprop") {
  34. add(spec.propertyKeywords);
  35. } else if (st == "prop" || st == "parens" || st == "at" || st == "params") {
  36. add(spec.valueKeywords);
  37. add(spec.colorKeywords);
  38. } else if (st == "media" || st == "media_parens") {
  39. add(spec.mediaTypes);
  40. add(spec.mediaFeatures);
  41. }
  42. if (result.length) return {
  43. list: result,
  44. from: CodeMirror.Pos(cur.line, start),
  45. to: CodeMirror.Pos(cur.line, end)
  46. };
  47. });
  48. });