trailingspace.js 1.2 KB

12345678910111213141516171819202122232425262728293031
  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'))
  7. else if (typeof define == 'function' && define.amd)
  8. // AMD
  9. define(['../../lib/codemirror'], mod)
  10. // Plain browser env
  11. else mod(CodeMirror)
  12. })(function (CodeMirror) {
  13. CodeMirror.defineOption('showTrailingSpace', false, function (cm, val, prev) {
  14. if (prev == CodeMirror.Init) prev = false
  15. if (prev && !val) cm.removeOverlay('trailingspace')
  16. else if (!prev && val)
  17. cm.addOverlay({
  18. token: function (stream) {
  19. for (var l = stream.string.length, i = l; i && /\s/.test(stream.string.charAt(i - 1)); --i) {}
  20. if (i > stream.pos) {
  21. stream.pos = i
  22. return null
  23. }
  24. stream.pos = l
  25. return 'trailingspace'
  26. },
  27. name: 'trailingspace',
  28. })
  29. })
  30. })