Skip to content

Commit 1e030f9

Browse files
ozgesolidkeyclaude
andcommitted
Auto-activate JSON formatting when opening .json files
Hoist formatAndLoadJson to top-level and auto-trigger it when a .json, .jsonl, or .ndjson file is opened, so users get formatted view immediately. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5899071 commit 1e030f9

1 file changed

Lines changed: 54 additions & 49 deletions

File tree

src/renderer/renderer.ts

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6578,6 +6578,55 @@ function showSearchConfigSessionContextMenu(e: MouseEvent, session: SearchConfig
65786578
setTimeout(() => document.addEventListener('click', closeMenu), 0);
65796579
}
65806580

6581+
async function formatAndLoadJson(): Promise<void> {
6582+
if (!state.filePath) return;
6583+
6584+
if (!jsonFormattingEnabled) {
6585+
// Enable JSON mode - format and open formatted file
6586+
elements.btnJsonFormat.classList.add('active');
6587+
const originalPath = state.filePath;
6588+
6589+
showProgress('Formatting JSON... 0%');
6590+
const unsubFormatProgress = (window.api as any).onJsonFormatProgress?.((data: { percent: number }) => {
6591+
updateProgress(data.percent);
6592+
updateProgressText(`Formatting JSON... ${data.percent}%`);
6593+
});
6594+
try {
6595+
const result = await (window.api as any).formatJsonFile(originalPath);
6596+
if (unsubFormatProgress) unsubFormatProgress();
6597+
if (result.success && result.formattedPath) {
6598+
jsonFormattingEnabled = true;
6599+
jsonOriginalFile = originalPath;
6600+
elements.longLinesWarning.classList.add('hidden');
6601+
await loadFile(result.formattedPath);
6602+
} else {
6603+
elements.btnJsonFormat.classList.remove('active');
6604+
hideProgress();
6605+
// Show error in the warning bar
6606+
const warningText = elements.longLinesWarning.querySelector('.warning-text');
6607+
if (warningText && result.error) {
6608+
warningText.innerHTML = `<strong>Format failed:</strong> ${escapeHtml(result.error)}`;
6609+
elements.longLinesWarning.classList.remove('hidden');
6610+
elements.btnFormatWarning.classList.add('hidden');
6611+
}
6612+
}
6613+
} catch (error) {
6614+
if (unsubFormatProgress) unsubFormatProgress();
6615+
elements.btnJsonFormat.classList.remove('active');
6616+
hideProgress();
6617+
}
6618+
} else {
6619+
// Disable JSON mode - go back to original file
6620+
jsonFormattingEnabled = false;
6621+
elements.btnJsonFormat.classList.remove('active');
6622+
6623+
if (jsonOriginalFile) {
6624+
await loadFile(jsonOriginalFile);
6625+
jsonOriginalFile = null;
6626+
}
6627+
}
6628+
}
6629+
65816630
async function loadFile(filePath: string, createNewTab: boolean = true): Promise<void> {
65826631
// Check if file is already open in a tab BEFORE calling backend
65836632
const existingTab = findTabByFilePath(filePath);
@@ -6718,6 +6767,11 @@ async function loadFile(filePath: string, createNewTab: boolean = true): Promise
67186767
jsonFormattingEnabled = false;
67196768
jsonOriginalFile = null;
67206769
elements.btnJsonFormat.classList.remove('active');
6770+
6771+
// Auto-activate JSON formatting for .json files
6772+
if (isJsonLike) {
6773+
formatAndLoadJson();
6774+
}
67216775
}
67226776

67236777
// Reset scroll slowness detection
@@ -10585,55 +10639,6 @@ function init(): void {
1058510639
elements.btnWordWrap.addEventListener('click', toggleMarkdownPreview);
1058610640

1058710641
// JSON formatting toggle
10588-
async function formatAndLoadJson(): Promise<void> {
10589-
if (!state.filePath) return;
10590-
10591-
if (!jsonFormattingEnabled) {
10592-
// Enable JSON mode - format and open formatted file
10593-
elements.btnJsonFormat.classList.add('active');
10594-
const originalPath = state.filePath;
10595-
10596-
showProgress('Formatting JSON... 0%');
10597-
const unsubFormatProgress = (window.api as any).onJsonFormatProgress?.((data: { percent: number }) => {
10598-
updateProgress(data.percent);
10599-
updateProgressText(`Formatting JSON... ${data.percent}%`);
10600-
});
10601-
try {
10602-
const result = await (window.api as any).formatJsonFile(originalPath);
10603-
if (unsubFormatProgress) unsubFormatProgress();
10604-
if (result.success && result.formattedPath) {
10605-
jsonFormattingEnabled = true;
10606-
jsonOriginalFile = originalPath;
10607-
elements.longLinesWarning.classList.add('hidden');
10608-
await loadFile(result.formattedPath);
10609-
} else {
10610-
elements.btnJsonFormat.classList.remove('active');
10611-
hideProgress();
10612-
// Show error in the warning bar
10613-
const warningText = elements.longLinesWarning.querySelector('.warning-text');
10614-
if (warningText && result.error) {
10615-
warningText.innerHTML = `<strong>Format failed:</strong> ${escapeHtml(result.error)}`;
10616-
elements.longLinesWarning.classList.remove('hidden');
10617-
elements.btnFormatWarning.classList.add('hidden');
10618-
}
10619-
}
10620-
} catch (error) {
10621-
if (unsubFormatProgress) unsubFormatProgress();
10622-
elements.btnJsonFormat.classList.remove('active');
10623-
hideProgress();
10624-
}
10625-
} else {
10626-
// Disable JSON mode - go back to original file
10627-
jsonFormattingEnabled = false;
10628-
elements.btnJsonFormat.classList.remove('active');
10629-
10630-
if (jsonOriginalFile) {
10631-
await loadFile(jsonOriginalFile);
10632-
jsonOriginalFile = null;
10633-
}
10634-
}
10635-
}
10636-
1063710642
elements.btnJsonFormat.addEventListener('click', formatAndLoadJson);
1063810643

1063910644
// Long lines warning buttons

0 commit comments

Comments
 (0)