@abaplint/core
Version:
abaplint - Core API
100 lines • 4.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.References = void 0;
const _abap_object_1 = require("../objects/_abap_object");
const _lsp_utils_1 = require("./_lsp_utils");
const syntax_1 = require("../abap/5_syntax/syntax");
const _lookup_1 = require("./_lookup");
const _scope_type_1 = require("../abap/5_syntax/_scope_type");
class References {
constructor(reg) {
this.reg = reg;
}
references(pos) {
const file = _lsp_utils_1.LSPUtils.getABAPFile(this.reg, pos.textDocument.uri);
if (file === undefined) {
return [];
}
const obj = this.reg.getObject(file.getObjectType(), file.getObjectName());
if (!(obj instanceof _abap_object_1.ABAPObject)) {
return [];
}
const found = _lsp_utils_1.LSPUtils.findCursor(this.reg, pos);
if ((found === null || found === void 0 ? void 0 : found.identifier) === undefined) {
return [];
}
const lookup = _lookup_1.LSPLookup.lookup(found, this.reg, obj);
if ((lookup === null || lookup === void 0 ? void 0 : lookup.definitionId) === undefined || (lookup === null || lookup === void 0 ? void 0 : lookup.scope) === undefined) {
return [];
}
const locs = this.search(lookup.definitionId, lookup.scope);
return locs.map(_lsp_utils_1.LSPUtils.identiferToLocation);
}
////////////////////////////////////////////
// todo, cleanup this mehtod, some of the method parameters are not used anymore?
search(identifier, node, exitAfterFound = false, removeDuplicates = true) {
let ret = [];
// todo, this first assumes that the identifier is a variable?
const stype = node.getIdentifier().stype;
if (stype === _scope_type_1.ScopeType.Method || stype === _scope_type_1.ScopeType.FunctionModule || stype === _scope_type_1.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 _abap_object_1.ABAPObject) {
if (this.reg.isDependency(o)) {
continue; // do not search in dependencies
}
ret.push(...this.findReferences(new syntax_1.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;
}
}
removeDuplicates(arr) {
const values = {};
return arr.filter(item => {
const val = item.getStart().getCol() + "_" + item.getStart().getRow() + "_" + item.getFilename();
const exists = values[val];
values[val] = true;
return !exists;
});
}
findReferences(node, identifier) {
var _a;
const ret = [];
if (node.getIdentifier().stype !== _scope_type_1.ScopeType.BuiltIn) {
const upper = identifier.getName().toUpperCase();
// this is for finding the definitions
const vars = node.getData().vars;
const vid = vars[upper];
if (vid === null || vid === void 0 ? void 0 : vid.equals(identifier)) {
ret.push(vid);
}
// this is for finding the definitions
const types = node.getData().types;
const tid = types[upper];
if (tid === null || tid === void 0 ? void 0 : tid.equals(identifier)) {
ret.push(tid);
}
for (const r of node.getData().references) {
if ((_a = r.resolved) === null || _a === void 0 ? void 0 : _a.equals(identifier)) {
ret.push(r.position);
}
}
}
for (const c of node.getChildren()) {
ret.push(...this.findReferences(c, identifier));
}
return ret;
}
}
exports.References = References;
//# sourceMappingURL=references.js.map