css-hint.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://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. if (token.type == "keyword" && "!important".indexOf(token.string) == 0)
  20. return {list: ["!important"], from: CodeMirror.Pos(cur.line, token.start),
  21. to: CodeMirror.Pos(cur.line, token.end)};
  22. var start = token.start, end = cur.ch, word = token.string.slice(0, end - start);
  23. if (/[^\w$_-]/.test(word)) {
  24. word = ""; start = end = cur.ch;
  25. }
  26. var spec = CodeMirror.resolveMode("text/css");
  27. var result = [];
  28. function add(keywords) {
  29. for (var name in keywords)
  30. if (!word || name.lastIndexOf(word, 0) == 0)
  31. result.push(name);
  32. }
  33. var st = inner.state.state;
  34. if (st == "pseudo" || token.type == "variable-3") {
  35. add(pseudoClasses);
  36. } else if (st == "block" || st == "maybeprop") {
  37. add(spec.propertyKeywords);
  38. } else if (st == "prop" || st == "parens" || st == "at" || st == "params") {
  39. add(spec.valueKeywords);
  40. add(spec.colorKeywords);
  41. } else if (st == "media" || st == "media_parens") {
  42. add(spec.mediaTypes);
  43. add(spec.mediaFeatures);
  44. }
  45. if (result.length) return {
  46. list: result,
  47. from: CodeMirror.Pos(cur.line, start),
  48. to: CodeMirror.Pos(cur.line, end)
  49. };
  50. });
  51. });