properties.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. (function(mod) {
  2. if (typeof exports == "object" && typeof module == "object") // CommonJS
  3. mod(require("../../lib/codemirror"));
  4. else if (typeof define == "function" && define.amd) // AMD
  5. define(["../../lib/codemirror"], mod);
  6. else // Plain browser env
  7. mod(CodeMirror);
  8. })(function(CodeMirror) {
  9. "use strict";
  10. CodeMirror.defineMode("properties", function() {
  11. return {
  12. token: function(stream, state) {
  13. var sol = stream.sol() || state.afterSection;
  14. var eol = stream.eol();
  15. state.afterSection = false;
  16. if (sol) {
  17. if (state.nextMultiline) {
  18. state.inMultiline = true;
  19. state.nextMultiline = false;
  20. } else {
  21. state.position = "def";
  22. }
  23. }
  24. if (eol && ! state.nextMultiline) {
  25. state.inMultiline = false;
  26. state.position = "def";
  27. }
  28. if (sol) {
  29. while(stream.eatSpace());
  30. }
  31. var ch = stream.next();
  32. if (sol && (ch === "#" || ch === "!" || ch === ";")) {
  33. state.position = "comment";
  34. stream.skipToEnd();
  35. return "comment";
  36. } else if (sol && ch === "[") {
  37. state.afterSection = true;
  38. stream.skipTo("]"); stream.eat("]");
  39. return "header";
  40. } else if (ch === "=" || ch === ":") {
  41. state.position = "quote";
  42. return null;
  43. } else if (ch === "\\" && state.position === "quote") {
  44. if (stream.next() !== "u") { // u = Unicode sequence \u1234
  45. // Multiline value
  46. state.nextMultiline = true;
  47. }
  48. }
  49. return state.position;
  50. },
  51. startState: function() {
  52. return {
  53. position : "def", // Current position, "def", "quote" or "comment"
  54. nextMultiline : false, // Is the next line multiline value
  55. inMultiline : false, // Is the current line a multiline value
  56. afterSection : false // Did we just open a section
  57. };
  58. }
  59. };
  60. });
  61. CodeMirror.defineMIME("text/x-properties", "properties");
  62. CodeMirror.defineMIME("text/x-ini", "properties");
  63. });