Skip to content

Commit

Permalink
feat(bookmark-group): 新增支持从控制命令板中快速列出已选择的分组中的书签列表
Browse files Browse the repository at this point in the history
  • Loading branch information
czfadmin committed Apr 5, 2024
1 parent 493efe5 commit 5228e2e
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 158 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@
"title": "%bookmark-manager.changeBookmarkGroup%",
"icon": "$(bookmark)",
"category": "Bookmark Manager (BM)"
},
{
"command": "bookmark-manager.listBookmarksInSelectedGroup",
"title": "%bookmark-manager.listBookmarksInSelectedGroup%",
"icon": "$(bookmark)",
"category": "Bookmark Manager (BM)"
}
],
"views": {
Expand Down
3 changes: 2 additions & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@
"bookmark-manager.groupedByCustom": "Grouped by custom",
"bookmark-manager.clearAllBookmarksInColor": "Clear bookmarks in this color",
"bookmark-manager.setAsDefaultActivedGroup": "Set as default activation group",
"bookmark-manager.changeBookmarkGroup": "Change group"
"bookmark-manager.changeBookmarkGroup": "Change group",
"bookmark-manager.listBookmarksInSelectedGroup": "List the bookmarks in the selected group"
}
3 changes: 2 additions & 1 deletion package.nls.zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@
"bookmark-manager.groupedByCustom": "自定义分组",
"bookmark-manager.clearAllBookmarksInColor": "清除此颜色下的所有书签",
"bookmark-manager.setAsDefaultActivedGroup": "设置为默认激活组",
"bookmark-manager.changeBookmarkGroup": "改变书签分组"
"bookmark-manager.changeBookmarkGroup": "改变书签分组",
"bookmark-manager.listBookmarksInSelectedGroup": "列出已选择分组中的书签列表"
}
71 changes: 2 additions & 69 deletions src/commands/bookmark/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,66 +15,15 @@ import {
checkIfBookmarksIsInCurrentEditor,
chooseBookmarkColor,
deleteBookmark as deleteBookmarkUtil,
getBookmarkFromLineNumber,
getBookmarksFromFileUri,
getLineInfoStrFromBookmark,
gotoSourceLocation as gotoSourceLocationUtil,
quicklyJumpToBookmark,
showGroupPickItems,
toggleBookmark,
getBookmarkFromCtx,
getBookmarkColorFromCtx,
toggleBookmarksWithSelections,
} from '../../utils';
import BookmarkTreeItem from '../../providers/BookmarksTreeItem';

/**
* 从`context`获取书签数据
* @param cb
* @returns
*/
function getBookmarkFromCtx(context: LineBookmarkContext, cb?: () => void) {
let bookmark: IBookmark | undefined;
if (
context &&
'contextValue' in context &&
context.contextValue === 'bookmark'
) {
bookmark = context.meta as IBookmark;
} else {
bookmark = getBookmarkFromLineNumber();
}

if (!bookmark && cb) {
cb();
return;
}
return bookmark;
}

/**
* 从`context`获取书签数据
* @param cb
* @returns
*/
function getBookmarkColorFromCtx(
context: LineBookmarkContext | BookmarkTreeItem | undefined,
cb?: () => void,
) {
let bookmark: IBookmark | undefined;
if (
context &&
'contextValue' in context &&
context.contextValue === 'color'
) {
bookmark = context.meta as IBookmark;
}

if (!bookmark && cb) {
cb();
return;
}
return bookmark;
}

/**
* 开启行书签, 使用默认颜色且无标签等相关信息
Expand Down Expand Up @@ -399,19 +348,3 @@ export function clearAllBookmarksInColor(args: any) {
const meta = args.meta as BookmarksGroupedByColorType;
controller.clearAllBookmarksInColor(meta.color);
}

/**
* 更改书签分组
* @param args
*/
export async function changeBookmarkGroup(args: any) {
const bookmark = getBookmarkFromCtx(args);
if (!bookmark) {
return;
}
const newGroup = await showGroupPickItems(true, bookmark.groupId);
if (!newGroup) {
return;
}
bookmark.changeGroupId(newGroup.id);
}
70 changes: 62 additions & 8 deletions src/commands/bookmark/group.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import {window, l10n} from 'vscode';
import {
window,
l10n,
Selection,
TextEditorRevealType,
Uri,
workspace,
QuickPickItem,
ThemeIcon,
} from 'vscode';
import {resolveBookmarkController} from '../../bootstrap';
import {DEFAULT_BOOKMARK_COLOR} from '../../constants';
import {DEFAULT_BOOKMARK_GROUP_ID} from '../../constants/bookmark';
import {IBookmarkGroup} from '../../stores';
import {IBookmark, IBookmarkGroup} from '../../stores';
import {BookmarksGroupedByCustomType} from '../../types';
import {showGroupPickItems} from '../../utils';
import {
showGroupQuickPick,
getBookmarkFromCtx,
gotoSourceLocation,
getLineInfoStrFromBookmark,
getBookmarkIcon,
showBookmarksQuickPick,
} from '../../utils';
import {IBookmarkCommand, IBookmarkCommandContext} from '../../types/command';

export const BookmarkGroupCommands: IBookmarkCommand[] = [
Expand All @@ -14,7 +30,7 @@ export const BookmarkGroupCommands: IBookmarkCommand[] = [
callback: async (ctx: IBookmarkCommandContext, args: any) => {
let meta: IBookmarkGroup | undefined;
if (!args || !args.meta) {
meta = await showGroupPickItems(true);
meta = await showGroupQuickPick(true);
if (!meta) {
return;
}
Expand All @@ -31,7 +47,7 @@ export const BookmarkGroupCommands: IBookmarkCommand[] = [
callback: async (ctx: IBookmarkCommandContext, args: any) => {
let meta: IBookmarkGroup | undefined;
if (!args || !args.meta) {
meta = await showGroupPickItems(false);
meta = await showGroupQuickPick(false);
if (!meta) {
return;
}
Expand All @@ -51,7 +67,7 @@ export const BookmarkGroupCommands: IBookmarkCommand[] = [
callback: async (ctx: IBookmarkCommandContext, args: any) => {
let meta: IBookmarkGroup | undefined;
if (!args || !args.meta) {
meta = await showGroupPickItems(false);
meta = await showGroupQuickPick(false);
if (!meta) {
return;
}
Expand All @@ -71,7 +87,7 @@ export const BookmarkGroupCommands: IBookmarkCommand[] = [
callback: async (ctx: IBookmarkCommandContext, args: any) => {
let meta: IBookmarkGroup | undefined;
if (!args || !args.meta) {
meta = await showGroupPickItems(false);
meta = await showGroupQuickPick(false);
if (!meta) {
return;
}
Expand Down Expand Up @@ -153,7 +169,7 @@ export const BookmarkGroupCommands: IBookmarkCommand[] = [
export async function setAsDefaultActivedGroup(args: any) {
let meta: IBookmarkGroup | undefined;
if (!args || !args.meta) {
meta = await showGroupPickItems(false);
meta = await showGroupQuickPick(false);
if (!meta) {
return;
}
Expand All @@ -163,3 +179,41 @@ export async function setAsDefaultActivedGroup(args: any) {
const controller = resolveBookmarkController();
controller.setAsDefaultActivedGroup(meta);
}

/**
* 更改书签分组
* @param args
*/
export async function changeBookmarkGroup(args: any) {
const bookmark = getBookmarkFromCtx(args);
if (!bookmark) {
return;
}
const newGroup = await showGroupQuickPick(true, bookmark.groupId);
if (!newGroup) {
return;
}
bookmark.changeGroupId(newGroup.id);
}

export async function listBookmarksInSelectedGroup(args: any) {
const group = await showGroupQuickPick(true);
if (!group) {
return;
}

const controller = resolveBookmarkController();

const bookmarks =
(
controller.store
.getBookmarksGroupedByCustom as BookmarksGroupedByCustomType[]
).find(it => it.id === group.id)?.bookmarks || [];

const selectedBookmark = await showBookmarksQuickPick(bookmarks);
if (!selectedBookmark) {
return;
}
// @ts-ignore
gotoSourceLocation(selectedBookmark.meta);
}
28 changes: 3 additions & 25 deletions src/controllers/BookmarksController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export default class BookmarksController implements IController {
}
return [];
}

private _disposables: IDisposable[] = [];

private _watcher: FileSystemWatcher | undefined;
Expand Down Expand Up @@ -117,8 +118,8 @@ export default class BookmarksController implements IController {
return this._context.workspaceState;
}

public get store(): IBookmarksStore | undefined {
return this._store;
public get store(): IBookmarksStore {
return this._store!;
}

/**
Expand Down Expand Up @@ -516,29 +517,6 @@ export default class BookmarksController implements IController {
}
}

/**
*
* 根据文件名对书签进行分组
* [
* { fileId: xxx ,
* bookmarks: [
*
* ]
* },
* { fileId: xxx ,
* bookmarks: [
*
* ]
* }
* ]
*/
private _getBookmarksGroupedByFile() {
if (!this._store) {
return [];
}
return this._store.bookmarksGroupedByFile;
}

dispose(): void {
this._disposables.filter(it => it).forEach(it => it.dispose());
this._storeDisposer?.();
Expand Down
Loading

0 comments on commit 5228e2e

Please sign in to comment.