@ui5-language-assistant/xml-views-tooltip
Version:
XML Views Tooltip
79 lines • 3.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.findUI5HoverNodeAtOffset = void 0;
const lodash_1 = require("lodash");
const assert_never_1 = require("assert-never");
const common_1 = require("@xml-tools/common");
const logic_utils_1 = require("@ui5-language-assistant/logic-utils");
const semantic_model_1 = require("@ui5-language-assistant/semantic-model");
function findUI5HoverNodeAtOffset(astPosition, model) {
switch (astPosition.kind) {
case "XMLElementOpenName":
return findUI5NodeByElement(astPosition.astNode, model, true);
case "XMLElementCloseName":
return findUI5NodeByElement(astPosition.astNode, model, false);
case "XMLAttributeKey":
return logic_utils_1.getUI5NodeByXMLAttribute(astPosition.astNode, model);
case "XMLAttributeValue":
return findUI5NodeByXMLAttributeValue(astPosition.astNode, model);
/* istanbul ignore next - defensive programming */
default:
assert_never_1.assertNever(astPosition);
}
}
exports.findUI5HoverNodeAtOffset = findUI5HoverNodeAtOffset;
function findUI5NodeByElement(astNode, model, isOpenName) {
var _a, _b;
const ui5Class = isOpenName
? logic_utils_1.getUI5ClassByXMLElement(astNode, model)
: logic_utils_1.getUI5ClassByXMLElementClosingTag(astNode, model);
if (astNode.parent.type === "XMLDocument" || ui5Class !== undefined) {
return ui5Class;
}
const parentElementClass = logic_utils_1.getUI5ClassByXMLElement(astNode.parent, model);
if (parentElementClass === undefined) {
return undefined;
}
// openName or closeName cannot be undefined here because otherwise the ast position visitor wouldn't return their types
const tagQName = isOpenName
? /* istanbul ignore next */ (_a = astNode.syntax.openName) === null || _a === void 0 ? void 0 : _a.image : /* istanbul ignore next */ (_b = astNode.syntax.closeName) === null || _b === void 0 ? void 0 : _b.image;
/* istanbul ignore if */
if (tagQName === undefined) {
return undefined;
}
// Aggregations must be in the same namespace as their parent
// https://ui5.sap.com/#/topic/19eabf5b13214f27b929b9473df3195b
const { prefix, localName } = logic_utils_1.splitQNameByNamespace(tagQName);
if (!logic_utils_1.isSameXMLNSFromPrefix(prefix, astNode, astNode.parent.ns, astNode.parent)) {
return undefined;
}
return findAggragationByName(parentElementClass, localName);
}
function findAggragationByName(ui5Class, targetName) {
const allAggregations = logic_utils_1.flattenAggregations(ui5Class);
const ui5Aggregation = lodash_1.find(allAggregations, (aggregation) => aggregation.name === targetName);
return ui5Aggregation;
}
function findUI5NodeByXMLAttributeValue(attribute, model) {
if (attribute.key !== null &&
attribute.value !== null &&
common_1.isXMLNamespaceKey({ key: attribute.key, includeEmptyPrefix: true })) {
const ui5Node = semantic_model_1.findSymbol(model, attribute.value);
return ui5Node;
}
return getUI5EnumByElement(attribute, model);
}
function getUI5EnumByElement(attribute, model) {
const enumProp = getEnumProperty(attribute, model);
if (enumProp !== undefined) {
const enumValue = lodash_1.find(enumProp.fields, ["name", attribute.value]);
return enumValue;
}
return undefined;
}
function getEnumProperty(attribute, model) {
var _a;
const ui5Prop = logic_utils_1.getUI5PropertyByXMLAttributeKey(attribute, model);
return ((_a = ui5Prop === null || ui5Prop === void 0 ? void 0 : ui5Prop.type) === null || _a === void 0 ? void 0 : _a.kind) === "UI5Enum" ? ui5Prop.type : undefined;
}
//# sourceMappingURL=tooltip.js.map