Skip to content

Commit cd02772

Browse files
committed
feat(bookmark): 支持同时在多个光标处创建和删除书签
1 parent e3b3ddd commit cd02772

File tree

2 files changed

+63
-53
lines changed

2 files changed

+63
-53
lines changed

.changeset/khaki-planets-laugh.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'bookmark-manager': patch
3+
---
4+
5+
feat(bookmark): 支持同时在多个光标处创建和删除书签

src/utils/bookmark.ts

Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -258,78 +258,83 @@ export async function toggleBookmark(
258258
return;
259259
}
260260

261-
let selection = editor.selection;
261+
let selections = editor.selections;
262262

263263
if (context && 'lineNumber' in context) {
264264
const {lineNumber} = context;
265265
const line = editor.document.lineAt(lineNumber - 1);
266-
selection = getSelectionFromLine(line);
266+
selections = [getSelectionFromLine(line)];
267267
}
268268

269-
if (!selection) {
269+
if (!selections.length) {
270270
return;
271271
}
272272

273-
const {type: bookmarkType, label = '', withColor = false} = extra;
273+
for (let selection of selections) {
274+
const {type: bookmarkType, label = '', withColor = false} = extra;
274275

275-
let range, selectionContent;
276-
const activeLine = editor.document.lineAt(selection.active.line);
277-
if (bookmarkType === BookmarkTypeEnum.LINE) {
278-
const startPos = activeLine.text.indexOf(activeLine.text.trim());
279-
range = new Selection(
280-
activeLine.lineNumber,
281-
startPos,
282-
activeLine.lineNumber,
283-
activeLine.range.end.character,
284-
);
276+
let range, selectionContent;
277+
const activeLine = editor.document.lineAt(selection.active.line);
278+
if (bookmarkType === BookmarkTypeEnum.LINE) {
279+
const startPos = activeLine.text.indexOf(activeLine.text.trim());
280+
range = new Selection(
281+
activeLine.lineNumber,
282+
startPos,
283+
activeLine.lineNumber,
284+
activeLine.range.end.character,
285+
);
285286

286-
selectionContent = activeLine.text.trim();
287-
} else {
288-
range = editor.selection;
289-
if (range.isEmpty) {
290-
return;
287+
selectionContent = activeLine.text.trim();
288+
} else {
289+
range = editor.selection;
290+
if (range.isEmpty) {
291+
return;
292+
}
293+
selectionContent = editor.document.getText(range);
291294
}
292-
selectionContent = editor.document.getText(range);
293-
}
294295

295-
if (
296-
bookmarkType === BookmarkTypeEnum.LINE &&
297-
checkIfBookmarkIsInGivenSelectionAndRemove(editor, range)
298-
) {
299-
return;
300-
}
296+
if (
297+
bookmarkType === BookmarkTypeEnum.LINE &&
298+
checkIfBookmarkIsInGivenSelectionAndRemove(editor, range)
299+
) {
300+
if (selections.length === 1) {
301+
return;
302+
}
303+
continue;
304+
}
301305

302-
let chosenColor: string | undefined = 'default';
303-
if (withColor) {
304-
chosenColor = await chooseBookmarkColor();
305-
if (!chosenColor) {
306-
return;
306+
let chosenColor: string | undefined = 'default';
307+
if (withColor) {
308+
chosenColor = await chooseBookmarkColor();
309+
if (!chosenColor) {
310+
return;
311+
}
307312
}
308-
}
309313

310-
const fileUri = editor.document.uri;
314+
const fileUri = editor.document.uri;
311315

312-
const bookmark: any = {
313-
type: bookmarkType,
314-
label,
315-
color: chosenColor,
316-
description: '',
317-
fileUri: editor.document.uri,
318-
languageId: editor.document.languageId,
319-
selectionContent,
320-
fileId: fileUri.fsPath,
321-
fileName: editor.document.fileName,
322-
rangesOrOptions: {
323-
range,
324-
hoverMessage: '',
325-
renderOptions: {
326-
after: {},
316+
const bookmark: any = {
317+
type: bookmarkType,
318+
label,
319+
color: chosenColor,
320+
description: '',
321+
fileUri: editor.document.uri,
322+
languageId: editor.document.languageId,
323+
selectionContent,
324+
fileId: fileUri.fsPath,
325+
fileName: editor.document.fileName,
326+
rangesOrOptions: {
327+
range,
328+
hoverMessage: '',
329+
renderOptions: {
330+
after: {},
331+
},
327332
},
328-
},
329-
workspaceFolder: workspace.getWorkspaceFolder(editor.document.uri!)!,
330-
};
333+
workspaceFolder: workspace.getWorkspaceFolder(editor.document.uri!)!,
334+
};
331335

332-
controller.add(bookmark);
336+
controller.add(bookmark);
337+
}
333338
}
334339
/**
335340
* 删除行标签

0 commit comments

Comments
 (0)