All files / src/pretty_printer pretty_printer.ts

100% Statements 23/23
86.67% Branches 13/15
100% Functions 2/2
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 551x 1x 1x   1x   1x   1x   1x             27x 27x 27x   27x   27x             27x 27x 48x         7x       41x 41x     27x 27x   27x 27x   27x      
import {Unknown, MacroContent, MacroCall, Comment} from "../abap/2_statements/statements/_statement";
import {FixCase} from "./fix_keyword_case";
import {Indent as Indent} from "./indent";
import {IIndentationOptions} from "./indentation_options";
import {RemoveSequentialBlanks} from "./remove_sequential_blanks";
import {IConfiguration} from "../_config";
import {VirtualPosition} from "../position";
import {ABAPFile} from "../abap/abap_file";
import {Indentation, IndentationConf} from "../rules/indentation";
 
export class PrettyPrinter {
  private result: string;
  private readonly file: ABAPFile;
  private readonly options: IIndentationOptions;
  private readonly config: IConfiguration;
 
  public constructor(file: ABAPFile, config: IConfiguration) {
    this.result = file.getRaw();
    this.file = file;
    this.config = config;
 
    const indentationConf: IndentationConf = config.readByRule(new Indentation().getMetadata().key);
 
    this.options = {
      alignTryCatch: indentationConf?.alignTryCatch,
      globalClassSkipFirst: indentationConf?.globalClassSkipFirst,
    };
  }
 
  public run(): string {
    const statements = this.file.getStatements();
    for (const statement of statements) {
      if (statement.get() instanceof Unknown
        || statement.get() instanceof MacroContent
        || statement.get() instanceof MacroCall
        || statement.getFirstToken().getStart() instanceof VirtualPosition
        || statement.get() instanceof Comment) {
        continue;
      }
 
      // note that no positions are changed when case is changed
      const fixCase = new FixCase(this.result, this.config);
      this.result = fixCase.execute(statement);
    }
 
    const indentation = new Indent(this.options);
    this.result = indentation.execute(this.file, this.result);
 
    const removeBlanks = new RemoveSequentialBlanks(this.config);
    this.result = removeBlanks.execute(this.file, this.result);
 
    return this.result;
  }
 
}