-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreeView.ts
More file actions
166 lines (137 loc) · 4.95 KB
/
treeView.ts
File metadata and controls
166 lines (137 loc) · 4.95 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import * as vscode from "vscode";
import * as path from "path";
import * as fs from "fs";
import { createCommandName, getLineFromOffset, getNotesDir } from "./utils";
import { NoteReference } from "./types";
export class NoteItem extends vscode.TreeItem {
constructor(
public readonly label: string,
public readonly collapsibleState: vscode.TreeItemCollapsibleState,
public readonly filePath?: string,
public readonly type?: "note" | "reference",
public readonly referencePath?: string,
public readonly lineNumber?: number
) {
super(label, collapsibleState);
if (type === "note") {
this.contextValue = "note";
this.iconPath = new vscode.ThemeIcon("notebook");
} else if (type === "reference") {
this.contextValue = "reference";
this.iconPath = new vscode.ThemeIcon("symbol-method");
}
}
}
// Tree view data provider
export class NotesTreeProvider implements vscode.TreeDataProvider<NoteItem> {
constructor(private readonly context: vscode.ExtensionContext) {}
private _onDidChangeTreeData: vscode.EventEmitter<
NoteItem | undefined | null | void
> = new vscode.EventEmitter<NoteItem | undefined | null | void>();
readonly onDidChangeTreeData: vscode.Event<
NoteItem | undefined | null | void
> = this._onDidChangeTreeData.event;
refresh(): void {
this._onDidChangeTreeData.fire();
}
getTreeItem(element: NoteItem): vscode.TreeItem {
return element;
}
getChildren(element?: NoteItem): Thenable<NoteItem[]> {
const NOTES_DIR = getNotesDir(this.context);
if (!fs.existsSync(NOTES_DIR)) {
return Promise.resolve([]);
}
if (element) {
// Return sections/references within a note
return Promise.resolve(this.getNoteSections(element.filePath!));
} else {
// Return all note files
return Promise.resolve(this.getNoteFiles());
}
}
private getNoteFiles(): NoteItem[] {
const NOTES_DIR = getNotesDir(this.context);
const files = fs
.readdirSync(NOTES_DIR)
.filter((f) => f.endsWith(".md"))
.sort();
return files.map((file) => {
const filePath = path.join(NOTES_DIR, file);
const stats = fs.statSync(filePath);
const item = new NoteItem(
path.basename(file, ".md"),
vscode.TreeItemCollapsibleState.Collapsed,
filePath,
"note"
);
item.tooltip = `Modified: ${stats.mtime.toLocaleString()}`;
item.description = this.getReferencesCount(filePath);
return item;
});
}
private getNoteSections(filePath: string): NoteItem[] {
const content = fs.readFileSync(filePath, "utf-8");
const sections: NoteItem[] = [];
// Match sections like "## filename.ts:123"
const sectionRegex = /## (.+?):(\d+)/g;
let match;
const noteIndex =
this.context.workspaceState.get<Record<string, NoteReference>>(
"noteIndex"
) || {};
while ((match = sectionRegex.exec(content)) !== null) {
const fileName = match[1];
const lineNumber = match[2];
const sectionStart = match.index;
// Extract the note content for this section
const nextSection = content.indexOf("\n## ", sectionStart + 1);
const sectionContent = content.substring(
sectionStart,
nextSection === -1 ? content.length : nextSection
);
// Extract the path from the section
const pathMatch = sectionContent.match(/\*\*Path:\*\* `(.+?)`/);
const relativePath = pathMatch ? pathMatch[1] : "";
// Extract annotation if exists
const noteMatch = sectionContent.match(/\*\*Note:\*\* (.+?)(?:\n|$)/);
const annotation = noteMatch ? noteMatch[1] : "";
const itemName = `${fileName}:${lineNumber}`;
const markdownLine = getLineFromOffset(content, sectionStart);
const item = new NoteItem(
itemName,
vscode.TreeItemCollapsibleState.None,
filePath,
"reference",
relativePath,
parseInt(lineNumber)
);
item.tooltip = annotation || relativePath;
item.description = annotation
? annotation.substring(0, 50) + (annotation.length > 50 ? "..." : "")
: "";
item.command = {
command: createCommandName("goToReference"),
title: "Go to Reference",
arguments: [relativePath, parseInt(lineNumber), filePath, markdownLine],
};
noteIndex[`@${fileName}:${relativePath}:${lineNumber}`] = {
noteName: fileName,
notePath: filePath,
noteLine: markdownLine,
file: relativePath,
line: parseInt(lineNumber),
};
sections.push(item);
}
console.log("kyonru noteIndex", noteIndex);
this.context.workspaceState.update("noteIndex", noteIndex);
return sections;
}
private getReferencesCount(filePath: string): string {
const content = fs.readFileSync(filePath, "utf-8");
const matches = content.match(/## .+?:\d+/g);
const count = matches ? matches.length : 0;
return count === 1 ? "1 ref" : `${count} refs`;
}
}