All files / src/pretty_printer pretty_printer.ts

100% Statements 51/51
81.81% Branches 9/11
100% Functions 2/2
100% Lines 51/51

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 511x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 1x 1x 25x 25x 38x 38x 38x 38x 38x 5x 5x 33x 33x 33x 33x 33x 25x 25x 25x 25x 25x 25x 1x 1x
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 {IConfiguration} from "../_config";
import {VirtualPosition} from "../virtual_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);
 
    return this.result;
  }
 
}