-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: hover support for proeprty binding (#645)
* feat: hover support for proeprty binding * fix: add seprator in doc and remove visibility * fix: tooltip title
- Loading branch information
1 parent
746c257
commit de2cccc
Showing
12 changed files
with
476 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@ui5-language-assistant/vscode-ui5-language-assistant-bas-ext": patch | ||
"vscode-ui5-language-assistant": patch | ||
"@ui5-language-assistant/language-server": patch | ||
"@ui5-language-assistant/binding": patch | ||
--- | ||
|
||
add hover support for proeprty binding |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { XMLAttribute } from "@xml-tools/ast"; | ||
import { getUI5PropertyByXMLAttributeKey } from "@ui5-language-assistant/logic-utils"; | ||
import { getLogger } from "../../utils"; | ||
import { Hover } from "vscode-languageserver"; | ||
import { | ||
parseBinding, | ||
isBindingAllowed, | ||
isBindingExpression, | ||
extractBindingSyntax, | ||
rangeContained, | ||
BindingParserTypes, | ||
} from "@ui5-language-assistant/binding-parser"; | ||
import { Position } from "vscode-languageserver-types"; | ||
import { BindContext } from "../../types"; | ||
import { PROPERTY_BINDING_INFO } from "../../constant"; | ||
import { getDocumentation } from "../../utils"; | ||
import { UI5Typedef } from "@ui5-language-assistant/semantic-model-types"; | ||
|
||
const getHoverFromBinding = ( | ||
context: BindContext, | ||
propertyBinding: UI5Typedef, | ||
binding: BindingParserTypes.StructureValue | ||
): Hover | undefined => { | ||
let hover: Hover | undefined; | ||
const cursorPos = context.textDocumentPosition?.position; | ||
for (const element of binding.elements) { | ||
if ( | ||
cursorPos && | ||
element.range && | ||
rangeContained(element.range, { | ||
start: cursorPos, | ||
end: cursorPos, | ||
}) | ||
) { | ||
// check if cursor is on key range | ||
if ( | ||
cursorPos && | ||
element.key && | ||
rangeContained(element.key.range, { | ||
start: cursorPos, | ||
end: cursorPos, | ||
}) | ||
) { | ||
// check valid key | ||
const property = propertyBinding.properties.find( | ||
(prop) => prop.name === element.key?.originalText | ||
); | ||
if (property) { | ||
return { contents: getDocumentation(context, property, true) }; | ||
} | ||
} | ||
|
||
// check collection value as they may have property binding | ||
if (element.value?.type === "collection-value") { | ||
for (const collectionEl of element.value.elements) { | ||
if (collectionEl.type !== "structure-value") { | ||
continue; | ||
} | ||
hover = getHoverFromBinding(context, propertyBinding, collectionEl); | ||
if (hover) { | ||
return hover; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return hover; | ||
}; | ||
|
||
export const getHover = ( | ||
context: BindContext, | ||
attribute: XMLAttribute | ||
): Hover | undefined => { | ||
try { | ||
const propBinding = context.ui5Model.typedefs[PROPERTY_BINDING_INFO]; | ||
if (!propBinding) { | ||
return; | ||
} | ||
const ui5Property = getUI5PropertyByXMLAttributeKey( | ||
attribute, | ||
context.ui5Model | ||
); | ||
if (!ui5Property) { | ||
return; | ||
} | ||
const value = attribute.syntax.value; | ||
const text = attribute.value ?? ""; | ||
if (text.trim().length === 0) { | ||
return; | ||
} | ||
|
||
const extractedText = extractBindingSyntax(text); | ||
for (const bindingSyntax of extractedText) { | ||
const { expression, startIndex } = bindingSyntax; | ||
if (isBindingExpression(expression)) { | ||
continue; | ||
} | ||
/* istanbul ignore next */ | ||
const position: Position = { | ||
character: (value?.startColumn ?? 0) + startIndex, | ||
line: value?.startLine ? value.startLine - 1 : 0, // zero based index | ||
}; | ||
const { ast, errors } = parseBinding(expression, position); | ||
const cursorPos = context.textDocumentPosition?.position; | ||
const binding = ast.bindings.find( | ||
(b) => | ||
cursorPos && | ||
b.range && | ||
rangeContained(b.range, { start: cursorPos, end: cursorPos }) | ||
); | ||
if (!binding) { | ||
continue; | ||
} | ||
if (!isBindingAllowed(text, binding, errors)) { | ||
continue; | ||
} | ||
return getHoverFromBinding(context, propBinding, binding); | ||
} | ||
return; | ||
} catch (error) { | ||
getLogger().debug("validatePropertyBindingInfo failed:", error); | ||
return; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.