Skip to content

Commit 3d69ed7

Browse files
committed
feat: implement moving to next and previous bookmark functionality
1 parent 669d0be commit 3d69ed7

File tree

1 file changed

+124
-2
lines changed

1 file changed

+124
-2
lines changed

src/extensionsIntegrated/Bookmarks/src/bookmarks.js

Lines changed: 124 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,53 @@ define(function (require, exports, module) {
77
const GUTTER_NAME = "CodeMirror-bookmarkGutter",
88
BOOKMARK_PRIORITY = 100;
99

10+
/**
11+
* This is where all the bookmarks will be stored
12+
* it is an array of objects where each object will be stored firstly based on the file and then as per line no.
13+
* the sorting is done to make sure we need not access the whole complete list when trying to move back and forth
14+
*
15+
* @type {[{file: {String}, line: {Number}}]}
16+
*/
17+
const BookmarksList = [];
18+
1019
// initialize the bookmark gutter
1120
Editor.registerGutter(GUTTER_NAME, BOOKMARK_PRIORITY);
1221

22+
/**
23+
* This function is responsible to remove the bookmark from the bookmarks list
24+
*
25+
* @private
26+
* @param {String} file - the file path
27+
* @param {Number} line - the line number
28+
*/
29+
function _removeFromBookmarksList(file, line) {
30+
for (let i = 0; i < BookmarksList.length; i++) {
31+
if (BookmarksList[i].file === file && BookmarksList[i].line === line) {
32+
BookmarksList.splice(i, 1);
33+
break;
34+
}
35+
}
36+
}
37+
38+
/**
39+
* This function is responsible to add the bookmark to the bookmarks list
40+
* after adding that we also sort that first by file path and then by line number to make accessing efficient
41+
*
42+
* @private
43+
* @param {String} file - the file path
44+
* @param {Number} line - the line number
45+
*/
46+
function _addToBookmarksList(file, line) {
47+
BookmarksList.push({ file: file, line: line });
48+
49+
BookmarksList.sort((a, b) => {
50+
if (a.file === b.file) {
51+
return a.line - b.line;
52+
}
53+
return a.file.localeCompare(b.file);
54+
});
55+
}
56+
1357
/**
1458
* This function toggles a bookmark on a specific line
1559
*
@@ -18,10 +62,16 @@ define(function (require, exports, module) {
1862
* @param {number} line - The line number to toggle bookmark on
1963
*/
2064
function _toggleLineBookmark(editor, line) {
65+
const file = editor.document.file.fullPath; // this file path will be used when storing in the bookmarks list
66+
67+
// remove bookmark
2168
if (Helper.hasBookmark(editor, line, GUTTER_NAME)) {
2269
editor.setGutterMarker(line, GUTTER_NAME, "");
70+
_removeFromBookmarksList(file, line);
2371
} else {
72+
// add bookmark
2473
editor.setGutterMarker(line, GUTTER_NAME, Helper.createBookmarkMarker());
74+
_addToBookmarksList(file, line);
2575
}
2676
}
2777

@@ -43,12 +93,84 @@ define(function (require, exports, module) {
4393
});
4494
}
4595

96+
/**
97+
* This function gets executed when users click on the go to next bookmark button in the navigate menu,
98+
* or its keyboard shortcut
99+
* This finds the next bookmark in the current file and moves the cursor there
100+
*/
46101
function goToNextBookmark() {
47-
//
102+
const editor = EditorManager.getFocusedEditor();
103+
if (!editor) {
104+
return;
105+
}
106+
107+
// get the file path and line as these values are needed when searching in the bookmarks list
108+
const currentFile = editor.document.file.fullPath;
109+
const currentLine = editor.getCursorPos().line;
110+
111+
// get all the bookmarks in current file (this is already sorted by line number)
112+
const fileBookmarks = BookmarksList.filter((bookmark) => bookmark.file === currentFile);
113+
if (fileBookmarks.length === 0) {
114+
return;
115+
}
116+
117+
// find the next bookmark after current position
118+
let nextBookmark = null;
119+
120+
// find the first bookmark after current line
121+
for (let i = 0; i < fileBookmarks.length; i++) {
122+
if (fileBookmarks[i].line > currentLine) {
123+
nextBookmark = fileBookmarks[i];
124+
break;
125+
}
126+
}
127+
128+
// If no next bookmark found, we wrap around to get the first bookmark in this file
129+
if (!nextBookmark && fileBookmarks.length > 0) {
130+
nextBookmark = fileBookmarks[0];
131+
}
132+
133+
// take the cursor to the bookmark
134+
if (nextBookmark) {
135+
editor.setCursorPos(nextBookmark.line, 0);
136+
}
48137
}
49138

139+
/**
140+
* This function gets executed when users click on the go to previous bookmark button in the navigate menu,
141+
* or its keyboard shortcut
142+
* This finds the previous bookmark in the current file and moves the cursor there
143+
*/
50144
function goToPrevBookmark() {
51-
//
145+
const editor = EditorManager.getFocusedEditor();
146+
if (!editor) {
147+
return;
148+
}
149+
150+
const currentFile = editor.document.file.fullPath;
151+
const currentLine = editor.getCursorPos().line;
152+
153+
const fileBookmarks = BookmarksList.filter((bookmark) => bookmark.file === currentFile);
154+
if (fileBookmarks.length === 0) {
155+
return;
156+
}
157+
158+
let prevBookmark = null;
159+
160+
for (let i = fileBookmarks.length - 1; i >= 0; i--) {
161+
if (fileBookmarks[i].line < currentLine) {
162+
prevBookmark = fileBookmarks[i];
163+
break;
164+
}
165+
}
166+
167+
if (!prevBookmark && fileBookmarks.length > 0) {
168+
prevBookmark = fileBookmarks[fileBookmarks.length - 1];
169+
}
170+
171+
if (prevBookmark) {
172+
editor.setCursorPos(prevBookmark.line, 0);
173+
}
52174
}
53175

54176
exports.toggleBookmark = toggleBookmark;

0 commit comments

Comments
 (0)