Skip to content

Commit 7b4f22a

Browse files
committed
fix: handle cases when multiple cursors are present
1 parent f508be7 commit 7b4f22a

File tree

2 files changed

+65
-19
lines changed

2 files changed

+65
-19
lines changed

src/extensionsIntegrated/Bookmarks/src/bookmarks.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,45 @@
11
define(function (require, exports, module) {
2-
const EditorManager = require("editor/EditorManager"),
3-
Editor = require("editor/Editor").Editor;
2+
const EditorManager = require("editor/EditorManager");
3+
const Editor = require("editor/Editor").Editor;
4+
5+
const Helper = require("./helper");
46

57
const GUTTER_NAME = "CodeMirror-bookmarkGutter",
68
BOOKMARK_PRIORITY = 100;
79

8-
// the bookmark svg icon
9-
const bookmarkSvg = require("text!styles/images/bookmark.svg");
10-
1110
// initialize the bookmark gutter
1211
Editor.registerGutter(GUTTER_NAME, BOOKMARK_PRIORITY);
1312

1413
/**
15-
* This function is responsible to toggle a bookmark at the current cursor position
14+
* This function toggles a bookmark on a specific line
15+
*
16+
* @param {Editor} editor - The current editor instance
17+
* @param {number} line - The line number to toggle bookmark on
18+
*/
19+
function toggleLineBookmark(editor, line) {
20+
if (Helper.hasBookmark(editor, line, GUTTER_NAME)) {
21+
editor.setGutterMarker(line, GUTTER_NAME, "");
22+
} else {
23+
editor.setGutterMarker(line, GUTTER_NAME, Helper.createBookmarkMarker());
24+
}
25+
}
26+
27+
/**
28+
* This function is responsible to toggle bookmarks at the current cursor position(s)
1629
*/
1730
function toggleBookmark() {
1831
const editor = EditorManager.getFocusedEditor();
1932
if (!editor) {
2033
return;
2134
}
2235

23-
const selection = editor.getSelection();
24-
const currentLine = selection.start.line;
25-
26-
// check whether there's already a bookmark on this line
27-
const marker = editor.getGutterMarker(currentLine, GUTTER_NAME);
36+
const selections = editor.getSelections();
37+
const uniqueLines = Helper.getUniqueLines(selections);
2838

29-
if (marker) {
30-
// remove bookmark if exists
31-
editor.setGutterMarker(currentLine, GUTTER_NAME, "");
32-
} else {
33-
// add the bookmark
34-
const $marker = $("<div>").addClass("bookmark-icon").html(bookmarkSvg);
35-
editor.setGutterMarker(currentLine, GUTTER_NAME, $marker[0]);
36-
}
39+
// process each unique line
40+
uniqueLines.forEach((line) => {
41+
toggleLineBookmark(editor, line);
42+
});
3743
}
3844

3945
exports.toggleBookmark = toggleBookmark;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
define(function (require, exports, module) {
2+
// the bookmark svg icon
3+
const bookmarkSvg = require("text!styles/images/bookmark.svg");
4+
5+
/**
6+
* This function creates a bookmark marker element
7+
*
8+
* @returns {HTMLElement} The bookmark marker element
9+
*/
10+
function createBookmarkMarker() {
11+
return $("<div>").addClass("bookmark-icon").html(bookmarkSvg)[0];
12+
}
13+
14+
/**
15+
* This function checks whether a line has a bookmark
16+
*
17+
* @param {Editor} editor - The current editor instance
18+
* @param {number} line - The line number to check
19+
* @param {string} gutterName - The name of the gutter
20+
* @returns {boolean} True if the line has a bookmark, false otherwise
21+
*/
22+
function hasBookmark(editor, line, gutterName) {
23+
return !!editor.getGutterMarker(line, gutterName);
24+
}
25+
26+
/**
27+
* This function gets unique line numbers from all selections
28+
* this is needed so that when multiple cursors are there at the same line, we can get the line only once
29+
*
30+
* @param {Array<{start: {line: number}}>} selections - Array of selections
31+
* @returns {Array<number>} Array of unique line numbers
32+
*/
33+
function getUniqueLines(selections) {
34+
return [...new Set(selections.map((selection) => selection.start.line))];
35+
}
36+
37+
exports.createBookmarkMarker = createBookmarkMarker;
38+
exports.hasBookmark = hasBookmark;
39+
exports.getUniqueLines = getUniqueLines;
40+
});

0 commit comments

Comments
 (0)