Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle double-indent followed by double-outdent #89

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
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', () => {
TwitchBronBron marked this conversation as resolved.
Show resolved Hide resolved
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
41 changes: 32 additions & 9 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 (
IndentSpacerTokenKinds.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,23 +163,34 @@ export class IndentFormatter {
}

nextLineOffset--;

let doubleIndentSkip = (token.kind === TokenKind.RightCurlyBrace || token.kind === TokenKind.RightSquareBracket) &&
//next is closing square
nextNonWhitespaceToken && nextNonWhitespaceToken.kind === TokenKind.RightSquareBracket &&
//both tokens are on the same line
token.range.start.line === nextNonWhitespaceToken.range.start.line;

if (
OutdentSpacerTokenKinds.includes(token.kind) &&
//if the line has already been outdented
(foundUnIndentOnThisLine) &&
!doubleIndentSkip
) {
continue;
}

foundUnIndentOnThisLine = true;

if (foundIndentorThisLine === false) {
currentLineOffset--;
}
parentIndentTokenKinds.pop();

//don't double un-indent if this is `[[...\n...]]` or `[{...\n...}]`
if (
//is closing curly or square
(token.kind === TokenKind.RightCurlyBrace || token.kind === TokenKind.RightSquareBracket) &&
//next is closing square
nextNonWhitespaceToken && nextNonWhitespaceToken.kind === TokenKind.RightSquareBracket &&
//both tokens are on the same line
token.range.start.line === nextNonWhitespaceToken.range.start.line
) {
if (doubleIndentSkip) {
let opener = this.getOpeningToken(
tokens,
tokens.indexOf(nextNonWhitespaceToken),
tokens.indexOf(nextNonWhitespaceToken!),
TokenKind.LeftSquareBracket,
TokenKind.RightSquareBracket
);
Expand Down