Skip to content

Commit 5660a50

Browse files
committed
Fixed folding not working for certain templates
We needed to put the cursor on the starting line of the template because the cursor moves to the end of the inserted content and editor.fold will only fold the suffix for some cases
1 parent e94b3a0 commit 5660a50

File tree

2 files changed

+19
-25
lines changed

2 files changed

+19
-25
lines changed

src/extension/index.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
resolveVariables,
1212
} from "./utils/vscode";
1313
import { initLogging } from "./utils/logging";
14-
import { TemplateFoldingProvider, type TemplateRange } from "./utils/folding";
14+
import { TemplateFoldingProvider } from "./utils/folding";
1515
import JudgeViewProvider from "./providers/JudgeViewProvider";
1616
import StressViewProvider from "./providers/StressViewProvider";
1717
import PanelViewProvider from "./providers/PanelViewProvider";
@@ -26,7 +26,7 @@ let templateFoldingProvider: TemplateFoldingProvider;
2626
type Dependencies = Record<string, string[]>;
2727

2828
// Track inserted template ranges by document URI for folding
29-
const templateRangesByUri = new Map<string, TemplateRange>();
29+
const templateRangesByUri = new Map<string, vscode.FoldingRange>();
3030

3131
async function getTemplateContent(
3232
relativeFile: string,
@@ -281,8 +281,9 @@ function registerCommands(context: vscode.ExtensionContext): void {
281281
return;
282282
}
283283

284-
const startLine = editor.selection.active.line;
285-
const templateLineCount = content.split("\n").length - 1;
284+
const start = editor.selection.active.line;
285+
const newlineCount = (content.match(/\n/g) || []).length;
286+
const originalSelection = editor.selection;
286287

287288
const inserted = await editor.edit((edit: vscode.TextEditorEdit) => {
288289
edit.insert(editor.selection.active, content);
@@ -295,16 +296,25 @@ function registerCommands(context: vscode.ExtensionContext): void {
295296
const foldTemplate = config.get<boolean>("foldFileTemplate")!;
296297
if (foldTemplate) {
297298
// Track the template range for folding
298-
const endLine = startLine + templateLineCount;
299+
// newlineCount is the number of newline characters; the range spans from start to start + newlineCount
300+
const end = start + newlineCount;
299301
const documentUri = editor.document.uri.toString();
300-
templateRangesByUri.set(documentUri, { startLine, endLine });
302+
templateRangesByUri.set(documentUri, new vscode.FoldingRange(start, end));
301303

302304
// Notify the folding provider that ranges have changed
303305
templateFoldingProvider.notifyFoldingRangesChanged();
304306

305307
// Wait for VS Code's internal folding debounce (~200ms), then fold
306308
setTimeout(async () => {
309+
// Move cursor to the start of the template so fold operates on the template region
310+
const startPos = new vscode.Position(start, 0);
311+
editor.selection = new vscode.Selection(startPos, startPos);
312+
313+
// Fold at the cursor position
307314
await vscode.commands.executeCommand("editor.fold", { levels: 1, direction: "down" });
315+
316+
// Restore the original selection
317+
editor.selection = originalSelection;
308318
templateRangesByUri.delete(documentUri);
309319
}, 200);
310320
}

src/extension/utils/folding.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import * as vscode from "vscode";
2-
import { getLogger } from "./logging";
32

4-
export interface TemplateRange {
5-
startLine: number;
6-
endLine: number;
7-
}
8-
9-
type GetTemplateRangesCallback = (documentUri: string) => TemplateRange | undefined;
3+
type GetTemplateRangesCallback = (documentUri: string) => vscode.FoldingRange | undefined;
104

115
/**
126
* Custom folding provider that folds inserted template regions.
@@ -21,18 +15,8 @@ export class TemplateFoldingProvider implements vscode.FoldingRangeProvider {
2115
provideFoldingRanges(
2216
document: vscode.TextDocument
2317
): vscode.ProviderResult<vscode.FoldingRange[]> {
24-
const templateRange = this.getTemplateRanges(document.uri.toString());
25-
if (!templateRange) {
26-
return [];
27-
}
28-
29-
const logger = getLogger("folding");
30-
logger.debug(
31-
`Providing folding range for ${document.uri.fsPath}: ${templateRange.startLine}-${templateRange.endLine}`
32-
);
33-
34-
// Simply fold the entire inserted template region
35-
return [new vscode.FoldingRange(templateRange.startLine, templateRange.endLine)];
18+
const range = this.getTemplateRanges(document.uri.toString());
19+
return range ? [range] : [];
3620
}
3721

3822
/**

0 commit comments

Comments
 (0)