All files / src/pretty_printer indent.ts

100% Statements 87/87
99.29% Branches 140/141
100% Functions 8/8
100% Lines 86/86

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 2121x 1x 1x     1x       1x   200x     200x       27x 27x   27x   27x 48x 1x   47x 47x 5x   42x 42x 42x 2x       27x         200x 200x 200x 200x 200x 200x   200x 960x 10x   950x 950x                                   94x 856x               12x 12x 844x         161x 161x 683x   3x 680x           72x 608x 3x 605x   155x 155x 450x       12x 12x 12x   938x       28x 28x 28x   910x 910x                                                                   179x 731x 3x 3x 728x       155x 155x   910x   200x     155x 150x   5x 5x 2x 2x 2x   2x 3x 2x 2x 1x     2x         200x     158x       233x       158x 158x 158x      
import * as Statements from "../abap/2_statements/statements";
import * as Expressions from "../abap/2_statements/expressions";
import {MacroContent, Comment, Empty} from "../abap/2_statements/statements/_statement";
import {StatementNode} from "../abap/nodes/statement_node";
import {IIndentationOptions} from "./indentation_options";
import {VirtualPosition} from "../position";
import {ABAPFile} from "../abap/abap_file";
 
// todo, will break if there is multiple statements per line?
export class Indent {
  private readonly options: IIndentationOptions;
  private readonly globalClasses = new Set();
 
  public constructor(options?: IIndentationOptions) {
    this.options = options || {};
  }
 
  public execute(original: ABAPFile, modified: string): string {
    const statements = original.getStatements();
    const expected = this.getExpectedIndents(original);
 
    const lines = modified.split("\n");
 
    for (const statement of statements) {
      if (statement.getFirstToken().getStart() instanceof VirtualPosition) {
        continue; // macro contents
      }
      const exp = expected.shift();
      if (exp === undefined || exp < 0) {
        continue;
      }
      const row = statement.getFirstToken().getStart().getRow() - 1;
      lines[row] = lines[row].trim();
      for (let i = 1; i < exp; i++) {
        lines[row] = " " + lines[row];
      }
    }
 
    return lines.join("\n");
  }
 
  // returns list of expected indentation for each line/statement?
  public getExpectedIndents(file: ABAPFile): number[] {
    const ret: number[] = [];
    const init: number = 1;
    const stack = new Stack();
    let indent: number = init;
    let parentIsEvent: boolean = false;
    let previousStatement: StatementNode | undefined = undefined;
 
    for (const statement of file.getStatements()) {
      if (statement.getFirstToken().getStart() instanceof VirtualPosition) {
        continue; // skip macro contents
      }
      const type = statement.get();
      if (type instanceof Statements.EndIf
        || type instanceof Statements.EndWhile
        || type instanceof Statements.EndModule
        || type instanceof Statements.EndSelect
        || type instanceof Statements.EndMethod
        || type instanceof Statements.EndAt
        || type instanceof Statements.Else
        || type instanceof Statements.EndExec
        || type instanceof Statements.EndOfDefinition
        || type instanceof Statements.EndLoop
        || type instanceof Statements.EndTestInjection
        || type instanceof Statements.EndTestSeam
        || type instanceof Statements.EndForm
        || type instanceof Statements.EndCatch
        || type instanceof Statements.ElseIf
        || type instanceof Statements.EndFunction
        || type instanceof Statements.EndInterface
        || type instanceof Statements.EndDo) {
        indent = indent - 2;
      } else if (type instanceof Statements.StartOfSelection
        || type instanceof Statements.AtSelectionScreen
        || type instanceof Statements.AtLineSelection
        || type instanceof Statements.Initialization
        || type instanceof Statements.AtUserCommand
        || type instanceof Statements.TopOfPage
        || type instanceof Statements.EndOfSelection
        || type instanceof Statements.LoadOfProgram) {
        indent = init;
        parentIsEvent = true;
      } else if (type instanceof Statements.Form
        || (type instanceof Statements.Include && parentIsEvent)
        || type instanceof Statements.Module
        || type instanceof Statements.ClassImplementation
        || type instanceof Statements.ClassDefinition) {
        indent = init;
        parentIsEvent = false;
      } else if (type instanceof Statements.Cleanup
        || type instanceof Statements.Catch) {
        indent = stack.peek() - 2;
      } else if (type instanceof Statements.Public
        || type instanceof Statements.Protected
        || type instanceof Statements.Private
        || type instanceof Statements.WhenType
        || type instanceof Statements.WhenOthers
        || type instanceof Statements.When) {
        indent = stack.peek();
      } else if (type instanceof Statements.EndTry) {
        indent = stack.pop() - (this.options.alignTryCatch ? 2 : 4);
      } else if (type instanceof Statements.EndClass
        || type instanceof Statements.EndCase) {
        indent = stack.pop() - 2;
        indent = Math.max(indent, init); // maybe move this out of switch before ret.push(indent)
      } else if (type instanceof Comment
        || type instanceof Statements.IncludeType
        || type instanceof Empty
        || type instanceof MacroContent) {
        ret.push(-1);
        previousStatement = statement;
        continue;
      }
      if (previousStatement
        && !(previousStatement.get() instanceof Comment)
        && previousStatement.getLastToken().getEnd().getRow() === statement.getFirstToken().getStart().getRow()) {
// any indentation allowed if there are multiple statements on the same line
        ret.push(-1);
        previousStatement = statement;
        continue;
      }
      ret.push(indent);
      if (type instanceof Statements.If
        || type instanceof Statements.While
        || type instanceof Statements.Module
        || type instanceof Statements.SelectLoop
        || type instanceof Statements.FunctionModule
        || type instanceof Statements.Interface
        || type instanceof Statements.Do
        || type instanceof Statements.At
        || type instanceof Statements.ExecSQL
        || type instanceof Statements.Catch
        || type instanceof Statements.Define
        || type instanceof Statements.When
        || type instanceof Statements.WhenType
        || type instanceof Statements.WhenOthers
        || type instanceof Statements.Cleanup
        || type instanceof Statements.Loop
        || type instanceof Statements.CatchSystemExceptions
        || type instanceof Statements.Form
        || type instanceof Statements.Else
        || type instanceof Statements.ElseIf
        || type instanceof Statements.MethodImplementation
        || type instanceof Statements.TestInjection
        || type instanceof Statements.TestSeam
        || type instanceof Statements.StartOfSelection
        || type instanceof Statements.AtSelectionScreen
        || type instanceof Statements.AtLineSelection
        || type instanceof Statements.LoadOfProgram
        || type instanceof Statements.Initialization
        || type instanceof Statements.AtUserCommand
        || type instanceof Statements.TopOfPage
        || type instanceof Statements.EndOfSelection
        || type instanceof Statements.Public
        || type instanceof Statements.Protected
        || type instanceof Statements.Private) {
        indent = indent + 2;
      } else if (type instanceof Statements.Try) {
        indent = indent + (this.options.alignTryCatch ? 2 : 4);
        stack.push(indent);
      } else if (type instanceof Statements.ClassDefinition
        || type instanceof Statements.Case
        || type instanceof Statements.CaseType
        || type instanceof Statements.ClassImplementation) {
        indent = indent + (this.skipIndentForGlobalClass(statement) ? 0 : 2);
        stack.push(indent);
      }
      previousStatement = statement;
    }
    return ret;
  }
  private skipIndentForGlobalClass(statement: StatementNode): boolean {
    if (!this.options.globalClassSkipFirst) {
      return false;
    }
    const type = statement.get();
    if (type instanceof Statements.ClassDefinition && statement.findFirstExpression(Expressions.ClassGlobal)) {
      const className = statement.findFirstExpression(Expressions.ClassName);
      Eif (className) {
        this.globalClasses.add(className.getFirstToken().getStr().toUpperCase());
      }
      return true;
    } else if (type instanceof Statements.ClassImplementation) {
      const className = statement.findFirstExpression(Expressions.ClassName);
      if (className && this.globalClasses.has(className.getFirstToken().getStr().toUpperCase())) {
        return true;
      }
    }
    return false;
  }
}
 
class Stack {
  private items: number[] = [];
 
  public push(item: number) {
    this.items.push(item);
  }
 
  public peek(): number {
    return this.items[this.items.length - 1];
  }
 
  public pop() {
    const peek = this.peek();
    this.items = this.items.slice(0, this.items.length - 1);
    return peek;
  }
}