All files / src/lsp references.ts

91.45% Statements 107/117
78.38% Branches 29/37
100% Functions 5/5
91.45% Lines 107/117

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 1171x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 275x 275x 1x 1x 9x 9x     9x 9x     9x 9x 9x     9x 9x 9x     9x 9x 9x 9x 1x 1x 275x 275x 275x 275x 275x 275x 34x 34x 275x 18x 18x 257x 275x 308x 295x     295x 295x 308x 257x 257x 275x 239x 275x 18x 18x 275x 1x 1x 1x 1x 239x 239x 475x 475x 475x 475x 239x 239x 1x 1x 1163x 1163x 1163x 870x 870x 870x 870x 355x 355x 870x 870x 870x 870x 870x 1x 1x 870x 870x 844x 212x 212x 844x 870x 1163x 1163x 834x 834x 1163x 1163x 1163x 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);
  }
 
  public search(identifier: Identifier, node: ISpaghettiScopeNode, exitAfterFound = false, removeDuplicates = true): Identifier[] {
    let ret: Identifier[] = [];
 
    // todo, this first assumes that the identifier is a variable?
    if (node.getIdentifier().stype === ScopeType.Method
        || node.getIdentifier().stype === ScopeType.FunctionModule
        || node.getIdentifier().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) {
      // this is for finding the definitions
      const vars = node.getData().vars;
      const vid = vars[identifier.getName().toUpperCase()];
      if (vid?.equals(identifier)) {
        ret.push(vid);
      }
 
      // this is for finding the definitions
      const types = node.getData().types;
      const tid = types[identifier.getName().toUpperCase()];
      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;
  }
 
}