Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1491,13 +1491,34 @@ define(function (require, exports, module) {
};

/**
* Clears all mark of the given type. If nothing is given, clears all marks(Don't use this API without types!).
* Clears all marks of the given type. If a lineNumbers array is given, only clears marks on those lines.
* If no markType or lineNumbers are given, clears all marks (use cautiously).
* @param {string} [markType] - Optional, if given will only delete marks of that type. Else delete everything.
* @param {number[]} [lineNumbers] - Optional, array of line numbers where marks should be cleared.
*/
Editor.prototype.clearAllMarks = function (markType) {
Editor.prototype.clearAllMarks = function (markType, lineNumbers) {
const self = this;

self._codeMirror.operation(function () {
let marks = self.getAllMarks(markType);

if (lineNumbers && Array.isArray(lineNumbers)) {
// Filter marks to only those within the specified line numbers
marks = marks.filter(function (mark) {
const range = mark.find(); // Get the range of the mark
if (!range) {
return false;
}

const startLine = range.from.line;
const endLine = range.to.line;

// Check if the mark overlaps with any of the specified lines
return lineNumbers.some(line => line >= startLine && line <= endLine);
});
}

// Clear the filtered marks
for (let mark of marks) {
mark.clear();
}
Expand Down
27 changes: 27 additions & 0 deletions test/spec/Editor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2458,6 +2458,33 @@ define(function (require, exports, module) {
expect(mark.length).toBe(0);
});

it("should clearAllMarks clear marks with and without markType on given line only", function () {
myEditor.markToken("token", {line: 0, ch: 1});
myEditor.markToken("token", {line: 1, ch: 1});

let mark = myEditor.getAllMarks();
expect(mark.length).toBe(2);
expect([mark[0].type, mark[1].type].includes("range")).toBeTrue();

// clear mark without markType
myEditor.clearAllMarks(null, [0]);
mark = myEditor.getAllMarks();
expect(mark.length).toBe(1);
expect(mark[0].type).toBe("range");
expect(mark[0].find().from.line).toBe(1);

// mark again
myEditor.markToken("token", {line: 3, ch: 1});
myEditor.markToken("markToDelete", {line: 2, ch: 1});
myEditor.clearAllMarks("markToDelete", [2]);
mark = myEditor.getAllMarks();
expect(mark.length).toBe(2);
expect(mark[0].type).toBe("range");
expect(mark[1].type).toBe("range");
expect(mark[0].find().from.line).toBe(1);
expect(mark[1].find().from.line).toBe(3);
});

it("should clearAllMarks clear bookmarks and range marks with markType", function () {
myEditor.setBookmark("book", {line: 0, ch: 3});
myEditor.markToken("token", {line: 0, ch: 1});
Expand Down
Loading