Skip to content

Commit 8f36915

Browse files
committed
Fix handling indentation for suppressed indentors and })] sequences
- Fixed an issue where lines ending in `})]` were incorrectly dedented due to multiple outdent tokens triggering excessive un-indentation. - Improved IndentFormatter logic to correctly handle suppressed indentors (e.g., `{` inside `[`) by checking for subsequent outdenters on the same line before skipping the dedent.
1 parent 4052861 commit 8f36915

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/formatters/IndentFormatter.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,35 @@ describe('IndentFormatter', () => {
163163
const actual = format(input);
164164
expect(actual).to.equal(expected);
165165
});
166+
167+
it('handles indentation for })] with function call', () => {
168+
const input = undent`
169+
function GetOverflowActionFromMetadata(metadata as object) as object
170+
if metadata._container.isLiveTV = true or metadata.type = "collection" then return []
171+
172+
return [API().CreateAction("pmsOverflow", "overflow-horizontal-alt", ltr("More"), {
173+
"data": {
174+
"originId": metadata._container._originId,
175+
"ratingKey": metadata["ratingKey"],
176+
"key": metadata["key"].Replace("/children", ""),
177+
},
178+
})]
179+
end function
180+
`;
181+
const expected = [
182+
'function GetOverflowActionFromMetadata(metadata as object) as object',
183+
' if metadata._container.isLiveTV = true or metadata.type = "collection" then return []',
184+
'',
185+
' return [API().CreateAction("pmsOverflow", "overflow-horizontal-alt", ltr("More"), {',
186+
' "data": {',
187+
' "originId": metadata._container._originId,',
188+
' "ratingKey": metadata["ratingKey"],',
189+
' "key": metadata["key"].Replace("/children", ""),',
190+
' },',
191+
' })]',
192+
'end function'
193+
].join('\n');
194+
const actual = format(input);
195+
expect(actual).to.equal(expected);
196+
});
166197
});

src/formatters/IndentFormatter.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,26 @@ export class IndentFormatter {
203203
activeIndentorsOnThisLine--;
204204
}
205205
if (foundIndentorThisLine === false) {
206-
currentLineOffset--;
206+
let shouldDecrement = true;
207+
//if this didn't cause an indent, and there is another outdenter on this line,
208+
//then we shouldn't outdent the current line because the next outdenter will handle it
209+
if (!popped?.causedIndent) {
210+
for (let j = i + 1; j < lineTokens.length; j++) {
211+
let nextTok = lineTokens[j];
212+
if (nextTok.kind === TokenKind.Whitespace) {
213+
continue;
214+
}
215+
let nextNextTok = util.getNextNonWhitespaceToken(lineTokens, j);
216+
if (this.isOutdentToken(nextTok, nextNextTok)) {
217+
shouldDecrement = false;
218+
break;
219+
}
220+
}
221+
}
222+
223+
if (shouldDecrement) {
224+
currentLineOffset--;
225+
}
207226
}
208227

209228
//don't double un-indent if this is `[[...\n...]]` or `[{...\n...}]`

0 commit comments

Comments
 (0)