Skip to content

Commit

Permalink
feat(bookmark): 支持同时在多个光标处创建和删除书签
Browse files Browse the repository at this point in the history
  • Loading branch information
czfadmin committed Nov 27, 2024
1 parent e3b3ddd commit cd02772
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 53 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-planets-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'bookmark-manager': patch
---

feat(bookmark): 支持同时在多个光标处创建和删除书签
111 changes: 58 additions & 53 deletions src/utils/bookmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,78 +258,83 @@ export async function toggleBookmark(
return;
}

let selection = editor.selection;
let selections = editor.selections;

if (context && 'lineNumber' in context) {
const {lineNumber} = context;
const line = editor.document.lineAt(lineNumber - 1);
selection = getSelectionFromLine(line);
selections = [getSelectionFromLine(line)];
}

if (!selection) {
if (!selections.length) {
return;
}

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

let range, selectionContent;
const activeLine = editor.document.lineAt(selection.active.line);
if (bookmarkType === BookmarkTypeEnum.LINE) {
const startPos = activeLine.text.indexOf(activeLine.text.trim());
range = new Selection(
activeLine.lineNumber,
startPos,
activeLine.lineNumber,
activeLine.range.end.character,
);
let range, selectionContent;
const activeLine = editor.document.lineAt(selection.active.line);
if (bookmarkType === BookmarkTypeEnum.LINE) {
const startPos = activeLine.text.indexOf(activeLine.text.trim());
range = new Selection(
activeLine.lineNumber,
startPos,
activeLine.lineNumber,
activeLine.range.end.character,
);

selectionContent = activeLine.text.trim();
} else {
range = editor.selection;
if (range.isEmpty) {
return;
selectionContent = activeLine.text.trim();
} else {
range = editor.selection;
if (range.isEmpty) {
return;
}
selectionContent = editor.document.getText(range);
}
selectionContent = editor.document.getText(range);
}

if (
bookmarkType === BookmarkTypeEnum.LINE &&
checkIfBookmarkIsInGivenSelectionAndRemove(editor, range)
) {
return;
}
if (
bookmarkType === BookmarkTypeEnum.LINE &&
checkIfBookmarkIsInGivenSelectionAndRemove(editor, range)
) {
if (selections.length === 1) {
return;
}
continue;
}

let chosenColor: string | undefined = 'default';
if (withColor) {
chosenColor = await chooseBookmarkColor();
if (!chosenColor) {
return;
let chosenColor: string | undefined = 'default';
if (withColor) {
chosenColor = await chooseBookmarkColor();
if (!chosenColor) {
return;
}
}
}

const fileUri = editor.document.uri;
const fileUri = editor.document.uri;

const bookmark: any = {
type: bookmarkType,
label,
color: chosenColor,
description: '',
fileUri: editor.document.uri,
languageId: editor.document.languageId,
selectionContent,
fileId: fileUri.fsPath,
fileName: editor.document.fileName,
rangesOrOptions: {
range,
hoverMessage: '',
renderOptions: {
after: {},
const bookmark: any = {
type: bookmarkType,
label,
color: chosenColor,
description: '',
fileUri: editor.document.uri,
languageId: editor.document.languageId,
selectionContent,
fileId: fileUri.fsPath,
fileName: editor.document.fileName,
rangesOrOptions: {
range,
hoverMessage: '',
renderOptions: {
after: {},
},
},
},
workspaceFolder: workspace.getWorkspaceFolder(editor.document.uri!)!,
};
workspaceFolder: workspace.getWorkspaceFolder(editor.document.uri!)!,
};

controller.add(bookmark);
controller.add(bookmark);
}
}
/**
* 删除行标签
Expand Down

0 comments on commit cd02772

Please sign in to comment.