ui5plugin-parser
Version:
74 lines (73 loc) • 4.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReferenceFinder = void 0;
/* eslint-disable @typescript-eslint/indent */
const ParserPool_1 = require("../../../../parser/pool/ParserPool");
const FieldsAndMethodForPositionBeforeCurrentStrategy_1 = require("../../jsparser/typesearch/FieldsAndMethodForPositionBeforeCurrentStrategy");
const CustomJSClass_1 = require("../../ui5class/js/CustomJSClass");
const RangeAdapter_1 = require("../range/adapters/RangeAdapter");
const ReferenceFinderBase_1 = require("./ReferenceFinderBase");
class ReferenceFinder extends ReferenceFinderBase_1.default {
getReferenceLocations(member) {
const locations = [];
const UIClasses = ParserPool_1.default.getAllCustomUIClasses().filter(UIClass => UIClass instanceof CustomJSClass_1.CustomJSClass);
UIClasses.forEach(UIClass => {
this._addLocationsFromUIClass(member, UIClass, locations);
});
const UIClass = this._parser.classFactory.getUIClass(member.owner);
if (UIClass instanceof CustomJSClass_1.CustomJSClass) {
const viewsAndFragments = this._parser.classFactory.getViewsAndFragmentsOfControlHierarchically(UIClass, [], true, true, true);
const viewAndFragmentArray = [...viewsAndFragments.fragments, ...viewsAndFragments.views];
viewAndFragmentArray.forEach(XMLDoc => {
this._addLocationsFromXMLDocument(XMLDoc, member, locations);
});
}
return locations;
}
_addLocationsFromUIClass(member, UIClass, locations) {
const cache = UIClass.getCache("referenceCodeLensCache") || {};
if (cache[member.owner]?.[`_${member.name}`]) {
locations.push(...cache[member.owner][`_${member.name}`]);
}
else if (UIClass.fsPath) {
const results = this._getCurrentMethodMentioning(member, UIClass);
const currentLocations = [];
const strategy = new FieldsAndMethodForPositionBeforeCurrentStrategy_1.FieldsAndMethodForPositionBeforeCurrentStrategy(this._parser.syntaxAnalyser, this._parser);
results.forEach(result => {
const calleeClassName = strategy.acornGetClassName(UIClass.className, result.index);
const calleeUIClass = calleeClassName && this._parser.classFactory.getUIClass(calleeClassName);
if (calleeUIClass &&
calleeUIClass instanceof CustomJSClass_1.CustomJSClass &&
calleeClassName &&
(this._parser.classFactory.isClassAChildOfClassB(calleeClassName, member.owner) ||
(UIClass.className === calleeClassName &&
this._parser.classFactory.isClassAChildOfClassB(member.owner, calleeClassName) &&
this._parser.classFactory.isClassAChildOfClassB(member.owner, UIClass.className)))) {
const range = RangeAdapter_1.RangeAdapter.offsetsRange(UIClass.classText, result.index, result.index + member.name.length);
if (range) {
currentLocations.push({ filePath: UIClass.fsPath || "", range: range });
}
}
});
if (currentLocations.length > 0) {
locations.push(...currentLocations);
}
if (!cache[member.owner]) {
cache[member.owner] = {};
}
cache[member.owner][`_${member.name}`] = currentLocations;
UIClass.setCache("referenceCodeLensCache", cache);
}
}
_getCurrentMethodMentioning(member, UIClass) {
const regexp = new RegExp(`(?<=\\.)${member.name}(\\}|\\(|\\)|\\,|\\.|\\s|;|\\[|\\])(?!=)`, "g");
const results = [];
let result = regexp.exec(UIClass.classText);
while (result) {
results.push(result);
result = regexp.exec(UIClass.classText);
}
return results;
}
}
exports.ReferenceFinder = ReferenceFinder;