File size: 4,692 Bytes
f59fbe2 | 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 167 168 169 170 | import * as vscode from "vscode";
import * as path from "node:path";
import { BaselineManager, type BaselineSession } from "./baseline.manager";
import { Events } from "../../shared/bridge";
import type { ProjectFile } from "../../shared/types";
import { buildCaseInsensitiveGlobLiteral } from "../utils/string";
import {
isWorkspacePathContained,
relativeWorkspacePath,
} from "../utils/workspace-path";
export type BroadcastFn = (event: string, data: unknown, webviewId?: string) => void;
const IGNORE_DIRS = new Set([
"node_modules",
".git",
".svn",
".hg",
"dist",
"build",
"out",
".next",
".nuxt",
"__pycache__",
".cache",
".venv",
"venv",
".gradle",
".idea",
".DS_Store",
"Thumbs.db",
"coverage",
".nyc_output",
".pytest_cache",
".mypy_cache",
".tox",
".eggs",
".sass-cache",
".parcel-cache",
"bower_components",
"jspm_packages",
".turbo",
]);
const SEARCH_EXCLUDE = `{${[...IGNORE_DIRS].map((d) => `**/${d}`).join(",")}}`;
interface ViewState {
session: BaselineSession | null;
trackedFiles: Set<string>;
}
export class FileManager {
private viewStates = new Map<string, ViewState>();
private disposables: vscode.Disposable[] = [];
constructor(
private readonly baselineManager: BaselineManager,
private broadcast: BroadcastFn,
) {
// Watch for file changes
const watcher = vscode.workspace.createFileSystemWatcher("**/*");
const refresh = (uri: vscode.Uri) => {
void this.onFileChange(uri).catch((error) => {
console.error("[kimi-vscode] Unable to refresh file changes", error);
});
};
watcher.onDidChange(refresh);
watcher.onDidCreate(refresh);
watcher.onDidDelete(refresh);
this.disposables.push(watcher);
}
private getViewState(webviewId: string): ViewState {
let state = this.viewStates.get(webviewId);
if (!state) {
state = { session: null, trackedFiles: new Set() };
this.viewStates.set(webviewId, state);
}
return state;
}
setSession(webviewId: string, session: BaselineSession): void {
this.getViewState(webviewId).session = session;
}
clearSession(webviewId: string): void {
const state = this.getViewState(webviewId);
state.session = null;
state.trackedFiles.clear();
}
getSessionId(webviewId: string): string | null {
return this.getViewState(webviewId).session?.id ?? null;
}
getSession(webviewId: string): BaselineSession | null {
return this.getViewState(webviewId).session;
}
trackFile(webviewId: string, absolutePath: string): void {
this.getViewState(webviewId).trackedFiles.add(absolutePath);
}
getTracked(webviewId: string): Set<string> {
return this.getViewState(webviewId).trackedFiles;
}
clearTracked(webviewId: string): void {
this.getViewState(webviewId).trackedFiles.clear();
}
disposeView(webviewId: string): void {
this.viewStates.delete(webviewId);
}
private async onFileChange(uri: vscode.Uri): Promise<void> {
const absolutePath = uri.fsPath;
for (const [webviewId, state] of this.viewStates) {
if (!state.session || !state.trackedFiles.has(absolutePath)) {
continue;
}
await this.refreshChanges(webviewId);
}
}
async refreshChanges(webviewId: string): Promise<void> {
const state = this.getViewState(webviewId);
if (state.session === null) {
this.broadcast(Events.FileChangesUpdated, [], webviewId);
return;
}
const changes = await this.baselineManager.getChanges(state.session);
this.broadcast(Events.FileChangesUpdated, changes, webviewId);
}
async searchFiles(workDirUri: vscode.Uri, query?: string): Promise<ProjectFile[]> {
query = query ? buildCaseInsensitiveGlobLiteral(query) : "";
const pattern = query ? `**/*${query}*` : "**/*";
const files = await vscode.workspace.findFiles(
new vscode.RelativePattern(workDirUri, pattern),
new vscode.RelativePattern(workDirUri, SEARCH_EXCLUDE),
200,
);
const results = await Promise.all(
files.map(async (uri): Promise<ProjectFile | undefined> => {
const relativePath = relativeWorkspacePath(workDirUri, uri);
if (relativePath === undefined || !(await isWorkspacePathContained(workDirUri, uri))) return undefined;
return {
path: relativePath,
name: path.posix.basename(relativePath),
isDirectory: false,
};
}),
);
return results.filter((result): result is ProjectFile => result !== undefined);
}
dispose(): void {
for (const d of this.disposables) {
d.dispose();
}
this.disposables = [];
this.viewStates.clear();
}
}
|