comment.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. var noOptions = {}
  15. var nonWS = /[^\s\u00a0]/
  16. var Pos = CodeMirror.Pos,
  17. cmp = CodeMirror.cmpPos
  18. function firstNonWS(str) {
  19. var found = str.search(nonWS)
  20. return found == -1 ? 0 : found
  21. }
  22. CodeMirror.commands.toggleComment = function (cm) {
  23. cm.toggleComment()
  24. }
  25. CodeMirror.defineExtension('toggleComment', function (options) {
  26. if (!options) options = noOptions
  27. var cm = this
  28. var minLine = Infinity,
  29. ranges = this.listSelections(),
  30. mode = null
  31. for (var i = ranges.length - 1; i >= 0; i--) {
  32. var from = ranges[i].from(),
  33. to = ranges[i].to()
  34. if (from.line >= minLine) continue
  35. if (to.line >= minLine) to = Pos(minLine, 0)
  36. minLine = from.line
  37. if (mode == null) {
  38. if (cm.uncomment(from, to, options)) mode = 'un'
  39. else {
  40. cm.lineComment(from, to, options)
  41. mode = 'line'
  42. }
  43. } else if (mode == 'un') {
  44. cm.uncomment(from, to, options)
  45. } else {
  46. cm.lineComment(from, to, options)
  47. }
  48. }
  49. })
  50. // Rough heuristic to try and detect lines that are part of multi-line string
  51. function probablyInsideString(cm, pos, line) {
  52. return /\bstring\b/.test(cm.getTokenTypeAt(Pos(pos.line, 0))) && !/^[\'\"\`]/.test(line)
  53. }
  54. function getMode(cm, pos) {
  55. var mode = cm.getMode()
  56. return mode.useInnerComments === false || !mode.innerMode ? mode : cm.getModeAt(pos)
  57. }
  58. CodeMirror.defineExtension('lineComment', function (from, to, options) {
  59. if (!options) options = noOptions
  60. var self = this,
  61. mode = getMode(self, from)
  62. var firstLine = self.getLine(from.line)
  63. if (firstLine == null || probablyInsideString(self, from, firstLine)) return
  64. var commentString = options.lineComment || mode.lineComment
  65. if (!commentString) {
  66. if (options.blockCommentStart || mode.blockCommentStart) {
  67. options.fullLines = true
  68. self.blockComment(from, to, options)
  69. }
  70. return
  71. }
  72. var end = Math.min(to.ch != 0 || to.line == from.line ? to.line + 1 : to.line, self.lastLine() + 1)
  73. var pad = options.padding == null ? ' ' : options.padding
  74. var blankLines = options.commentBlankLines || from.line == to.line
  75. self.operation(function () {
  76. if (options.indent) {
  77. var baseString = null
  78. for (var i = from.line; i < end; ++i) {
  79. var line = self.getLine(i)
  80. var whitespace = line.slice(0, firstNonWS(line))
  81. if (baseString == null || baseString.length > whitespace.length) {
  82. baseString = whitespace
  83. }
  84. }
  85. for (var i = from.line; i < end; ++i) {
  86. var line = self.getLine(i),
  87. cut = baseString.length
  88. if (!blankLines && !nonWS.test(line)) continue
  89. if (line.slice(0, cut) != baseString) cut = firstNonWS(line)
  90. self.replaceRange(baseString + commentString + pad, Pos(i, 0), Pos(i, cut))
  91. }
  92. } else {
  93. for (var i = from.line; i < end; ++i) {
  94. if (blankLines || nonWS.test(self.getLine(i))) self.replaceRange(commentString + pad, Pos(i, 0))
  95. }
  96. }
  97. })
  98. })
  99. CodeMirror.defineExtension('blockComment', function (from, to, options) {
  100. if (!options) options = noOptions
  101. var self = this,
  102. mode = getMode(self, from)
  103. var startString = options.blockCommentStart || mode.blockCommentStart
  104. var endString = options.blockCommentEnd || mode.blockCommentEnd
  105. if (!startString || !endString) {
  106. if ((options.lineComment || mode.lineComment) && options.fullLines != false) self.lineComment(from, to, options)
  107. return
  108. }
  109. if (/\bcomment\b/.test(self.getTokenTypeAt(Pos(from.line, 0)))) return
  110. var end = Math.min(to.line, self.lastLine())
  111. if (end != from.line && to.ch == 0 && nonWS.test(self.getLine(end))) --end
  112. var pad = options.padding == null ? ' ' : options.padding
  113. if (from.line > end) return
  114. self.operation(function () {
  115. if (options.fullLines != false) {
  116. var lastLineHasText = nonWS.test(self.getLine(end))
  117. self.replaceRange(pad + endString, Pos(end))
  118. self.replaceRange(startString + pad, Pos(from.line, 0))
  119. var lead = options.blockCommentLead || mode.blockCommentLead
  120. if (lead != null) for (var i = from.line + 1; i <= end; ++i) if (i != end || lastLineHasText) self.replaceRange(lead + pad, Pos(i, 0))
  121. } else {
  122. var atCursor = cmp(self.getCursor('to'), to) == 0,
  123. empty = !self.somethingSelected()
  124. self.replaceRange(endString, to)
  125. if (atCursor) self.setSelection(empty ? to : self.getCursor('from'), to)
  126. self.replaceRange(startString, from)
  127. }
  128. })
  129. })
  130. CodeMirror.defineExtension('uncomment', function (from, to, options) {
  131. if (!options) options = noOptions
  132. var self = this,
  133. mode = getMode(self, from)
  134. var end = Math.min(to.ch != 0 || to.line == from.line ? to.line : to.line - 1, self.lastLine()),
  135. start = Math.min(from.line, end)
  136. // Try finding line comments
  137. var lineString = options.lineComment || mode.lineComment,
  138. lines = []
  139. var pad = options.padding == null ? ' ' : options.padding,
  140. didSomething
  141. lineComment: {
  142. if (!lineString) break lineComment
  143. for (var i = start; i <= end; ++i) {
  144. var line = self.getLine(i)
  145. var found = line.indexOf(lineString)
  146. if (found > -1 && !/comment/.test(self.getTokenTypeAt(Pos(i, found + 1)))) found = -1
  147. if (found == -1 && nonWS.test(line)) break lineComment
  148. if (found > -1 && nonWS.test(line.slice(0, found))) break lineComment
  149. lines.push(line)
  150. }
  151. self.operation(function () {
  152. for (var i = start; i <= end; ++i) {
  153. var line = lines[i - start]
  154. var pos = line.indexOf(lineString),
  155. endPos = pos + lineString.length
  156. if (pos < 0) continue
  157. if (line.slice(endPos, endPos + pad.length) == pad) endPos += pad.length
  158. didSomething = true
  159. self.replaceRange('', Pos(i, pos), Pos(i, endPos))
  160. }
  161. })
  162. if (didSomething) return true
  163. }
  164. // Try block comments
  165. var startString = options.blockCommentStart || mode.blockCommentStart
  166. var endString = options.blockCommentEnd || mode.blockCommentEnd
  167. if (!startString || !endString) return false
  168. var lead = options.blockCommentLead || mode.blockCommentLead
  169. var startLine = self.getLine(start),
  170. open = startLine.indexOf(startString)
  171. if (open == -1) return false
  172. var endLine = end == start ? startLine : self.getLine(end)
  173. var close = endLine.indexOf(endString, end == start ? open + startString.length : 0)
  174. var insideStart = Pos(start, open + 1),
  175. insideEnd = Pos(end, close + 1)
  176. if (close == -1 || !/comment/.test(self.getTokenTypeAt(insideStart)) || !/comment/.test(self.getTokenTypeAt(insideEnd)) || self.getRange(insideStart, insideEnd, '\n').indexOf(endString) > -1)
  177. return false
  178. // Avoid killing block comments completely outside the selection.
  179. // Positions of the last startString before the start of the selection, and the first endString after it.
  180. var lastStart = startLine.lastIndexOf(startString, from.ch)
  181. var firstEnd = lastStart == -1 ? -1 : startLine.slice(0, from.ch).indexOf(endString, lastStart + startString.length)
  182. if (lastStart != -1 && firstEnd != -1 && firstEnd + endString.length != from.ch) return false
  183. // Positions of the first endString after the end of the selection, and the last startString before it.
  184. firstEnd = endLine.indexOf(endString, to.ch)
  185. var almostLastStart = endLine.slice(to.ch).lastIndexOf(startString, firstEnd - to.ch)
  186. lastStart = firstEnd == -1 || almostLastStart == -1 ? -1 : to.ch + almostLastStart
  187. if (firstEnd != -1 && lastStart != -1 && lastStart != to.ch) return false
  188. self.operation(function () {
  189. self.replaceRange('', Pos(end, close - (pad && endLine.slice(close - pad.length, close) == pad ? pad.length : 0)), Pos(end, close + endString.length))
  190. var openEnd = open + startString.length
  191. if (pad && startLine.slice(openEnd, openEnd + pad.length) == pad) openEnd += pad.length
  192. self.replaceRange('', Pos(start, open), Pos(start, openEnd))
  193. if (lead)
  194. for (var i = start + 1; i <= end; ++i) {
  195. var line = self.getLine(i),
  196. found = line.indexOf(lead)
  197. if (found == -1 || nonWS.test(line.slice(0, found))) continue
  198. var foundEnd = found + lead.length
  199. if (pad && line.slice(foundEnd, foundEnd + pad.length) == pad) foundEnd += pad.length
  200. self.replaceRange('', Pos(i, found), Pos(i, foundEnd))
  201. }
  202. })
  203. return true
  204. })
  205. })