properties.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. 'use strict'
  14. CodeMirror.defineMode('properties', function () {
  15. return {
  16. token: function (stream, state) {
  17. var sol = stream.sol() || state.afterSection
  18. var eol = stream.eol()
  19. state.afterSection = false
  20. if (sol) {
  21. if (state.nextMultiline) {
  22. state.inMultiline = true
  23. state.nextMultiline = false
  24. } else {
  25. state.position = 'def'
  26. }
  27. }
  28. if (eol && !state.nextMultiline) {
  29. state.inMultiline = false
  30. state.position = 'def'
  31. }
  32. if (sol) {
  33. while (stream.eatSpace()) {}
  34. }
  35. var ch = stream.next()
  36. if (sol && (ch === '#' || ch === '!' || ch === ';')) {
  37. state.position = 'comment'
  38. stream.skipToEnd()
  39. return 'comment'
  40. } else if (sol && ch === '[') {
  41. state.afterSection = true
  42. stream.skipTo(']')
  43. stream.eat(']')
  44. return 'header'
  45. } else if (ch === '=' || ch === ':') {
  46. state.position = 'quote'
  47. return null
  48. } else if (ch === '\\' && state.position === 'quote') {
  49. if (stream.eol()) {
  50. // end of line?
  51. // Multiline value
  52. state.nextMultiline = true
  53. }
  54. }
  55. return state.position
  56. },
  57. startState: function () {
  58. return {
  59. position: 'def', // Current position, "def", "quote" or "comment"
  60. nextMultiline: false, // Is the next line multiline value
  61. inMultiline: false, // Is the current line a multiline value
  62. afterSection: false, // Did we just open a section
  63. }
  64. },
  65. }
  66. })
  67. CodeMirror.defineMIME('text/x-properties', 'properties')
  68. CodeMirror.defineMIME('text/x-ini', 'properties')
  69. })