All files / src/lsp references.ts

91.07% Statements 51/56
86.44% Branches 51/59
100% Functions 6/6
91.07% Lines 51/56

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    1x   1x   1x   1x 1x   1x       249x       9x 9x     9x 9x       9x 9x       9x 9x       9x 9x       249x     249x     31x   249x 18x     231x 276x 269x     269x         231x 218x   13x             218x 218x 439x 439x 439x 439x         1068x   1068x   801x 801x 801x 326x       801x 801x 801x 2x     801x 730x 188x         1068x 768x     1068x      
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);
    Iif (file === undefined) {
      return [];
    }
    const obj = this.reg.getObject(file.getObjectType(), file.getObjectName());
    Iif (!(obj instanceof ABAPObject)) {
      return [];
    }
 
    const found = LSPUtils.findCursor(this.reg, pos);
    Iif (found?.identifier === undefined) {
      return [];
    }
 
    const lookup = LSPLookup.lookup(found, this.reg, obj);
    Iif (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) {
        Iif (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;
  }
 
}