css-hint.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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')
  5. // CommonJS
  6. mod(require('../../lib/codemirror'), require('../../mode/css/css'))
  7. else if (typeof define == 'function' && define.amd)
  8. // AMD
  9. define(['../../lib/codemirror', '../../mode/css/css'], mod)
  10. // Plain browser env
  11. else mod(CodeMirror)
  12. })(function (CodeMirror) {
  13. 'use strict'
  14. var pseudoClasses = {
  15. active: 1,
  16. after: 1,
  17. before: 1,
  18. checked: 1,
  19. default: 1,
  20. disabled: 1,
  21. empty: 1,
  22. enabled: 1,
  23. 'first-child': 1,
  24. 'first-letter': 1,
  25. 'first-line': 1,
  26. 'first-of-type': 1,
  27. focus: 1,
  28. hover: 1,
  29. 'in-range': 1,
  30. indeterminate: 1,
  31. invalid: 1,
  32. lang: 1,
  33. 'last-child': 1,
  34. 'last-of-type': 1,
  35. link: 1,
  36. not: 1,
  37. 'nth-child': 1,
  38. 'nth-last-child': 1,
  39. 'nth-last-of-type': 1,
  40. 'nth-of-type': 1,
  41. 'only-of-type': 1,
  42. 'only-child': 1,
  43. optional: 1,
  44. 'out-of-range': 1,
  45. placeholder: 1,
  46. 'read-only': 1,
  47. 'read-write': 1,
  48. required: 1,
  49. root: 1,
  50. selection: 1,
  51. target: 1,
  52. valid: 1,
  53. visited: 1,
  54. }
  55. CodeMirror.registerHelper('hint', 'css', function (cm) {
  56. var cur = cm.getCursor(),
  57. token = cm.getTokenAt(cur)
  58. var inner = CodeMirror.innerMode(cm.getMode(), token.state)
  59. if (inner.mode.name != 'css') return
  60. if (token.type == 'keyword' && '!important'.indexOf(token.string) == 0) return { list: ['!important'], from: CodeMirror.Pos(cur.line, token.start), to: CodeMirror.Pos(cur.line, token.end) }
  61. var start = token.start,
  62. end = cur.ch,
  63. word = token.string.slice(0, end - start)
  64. if (/[^\w$_-]/.test(word)) {
  65. word = ''
  66. start = end = cur.ch
  67. }
  68. var spec = CodeMirror.resolveMode('text/css')
  69. var result = []
  70. function add(keywords) {
  71. for (var name in keywords) if (!word || name.lastIndexOf(word, 0) == 0) result.push(name)
  72. }
  73. var st = inner.state.state
  74. if (st == 'pseudo' || token.type == 'variable-3') {
  75. add(pseudoClasses)
  76. } else if (st == 'block' || st == 'maybeprop') {
  77. add(spec.propertyKeywords)
  78. } else if (st == 'prop' || st == 'parens' || st == 'at' || st == 'params') {
  79. add(spec.valueKeywords)
  80. add(spec.colorKeywords)
  81. } else if (st == 'media' || st == 'media_parens') {
  82. add(spec.mediaTypes)
  83. add(spec.mediaFeatures)
  84. }
  85. if (result.length)
  86. return {
  87. list: result,
  88. from: CodeMirror.Pos(cur.line, start),
  89. to: CodeMirror.Pos(cur.line, end),
  90. }
  91. })
  92. })