All files / src/pretty_printer indent.ts

100% Statements 228/228
100% Branches 150/150
100% Functions 8/8
100% Lines 228/228

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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 2291x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 322x 322x 1x 1x 25x 25x 25x 25x 25x 25x 38x 1x 1x 37x 38x 5x 5x 32x 32x 38x 2x 2x 32x 25x 25x 25x 1x 1x 1x 322x 322x 322x 322x 322x 322x 322x 322x 1662x 10x 10x 1652x 1652x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1517x 1517x 16x 1517x 1662x 1662x 1662x 1662x 197x 1662x 1455x 1455x 1455x 1455x 1455x 1455x 1455x 1455x 15x 15x 1455x 1440x 1440x 1440x 1440x 269x 269x 1440x 1171x 5x 1171x 1166x 1166x 1166x 1166x 1166x 115x 1166x 6x 1051x 1045x 236x 236x 1045x 809x 809x 809x 32x 32x 32x 32x 1620x 1662x 1662x 45x 45x 45x 45x 45x 1575x 1575x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1373x 1373x 16x 16x 1373x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 1662x 330x 1662x 6x 6x 1245x 1239x 1239x 1239x 236x 236x 236x 1575x 1575x 322x 322x 1x 1x 236x 231x 231x 5x 236x 2x 2x 2x 2x 2x 236x 2x 2x 1x 1x 2x 2x 2x 1x 1x 322x 322x 322x 322x 242x 242x 322x 322x 362x 362x 322x 322x 242x 242x 242x 242x 322x  
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
        || (this.options.selectionScreenBlockIndentation === true
          && type instanceof Statements.SelectionScreen
          && (statement.concatTokens().toUpperCase().includes("END OF SCREEN") ||
          statement.concatTokens().toUpperCase().includes("END OF BLOCK") ||
          statement.concatTokens().toUpperCase().includes("END OF LINE")))
        || 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.Get
        || 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.AtFirst
        || type instanceof Statements.AtLast
        || 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.LoopAtScreen
        || 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
        || (this.options.selectionScreenBlockIndentation === true
          && type instanceof Statements.SelectionScreen
          && (statement.concatTokens().toUpperCase().includes("BEGIN OF SCREEN") ||
          statement.concatTokens().toUpperCase().includes("BEGIN OF TABBED BLOCK") ||
          statement.concatTokens().toUpperCase().includes("BEGIN OF BLOCK") ||
          statement.concatTokens().toUpperCase().includes("BEGIN OF LINE")))
        || type instanceof Statements.StartOfSelection
        || type instanceof Statements.Get
        || 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);
      if (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;
  }
}