From 9338989d737db7f57c276236462932ce7e937251 Mon Sep 17 00:00:00 2001 From: abose Date: Tue, 10 Dec 2024 20:58:29 +0530 Subject: [PATCH 1/2] chore: editor.clearAllMarks api will now take optional linenumbers to clear only --- src/editor/Editor.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/editor/Editor.js b/src/editor/Editor.js index ebf427909c..6120148abd 100644 --- a/src/editor/Editor.js +++ b/src/editor/Editor.js @@ -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(); } From 1e13a29f3f114fb5180fc95940914c1e663504fb Mon Sep 17 00:00:00 2001 From: abose Date: Tue, 10 Dec 2024 21:10:10 +0530 Subject: [PATCH 2/2] chore: editor.clearAllMarks api with line number uts --- test/spec/Editor-test.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/spec/Editor-test.js b/test/spec/Editor-test.js index 178b55b0e6..e48e3d7a51 100644 --- a/test/spec/Editor-test.js +++ b/test/spec/Editor-test.js @@ -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});