Skip to content

Commit

Permalink
Added support to check multiple indents followed by multiple outdents
Browse files Browse the repository at this point in the history
  • Loading branch information
fumer-fubotv committed Apr 4, 2024
1 parent bc8660d commit 7f4c09b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 11 deletions.
47 changes: 47 additions & 0 deletions src/Formatter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,53 @@ describe('Formatter', () => {
end function }
}
`);

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

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

});
});

Expand Down
53 changes: 42 additions & 11 deletions src/formatters/IndentFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,6 @@ 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 @@ -212,12 +201,54 @@ export class IndentFormatter {
// tabCount--;
// }
}

//check if next multiple indents are followed by multiple outdents and update indentation accordingly
if (nextLineOffset > 1) {
nextLineOffset = this.lookaheadSameLineMultiOutdents(tokens, lineTokens[lineTokens.length - 1], nextLineOffset, currentLineOffset);
}

return {
currentLineOffset: currentLineOffset,
nextLineOffset: nextLineOffset
};
}

/**
* Lookahead if next line with oudents are same as indents on current line
* @param tokens the array of tokens in a file
* @param curLineToken token of curent line
* @param nextLineOffset the number of tabs to indent the next line
* @param currentLineOffset the number of tabs to indent the current line
*/
private lookaheadSameLineMultiOutdents(tokens: Token[], curLineToken: Token, nextLineOffset: number, currentLineOffset: number): number {
let outdentCount = 0;
let tokenLineNum = 0;
let currentLineTokenIndex = tokens.indexOf(curLineToken);

for (let i = currentLineTokenIndex + 1; i < tokens.length; i++) {
let token = tokens[i];
//next line with outdents
if (OutdentSpacerTokenKinds.includes(token.kind)) {
if (tokenLineNum === 0) {
tokenLineNum = token.range.start.line;
}

if (token.range.start.line === tokenLineNum) {
outdentCount++;
} else {
//exit when the line ends
break;
}
}
}

//if outdents on next line with outdents = indents on current line then indent next line by one tab only
if (outdentCount > 0 && outdentCount === nextLineOffset) {
nextLineOffset = currentLineOffset + 1;
}
return nextLineOffset;
}

/**
* Ensure the list of tokens contains the correct number of tabs
* @param tokens the array of tokens to be modified in-place
Expand Down

0 comments on commit 7f4c09b

Please sign in to comment.