Skip to content

Commit b4eb45d

Browse files
committed
Fix golden fixture failures in formatter and extract-literal refactor
1 parent 576a099 commit b4eb45d

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

extensions/allium/src/format.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,13 @@ export function formatAlliumText(
134134

135135
const isTopLevelDeclaration =
136136
indentLevel === 0 && isTopLevelDeclarationLine(trimmed);
137+
const prevNonBlank = lastNonBlankLine(formattedLines);
137138
if (
138139
isTopLevelDeclaration &&
139140
formattedLines.length > 0 &&
140-
blankLinesAtEnd(formattedLines) < topLevelSpacing
141+
blankLinesAtEnd(formattedLines) < topLevelSpacing &&
142+
prevNonBlank !== null &&
143+
!prevNonBlank.trimStart().startsWith("--")
141144
) {
142145
while (blankLinesAtEnd(formattedLines) < topLevelSpacing) {
143146
formattedLines.push("");
@@ -378,6 +381,15 @@ function wildcardToRegex(pattern: string): RegExp {
378381
return new RegExp(`^${escaped}$`);
379382
}
380383

384+
function lastNonBlankLine(lines: string[]): string | null {
385+
for (let i = lines.length - 1; i >= 0; i -= 1) {
386+
if (lines[i] !== "") {
387+
return lines[i];
388+
}
389+
}
390+
return null;
391+
}
392+
381393
function blankLinesAtEnd(lines: string[]): number {
382394
let count = 0;
383395
for (let i = lines.length - 1; i >= 0; i -= 1) {

extensions/allium/src/language-tools/extract-literal-refactor.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ export function planExtractLiteralToConfig(
4848
text: `${configInsert.indent}${key}: ${typeName} = ${selected}\n`,
4949
});
5050
} else {
51+
const insertOffset = findInsertAfterVersionMarker(text);
5152
edits.push({
52-
startOffset: 0,
53-
endOffset: 0,
53+
startOffset: insertOffset,
54+
endOffset: insertOffset,
5455
text: `config {\n ${key}: ${typeName} = ${selected}\n}\n\n`,
5556
});
5657
}
@@ -198,6 +199,11 @@ function findMatchingBrace(text: string, openOffset: number): number {
198199
return -1;
199200
}
200201

202+
function findInsertAfterVersionMarker(text: string): number {
203+
const match = text.match(/^--\s*allium:\s*\d+[^\n]*\n/);
204+
return match ? match[0].length : 0;
205+
}
206+
201207
function escapeRegex(value: string): string {
202208
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
203209
}

0 commit comments

Comments
 (0)