Skip to content

Commit e36baed

Browse files
ozgesolidkeyclaude
andcommitted
Improve JSON formatting with file-based approach
- Add format-json-file IPC handler to create formatted copy of JSON files - JSON button now creates .formatted.json file and opens it for viewing - Toggle back to original file when JSON mode disabled - Improves performance for large JSON files by using disk instead of memory - Add syntax highlighting for JSON content - Fix scroll position preservation in JSON mode - Add support for highlights in JSON formatted view Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent a2b5cb2 commit e36baed

4 files changed

Lines changed: 401 additions & 74 deletions

File tree

src/main/index.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,6 +1760,49 @@ ipcMain.handle('split-file', async (_, options: SplitOptions) => {
17601760
}
17611761
});
17621762

1763+
// === Format JSON File ===
1764+
1765+
ipcMain.handle('format-json-file', async (_, filePath: string) => {
1766+
try {
1767+
// Get file size first
1768+
const stats = fs.statSync(filePath);
1769+
console.log(`[JSON Format] File size: ${stats.size} bytes`);
1770+
1771+
// Read the original file
1772+
const content = fs.readFileSync(filePath, 'utf-8');
1773+
console.log(`[JSON Format] Content length: ${content.length} characters`);
1774+
1775+
// Try to parse as JSON
1776+
const parsed = JSON.parse(content);
1777+
console.log(`[JSON Format] Parsed successfully, type: ${Array.isArray(parsed) ? 'array' : typeof parsed}`);
1778+
if (Array.isArray(parsed)) {
1779+
console.log(`[JSON Format] Array length: ${parsed.length}`);
1780+
}
1781+
1782+
// Pretty-print with 2-space indentation
1783+
const formatted = JSON.stringify(parsed, null, 2);
1784+
console.log(`[JSON Format] Formatted length: ${formatted.length} characters`);
1785+
1786+
// Generate output path
1787+
const dir = path.dirname(filePath);
1788+
const baseName = path.basename(filePath, path.extname(filePath));
1789+
const ext = path.extname(filePath);
1790+
const formattedPath = path.join(dir, `${baseName}.formatted${ext}`);
1791+
1792+
// Write formatted file
1793+
fs.writeFileSync(formattedPath, formatted, 'utf-8');
1794+
1795+
// Verify written file
1796+
const writtenStats = fs.statSync(formattedPath);
1797+
console.log(`[JSON Format] Written file size: ${writtenStats.size} bytes`);
1798+
1799+
return { success: true, formattedPath };
1800+
} catch (error) {
1801+
console.error(`[JSON Format] Error:`, error);
1802+
return { success: false, error: String(error) };
1803+
}
1804+
});
1805+
17631806
// === Terminal ===
17641807

17651808
let ptyProcess: pty.IPty | null = null;

src/preload/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ const api = {
179179
openExternalUrl: (url: string): Promise<void> =>
180180
ipcRenderer.invoke('open-external-url', url),
181181

182+
// JSON formatting
183+
formatJsonFile: (filePath: string): Promise<{ success: boolean; formattedPath?: string; error?: string }> =>
184+
ipcRenderer.invoke('format-json-file', filePath),
185+
182186
// Terminal
183187
terminalCreate: (options?: { cwd?: string; cols?: number; rows?: number }): Promise<{ success: boolean; pid?: number; error?: string }> =>
184188
ipcRenderer.invoke('terminal-create', options),

0 commit comments

Comments
 (0)