All files / src/lsp references.ts

88.23% Statements 105/119
65.62% Branches 21/32
100% Functions 5/5
88.23% Lines 105/119

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 1191x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15x 15x 1x 1x 15x 15x     15x 15x     15x 15x 15x     15x 15x 15x     15x 15x 15x 15x 1x 1x 1x 1x 1x 15x 15x 15x 15x 15x 4x 4x 15x     15x 15x 15x 15x     15x 15x 15x 15x 15x 15x 15x 15x     15x 1x 1x 15x 15x 28x 28x 28x 28x 15x 15x 1x 1x 81x 81x 81x 66x 66x 66x 66x 66x 66x 7x 7x 66x 66x 66x 66x 66x 1x 1x 66x 66x 42x 20x 20x 42x 66x 81x 81x 62x 62x 81x 81x 81x 1x 1x
import * as LServer from "vscode-languageserver-types";
import {IRegistry} from "../_iregistry";
import {ABAPObject} from "../objects/_abap_object";
import {ITextDocumentPositionParams} from "./_interfaces";
import {LSPUtils} from "./_lsp_utils";
import {Identifier} from "../abap/4_file_information/_identifier";
import {SyntaxLogic} from "../abap/5_syntax/syntax";
import {ISpaghettiScopeNode} from "../abap/5_syntax/_spaghetti_scope";
import {LSPLookup} from "./_lookup";
import {ScopeType} from "../abap/5_syntax/_scope_type";
 
export class References {
  private readonly reg: IRegistry;
 
  public constructor(reg: IRegistry) {
    this.reg = reg;
  }
 
  public references(pos: ITextDocumentPositionParams): LServer.Location[] {
    const file = LSPUtils.getABAPFile(this.reg, pos.textDocument.uri);
    if (file === undefined) {
      return [];
    }
    const obj = this.reg.getObject(file.getObjectType(), file.getObjectName());
    if (!(obj instanceof ABAPObject)) {
      return [];
    }
 
    const found = LSPUtils.findCursor(this.reg, pos);
    if (found?.identifier === undefined) {
      return [];
    }
 
    const lookup = LSPLookup.lookup(found, this.reg, obj);
    if (lookup?.definitionId === undefined || lookup?.scope === undefined) {
      return [];
    }
 
    const locs = this.search(lookup.definitionId, lookup.scope);
    return locs.map(LSPUtils.identiferToLocation);
  }
 
////////////////////////////////////////////
 
// todo, cleanup this mehtod, some of the method parameters are not used anymore?
  private search(identifier: Identifier, node: ISpaghettiScopeNode, exitAfterFound = false, removeDuplicates = true): Identifier[] {
    let ret: Identifier[] = [];
 
    // todo, this first assumes that the identifier is a variable?
    const stype = node.getIdentifier().stype;
    if (stype === ScopeType.Method || stype === ScopeType.FunctionModule || stype === ScopeType.Form) {
      ret = this.findReferences(node, identifier);
    }
    if (ret.length > 1 && exitAfterFound === true) {
      return ret;
    }
 
    for (const o of this.reg.getObjects()) {
      if (o instanceof ABAPObject) {
        if (this.reg.isDependency(o)) {
          continue; // do not search in dependencies
        }
        ret.push(...this.findReferences(new SyntaxLogic(this.reg, o).run().spaghetti.getTop(), identifier));
      }
    }
 
    // remove duplicates, might be a changing(read and write) position
    if (removeDuplicates === true) {
      return this.removeDuplicates(ret);
    } else {
      return ret;
    }
  }
 
  private removeDuplicates(arr: Identifier[]): Identifier[] {
    const values: any = {};
    return arr.filter(item => {
      const val = item.getStart().getCol() + "_" + item.getStart().getRow() + "_" + item.getFilename();
      const exists = values[val];
      values[val] = true;
      return !exists;
    });
  }
 
  private findReferences(node: ISpaghettiScopeNode, identifier: Identifier): Identifier[] {
    const ret: Identifier[] = [];
 
    if (node.getIdentifier().stype !== ScopeType.BuiltIn) {
      const upper = identifier.getName().toUpperCase();
 
      // this is for finding the definitions
      const vars = node.getData().vars;
      const vid = vars[upper];
      if (vid?.equals(identifier)) {
        ret.push(vid);
      }
 
      // this is for finding the definitions
      const types = node.getData().types;
      const tid = types[upper];
      if (tid?.equals(identifier)) {
        ret.push(tid);
      }
 
      for (const r of node.getData().references) {
        if (r.resolved?.equals(identifier)) {
          ret.push(r.position);
        }
      }
    }
 
    for (const c of node.getChildren()) {
      ret.push(...this.findReferences(c, identifier));
    }
 
    return ret;
  }
 
}