forked from xusiyuan841028/coc-project-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlists.ts
57 lines (53 loc) · 1.69 KB
/
lists.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { BasicList, ListAction, ListContext, ListItem, Neovim, workspace } from 'coc.nvim';
import { ProjectData, updateItem, createItem } from './model';
import DB from './db';
export default class ProjectList extends BasicList {
public readonly name = 'projects';
public readonly description = 'CocList for project-manager';
public readonly defaultAction = 'switch';
public actions: ListAction[] = [];
constructor(nvim: Neovim, private db: DB) {
super(nvim);
this.addAction(
'delete',
async (item: ListItem) => {
const { uid } = item.data as ProjectData;
await this.db.delete(uid);
},
{ persist: true, reload: true }
);
this.addAction(
'switch',
(item: ListItem) => {
const { data } = item.data as ProjectData;
const command = `CocCommand explorer ${data.path}`;
workspace.nvim.command(command);
},
{ persist: true, reload: true }
);
this.addAction('update', async (item: ListItem) => {
const { uid, data } = item.data as ProjectData;
const newItem = await updateItem(data);
if (newItem.name && newItem.path) {
await this.db.update(uid, newItem);
}
});
this.addAction('create', async (_item: ListItem) => {
createItem(this.db);
});
}
public async loadItems(_context: ListContext): Promise<ListItem[]> {
const arr = await this.db.load();
const res: ListItem[] = [];
for (const item of arr) {
const { name, description } = item.data;
const label = `${name} \t ${description ? description : ''}`;
res.push({
label,
filterText: name,
data: Object.assign({}, item),
});
}
return res;
}
}