Code cleanup

Code cleanup
This commit is contained in:
Mateusz Tymek
2026-01-07 21:10:58 +00:00
parent 59b3e2709d
commit ee0e9069c5
4 changed files with 24 additions and 117 deletions

View File

@@ -123,7 +123,6 @@ export class OpenCodeView extends ItemView {
private renderRunningState(): void {
this.contentEl.empty();
// Create header with controls
const headerEl = this.contentEl.createDiv({ cls: "opencode-header" });
const titleSection = headerEl.createDiv({ cls: "opencode-header-title" });
@@ -133,7 +132,6 @@ export class OpenCodeView extends ItemView {
const actionsEl = headerEl.createDiv({ cls: "opencode-header-actions" });
// Reload button
const reloadButton = actionsEl.createEl("button", {
attr: { "aria-label": "Reload" },
});
@@ -142,16 +140,6 @@ export class OpenCodeView extends ItemView {
this.reloadIframe();
});
// Open in browser button
const externalButton = actionsEl.createEl("button", {
attr: { "aria-label": "Open in browser" },
});
setIcon(externalButton, "external-link");
externalButton.addEventListener("click", () => {
window.open(this.plugin.getServerUrl(), "_blank");
});
// Stop button
const stopButton = actionsEl.createEl("button", {
attr: { "aria-label": "Stop server" },
});
@@ -160,7 +148,6 @@ export class OpenCodeView extends ItemView {
this.plugin.stopServer();
});
// Create iframe container
const iframeContainer = this.contentEl.createDiv({
cls: "opencode-iframe-container",
});
@@ -174,7 +161,6 @@ export class OpenCodeView extends ItemView {
},
});
// Handle iframe load errors
this.iframeEl.addEventListener("error", () => {
console.error("Failed to load OpenCode iframe");
});
@@ -192,7 +178,6 @@ export class OpenCodeView extends ItemView {
statusContainer.createEl("h3", { text: "Failed to start OpenCode" });
// Display specific error message if available
const errorMessage = this.plugin.getLastError();
if (errorMessage) {
statusContainer.createEl("p", {

View File

@@ -25,10 +25,7 @@ export class OpenCodeSettingTab extends PluginSettingTab {
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl("h2", { text: "OpenCode Settings" });
// Server settings section
containerEl.createEl("h3", { text: "Server Configuration" });
new Setting(containerEl)
@@ -95,7 +92,6 @@ export class OpenCodeSettingTab extends PluginSettingTab {
})
);
// Behavior settings section
containerEl.createEl("h3", { text: "Behavior" });
new Setting(containerEl)
@@ -112,7 +108,6 @@ export class OpenCodeSettingTab extends PluginSettingTab {
})
);
// Server status section
containerEl.createEl("h3", { text: "Server Status" });
const statusContainer = containerEl.createDiv({ cls: "opencode-settings-status" });
@@ -134,10 +129,8 @@ export class OpenCodeSettingTab extends PluginSettingTab {
return;
}
// Expand tilde for validation
const expanded = expandTilde(trimmed);
// Validate path exists and is a directory
try {
if (!existsSync(expanded)) {
new Notice("Project directory does not exist");
@@ -153,7 +146,6 @@ export class OpenCodeSettingTab extends PluginSettingTab {
return;
}
// Store the expanded path
await this.plugin.updateProjectDirectory(expanded);
}
@@ -195,7 +187,6 @@ export class OpenCodeSettingTab extends PluginSettingTab {
});
}
// Control buttons
const buttonContainer = container.createDiv({ cls: "opencode-settings-buttons" });
if (state === "stopped" || state === "error") {

View File

@@ -7,23 +7,19 @@ import { registerOpenCodeIcons, OPENCODE_ICON_NAME } from "./icons";
export default class OpenCodePlugin extends Plugin {
settings: OpenCodeSettings = DEFAULT_SETTINGS;
private processManager: ProcessManager | null = null;
private processManager: ProcessManager;
private stateChangeCallbacks: Array<(state: ProcessState) => void> = [];
async onload(): Promise<void> {
console.log("Loading OpenCode plugin");
// Register custom icons
registerOpenCodeIcons();
await this.loadSettings();
// Get the vault directory path to pass to OpenCode
const vaultPath = this.getVaultPath();
const projectDirectory = this.getProjectDirectory();
// Initialize process manager with vault as the working directory
// and either the configured project directory or vault as the project
this.processManager = new ProcessManager(
this.settings,
vaultPath,
@@ -33,15 +29,13 @@ export default class OpenCodePlugin extends Plugin {
console.log("[OpenCode] Configured with project directory:", projectDirectory);
// Register the OpenCode view
this.registerView(OPENCODE_VIEW_TYPE, (leaf) => new OpenCodeView(leaf, this));
this.addSettingTab(new OpenCodeSettingTab(this.app, this));
// Add ribbon icon
this.addRibbonIcon(OPENCODE_ICON_NAME, "OpenCode", () => {
this.activateView();
});
// Add command to toggle view
this.addCommand({
id: "toggle-opencode-view",
name: "Toggle OpenCode panel",
@@ -56,7 +50,6 @@ export default class OpenCodePlugin extends Plugin {
],
});
// Add command to start server
this.addCommand({
id: "start-opencode-server",
name: "Start OpenCode server",
@@ -65,7 +58,6 @@ export default class OpenCodePlugin extends Plugin {
},
});
// Add command to stop server
this.addCommand({
id: "stop-opencode-server",
name: "Stop OpenCode server",
@@ -74,10 +66,6 @@ export default class OpenCodePlugin extends Plugin {
},
});
// Register settings tab
this.addSettingTab(new OpenCodeSettingTab(this.app, this));
// Auto-start if enabled
if (this.settings.autoStart) {
this.app.workspace.onLayoutReady(async () => {
await this.startServer();
@@ -88,15 +76,8 @@ export default class OpenCodePlugin extends Plugin {
}
async onunload(): Promise<void> {
console.log("Unloading OpenCode plugin");
// Stop the server
this.stopServer();
// Detach all views
this.app.workspace.detachLeavesOfType(OPENCODE_VIEW_TYPE);
console.log("OpenCode plugin unloaded");
}
async loadSettings(): Promise<void> {
@@ -105,10 +86,7 @@ export default class OpenCodePlugin extends Plugin {
async saveSettings(): Promise<void> {
await this.saveData(this.settings);
// Update process manager with new settings
if (this.processManager) {
this.processManager.updateSettings(this.settings);
}
this.processManager.updateSettings(this.settings);
}
// Update project directory and restart server if running
@@ -116,14 +94,12 @@ export default class OpenCodePlugin extends Plugin {
this.settings.projectDirectory = directory;
await this.saveData(this.settings);
if (this.processManager) {
this.processManager.updateProjectDirectory(this.getProjectDirectory());
this.processManager.updateProjectDirectory(this.getProjectDirectory());
// Restart server if it's currently running
if (this.getProcessState() === "running") {
this.stopServer();
await this.startServer();
}
// Restart server if it's currently running
if (this.getProcessState() === "running") {
this.stopServer();
await this.startServer();
}
}
@@ -170,13 +146,7 @@ export default class OpenCodePlugin extends Plugin {
}
}
// Start the OpenCode server
async startServer(): Promise<boolean> {
if (!this.processManager) {
new Notice("OpenCode: Process manager not initialized");
return false;
}
const success = await this.processManager.start();
if (success) {
new Notice("OpenCode server started");
@@ -184,30 +154,23 @@ export default class OpenCodePlugin extends Plugin {
return success;
}
// Stop the OpenCode server
stopServer(): void {
if (this.processManager) {
this.processManager.stop();
new Notice("OpenCode server stopped");
}
this.processManager.stop();
new Notice("OpenCode server stopped");
}
// Get the current process state
getProcessState(): ProcessState {
return this.processManager?.getState() ?? "stopped";
}
// Get the last error message from the process manager
getLastError(): string | null {
return this.processManager?.getLastError() ?? null;
return this.processManager.getLastError() ?? null;
}
// Get the server URL
getServerUrl(): string {
return this.processManager?.getUrl() ?? `http://127.0.0.1:${this.settings.port}`;
return this.processManager.getUrl() ?? `http://127.0.0.1:${this.settings.port}`;
}
// Subscribe to process state changes, returns unsubscribe function
onProcessStateChange(callback: (state: ProcessState) => void): () => void {
this.stateChangeCallbacks.push(callback);
return () => {
@@ -218,14 +181,12 @@ export default class OpenCodePlugin extends Plugin {
};
}
// Notify all subscribers of state change
private notifyStateChange(state: ProcessState): void {
for (const callback of this.stateChangeCallbacks) {
callback(state);
}
}
// Get the vault path - this is the root directory of the Obsidian vault
private getVaultPath(): string {
const adapter = this.app.vault.adapter as any;
const vaultPath = adapter.basePath || "";
@@ -235,7 +196,6 @@ export default class OpenCodePlugin extends Plugin {
return vaultPath;
}
// Get the project directory - uses the configured setting if set, otherwise vault path
getProjectDirectory(): string {
if (this.settings.projectDirectory) {
return this.settings.projectDirectory;