Skip to content

Commit

Permalink
Make toggleBlockCommentByLine not affect lines with no text selected
Browse files Browse the repository at this point in the history
FIX: Change `toggleBlockCommentByLine` to not affect lines with the selection
end right at their start.

See https://discuss.codemirror.net/t/togglecomment-after-triple-click-line-selection/8720
  • Loading branch information
marijnh committed Oct 18, 2024
1 parent 2be11c5 commit c53b542
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ function selectedLineRanges(state: EditorState) {
for (let r of state.selection.ranges) {
let fromLine = state.doc.lineAt(r.from)
let toLine = r.to <= fromLine.to ? fromLine : state.doc.lineAt(r.to)
if (toLine.from > fromLine.from && toLine.from == r.to)
toLine = r.to == fromLine.to + 1 ? fromLine : state.doc.lineAt(r.to - 1)
let last = ranges.length - 1
if (last >= 0 && ranges[last].to > fromLine.from) ranges[last].to = toLine.to
else ranges.push({from: fromLine.from + /^\s*/.exec(fromLine.text)![0].length, to: toLine.to})
Expand Down
8 changes: 8 additions & 0 deletions test/test-comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ describe("comment", () => {
checkLine(`on|e\nt|w|o\nth|ree`,
`${o} on|e\nt|w|o\nth|ree ${c}`)
})

it("doesn't include lines that the selection stops at the start of", () => {
checkLine(`|one\n|two`, `${o} |one ${c}\n|two`)
})

it("does include lines with cursor selection at the start", () => {
checkLine(`|one\ntwo`, `|${o} one ${c}\ntwo`)
})
})
}

Expand Down

0 comments on commit c53b542

Please sign in to comment.