Skip to content

Commit

Permalink
Merge pull request atom#18471 from Aerijo/comment-cursor-placement
Browse files Browse the repository at this point in the history
Toggle comment cursor placement
  • Loading branch information
Nathan Sobo authored Apr 10, 2019
2 parents ca5401a + 513a38b commit 57d00b6
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
60 changes: 60 additions & 0 deletions spec/text-editor-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8424,6 +8424,66 @@ describe('TextEditor', () => {
editor.toggleLineCommentsForBufferRows(0, 0)
expect(editor.lineTextForBufferRow(0)).toBe('test')
})

it('does not select the new delimiters', () => {
editor.setText('<!-- test -->')
let delimLength = '<!--'.length
let selection = editor.addSelectionForBufferRange([[0, delimLength], [0, delimLength]])

{
selection.toggleLineComments()

const range = selection.getBufferRange()
expect(range.isEmpty()).toBe(true)
expect(range.start.column).toBe(0)
}

{
selection.toggleLineComments()

const range = selection.getBufferRange()
expect(range.isEmpty()).toBe(true)
expect(range.start.column).toBe(delimLength + 1)
}

{
selection.setBufferRange([[0, delimLength], [0, delimLength + 1 + 'test'.length]])
selection.toggleLineComments()

const range = selection.getBufferRange()
expect(range.start.column).toBe(0)
expect(range.end.column).toBe('test'.length)
}

{
selection.toggleLineComments()

const range = selection.getBufferRange()
expect(range.start.column).toBe(delimLength + 1)
expect(range.end.column).toBe(delimLength + 1 + 'test'.length)
}

{
editor.setText(' test')
selection.setBufferRange([[0, 4], [0, 4]])
selection.toggleLineComments()

const range = selection.getBufferRange()
expect(range.isEmpty()).toBe(true)
expect(range.start.column).toBe(4 + delimLength + 1)
}

{
editor.setText(' test')
selection.setBufferRange([[0, 8], [0, 8]])
selection.selectToBeginningOfWord()
selection.toggleLineComments()

const range = selection.getBufferRange()
expect(range.start.column).toBe(4 + delimLength + 1)
expect(range.end.column).toBe(4 + delimLength + 1 + 4)
}
})
})

describe('less', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 18 additions & 1 deletion src/text-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4756,7 +4756,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 &&
Expand Down Expand Up @@ -4786,6 +4786,23 @@ 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
// See https://github.com/atom/atom/pull/17519
if (options.correctSelection && options.selection) {
const endLineLength = this.buffer.lineLengthForRow(end)
const oldRange = options.selection.getBufferRange()
if (oldRange.isEmpty()) {
if (oldRange.start.column === endLineLength) {
const endCol = endLineLength - commentEndString.length - 1
options.selection.setBufferRange([[end, endCol], [end, endCol]], {autoscroll: false})
}
} else {
const startDelta = oldRange.start.column === indentLength ? [0, commentStartString.length + 1] : [0, 0]
const endDelta = oldRange.end.column === endLineLength ? [0, -commentEndString.length - 1] : [0, 0]
options.selection.setBufferRange(oldRange.translate(startDelta, endDelta), {autoscroll: false})
}
}
})
}
} else {
Expand Down

0 comments on commit 57d00b6

Please sign in to comment.