|
| 1 | +import { XMLAttribute } from "@xml-tools/ast"; |
| 2 | +import { getUI5PropertyByXMLAttributeKey } from "@ui5-language-assistant/logic-utils"; |
| 3 | +import { getLogger } from "../../utils"; |
| 4 | +import { Hover } from "vscode-languageserver"; |
| 5 | +import { |
| 6 | + parseBinding, |
| 7 | + isBindingAllowed, |
| 8 | + isBindingExpression, |
| 9 | + extractBindingSyntax, |
| 10 | + rangeContained, |
| 11 | + BindingParserTypes, |
| 12 | +} from "@ui5-language-assistant/binding-parser"; |
| 13 | +import { Position } from "vscode-languageserver-types"; |
| 14 | +import { BindContext } from "../../types"; |
| 15 | +import { PROPERTY_BINDING_INFO } from "../../constant"; |
| 16 | +import { getDocumentation } from "../../utils"; |
| 17 | +import { UI5Typedef } from "@ui5-language-assistant/semantic-model-types"; |
| 18 | + |
| 19 | +const getHoverFromBinding = ( |
| 20 | + context: BindContext, |
| 21 | + propertyBinding: UI5Typedef, |
| 22 | + binding: BindingParserTypes.StructureValue |
| 23 | +): Hover | undefined => { |
| 24 | + let hover: Hover | undefined; |
| 25 | + const cursorPos = context.textDocumentPosition?.position; |
| 26 | + for (const element of binding.elements) { |
| 27 | + if ( |
| 28 | + cursorPos && |
| 29 | + element.range && |
| 30 | + rangeContained(element.range, { |
| 31 | + start: cursorPos, |
| 32 | + end: cursorPos, |
| 33 | + }) |
| 34 | + ) { |
| 35 | + // check if cursor is on key range |
| 36 | + if ( |
| 37 | + cursorPos && |
| 38 | + element.key && |
| 39 | + rangeContained(element.key.range, { |
| 40 | + start: cursorPos, |
| 41 | + end: cursorPos, |
| 42 | + }) |
| 43 | + ) { |
| 44 | + // check valid key |
| 45 | + const property = propertyBinding.properties.find( |
| 46 | + (prop) => prop.name === element.key?.originalText |
| 47 | + ); |
| 48 | + if (property) { |
| 49 | + return { contents: getDocumentation(context, property, true) }; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + // check collection value as they may have property binding |
| 54 | + if (element.value?.type === "collection-value") { |
| 55 | + for (const collectionEl of element.value.elements) { |
| 56 | + if (collectionEl.type !== "structure-value") { |
| 57 | + continue; |
| 58 | + } |
| 59 | + hover = getHoverFromBinding(context, propertyBinding, collectionEl); |
| 60 | + if (hover) { |
| 61 | + return hover; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + return hover; |
| 68 | +}; |
| 69 | + |
| 70 | +export const getHover = ( |
| 71 | + context: BindContext, |
| 72 | + attribute: XMLAttribute |
| 73 | +): Hover | undefined => { |
| 74 | + try { |
| 75 | + const propBinding = context.ui5Model.typedefs[PROPERTY_BINDING_INFO]; |
| 76 | + if (!propBinding) { |
| 77 | + return; |
| 78 | + } |
| 79 | + const ui5Property = getUI5PropertyByXMLAttributeKey( |
| 80 | + attribute, |
| 81 | + context.ui5Model |
| 82 | + ); |
| 83 | + if (!ui5Property) { |
| 84 | + return; |
| 85 | + } |
| 86 | + const value = attribute.syntax.value; |
| 87 | + const text = attribute.value ?? ""; |
| 88 | + if (text.trim().length === 0) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + const extractedText = extractBindingSyntax(text); |
| 93 | + for (const bindingSyntax of extractedText) { |
| 94 | + const { expression, startIndex } = bindingSyntax; |
| 95 | + if (isBindingExpression(expression)) { |
| 96 | + continue; |
| 97 | + } |
| 98 | + /* istanbul ignore next */ |
| 99 | + const position: Position = { |
| 100 | + character: (value?.startColumn ?? 0) + startIndex, |
| 101 | + line: value?.startLine ? value.startLine - 1 : 0, // zero based index |
| 102 | + }; |
| 103 | + const { ast, errors } = parseBinding(expression, position); |
| 104 | + const cursorPos = context.textDocumentPosition?.position; |
| 105 | + const binding = ast.bindings.find( |
| 106 | + (b) => |
| 107 | + cursorPos && |
| 108 | + b.range && |
| 109 | + rangeContained(b.range, { start: cursorPos, end: cursorPos }) |
| 110 | + ); |
| 111 | + if (!binding) { |
| 112 | + continue; |
| 113 | + } |
| 114 | + if (!isBindingAllowed(text, binding, errors)) { |
| 115 | + continue; |
| 116 | + } |
| 117 | + return getHoverFromBinding(context, propBinding, binding); |
| 118 | + } |
| 119 | + return; |
| 120 | + } catch (error) { |
| 121 | + getLogger().debug("validatePropertyBindingInfo failed:", error); |
| 122 | + return; |
| 123 | + } |
| 124 | +}; |
0 commit comments