Skip to content

Commit

Permalink
handle double-indent followed by double-outdent
Browse files Browse the repository at this point in the history
  • Loading branch information
fumer-fubotv committed Apr 2, 2024
1 parent b634f67 commit d053541
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Formatter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,24 @@ describe('Formatter', () => {
end sub
`);
});

it('formats outdents', () => {
expect(formatter.format(undent`
temp = {
key_9: { env: ["any"], themes: ["any"], runtimeCheck: function() as boolean
return true
end function }
}
`, {
formatMultiLineObjectsAndArrays: false
})).to.equal(undent`
temp = {
key_9: { env: ["any"], themes: ["any"], runtimeCheck: function() as boolean
return true
end function }
}
`);
});
});

describe('formatMultiLineObjectsAndArrays', () => {
Expand Down
23 changes: 23 additions & 0 deletions src/formatters/IndentFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export class IndentFormatter {
let currentLineOffset = 0;
let nextLineOffset = 0;
let foundIndentorThisLine = false;
let foundUnIndentOnThisLine = false;

for (let i = 0; i < lineTokens.length; i++) {
let token = lineTokens[i];
Expand Down Expand Up @@ -90,6 +91,17 @@ export class IndentFormatter {
continue;
}

//skip indent for 'function'|'sub' used as type if another indent already exist in this line
if (
CallableKeywordTokenKinds.includes(token.kind) &&
//validate if its a function/sub call
nextNonWhitespaceToken.kind === TokenKind.LeftParen &&
foundIndentorThisLine
) {
parentIndentTokenKinds.push(token.kind);
continue;
}

//skip indent for single-line if statements
let ifStatement = ifStatements.get(token);
const endIfToken = this.getEndIfToken(ifStatement);
Expand Down Expand Up @@ -151,6 +163,17 @@ export class IndentFormatter {
}

nextLineOffset--;

if (
[TokenKind.RightCurlyBrace].includes(token.kind) &&
//if the line has already been unindented
(foundUnIndentOnThisLine)
) {
continue;
}

foundUnIndentOnThisLine = true;

if (foundIndentorThisLine === false) {
currentLineOffset--;
}
Expand Down

0 comments on commit d053541

Please sign in to comment.