comparison .cms/lib/codemirror/addon/comment/continuecomment.js @ 0:78edf6b517a0 draft

24.10
author Coffee CMS <info@coffee-cms.ru>
date Fri, 11 Oct 2024 22:40:23 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:78edf6b517a0
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others
2 // Distributed under an MIT license: https://codemirror.net/5/LICENSE
3
4 (function(mod) {
5 if (typeof exports == "object" && typeof module == "object") // CommonJS
6 mod(require("../../lib/codemirror"));
7 else if (typeof define == "function" && define.amd) // AMD
8 define(["../../lib/codemirror"], mod);
9 else // Plain browser env
10 mod(CodeMirror);
11 })(function(CodeMirror) {
12 var nonspace = /\S/g;
13 var repeat = String.prototype.repeat || function (n) { return Array(n + 1).join(this); };
14 function continueComment(cm) {
15 if (cm.getOption("disableInput")) return CodeMirror.Pass;
16 var ranges = cm.listSelections(), mode, inserts = [];
17 for (var i = 0; i < ranges.length; i++) {
18 var pos = ranges[i].head
19 if (!/\bcomment\b/.test(cm.getTokenTypeAt(pos))) return CodeMirror.Pass;
20 var modeHere = cm.getModeAt(pos)
21 if (!mode) mode = modeHere;
22 else if (mode != modeHere) return CodeMirror.Pass;
23
24 var insert = null, line, found;
25 var blockStart = mode.blockCommentStart, lineCmt = mode.lineComment;
26 if (blockStart && mode.blockCommentContinue) {
27 line = cm.getLine(pos.line);
28 var end = line.lastIndexOf(mode.blockCommentEnd, pos.ch - mode.blockCommentEnd.length);
29 // 1. if this block comment ended
30 // 2. if this is actually inside a line comment
31 if (end != -1 && end == pos.ch - mode.blockCommentEnd.length ||
32 lineCmt && (found = line.lastIndexOf(lineCmt, pos.ch - 1)) > -1 &&
33 /\bcomment\b/.test(cm.getTokenTypeAt({line: pos.line, ch: found + 1}))) {
34 // ...then don't continue it
35 } else if (pos.ch >= blockStart.length &&
36 (found = line.lastIndexOf(blockStart, pos.ch - blockStart.length)) > -1 &&
37 found > end) {
38 // reuse the existing leading spaces/tabs/mixed
39 // or build the correct indent using CM's tab/indent options
40 if (nonspaceAfter(0, line) >= found) {
41 insert = line.slice(0, found);
42 } else {
43 var tabSize = cm.options.tabSize, numTabs;
44 found = CodeMirror.countColumn(line, found, tabSize);
45 insert = !cm.options.indentWithTabs ? repeat.call(" ", found) :
46 repeat.call("\t", (numTabs = Math.floor(found / tabSize))) +
47 repeat.call(" ", found - tabSize * numTabs);
48 }
49 } else if ((found = line.indexOf(mode.blockCommentContinue)) > -1 &&
50 found <= pos.ch &&
51 found <= nonspaceAfter(0, line)) {
52 insert = line.slice(0, found);
53 }
54 if (insert != null) insert += mode.blockCommentContinue
55 }
56 if (insert == null && lineCmt && continueLineCommentEnabled(cm)) {
57 if (line == null) line = cm.getLine(pos.line);
58 found = line.indexOf(lineCmt);
59 // cursor at pos 0, line comment also at pos 0 => shift it down, don't continue
60 if (!pos.ch && !found) insert = "";
61 // continue only if the line starts with an optional space + line comment
62 else if (found > -1 && nonspaceAfter(0, line) >= found) {
63 // don't continue if there's only space(s) after cursor or the end of the line
64 insert = nonspaceAfter(pos.ch, line) > -1;
65 // but always continue if the next line starts with a line comment too
66 if (!insert) {
67 var next = cm.getLine(pos.line + 1) || '',
68 nextFound = next.indexOf(lineCmt);
69 insert = nextFound > -1 && nonspaceAfter(0, next) >= nextFound || null;
70 }
71 if (insert) {
72 insert = line.slice(0, found) + lineCmt +
73 line.slice(found + lineCmt.length).match(/^\s*/)[0];
74 }
75 }
76 }
77 if (insert == null) return CodeMirror.Pass;
78 inserts[i] = "\n" + insert;
79 }
80
81 cm.operation(function() {
82 for (var i = ranges.length - 1; i >= 0; i--)
83 cm.replaceRange(inserts[i], ranges[i].from(), ranges[i].to(), "+insert");
84 });
85 }
86
87 function nonspaceAfter(ch, str) {
88 nonspace.lastIndex = ch;
89 var m = nonspace.exec(str);
90 return m ? m.index : -1;
91 }
92
93 function continueLineCommentEnabled(cm) {
94 var opt = cm.getOption("continueComments");
95 if (opt && typeof opt == "object")
96 return opt.continueLineComment !== false;
97 return true;
98 }
99
100 CodeMirror.defineOption("continueComments", null, function(cm, val, prev) {
101 if (prev && prev != CodeMirror.Init)
102 cm.removeKeyMap("continueComment");
103 if (val) {
104 var key = "Enter";
105 if (typeof val == "string")
106 key = val;
107 else if (typeof val == "object" && val.key)
108 key = val.key;
109 var map = {name: "continueComment"};
110 map[key] = continueComment;
111 cm.addKeyMap(map);
112 }
113 });
114 });