Reimplement toggle comment cursor behaviour (and fix bug)

This commit is contained in:
Benjamin Gray
2018-11-21 16:45:55 +11:00
parent a384dde3f5
commit 81bbd9c304
2 changed files with 24 additions and 2 deletions

View File

@@ -770,7 +770,8 @@ class Selection {
// * `bypassReadOnly` (optional) {Boolean} Must be `true` to modify text within a read-only editor. (default: false)
toggleLineComments (options = {}) {
if (!this.ensureWritable('toggleLineComments', options)) return
this.editor.toggleLineCommentsForBufferRows(...(this.getBufferRowRange() || []))
let bufferRowRange = this.getBufferRowRange() || [null, null]
this.editor.toggleLineCommentsForBufferRows(...bufferRowRange, {correctSelection: true, selection: this})
}
// Public: Cuts the selection until the end of the screen line.

View File

@@ -4764,7 +4764,7 @@ class TextEditor {
toggleLineCommentForBufferRow (row) { this.toggleLineCommentsForBufferRows(row, row) }
toggleLineCommentsForBufferRows (start, end) {
toggleLineCommentsForBufferRows (start, end, options={}) {
const languageMode = this.buffer.getLanguageMode()
let {commentStartString, commentEndString} =
languageMode.commentStringsForPosition &&
@@ -4794,6 +4794,27 @@ class TextEditor {
const indentLength = this.buffer.lineForRow(start).match(/^\s*/)[0].length
this.buffer.insert([start, indentLength], commentStartString + ' ')
this.buffer.insert([end, this.buffer.lineLengthForRow(end)], ' ' + commentEndString)
// Prevent the cursor from selecting / passing the delimiters
if (options.correctSelection && options.selection) {
let endLineLength = this.buffer.lineLengthForRow(end)
let startDelta, endDelta
let oldRange = options.selection.getBufferRange()
if (oldRange.isEmpty()) {
if (oldRange.start.column === indentLength) {
startDelta = [0, commentStartString.length + 1]
} else if (oldRange.start.column === endLineLength) {
startDelta = [0, -commentEndString.length - 1]
} else {
startDelta = [0, 0]
}
options.selection.setBufferRange(oldRange.translate(startDelta), { autoscroll: false })
} else {
startDelta = oldRange.start.column === indentLength ? [0, commentStartString.length + 1] : [0, 0]
endDelta = oldRange.end.column === endLineLength ? [0, -commentEndString.length - 1] : [0, 0]
options.selection.setBufferRange(oldRange.translate(startDelta, endDelta), { autoscroll: false })
}
}
})
}
} else {