use of org.openehr.bmm.core.BmmClass in project archetype-languageserver by nedap.
the class AQLStorage method completion.
public Either<List<CompletionItem>, CompletionList> completion(CompletionParams position) {
String uri = position.getTextDocument().getUri();
AQLDocument aqlDocument = this.aqlDocumentsByUri.get(uri);
if (aqlDocument == null) {
return Either.forRight(new CompletionList());
}
List<CompletionItem> items = new ArrayList<>();
CompletionList result = new CompletionList(items);
MetaModels metaModels = BuiltinReferenceModels.getMetaModels();
for (ArchetypePathReference reference : aqlDocument.getArchetypePathReferences()) {
// copy the range, adding one because typing at the end of course
Range range = new Range();
range.setStart(new Position(reference.getRange().getStart().getLine(), reference.getRange().getStart().getCharacter()));
range.setEnd(new Position(reference.getRange().getEnd().getLine(), reference.getRange().getEnd().getCharacter() + 1));
if (reference.getArchetypeId() != null && Ranges.containsPosition(range, position.getPosition())) {
// TODO: get operational template here. I think that should be cached?
ValidationResult validationResult = archetypeRepository.getValidationResult(reference.getArchetypeId());
if (validationResult != null && validationResult.getFlattened() != null) {
// ok we have code completion!
// now to find the correct archetype path
Archetype flat = validationResult.getFlattened();
metaModels.selectModel(flat);
PartialAOMPathQuery partialAOMPathQuery = new PartialAOMPathQuery(reference.getPath());
// TODO: get path UP TO where we are typing!
PartialAOMPathQuery.PartialMatch partial = partialAOMPathQuery.findLSPPartial(flat.getDefinition());
// TODO: add BMM path traversal here as well, so you can do /value/magnitude
for (ArchetypeModelObject object : partial.getMatches()) {
if (object instanceof CObject) {
for (CAttribute attribute : ((CObject) object).getAttributes()) {
for (CObject child : attribute.getChildren()) {
if (child.getNodeId() != null && !"id9999".equalsIgnoreCase(child.getNodeId())) {
String text = child.getTerm() == null ? attribute.getRmAttributeName() + "[" + child.getNodeId() + "] (" + child.getRmTypeName() + ")" : child.getTerm().getText() + " (" + attribute.getRmAttributeName() + " " + child.getRmTypeName() + "[" + child.getNodeId() + "])";
CompletionItem completionItem = new CompletionItem(text);
completionItem.setInsertText(attribute.getRmAttributeName() + "[" + child.getNodeId() + "]");
completionItem.setFilterText(attribute.getRmAttributeName() + "[" + child.getNodeId() + "]" + text);
// the 0 is to sort this before others
completionItem.setSortText(attribute.getRmAttributeName() + "0" + text);
completionItem.setKind(CompletionItemKind.Reference);
items.add(completionItem);
}
}
}
BmmClass classDefinition = metaModels.getSelectedBmmModel().getClassDefinition(BmmDefinitions.typeNameToClassKey(((CObject) object).getRmTypeName()));
if (classDefinition != null) {
for (BmmProperty property : classDefinition.getFlatProperties().values()) {
CompletionItem completionItem = new CompletionItem(property.getName() + "(" + property.getType().toDisplayString() + ")");
completionItem.setInsertText(property.getName());
// sort this last please
completionItem.setSortText(property.getName() + "zzzz");
completionItem.setKind(CompletionItemKind.Reference);
items.add(completionItem);
}
}
} else if (object instanceof CAttribute) {
for (CObject child : ((CAttribute) object).getChildren()) {
if (child.getNodeId() != null && !child.getNodeId().equalsIgnoreCase("id9999")) {
CompletionItem completionItem = new CompletionItem();
completionItem.setLabel(child.getTerm() == null ? child.getRmTypeName() : child.getTerm().getText());
completionItem.setInsertText("[" + child.getNodeId() + "]");
completionItem.setKind(CompletionItemKind.Reference);
// completionItem.setInsertText();
items.add(completionItem);
}
}
}
}
}
}
}
return Either.forRight(result);
}
use of org.openehr.bmm.core.BmmClass in project archetype-languageserver by nedap.
the class ArchetypeHoverInfo method extractHoverInfo.
private void extractHoverInfo(DocumentInformation documentInformation, CAttribute attribute, Archetype archetypeForTerms) {
for (CObject object : attribute.getChildren()) {
try {
if (object instanceof CComplexObject) {
extractHoverInfo(documentInformation, (CComplexObject) object, archetypeForTerms);
} else if (object instanceof CTerminologyCode) {
extractHoverInfo(documentInformation, (CTerminologyCode) object, archetypeForTerms);
} else if (object instanceof ArchetypeSlot) {
getHoverInfoForCObject(documentInformation, object, archetypeForTerms);
}
// for the other primitives, hovers should not be important
} catch (Exception e) {
// If this fails, fine, continue with the rest of the file!
// TODO: report to client?
e.printStackTrace();
}
}
try {
CAttribute flatAttribute = archetypeForTerms.itemAtPath(attribute.getPath());
if (flatAttribute == null) {
flatAttribute = attribute;
}
Cardinality cardinality = null;
MultiplicityInterval existence = null;
// TODO: do a proper path lookup through the RM model?
CAttribute defaults = new BMMConstraintImposer(metaModels.getSelectedBmmModel()).getDefaultAttribute(flatAttribute.getParent().getRmTypeName(), flatAttribute.getRmAttributeName());
if (flatAttribute.getCardinality() != null) {
cardinality = flatAttribute.getCardinality();
} else {
if (defaults != null) {
cardinality = defaults.getCardinality();
}
}
if (flatAttribute.getExistence() != null) {
existence = flatAttribute.getExistence();
} else {
if (defaults != null) {
existence = flatAttribute.getExistence();
}
}
boolean multiple = flatAttribute.isMultiple();
StringBuilder content = new StringBuilder();
if (cardinality != null) {
content.append("Cardinality: ");
content.append(cardinality.toString());
}
if (existence != null) {
content.append(", existence: " + existence.toString());
}
content.append(multiple ? "\n multiple valued attribute" : "\n single valued attribute");
BmmClass classDefinition = metaModels.getSelectedBmmModel().getClassDefinition(flatAttribute.getParent().getRmTypeName());
if (classDefinition != null) {
BmmProperty bmmProperty = classDefinition.getFlatProperties().get(flatAttribute.getRmAttributeName());
if (bmmProperty != null) {
content.append("\n\nRM type name: *" + bmmProperty.getType().toDisplayString() + "*");
}
}
content.append("\n\n path: " + attribute.getPath());
Hover hover = new Hover();
hover.setContents(new MarkupContent(MARKDOWN, content.toString()));
Range range = getHoverRange(documentInformation, attribute);
if (range != null) {
hoverRanges.addRange(range, hover);
}
} catch (Exception e) {
// TODO: report to client?
e.printStackTrace();
}
}
use of org.openehr.bmm.core.BmmClass in project archetype-languageserver by nedap.
the class AQLStorage method extractHoverInfo.
private void extractHoverInfo(AQLDocument document) {
HoverInfo hoverInfo = new HoverInfo("aql");
MetaModels metaModels = BuiltinReferenceModels.getMetaModels();
for (ArchetypePathReference reference : document.getArchetypePathReferences()) {
if (reference.getArchetypeId() == null) {
continue;
}
try {
// TODO: get operational template here. I think that should be cached?
ValidationResult validationResult = archetypeRepository.getValidationResult(reference.getArchetypeId());
if (validationResult != null) {
Archetype flattened = validationResult.getFlattened();
metaModels.selectModel(flattened);
if (flattened != null) {
PartialAOMPathQuery aomPathQuery = new PartialAOMPathQuery(reference.getPath());
PartialAOMPathQuery.PartialMatch partial = aomPathQuery.findLSPPartial(flattened.getDefinition());
if (partial.getMatches().size() > 0) {
ArchetypeModelObject archetypeModelObject = partial.getMatches().get(0);
String content = null;
String description = null;
String typeName = "";
if (archetypeModelObject instanceof CAttribute) {
CAttribute attribute = (CAttribute) archetypeModelObject;
content = findNearestText((CAttribute) archetypeModelObject);
description = findNearestDescription((CAttribute) archetypeModelObject);
CObject parent = attribute.getParent();
if (partial.getRemainingQuery().isEmpty()) {
// TODO: proper path lookup here
BmmClass classDefinition = metaModels.getSelectedBmmModel().getClassDefinition(BmmDefinitions.typeNameToClassKey(parent.getRmTypeName()));
if (classDefinition != null) {
BmmProperty bmmProperty = classDefinition.getFlatProperties().get(attribute.getRmAttributeName());
if (bmmProperty != null) {
bmmProperty.getType().toDisplayString();
}
}
}
} else if (archetypeModelObject instanceof CObject) {
content = findNearestText((CObject) archetypeModelObject);
description = findNearestDescription((CObject) archetypeModelObject);
if (partial.getRemainingQuery().isEmpty()) {
// TODO: proper path lookup here.
typeName = ((CObject) archetypeModelObject).getRmTypeName();
}
}
String text = content + partial.getRemainingQuery().stream().map(PathSegment::toString).collect(Collectors.joining("/"));
text += "\n\n" + typeName;
text += "\n\n" + description;
text += "\n\nIn Archetype " + flattened.getDefinition().getTerm().getText() + " (" + reference.getArchetypeId() + ")";
hoverInfo.getHoverRanges().addRange(reference.getRange(), new Hover(new MarkupContent(HoverInfo.MARKDOWN, text)));
} else {
hoverInfo.getHoverRanges().addRange(reference.getRange(), new Hover(new MarkupContent(HoverInfo.MARKDOWN, "path " + reference.getPath() + " not found")));
}
} else {
hoverInfo.getHoverRanges().addRange(reference.getRange(), new Hover(new MarkupContent(HoverInfo.MARKDOWN, "flattened archetype not found")));
}
} else {
hoverInfo.getHoverRanges().addRange(reference.getRange(), new Hover(new MarkupContent(HoverInfo.MARKDOWN, "archetype not found")));
}
} catch (Exception e) {
// ok... report as diagnostics or log
e.printStackTrace();
}
}
document.setHoverInfo(hoverInfo);
}
Aggregations