Skip to content

Commit 9ce5e50

Browse files
committed
update
1 parent e83892d commit 9ce5e50

File tree

8 files changed

+179
-55
lines changed

8 files changed

+179
-55
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## version 2.4.1
2+
3+
- 简易 remark 功能, 用于回忆思考过程? 后续需要用这个数据生成文章
4+
15
## version 2.3.2
26

37
- WC320 数据

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- [新增区块测试用例](#区块测试用例)
2525
- [新增搬砖功能(重复练习?)](#搬砖功能的说明)
2626
- [状态栏增加简易计时器](#状态栏增加简易计时器)
27+
- [新增一个 remark 功能](#新增在工作目录存放数据)
2728

2829
# 关于本项目
2930

@@ -100,6 +101,10 @@
100101
> > .lcpr_data/ 存数据
101102
> >
102103
> > > bricks.json
104+
> >
105+
> > > remark 备注数据
106+
> > >
107+
> > > > qid 备注 remark 数据
103108
104109
### bricks.json 存放格式
105110

package.json

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-leetcode-problem-rating",
33
"displayName": "LeetCode",
44
"description": "LeetCode 官方插件增强, 代码开源, 增加 LeetCode 题目难度分, 给个star吧, 球球了",
5-
"version": "2.3.2",
5+
"version": "2.4.1",
66
"author": "ccagml",
77
"publisher": "ccagml",
88
"license": "MIT",
@@ -188,8 +188,8 @@
188188
"title": "Create Note"
189189
},
190190
{
191-
"command": "lcpr.remarkAdd",
192-
"title": "测试新加线程?"
191+
"command": "lcpr.remarkClose",
192+
"title": "关闭?"
193193
},
194194
{
195195
"command": "lcpr.remarkReplyNote",
@@ -249,20 +249,20 @@
249249
"commandPalette": [
250250
{
251251
"command": "lcpr.remarkCreateNote",
252-
"when": "false"
252+
"when": "true"
253253
},
254254
{
255255
"command": "lcpr.remarkReplyNote",
256-
"when": "false"
256+
"when": "true"
257257
},
258258
{
259259
"command": "lcpr.remarkDeleteNoteComment",
260-
"when": "false"
260+
"when": "true"
261261
}
262262
],
263263
"comments/commentThread/title": [
264264
{
265-
"command": "lcpr.remarkAdd",
265+
"command": "lcpr.remarkClose",
266266
"group": "inline",
267267
"when": "commentController == comment-sample"
268268
}
@@ -292,14 +292,9 @@
292292
}
293293
],
294294
"comments/comment/context": [
295-
{
296-
"command": "lcpr.remarkCancelsaveNote",
297-
"group": "inline@1",
298-
"when": "commentController == comment-sample"
299-
},
300295
{
301296
"command": "lcpr.remarkSaveNote",
302-
"group": "inline@2",
297+
"group": "inline@1",
303298
"when": "commentController == comment-sample"
304299
}
305300
],

src/controller/RemarkController.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@ import { remarkService } from "../service/RemarkService";
1515
class RemarkController implements Disposable {
1616
public dispose(): void {}
1717

18-
public remarkAdd(a, b, c, d) {
19-
console.log(a);
20-
console.log(b);
21-
console.log(c);
22-
console.log(d);
18+
public remarkClose(a) {
19+
remarkService.remarkClose(a);
2320
}
2421

2522
public remarkCreateNote(reply: CommentReply) {
@@ -33,8 +30,8 @@ class RemarkController implements Disposable {
3330
public remarkDeleteNoteComment(comment: RemarkComment) {
3431
remarkService.remarkDeleteNoteComment(comment);
3532
}
36-
public startRemark(document: TextDocument) {
37-
remarkService.startRemark(document);
33+
public async startRemark(document: TextDocument) {
34+
await remarkService.startRemark(document);
3835
}
3936

4037
public remarkCancelsaveNote(comment: RemarkComment) {

src/dao/remarkDao.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// > workspace/ 工作目录
2+
// >
3+
// > > .lcpr_data/ 存数据
4+
// > >
5+
// > > > remake/ 备注
6+
// > > >
7+
// > > > > 题目内部编号.json 根据 qid 备注的信息
8+
// > >
9+
10+
import { fetchProblemLanguage, selectWorkspaceFolder } from "../utils/ConfigUtils";
11+
import { useWsl, toWinPath } from "../utils/SystemUtils";
12+
import * as path from "path";
13+
import * as fse from "fs-extra";
14+
15+
class RemarkDao {
16+
version = 1;
17+
public async get_remark_dir_path() {
18+
const language: string | undefined = await fetchProblemLanguage();
19+
if (!language) {
20+
return;
21+
}
22+
const workspaceFolder: string = await selectWorkspaceFolder(false);
23+
if (!workspaceFolder) {
24+
return;
25+
}
26+
let lcpr_data_path: string = path.join(workspaceFolder, ".lcpr_data");
27+
await fse.ensureDir(lcpr_data_path);
28+
29+
let remark_dir: string = path.join(lcpr_data_path, "remark");
30+
await fse.ensureDir(remark_dir);
31+
32+
remark_dir = useWsl() ? await toWinPath(remark_dir) : remark_dir;
33+
return remark_dir;
34+
}
35+
public async init() {
36+
let lcpr_data_path = await this.get_remark_dir_path();
37+
if (!lcpr_data_path) {
38+
return;
39+
}
40+
}
41+
42+
private async getQidPath(qid: string) {
43+
let remark_dir = await this.get_remark_dir_path();
44+
if (!remark_dir) {
45+
return;
46+
}
47+
if (!qid) {
48+
return;
49+
}
50+
let qid_path: string = path.join(remark_dir, qid);
51+
qid_path = useWsl() ? await toWinPath(qid_path) : qid_path;
52+
return qid_path;
53+
}
54+
55+
private async _write_data(qid: string, data: object) {
56+
let qid_data_path = await this.getQidPath(qid);
57+
if (!qid_data_path) {
58+
return;
59+
}
60+
return await fse.writeFile(qid_data_path, JSON.stringify(data));
61+
}
62+
63+
private async _read_data(qid: string) {
64+
let qid_data_path = await this.getQidPath(qid);
65+
if (!qid_data_path) {
66+
return;
67+
}
68+
69+
if (!(await fse.pathExists(qid_data_path))) {
70+
return {};
71+
}
72+
let temp_data = await fse.readFile(qid_data_path, "utf8");
73+
return JSON.parse(temp_data) || {};
74+
}
75+
76+
public async getInfoByQid(qid: string) {
77+
let all_remark = await this._read_data(qid);
78+
return all_remark || {};
79+
}
80+
public async setInfoByQid(qid: string, info) {
81+
await this._write_data(qid, info);
82+
}
83+
}
84+
85+
export const remarkDao: RemarkDao = new RemarkDao();

src/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ export async function activate(context: ExtensionContext): Promise<void> {
109109
commands.registerCommand("lcpr.remarkCreateNote", (reply: CommentReply) => {
110110
remarkController.remarkCreateNote(reply);
111111
}),
112-
commands.registerCommand("lcpr.remarkAdd", (a, b, c, d) => {
113-
remarkController.remarkAdd(a, b, c, d);
112+
commands.registerCommand("lcpr.remarkClose", (a) => {
113+
remarkController.remarkClose(a);
114114
}),
115115
commands.registerCommand("lcpr.remarkReplyNote", (reply: CommentReply) => {
116116
remarkController.remarkReplyNote(reply);

src/model/Model.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
CommentAuthorInformation,
1818
CommentThread,
1919
} from "vscode";
20-
import { getDayNowM } from "../utils/SystemUtils";
20+
import { getDayNowM, getRemakeName } from "../utils/SystemUtils";
2121

2222
export interface IQuickItemEx<T> extends QuickPickItem {
2323
value: T;
@@ -272,23 +272,32 @@ export interface ISubmitEvent {
272272
export class RemarkComment implements Comment {
273273
id: number;
274274
label: string | undefined;
275-
constructor(
276-
public body: string | MarkdownString,
277-
public mode: CommentMode,
278-
public author: CommentAuthorInformation,
279-
public parent?: CommentThread,
280-
public contextValue?: string
281-
) {
275+
mode: CommentMode;
276+
author: CommentAuthorInformation;
277+
contextValue?: string;
278+
279+
constructor(public body: string | MarkdownString, public parent?: CommentThread) {
282280
this.id = getDayNowM();
283281
this.label = "";
282+
this.contextValue = "canDelete";
283+
this.mode = CommentMode.Preview;
284+
this.author = { name: getRemakeName() };
284285
}
285286

286-
getDbString() {
287+
getDbData() {
287288
let a = {
288289
name: this.author.name,
289290
id: this.id,
290291
body: this.body,
291292
};
293+
return a;
294+
}
295+
296+
static getObjByDbData(dbData, thread?): RemarkComment {
297+
let obj = new RemarkComment(dbData.body, thread);
298+
obj.id = dbData.id || getDayNowM();
299+
obj.author = { name: dbData.name };
300+
return obj;
292301
}
293302
}
294303

src/service/RemarkService.ts

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import {
1010
Disposable,
1111
} from "vscode";
1212
import { treeViewController } from "../controller/TreeViewController";
13+
import { remarkDao } from "../dao/remarkDao";
1314
import { RemarkComment } from "../model/Model";
14-
import { getRemakeName } from "../utils/SystemUtils";
1515

1616
class RemarkService implements Disposable {
1717
private _remarkComment;
@@ -44,7 +44,7 @@ class RemarkService implements Disposable {
4444
};
4545
}
4646

47-
public startRemark(document: TextDocument) {
47+
public async startRemark(document: TextDocument) {
4848
let docInfo = this.getQidByDocument(document);
4949
if (docInfo["qid"] == undefined) {
5050
return;
@@ -54,12 +54,17 @@ class RemarkService implements Disposable {
5454
this._qid_map_thread.get(docInfo["qid"])?.dispose();
5555
this._qid_map_thread.delete(docInfo["qid"]);
5656
}
57+
let oldRemark = await this.getOldThreadRemarkByQid(docInfo["qid"]);
5758
for (let i: number = 0; i < document.lineCount; i++) {
5859
const lineContent: string = document.lineAt(i).text;
5960
if (lineContent.indexOf("@lc code=start") >= 0) {
60-
let newRemark = this._remarkComment.createCommentThread(document.uri, new Range(i - 1, 0, i - 1, 0), []);
61-
newRemark.contextValue = `qid:${docInfo["qid"]}`;
62-
newRemark.label = `${docInfo["fid"]}题`;
61+
let newRemark = this._remarkComment.createCommentThread(document.uri, new Range(i - 1, 0, i - 1, 0), oldRemark);
62+
newRemark.comments.forEach((element) => {
63+
element.parent = newRemark;
64+
});
65+
66+
newRemark.contextValue = `qid=${docInfo["qid"]}`;
67+
newRemark.label = `${docInfo["fid"]}`;
6368
newRemark.collapsibleState = CommentThreadCollapsibleState.Expanded;
6469
this._qid_map_thread.set(docInfo["qid"], newRemark);
6570
break;
@@ -76,16 +81,12 @@ class RemarkService implements Disposable {
7681
}
7782

7883
public remarkDeleteNoteComment(comment: RemarkComment) {
79-
let thread = comment.parent;
80-
if (!thread) {
84+
if (!comment.parent) {
8185
return;
8286
}
8387

84-
thread.comments = thread.comments.filter((cmt) => (cmt as RemarkComment).id !== comment.id);
85-
86-
// if (thread.comments.length === 0) {
87-
// thread.dispose();
88-
// }
88+
comment.parent.comments = comment.parent.comments.filter((cmt) => (cmt as RemarkComment).id !== comment.id);
89+
this.saveThreadRemark(comment.parent);
8990
}
9091

9192
public remarkCancelsaveNote(comment: RemarkComment) {
@@ -100,19 +101,10 @@ class RemarkService implements Disposable {
100101

101102
return cmt;
102103
});
104+
this.saveThreadRemark(comment.parent);
103105
}
104106
public remarkSaveNote(comment: RemarkComment) {
105-
if (!comment.parent) {
106-
return;
107-
}
108-
109-
comment.parent.comments = comment.parent.comments.map((cmt) => {
110-
if ((cmt as RemarkComment).id === comment.id) {
111-
cmt.mode = CommentMode.Preview;
112-
}
113-
114-
return cmt;
115-
});
107+
this.remarkCancelsaveNote(comment);
116108
}
117109
public remarkEditNote(comment: RemarkComment) {
118110
if (!comment.parent) {
@@ -126,12 +118,49 @@ class RemarkService implements Disposable {
126118

127119
return cmt;
128120
});
121+
122+
this.saveThreadRemark(comment.parent);
129123
}
130124

131125
public replyNote(reply: CommentReply) {
132126
let thread = reply.thread;
133-
let newComment = new RemarkComment(reply.text, CommentMode.Preview, { name: getRemakeName() }, thread, "canDelete");
127+
let newComment = new RemarkComment(reply.text, thread);
134128
thread.comments = [...thread.comments, newComment];
129+
this.saveThreadRemark(thread);
130+
}
131+
132+
public saveThreadRemark(thread) {
133+
const params: URLSearchParams = new URLSearchParams(thread.contextValue);
134+
let qid = params.get("qid");
135+
if (!qid) {
136+
return;
137+
}
138+
let data: Array<any> = [];
139+
thread.comments.forEach((element) => {
140+
data.push(element.getDbData());
141+
});
142+
let remarkData = {};
143+
remarkData["data"] = data;
144+
remarkDao.setInfoByQid(qid, remarkData);
145+
}
146+
public async getOldThreadRemarkByQid(qid: string, thread?) {
147+
let remarkData = await remarkDao.getInfoByQid(qid);
148+
let remarkDataBody = remarkData["data"] || [];
149+
let OldRemark: Array<any> = [];
150+
remarkDataBody.forEach((element) => {
151+
OldRemark.push(RemarkComment.getObjByDbData(element, thread));
152+
});
153+
return OldRemark;
154+
}
155+
156+
public remarkClose(thread) {
157+
const params: URLSearchParams = new URLSearchParams(thread.contextValue);
158+
let qid = params.get("qid");
159+
if (!qid) {
160+
return;
161+
}
162+
163+
this._qid_map_thread.get(qid)?.dispose();
135164
}
136165

137166
public dispose(): void {}

0 commit comments

Comments
 (0)