Skip to content

Commit

Permalink
remove console.logs and tidy up code
Browse files Browse the repository at this point in the history
  • Loading branch information
philanderson888 committed Jan 5, 2025
1 parent 44ca5ba commit 88272b2
Showing 1 changed file with 2 additions and 55 deletions.
57 changes: 2 additions & 55 deletions src/formatters/IndentFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@ import { OutdentSpacerTokenKinds, IndentSpacerTokenKinds, CallableKeywordTokenKi
import type { FormattingOptions } from '../FormattingOptions';
import { util } from '../util';

console.log('IndentFormatter.ts');

export class IndentFormatter {

/**
* Handle indentation for an array of tokens
*/
public format(tokens: Token[], options: FormattingOptions, parser: Parser) {

console.log('IndentFormatter.format');

// The text used for each tab
let tabText = options.indentStyle === 'tabs' ? '\t' : ' '.repeat(options.indentSpaceCount ?? DEFAULT_INDENT_SPACE_COUNT);

Expand All @@ -35,24 +30,20 @@ export class IndentFormatter {
const { currentLineOffset, nextLineOffset } = this.processLine(lineTokens, tokens, ifStatements, parentIndentTokenKinds);

//uncomment the next line to debug indent/outdent issues
console.log(currentLineOffset.toString().padStart(3, ' '), nextLineOffset.toString().padStart(3, ' '), lineTokens.map(x => x.text).join('').replace(/\r?\n/, '').replace(/^\s*/, ''));
//console.log(currentLineOffset.toString().padStart(3, ' '), nextLineOffset.toString().padStart(3, ' '), lineTokens.map(x => x.text).join('').replace(/\r?\n/, '').replace(/^\s*/, ''));

//compute the current line's tab count (default to 0 if we somehow went negative)
let currentLineTabCount = Math.max(globalTabCount + currentLineOffset, 0);
console.log('currentLineTabCount', currentLineTabCount);

//update the offset for the next line (default to 0 if we somehow went negative)
globalTabCount = Math.max(globalTabCount + nextLineOffset, 0);
console.log('globalTabCount', globalTabCount);

this.ensureTokenIndentation(lineTokens, currentLineTabCount, tabText);
this.trimWhitespaceOnlyLines(lineTokens);

//push these tokens to the result list
console.log('lineTokens', lineTokens.map(x => x.text).join(''));
result.push(...lineTokens);
}
console.log('result', result.map(x => x.text).join(''));
return result;
}

Expand All @@ -63,28 +54,15 @@ export class IndentFormatter {
parentIndentTokenKinds: TokenKind[]
): { currentLineOffset: number; nextLineOffset: number } {

console.log(`\n\nIndentFormatter.processLine`);

console.log('parentIndentTokenKinds at start of line processing ', parentIndentTokenKinds.map(x => TokenKind[x]));

const getParentIndentTokenKind = () => {
const parentIndentTokenKind = parentIndentTokenKinds.length > 0 ? parentIndentTokenKinds[parentIndentTokenKinds.length - 1] : undefined;
console.log('parentIndentTokenKinds at end of line processing', parentIndentTokenKinds.map(x => TokenKind[x]));
console.log('parentIndentTokenKind', parentIndentTokenKind);
return parentIndentTokenKind;
};

let currentLineOffset = 0;
let nextLineOffset = 0;
let foundIndentorThisLine = false;

console.log(`\nlineTokens`, lineTokens.map(x => x.text).join(''));
console.log(`lineTokens.length: ${lineTokens.length}`);
lineTokens.forEach((token, index) => {
console.log(`token `, index, TokenKind[token.kind]);
});
console.log('parentIndentTokenKinds', parentIndentTokenKinds.map(x => TokenKind[x]));

for (let i = 0; i < lineTokens.length; i++) {
let token = lineTokens[i];
let previousNonWhitespaceToken = util.getPreviousNonWhitespaceToken(lineTokens, i);
Expand Down Expand Up @@ -136,11 +114,7 @@ export class IndentFormatter {
}

// check for specifically mentioned tokens to NOT indent
console.log(`checking for items not to indent`);
const parentIndentTokenKind = getParentIndentTokenKind();
console.log('parentIndentTokenKind at point of decision is ', parentIndentTokenKind);
console.log('token.kind', TokenKind[token.kind]);

const parentIndentTokenKindsContainsSubOrFunction = parentIndentTokenKinds.includes(TokenKind.Sub) || parentIndentTokenKinds.includes(TokenKind.Function);

const tokenKindIsClass = token.kind === TokenKind.Class;
Expand All @@ -149,18 +123,13 @@ export class IndentFormatter {
const tokenKindIsNamespace = token.kind === TokenKind.Namespace;
const tokenKindIsTry = token.kind === TokenKind.Try;

// don't indent class or enum if parent is sub or function
// dont indent if parent is sub or function and this is a class, enum, interface, namespace, or try
const preventIndent = parentIndentTokenKindsContainsSubOrFunction && (tokenKindIsClass || tokenKindIsEnum || tokenKindIsInterface || tokenKindIsNamespace || tokenKindIsTry);

if (preventIndent) {
console.log('parentIndentTokenKindsContainsSubOrFunction && tokenKindIsClass');
// don't indent class if parent is sub or function
continue;
}

if (parentIndentTokenKind && IgnoreIndentSpacerByParentTokenKind.get(parentIndentTokenKind)?.includes(token.kind)) {
console.log('IgnoreIndentSpacerByParentTokenKind.get(parentIndentTokenKind)');
console.log(IgnoreIndentSpacerByParentTokenKind.get(parentIndentTokenKind)?.includes(token.kind));
// This particular token should not be indented because it is listed in the ignore group for its parent
continue;
}
Expand All @@ -169,7 +138,6 @@ export class IndentFormatter {
foundIndentorThisLine = true;
parentIndentTokenKinds.push(token.kind);


//don't double indent if this is `[[...\n...]]` or `[{...\n...}]`
if (
//is open square
Expand Down Expand Up @@ -237,8 +205,6 @@ export class IndentFormatter {
// tabCount--;
// }
}
console.log('currentLineOffset', currentLineOffset);
console.log('nextLineOffset', nextLineOffset);
return {
currentLineOffset: currentLineOffset,
nextLineOffset: nextLineOffset
Expand All @@ -253,8 +219,6 @@ export class IndentFormatter {
*/
private ensureTokenIndentation(tokens: Token[], tabCount: number, tabText = ' '): Token[] {

console.log('IndentFormatter.ensureTokenIndentation');

//do nothing if we have an empty tokens list
if (!tokens || tokens.length === 0) {
return tokens;
Expand Down Expand Up @@ -286,8 +250,6 @@ export class IndentFormatter {
*/
private trimWhitespaceOnlyLines(tokens: Token[]) {

console.log('IndentFormatter.trimWhitespaceOnlyLines');

//if the first token is whitespace, and the next is EOL or EOF
if (
tokens[0].kind === TokenKind.Whitespace &&
Expand All @@ -304,8 +266,6 @@ export class IndentFormatter {
*/
private getAllIfStatements(parser: Parser) {

console.log('IndentFormatter.getAllIfStatements');

const ifStatements = new Map<Token, IfStatement>();

parser.ast.walk(createVisitor({
Expand All @@ -322,9 +282,6 @@ export class IndentFormatter {
* Split the tokens by newline (including the newline or EOF as the last token in that array)
*/
private splitTokensByLine(tokens: Token[]) {

console.log('IndentFormatter.splitTokensByLine');

const result: Array<Token[]> = [];
let line: Token[] = [];
for (let token of tokens) {
Expand All @@ -345,9 +302,6 @@ export class IndentFormatter {
* Then return the endIf token if it exists
*/
private getEndIfToken(ifStatement: IfStatement | undefined) {

console.log('IndentFormatter.getEndIfToken');

if (isIfStatement(ifStatement)) {
while (true) {
if (isIfStatement(ifStatement.elseBranch)) {
Expand All @@ -364,9 +318,6 @@ export class IndentFormatter {
* Given a kind like `}` or `]`, walk backwards until we find its match
*/
public getOpeningToken(tokens: Token[], currentIndex: number, openKind: TokenKind, closeKind: TokenKind) {

console.log('IndentFormatter.getOpeningToken');

let openCount = 0;
for (let i = currentIndex; i >= 0; i--) {
let token = tokens[i];
Expand All @@ -385,10 +336,6 @@ export class IndentFormatter {
* Determines if this is an outdent token
*/
public isOutdentToken(token: Token, nextNonWhitespaceToken?: Token) {

console.log('IndentFormatter.isOutdentToken');


//this is a temporary fix for broken indentation for brighterscript ternary operations.
const isSymbol = [TokenKind.RightCurlyBrace, TokenKind.RightSquareBracket].includes(token.kind);
if (
Expand Down

0 comments on commit 88272b2

Please sign in to comment.