Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZEN-4367, ZEN-3845 - Resource templates don't populate schema reference correctly #473

Merged
merged 8 commits into from
Sep 18, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public abstract class JsonContentAssistProcessor extends TemplateCompletionProce
private final JsonProposalProvider proposalProvider;
private final JsonReferenceProposalProvider referenceProposalProvider;
private final ContentAssistant contentAssistant;

/**
* The pointer that helps us locate the current position of the cursor inside the document.
*
Expand Down Expand Up @@ -122,7 +122,7 @@ public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int
column = selection.getOffset() - lineOffset;
} catch (BadLocationException e) {
}

final String prefix = extractPrefix(viewer, documentOffset);
// we have to remove the length of
// the prefix to obtain the correct
Expand All @@ -135,16 +135,16 @@ public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int
currentPath = model.getPath(line, column);
isRefCompletion = referenceProposalProvider.canProvideProposal(model, currentPath);

Collection<Proposal> p;
Collection<ProposalDescriptor> kaizenProposals;
if (isRefCompletion) {
updateStatus(model);
p = referenceProposalProvider.getProposals(currentPath, document, currentScope);
kaizenProposals = referenceProposalProvider.getProposals(currentPath, document, currentScope);
} else {
clearStatus();
p = proposalProvider.getProposals(currentPath, model, prefix);
kaizenProposals = proposalProvider.getProposals(currentPath, model, prefix);
}

final Collection<ICompletionProposal> proposals = getCompletionProposals(p, prefix, documentOffset);
final Collection<ICompletionProposal> proposals = getCompletionProposals(kaizenProposals, prefix, documentOffset, selection.getText());
// compute template proposals
if (!isRefCompletion) {
final ICompletionProposal[] templateProposals = super.computeCompletionProposals(viewer, documentOffset);
Expand Down Expand Up @@ -192,14 +192,16 @@ protected String[] initTextMessages(Model model) {
String.format(Messages.content_assist_proposal_local, bindingKey, context) };
}

protected Collection<ICompletionProposal> getCompletionProposals(Collection<Proposal> proposals, String prefix,
int offset) {
protected Collection<ICompletionProposal> getCompletionProposals(Collection<ProposalDescriptor> proposals,
String prefix, int offset, String selection) {
final List<ICompletionProposal> result = new ArrayList<>();

prefix = StringUtils.emptyToNull(prefix);

for (Proposal proposal : proposals) {
StyledCompletionProposal styledProposal = proposal.asStyledCompletionProposal(prefix, offset);
for (ProposalDescriptor proposal : proposals) {
int selectionLength = selection == null ? 0 : selection.length();
StyledCompletionProposal styledProposal = StyledCompletionProposal.create(proposal, prefix, offset,
selectionLength);
if (styledProposal != null) {
result.add(styledProposal);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public JsonProposalProvider(ContentAssistExt... extensions) {
* @param prefix
* @return proposals
*/
public Collection<Proposal> getProposals(JsonPointer pointer, Model model, String prefix) {
public Collection<ProposalDescriptor> getProposals(JsonPointer pointer, Model model, String prefix) {
final AbstractNode node = model.find(pointer);
if (node == null) {
return Collections.emptyList();
Expand All @@ -73,7 +73,7 @@ public Collection<Proposal> getProposals(JsonPointer pointer, Model model, Strin
* @param model
* @return proposals
*/
public Collection<Proposal> getProposals(JsonPointer pointer, Model model) {
public Collection<ProposalDescriptor> getProposals(JsonPointer pointer, Model model) {
return getProposals(pointer, model, null);
}

Expand All @@ -83,11 +83,11 @@ public Collection<Proposal> getProposals(JsonPointer pointer, Model model) {
* @param node
* @return proposals
*/
public Collection<Proposal> getProposals(AbstractNode node) {
public Collection<ProposalDescriptor> getProposals(AbstractNode node) {
return getProposals(node.getType(), node, null);
}

protected Collection<Proposal> getProposals(TypeDefinition type, AbstractNode node, String prefix) {
protected Collection<ProposalDescriptor> getProposals(TypeDefinition type, AbstractNode node, String prefix) {
if (type instanceof ReferenceTypeDefinition) {
type = ((ReferenceTypeDefinition) type).resolve();
}
Expand Down Expand Up @@ -115,7 +115,7 @@ protected Collection<Proposal> getProposals(TypeDefinition type, AbstractNode no
case ONE_OF:
return createComplextTypeProposals((ComplexTypeDefinition) type, node, prefix);
case UNDEFINED:
Collection<Proposal> proposals = new LinkedHashSet<>();
Collection<ProposalDescriptor> proposals = new LinkedHashSet<>();
if (type instanceof MultipleTypeDefinition) {
for (TypeDefinition currentType : ((MultipleTypeDefinition) type).getMultipleTypes()) {
proposals.addAll(getProposals(currentType, node, prefix));
Expand All @@ -126,29 +126,29 @@ protected Collection<Proposal> getProposals(TypeDefinition type, AbstractNode no
return Collections.emptyList();
}

protected Collection<Proposal> createPrimitiveProposals(TypeDefinition type) {
protected Collection<ProposalDescriptor> createPrimitiveProposals(TypeDefinition type) {
String label;
if (type.getType() == JsonType.UNDEFINED) {
label = type.getContainingProperty();
} else {
label = type.getType().getValue();
}

return Arrays.asList(new Proposal("", "", type.getDescription(), label));
return Arrays.asList(new ProposalDescriptor("").replacementString("").description(type.getDescription()).type(label));
}

protected Collection<Proposal> createBooleanProposals(TypeDefinition type) {
Collection<Proposal> proposals = new LinkedHashSet<>();
protected Collection<ProposalDescriptor> createBooleanProposals(TypeDefinition type) {
Collection<ProposalDescriptor> proposals = new LinkedHashSet<>();

String labelType = type.getType().getValue();

proposals.add(new Proposal("true", "true", type.getDescription(), labelType));
proposals.add(new Proposal("false", "false", type.getDescription(), labelType));
proposals.add(new ProposalDescriptor("true").replacementString("true").description(type.getDescription()).type(labelType));
proposals.add(new ProposalDescriptor("false").replacementString("false").description(type.getDescription()).type(labelType));

return proposals;
}

protected Proposal createPropertyProposal(String key, TypeDefinition type) {
protected ProposalDescriptor createPropertyProposal(String key, TypeDefinition type) {
if (type == null || "default".equals(key)) {
return null;
}
Expand All @@ -164,15 +164,15 @@ protected Proposal createPropertyProposal(String key, TypeDefinition type) {
labelType = type.getContainingProperty();
}

return new Proposal(key + ":", key, type.getDescription(), labelType);
return new ProposalDescriptor(key).replacementString(key + ":").description(type.getDescription()).type(labelType);
}

protected Collection<Proposal> createObjectProposals(ObjectTypeDefinition type, AbstractNode element,
protected Collection<ProposalDescriptor> createObjectProposals(ObjectTypeDefinition type, AbstractNode element,
String prefix) {
final Collection<Proposal> proposals = new LinkedHashSet<>();
final Collection<ProposalDescriptor> proposals = new LinkedHashSet<>();

for (String property : type.getProperties().keySet()) {
Proposal proposal = createPropertyProposal(property, type.getProperties().get(property));
ProposalDescriptor proposal = createPropertyProposal(property, type.getProperties().get(property));
if (proposal != null) {
if (StringUtils.emptyToNull(prefix) != null && property.startsWith(prefix)) {
proposals.add(proposal);
Expand All @@ -189,7 +189,7 @@ protected Collection<Proposal> createObjectProposals(ObjectTypeDefinition type,
property = property.substring(1);
}

Proposal proposal = createPropertyProposal(property, typeDef);
ProposalDescriptor proposal = createPropertyProposal(property, typeDef);
if (proposal != null) {
proposals.add(proposal);
}
Expand All @@ -200,34 +200,34 @@ protected Collection<Proposal> createObjectProposals(ObjectTypeDefinition type,
String elementName = elementTitle != null? elementTitle : type.getAdditionalProperties().getLabel();
if (elementName != null) {
elementName = String.format("(%s name)", elementName);
proposals.add(new Proposal(elementName + ":", elementName, null, null, elementName));
proposals.add(new ProposalDescriptor(elementName).replacementString(elementName + ":").selection(elementName));
}
}
if (proposals.isEmpty()) {
proposals.add(new Proposal("_key_" + ":", "_key_", null, null));
proposals.add(new ProposalDescriptor("_key_").replacementString("_key_" + ":"));
}
return proposals;
}

protected Collection<Proposal> createArrayProposals(ArrayTypeDefinition type, AbstractNode node) {
Collection<Proposal> proposals = new LinkedHashSet<>();
protected Collection<ProposalDescriptor> createArrayProposals(ArrayTypeDefinition type, AbstractNode node) {
Collection<ProposalDescriptor> proposals = new LinkedHashSet<>();

if (type.itemsType.getType() == JsonType.ENUM) {
String labelType = type.itemsType.getContainingProperty();

for (String literal : enumLiterals(type.itemsType)) {
proposals.add(new Proposal("- " + literal, literal, type.getDescription(), labelType));
proposals.add(new ProposalDescriptor(literal).replacementString("- " + literal).description(type.getDescription()).type(labelType));
}
} else {
proposals.add(new Proposal("-", "-", type.getDescription(), "array item"));
proposals.add(new ProposalDescriptor("-").replacementString("-").description(type.getDescription()).type("array item"));
}

return proposals;
}

protected Collection<Proposal> createComplextTypeProposals(ComplexTypeDefinition type, AbstractNode node,
protected Collection<ProposalDescriptor> createComplextTypeProposals(ComplexTypeDefinition type, AbstractNode node,
String prefix) {
final Collection<Proposal> proposals = new LinkedHashSet<>();
final Collection<ProposalDescriptor> proposals = new LinkedHashSet<>();

for (TypeDefinition definition : type.getComplexTypes()) {
proposals.addAll(getProposals(definition, node, prefix));
Expand All @@ -244,8 +244,8 @@ protected Collection<String> enumLiterals(TypeDefinition type) {
return literals;
}

protected Collection<Proposal> createEnumProposals(TypeDefinition type, AbstractNode node) {
final Collection<Proposal> proposals = new LinkedHashSet<>();
protected Collection<ProposalDescriptor> createEnumProposals(TypeDefinition type, AbstractNode node) {
final Collection<ProposalDescriptor> proposals = new LinkedHashSet<>();
final String subType = type.asJson().has("type") ? //
type.asJson().get("type").asText() : //
null;
Expand All @@ -264,7 +264,7 @@ protected Collection<Proposal> createEnumProposals(TypeDefinition type, Abstract

String labelType = type.getType().getValue();

proposals.add(new Proposal(replStr, literal, type.getDescription(), labelType));
proposals.add(new ProposalDescriptor(literal).replacementString(replStr).description(type.getDescription()).type(labelType));
}

return proposals;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ public boolean canProvideProposal(Model model, JsonPointer pointer) {
* @param scope
* @return proposals
*/
public Collection<Proposal> getProposals(JsonPointer pointer, JsonDocument document, Scope scope) {
public Collection<ProposalDescriptor> getProposals(JsonPointer pointer, JsonDocument document, Scope scope) {
final ContextType type = contextTypes.get(document.getModel(), pointer);
final IFile currentFile = getActiveFile();
final IPath basePath = currentFile.getParent().getFullPath();
final List<Proposal> proposals = new ArrayList<>();
final List<ProposalDescriptor> proposals = new ArrayList<>();

if (scope == Scope.LOCAL) {
proposals.addAll(type.collectProposals(document, null));
Expand Down

This file was deleted.

Loading