use of com.nedap.archie.paths.PathSegment in project archetype-languageserver by nedap.
the class PartialAOMPathQuery method findLSPPartial.
// TODO: check if/how this is different from AOMPathQuery.findPartial, and potentially remove
public PartialMatch findLSPPartial(CComplexObject root) {
List<ArchetypeModelObject> result = new ArrayList<>();
boolean findThroughDifferentialPaths = true, matchSpecializedNodes = true;
result.add(root);
List<PathSegment> pathSegments = getPathSegments();
for (int i = 0; i < pathSegments.size(); i++) {
List<ArchetypeModelObject> previousResult = result;
PathSegment segment = pathSegments.get(i);
CAttribute differentialAttribute = null;
if (findThroughDifferentialPaths) {
differentialAttribute = findMatchingDifferentialPath(pathSegments.subList(i, pathSegments.size()), result);
}
if (differentialAttribute != null) {
// skip a few pathsegments for this differential path match
i = i + new APathQuery(differentialAttribute.getDifferentialPath()).getPathSegments().size() - 1;
PathSegment lastPathSegment = pathSegments.get(i);
ArchetypeModelObject oneMatchingObject = findOneMatchingObject(differentialAttribute, lastPathSegment, matchSpecializedNodes);
if (oneMatchingObject != null) {
result = Lists.newArrayList(oneMatchingObject);
} else {
result = findOneSegment(segment, result, matchSpecializedNodes);
}
} else {
result = findOneSegment(segment, result, matchSpecializedNodes);
}
List<ArchetypeModelObject> returnValue = previousResult.stream().filter((object) -> object != null).collect(Collectors.toList());
if (result.isEmpty()) {
// we have to return our partial match here
return new PartialMatch(pathSegments, pathSegments.subList(i, pathSegments.size()), returnValue);
}
}
List<ArchetypeModelObject> returnValue = result.stream().filter((object) -> object != null).collect(Collectors.toList());
return new PartialMatch(pathSegments, new ArrayList<>(), returnValue);
}
use of com.nedap.archie.paths.PathSegment in project archetype-languageserver by nedap.
the class DocumentInformation method checkDifferentialMatch.
private boolean checkDifferentialMatch(List<PathSegment> pathSegments, List<PathSegment> differentialPathSegments) {
if (differentialPathSegments.size() <= pathSegments.size()) {
for (int i = 0; i < differentialPathSegments.size(); i++) {
PathSegment segment = pathSegments.get(i);
PathSegment differentialPathSegment = differentialPathSegments.get(i);
if (!matches(segment, differentialPathSegment)) {
return false;
}
}
return true;
}
return false;
}
use of com.nedap.archie.paths.PathSegment 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);
}
use of com.nedap.archie.paths.PathSegment in project archetype-languageserver by nedap.
the class AQLStorage method getCodeLens.
public List<? extends CodeLens> getCodeLens(CodeLensParams params) {
AQLDocument document = this.aqlDocumentsByUri.get(params.getTextDocument().getUri());
if (document == null) {
return new ArrayList<>();
}
List<CodeLens> result = new ArrayList<>();
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();
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;
if (archetypeModelObject instanceof CAttribute) {
content = findNearestText((CAttribute) archetypeModelObject);
description = findNearestDescription((CAttribute) archetypeModelObject);
} else if (archetypeModelObject instanceof CObject) {
content = findNearestText((CObject) archetypeModelObject);
description = findNearestDescription((CObject) archetypeModelObject);
}
String text = content + partial.getRemainingQuery().stream().map(PathSegment::toString).collect(Collectors.joining("/"));
String extraText = description;
extraText += "\n\nIn Archetype " + flattened.getDefinition().getTerm().getText() + " (" + reference.getArchetypeId() + ")";
// result.add(new CodeLens(reference.getRange(), new Command(text, ADL2TextDocumentService.SHOW_INFO_COMMAND, Lists.newArrayList(extraText)), null));
result.add(new CodeLens(reference.getRange(), new Command(text, ADL2TextDocumentService.SHOW_INFO_COMMAND, Lists.newArrayList(extraText)), null));
} 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();
}
}
return result;
}
use of com.nedap.archie.paths.PathSegment in project archetype-languageserver by nedap.
the class DocumentInformation method lookupCObjectOrAttribute.
/**
* Archetype path lookup. Definition only for now. Sorry for the ugly code, but this works rather well :)
* @param path
* @return
*/
public DocumentSymbol lookupCObjectOrAttribute(String path, boolean returnResultSoFar) {
APathQuery query = new APathQuery(path);
List<PathSegment> pathSegments = query.getPathSegments();
List<DocumentSymbol> documentSymbols = DocumentSymbolUtils.getDocumentSymbols(symbols);
if (documentSymbols.isEmpty()) {
return null;
}
DocumentSymbol definitionSections = DocumentSymbolUtils.getDocumentSymbolOrThrow(documentSymbols.get(0).getChildren(), DEFINITION_SECTION_NAME);
DocumentSymbol rootNode = definitionSections.getChildren().get(0);
DocumentSymbol currentSymbol = rootNode;
for (int i = 0; i < pathSegments.size(); i++) {
PathSegment segment = pathSegments.get(i);
// search attribute
if (currentSymbol.getChildren() == null || currentSymbol.getChildren().isEmpty()) {
return currentSymbol;
}
{
DocumentSymbol foundAttribute = findAttribute(currentSymbol, segment, pathSegments, i);
if (foundAttribute == null) {
return returnResultSoFar ? currentSymbol : null;
} else if (foundAttribute.getName().startsWith("/")) {
// differential path. Skip a couple of path segments
int numberOfSegments = new APathQuery(foundAttribute.getName()).getPathSegments().size();
i = i + numberOfSegments - 1;
segment = pathSegments.get(i);
}
currentSymbol = foundAttribute;
}
if (currentSymbol.getChildren() == null || currentSymbol.getChildren().isEmpty()) {
return returnCurrentSymbolIfPossible(returnResultSoFar, pathSegments, currentSymbol, i);
}
if (segment.getIndex() != null) {
if (segment.getIndex() > currentSymbol.getChildren().size()) {
return returnResultSoFar ? currentSymbol : null;
}
currentSymbol = currentSymbol.getChildren().get(segment.getIndex() - 1);
} else if (segment.hasIdCode() || segment.hasArchetypeRef()) {
DocumentSymbol foundCObject = findCObject(currentSymbol, segment);
if (foundCObject == null) {
return returnCurrentSymbolIfPossible(returnResultSoFar, pathSegments, currentSymbol, i);
}
currentSymbol = foundCObject;
} else {
if (i == pathSegments.size() - 1) {
// this just points at an attribute, that's ok.
return returnCurrentSymbolIfPossible(returnResultSoFar, pathSegments, currentSymbol, i);
} else // let's hope it's size 1
if (currentSymbol.getChildren() == null || currentSymbol.getChildren().isEmpty()) {
return returnCurrentSymbolIfPossible(returnResultSoFar, pathSegments, currentSymbol, i);
} else if (currentSymbol.getChildren().size() == 1) {
currentSymbol = currentSymbol.getChildren().get(0);
} else {
throw new RuntimeException("cannot find path, it has too many possible values without an id at segment " + segment);
}
}
}
return currentSymbol;
}
Aggregations