All files / src edit_helper.ts

90.49% Statements 238/263
85.96% Branches 49/57
92.86% Functions 13/14
90.49% Lines 238/263

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 2631x 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 46x 46x 46x 1x 1x 1x 59x 46x 46x 59x 59x 59x 59x 59x 59x 59x 59x 1x 1x 46x   46x     46x 46x 48x 44x 44x 48x 2x 2x 2x 2x 2x 2x       48x 46x 46x 1x 1x 1x 1x 1x                       1x 1x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x     90x 90x 90x 90x 90x 1x 1x 240x     240x 875x 240x 240x 875x     1x 1x 138x 138x 114x 114x 24x 24x 24x 24x 24x 24x 138x 152x 152x 84x 152x 24x 24x 68x 26x 26x 152x 19x 19x 152x 138x 10x 10x 10x 14x 14x 14x 138x 28x 14x 14x 14x 28x 14x 138x 4x 4x 4x 138x 10x 10x 138x 1x 1x 8x 8x 8x 8x 1x 1x 237x 237x 237x 237x 1x 1x 84x 84x 84x 84x 1x 1x 35x 35x 1x 1x 354x 354x 354x 354x 1x 1x 173x 173x 173x 173x     173x 173x 236x 214x 214x 214x 214x 214x 236x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 236x 173x 173x 173x 173x 173x 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());
    }
 
    // find statements part of chain
    let chainCount = 0;
    let setPrevious = true;
    /** previous statement in the chain */
    let previousStatement: StatementNode | undefined = undefined;
    for (const s of file.getStatements()) {
      const colon = s.getColon();
      if (colon === undefined) {
        continue;
      } else if (s === statement) {
        setPrevious = false;
        continue;
      } else if (colon.getStart().equals(scolon.getStart())) {
        chainCount = chainCount + 1;
      }
      if (setPrevious === true) {
        previousStatement = s;
      }
    }
    if (chainCount === 0) {
      // 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;
      }
    }
 
    if (statement.getLastToken().getStr() === "." && previousStatement) {
      const edit1 = EditHelper.replaceToken(file, previousStatement.getLastToken(), ".");
      const edit2 = EditHelper.deleteRange(file, startDelete, statement.getLastToken().getEnd());
      return EditHelper.merge(edit1, edit2);
    } else {
      return EditHelper.deleteRange(file, startDelete, statement.getLastToken().getEnd());
    }
  }
 
  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;
}