@codemirror/comment
Advanced tools
Comparing version 0.17.1 to 0.17.2
@@ -0,1 +1,9 @@ | ||
## 0.17.2 (2021-02-18) | ||
### Bug fixes | ||
Don't insert multiple line comment markers when there are multiple selections on a line. | ||
Move the cursor after the comment marker when inserting a line comment directly at the cursor. | ||
## 0.17.1 (2021-01-06) | ||
@@ -2,0 +10,0 @@ |
@@ -111,66 +111,52 @@ import { EditorSelection } from '@codemirror/state'; | ||
} | ||
function findLineComment(token, lines) { | ||
let minCol = 1e9, commented = null, skipped = []; | ||
for (let i = 0; i < lines.length; i++) { | ||
let line = lines[i], col = /^\s*/.exec(line.text)[0].length; | ||
let empty = skipped[line.number] = col == line.length; | ||
if (col < minCol && (!empty || minCol == 1e9 && i == lines.length - 1)) | ||
minCol = col; | ||
if (commented != false && (!empty || commented == null && i == lines.length - 1)) | ||
commented = line.text.slice(col, col + token.length) == token; | ||
} | ||
return { minCol, commented: commented, skipped }; | ||
} | ||
// Performs toggle, comment and uncomment of line comments. | ||
function changeLineComment(option, ranges, state) { | ||
let lines = [], tokens = [], lineRanges = []; | ||
let lines = []; | ||
let prevLine = -1; | ||
for (let { from, to } of ranges) { | ||
let token = getConfig(state, from).line; | ||
if (!token) | ||
return null; | ||
tokens.push(token); | ||
let lns = getLinesInRange(state.doc, from, to); | ||
lines.push(lns); | ||
lineRanges.push(findLineComment(token, lns)); | ||
let startI = lines.length, minIndent = 1e9; | ||
for (let pos = from; pos <= to;) { | ||
let line = state.doc.lineAt(pos); | ||
if (line.from > prevLine) { | ||
prevLine = line.from; | ||
let token = getConfig(state, pos).line; | ||
if (!token) | ||
continue; | ||
let indent = /^\s*/.exec(line.text)[0].length; | ||
let comment = line.text.slice(indent, indent + token.length) == token ? indent : -1; | ||
if (indent < line.text.length && indent < minIndent) | ||
minIndent = indent; | ||
lines.push({ line, comment, token, indent, single: false }); | ||
} | ||
pos = line.to + 1; | ||
} | ||
if (minIndent < 1e9) | ||
for (let i = startI; i < lines.length; i++) | ||
if (lines[i].indent < lines[i].line.text.length) | ||
lines[i].indent = minIndent; | ||
if (lines.length == startI + 1) | ||
lines[startI].single = true; | ||
} | ||
if (option != 2 /* Uncomment */ && lineRanges.some(c => !c.commented)) { | ||
if (option != 1 /* Comment */ && lines.some(l => l.comment >= 0)) { | ||
let changes = []; | ||
for (let i = 0, lineRange; i < ranges.length; i++) | ||
if (!(lineRange = lineRanges[i]).commented) { | ||
for (let line of lines[i]) { | ||
if (!lineRange.skipped[line.number] || lines[i].length == 1) | ||
changes.push({ from: line.from + lineRange.minCol, insert: tokens[i] + " " }); | ||
} | ||
for (let { line, comment, token } of lines) | ||
if (comment >= 0) { | ||
let from = line.from + comment, to = from + token.length; | ||
if (line.text[to - line.from] == " ") | ||
to++; | ||
changes.push({ from, to }); | ||
} | ||
return { changes }; | ||
} | ||
else if (option != 1 /* Comment */ && lineRanges.some(c => c.commented)) { | ||
else if (option != 2 /* Uncomment */ && lines.some(l => l.comment < 0)) { | ||
let changes = []; | ||
for (let i = 0, lineRange; i < ranges.length; i++) | ||
if ((lineRange = lineRanges[i]).commented) { | ||
let token = tokens[i]; | ||
for (let line of lines[i]) { | ||
if (lineRange.skipped[line.number] && lines[i].length > 1) | ||
continue; | ||
let pos = line.from + lineRange.minCol; | ||
let posAfter = lineRange.minCol + token.length; | ||
let marginLen = line.text.slice(posAfter, posAfter + 1) == " " ? 1 : 0; | ||
changes.push({ from: pos, to: pos + token.length + marginLen }); | ||
} | ||
} | ||
return { changes }; | ||
for (let { line, comment, token, indent, single } of lines) | ||
if (comment != indent && (single || /\S/.test(line.text))) | ||
changes.push({ from: line.from + indent, insert: token + " " }); | ||
let changeSet = state.changes(changes); | ||
return { changes: changeSet, selection: state.selection.map(changeSet, 1) }; | ||
} | ||
return null; | ||
} | ||
function getLinesInRange(doc, from, to) { | ||
let line = doc.lineAt(from), lines = []; | ||
while (line.to < to || (line.from <= to && to <= line.to)) { | ||
lines.push(line); | ||
if (line.number == doc.lines) | ||
break; | ||
line = doc.line(line.number + 1); | ||
} | ||
return lines; | ||
} | ||
export { blockComment, blockUncomment, commentKeymap, lineComment, lineUncomment, toggleBlockComment, toggleComment, toggleLineComment }; |
{ | ||
"name": "@codemirror/comment", | ||
"version": "0.17.1", | ||
"version": "0.17.2", | ||
"description": "Commenting and uncommenting commands for the CodeMirror code editor", | ||
@@ -5,0 +5,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
36078
364