All files / src edit_helper.ts

90.52% Statements 258/285
86.56% Branches 58/67
93.33% Functions 14/15
90.52% Lines 258/285

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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 2851x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 44x 44x 44x 1x 1x 1x 60x 44x 44x 60x 60x 60x 60x 60x 60x 60x 60x 1x 1x 44x   44x     44x 44x 46x 42x 42x 46x 2x 2x 2x 2x 2x 2x       46x 44x 44x 1x 1x 1x 1x 1x                       1x 1x 351x 351x 351x 351x 351x 351x 351x 351x 351x 351x 351x     351x 351x 351x 351x 351x 1x 1x 304x     304x 1205x 304x 304x 1205x     1x 1x 162x 162x 134x 134x 28x 28x 28x 28x 28x 28x 28x 162x 121x 121x 61x 121x 28x 28x 28x 60x 19x 7x 7x 32x 13x 10x 10x 13x 13x 121x 162x 13x 13x 13x 15x 15x 15x 162x 30x 15x 15x 15x 30x 15x 15x 162x 5x 5x 5x 5x 162x 8x 8x 10x 2x 2x 2x 162x 1x 1x 18x 18x 36x 18x 18x 36x     1x 1x 24x 24x 24x 24x 1x 1x 300x 300x 300x 300x 1x 1x 328x 328x 328x 328x 1x 1x 38x 38x 1x 1x 729x 729x 729x 729x 1x 1x 347x 347x 347x 347x     347x 347x 546x 475x 475x 475x 475x 475x 546x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 546x 347x 347x 347x 347x 347x 1x 1x 2x 2x 2x 2x 2x 2x 4x 4x 2x 2x 2x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x
import {Token} from "./abap/1_lexer/tokens/_token";
import {IFile} from "./files/_ifile";
import {Position} from "./position";
import {IRegistry} from "./_iregistry";
import {MemoryFile} from "./files/memory_file";
import {StatementNode} from "./abap/nodes/statement_node";
import {ABAPFile} from "./abap/abap_file";
 
export interface IRange {
  start: Position;
  end: Position;
}
 
export interface ITextEdit {
  range: IRange;
  newText: string;
}
 
export interface IEdit {
  [filename: string]: ITextEdit[];
}
 
export class EditDraft {
  private start: Position | undefined = undefined;
  private end: Position | undefined = undefined;
  private readonly rows: string[];
  private readonly file: IFile;
 
  public constructor(file: IFile) {
    this.rows = file.getRawRows();
    this.file = file;
  }
 
  /** replace existing text, insert text wont work */
  public replace(pos: Position, value: string) {
    if (this.start === undefined || pos.isBefore(this.start)) {
      this.start = pos;
    }
    const end = new Position(pos.getRow(), pos.getCol() + value.length);
    if (this.end === undefined || end.isAfter(this.end)) {
      this.end = end;
    }
 
    const str = this.rows[pos.getRow() - 1];
    this.rows[pos.getRow() - 1] = str.substr(0, pos.getCol() - 1) + value + str.substr(pos.getCol() + value.length - 1);
  }
 
  public toEdit(): IEdit {
    if (this.start === undefined) {
      throw "EditDraft, start undefined";
    } else if (this.end === undefined) {
      throw "EditDraft, end undefined";
    }
    let value = "";
    for (let row = this.start.getRow(); row <= this.end.getRow(); row++) {
      if (row === this.start.getRow() && row === this.end.getRow()) {
        // first and last row
        value = this.rows[row - 1].substring(this.start.getCol() - 1, this.end.getCol() - 1);
      } else if (row === this.start.getRow()) {
        // first row
        value = this.rows[row - 1].substring(this.start.getCol() - 1);
      } else if (row === this.end.getRow()) {
        // last row
        value += "\n" + this.rows[row - 1].substring(0, this.end.getCol() - 1);
      } else {
        // middle row
        value += "\n" + this.rows[row - 1];
      }
    }
    return EditHelper.replaceRange(this.file, this.start, this.end, value);
  }
}
 
export class EditHelper {
 
  public static mergeList(fixes: IEdit[]): IEdit {
    const results: IEdit = {};
    for (const f of fixes) {
      for (const filename in f) {
        if (results[filename] === undefined) {
          results[filename] = [];
        }
        results[filename] = results[filename].concat(f[filename]);
      }
    }
    return results;
  }
 
  public static merge(fix1: IEdit, fix2: IEdit): IEdit {
    const ret: IEdit = {};
 
    for (const k of Object.keys(fix1)) {
      if (ret[k] === undefined) {
        ret[k] = [];
      }
      ret[k] = ret[k].concat(fix1[k]);
    }
 
    for (const k of Object.keys(fix2)) {
      if (ret[k] === undefined) {
        ret[k] = [];
      }
      ret[k] = ret[k].concat(fix2[k]);
    }
 
    return ret;
  }
 
  public static findStatement(token: Token, file: ABAPFile | undefined): StatementNode | undefined {
    if (file === undefined) {
      return undefined;
    }
    for (const s of file.getStatements()) {
      if (s.includesToken(token)) {
        return s;
      }
    }
    return undefined;
  }
 
  public static deleteStatement(file: ABAPFile, statement: StatementNode): IEdit {
    const scolon = statement.getColon();
    if (scolon === undefined) {
      return EditHelper.deleteRange(file, statement.getFirstToken().getStart(), statement.getLastToken().getEnd());
    }
 
    let setPrevious = true;
    let setNext = true;
    /** previous statement in the chain */
    let previousStatement: StatementNode | undefined = undefined;
    /** next statement in the chain */
    let nextStatement: StatementNode | undefined = undefined;
    for (const s of file.getStatements()) {
      const colon = s.getColon();
      if (colon === undefined) {
        continue;
      } else if (s === statement) {
        setPrevious = false;
        setNext = true;
        continue;
      } else if (setPrevious === true) {
        if (scolon.getStart().equals(colon.getStart())) {
          previousStatement = s;
        }
      } else if (setNext === true) {
        if (scolon.getStart().equals(colon.getStart())) {
          nextStatement = s;
        }
        break;
      }
    }
    if (previousStatement === undefined && nextStatement === undefined) {
      // the statement to be deleted is the only one in the chain
      return EditHelper.deleteRange(file, statement.getFirstToken().getStart(), statement.getLastToken().getEnd());
    }
 
    // the start of deletion should happen for tokens after the colon
    let startDelete = statement.getFirstToken().getStart();
    for (const t of statement.getTokens()) {
      if (t.getStart().isAfter(scolon.getEnd())) {
        startDelete = t.getStart();
        break;
      }
    }
 
    const colon = statement.getColon();
    if (statement.getLastToken().getStr() === "." && previousStatement) {
// last statement in chain
      const edit1 = EditHelper.replaceToken(file, previousStatement.getLastToken(), ".");
      const edit2 = EditHelper.deleteRange(file, previousStatement.getLastToken().getEnd(), statement.getLastToken().getEnd());
      return EditHelper.merge(edit1, edit2);
    } else if (previousStatement === undefined && colon && nextStatement) {
// first statement in chain
      return EditHelper.deleteRange(file, this.firstAfterColon(statement), this.firstAfterColon(nextStatement));
    } else {
// middle statement
      return EditHelper.deleteRange(file, startDelete, this.firstAfterColon(nextStatement!));
    }
  }
 
  public static firstAfterColon(statement: StatementNode): Position {
    const colon = statement.getColon()!.getStart();
    for (const t of statement.getTokens()) {
      if (t.getStart().isAfter(colon)) {
        return t.getStart();
      }
    }
    throw new Error("firstAfterColon, emtpy statement?");
  }
 
  public static deleteToken(file: IFile, token: Token): IEdit {
    const filename = file.getFilename();
    const range: IRange = {start: token.getStart(), end: token.getEnd()};
    return {[filename]: [{range, newText: ""}]};
  }
 
  public static deleteRange(file: IFile, start: Position, end: Position): IEdit {
    const filename = file.getFilename();
    const range: IRange = {start, end};
    return {[filename]: [{range, newText: ""}]};
  }
 
  public static insertAt(file: IFile, pos: Position, text: string): IEdit {
    const filename = file.getFilename();
    const range: IRange = {start: pos, end: pos};
    return {[filename]: [{range, newText: text}]};
  }
 
  public static replaceToken(file: IFile, token: Token, text: string): IEdit {
    return this.replaceRange(file, token.getStart(), token.getEnd(), text);
  }
 
  public static replaceRange(file: IFile, start: Position, end: Position, text: string): IEdit {
    const filename = file.getFilename();
    const range: IRange = {start, end};
    return {[filename]: [{range, newText: text}]};
  }
}
 
export function applyEditSingle(reg: IRegistry, edit: IEdit) {
  for (const filename in edit) {
    let rows = reg.getFileByName(filename)?.getRawRows();
    if (rows === undefined) {
      throw new Error("applyEdit, file not found");
    }
 
    for (const e of edit[filename]) {
      if (e.range.start.getRow() === e.range.end.getRow()) {
        const line = rows[e.range.start.getRow() - 1];
        rows[e.range.start.getRow() - 1] =
          line.substr(0, e.range.start.getCol() - 1) +
          e.newText +
          line.substr(e.range.end.getCol() - 1);
      } else {
        const first = rows[e.range.start.getRow() - 1];
        let res = first.substr(0, e.range.start.getCol() - 1) + e.newText;
        const last = rows[e.range.end.getRow() - 1];
        res = res + last.substr(e.range.end.getCol() - 1);
        // delete middle lines
        rows.splice(e.range.start.getRow(), e.range.end.getRow() - e.range.start.getRow());
        // clean up
        rows[e.range.start.getRow() - 1] = res;
        rows = rows.join("\n").split("\n"); // if the edit contained newlines and multiple edits
      }
    }
    const result = new MemoryFile(filename, rows.join("\n"));
 
    reg.updateFile(result);
  }
}
 
/** returns list of filenames which were changed */
export function applyEditList(reg: IRegistry, edits: IEdit[]): string[] {
  const ret: string[] = [];
  let length = 0;
 
  const merged: IEdit = {};
  for (const e of edits) {
    for (const f in e) {
      if (merged[f] === undefined) {
        merged[f] = [];
        length = length + 1;
      }
      merged[f] = merged[f].concat(e[f]);
    }
  }
 
  for (const f in merged) {
    const singleFile: IEdit = {};
    // sort, start with the last position first
    singleFile[f] = merged[f].sort((a, b) => {
      let val = b.range.start.getRow() - a.range.start.getRow();
      if (val === 0) {
        val = b.range.start.getCol() - a.range.start.getCol();
      }
      return val;
    });
 
    applyEditSingle(reg, singleFile);
 
    ret.push(f);
  }
 
  return ret;
}