DC Team 的 Laravel 基底框架、資料庫描述、開發規範、環境佈署、版控規範

formatting.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. (function() {
  2. CodeMirror.extendMode("css", {
  3. commentStart: "/*",
  4. commentEnd: "*/",
  5. newlineAfterToken: function(type, content) {
  6. return /^[;{}]$/.test(content);
  7. }
  8. });
  9. CodeMirror.extendMode("javascript", {
  10. commentStart: "/*",
  11. commentEnd: "*/",
  12. // FIXME semicolons inside of for
  13. newlineAfterToken: function(type, content, textAfter, state) {
  14. if (this.jsonMode) {
  15. return /^[\[,{]$/.test(content) || /^}/.test(textAfter);
  16. } else {
  17. if (content == ";" && state.lexical && state.lexical.type == ")") return false;
  18. return /^[;{}]$/.test(content) && !/^;/.test(textAfter);
  19. }
  20. }
  21. });
  22. CodeMirror.extendMode("xml", {
  23. commentStart: "<!--",
  24. commentEnd: "-->",
  25. newlineAfterToken: function(type, content, textAfter) {
  26. return type == "tag" && />$/.test(content) || /^</.test(textAfter);
  27. }
  28. });
  29. // Comment/uncomment the specified range
  30. CodeMirror.defineExtension("commentRange", function (isComment, from, to) {
  31. var cm = this, curMode = CodeMirror.innerMode(cm.getMode(), cm.getTokenAt(from).state).mode;
  32. cm.operation(function() {
  33. if (isComment) { // Comment range
  34. cm.replaceRange(curMode.commentEnd, to);
  35. cm.replaceRange(curMode.commentStart, from);
  36. if (from.line == to.line && from.ch == to.ch) // An empty comment inserted - put cursor inside
  37. cm.setCursor(from.line, from.ch + curMode.commentStart.length);
  38. } else { // Uncomment range
  39. var selText = cm.getRange(from, to);
  40. var startIndex = selText.indexOf(curMode.commentStart);
  41. var endIndex = selText.lastIndexOf(curMode.commentEnd);
  42. if (startIndex > -1 && endIndex > -1 && endIndex > startIndex) {
  43. // Take string till comment start
  44. selText = selText.substr(0, startIndex)
  45. // From comment start till comment end
  46. + selText.substring(startIndex + curMode.commentStart.length, endIndex)
  47. // From comment end till string end
  48. + selText.substr(endIndex + curMode.commentEnd.length);
  49. }
  50. cm.replaceRange(selText, from, to);
  51. }
  52. });
  53. });
  54. // Applies automatic mode-aware indentation to the specified range
  55. CodeMirror.defineExtension("autoIndentRange", function (from, to) {
  56. var cmInstance = this;
  57. this.operation(function () {
  58. for (var i = from.line; i <= to.line; i++) {
  59. cmInstance.indentLine(i, "smart");
  60. }
  61. });
  62. });
  63. // Applies automatic formatting to the specified range
  64. CodeMirror.defineExtension("autoFormatRange", function (from, to) {
  65. var cm = this;
  66. var outer = cm.getMode(), text = cm.getRange(from, to).split("\n");
  67. var state = CodeMirror.copyState(outer, cm.getTokenAt(from).state);
  68. var tabSize = cm.getOption("tabSize");
  69. var out = "", lines = 0, atSol = from.ch == 0;
  70. function newline() {
  71. out += "\n";
  72. atSol = true;
  73. ++lines;
  74. }
  75. for (var i = 0; i < text.length; ++i) {
  76. var stream = new CodeMirror.StringStream(text[i], tabSize);
  77. while (!stream.eol()) {
  78. var inner = CodeMirror.innerMode(outer, state);
  79. var style = outer.token(stream, state), cur = stream.current();
  80. stream.start = stream.pos;
  81. if (!atSol || /\S/.test(cur)) {
  82. out += cur;
  83. atSol = false;
  84. }
  85. if (!atSol && inner.mode.newlineAfterToken &&
  86. inner.mode.newlineAfterToken(style, cur, stream.string.slice(stream.pos) || text[i+1] || "", inner.state))
  87. newline();
  88. }
  89. if (!stream.pos && outer.blankLine) outer.blankLine(state);
  90. if (!atSol) newline();
  91. }
  92. cm.operation(function () {
  93. cm.replaceRange(out, from, to);
  94. for (var cur = from.line + 1, end = from.line + lines; cur <= end; ++cur)
  95. cm.indentLine(cur, "smart");
  96. cm.setSelection(from, cm.getCursor(false));
  97. });
  98. });
  99. })();