Skip to content

Commit fe90082

Browse files
authored
Merge pull request #402 from RepreZen/task/227
#227 Better code completion proposal for name/key of additional properties
2 parents 12d6e90 + 476b653 commit fe90082

File tree

27 files changed

+638
-115
lines changed

27 files changed

+638
-115
lines changed

com.reprezen.swagedit.core/src/com/reprezen/swagedit/core/assist/JsonProposalProvider.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,17 @@ protected Collection<Proposal> createObjectProposals(ObjectTypeDefinition type,
195195
}
196196
}
197197

198+
if (type.getAdditionalProperties() != null) {
199+
String elementTitle = type.getLabel();
200+
String elementName = elementTitle != null? elementTitle : type.getAdditionalProperties().getLabel();
201+
if (elementName != null) {
202+
elementName = String.format("(%s name)", elementName);
203+
proposals.add(new Proposal(elementName + ":", elementName, null, null, elementName));
204+
}
205+
}
198206
if (proposals.isEmpty()) {
199207
proposals.add(new Proposal("_key_" + ":", "_key_", null, null));
200208
}
201-
202209
return proposals;
203210
}
204211

com.reprezen.swagedit.core/src/com/reprezen/swagedit/core/assist/Proposal.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class Proposal {
3030
public final String displayString;
3131
public final String type;
3232
public final String description;
33+
private final String selection;
3334

3435
protected final Styler typeStyler = new StyledString.Styler() {
3536
@Override
@@ -38,11 +39,16 @@ public void applyStyles(TextStyle textStyle) {
3839
}
3940
};
4041

41-
public Proposal(String replacementString, String displayString, String description, String type) {
42+
public Proposal(String replacementString, String displayString, String description, String type, String selection) {
4243
this.replacementString = replacementString;
4344
this.displayString = displayString;
4445
this.type = type;
4546
this.description = description;
47+
this.selection = selection;
48+
}
49+
50+
public Proposal(String replacementString, String displayString, String description, String type) {
51+
this(replacementString, displayString, description, type, "");
4652
}
4753

4854
/**
@@ -93,9 +99,10 @@ public StyledCompletionProposal asStyledCompletionProposal(String prefix, int of
9399

94100
StyledCompletionProposal proposal = null;
95101
if (Strings.emptyToNull(prefix) == null) {
96-
proposal = new StyledCompletionProposal(replacementString, styledString, null, description, offset);
102+
proposal = new StyledCompletionProposal(replacementString, styledString, null, description, offset,
103+
selection);
97104
} else if (rString.toLowerCase().contains(prefix.toLowerCase())) {
98-
proposal = new StyledCompletionProposal(rString, styledString, prefix, description, offset);
105+
proposal = new StyledCompletionProposal(rString, styledString, prefix, description, offset, selection);
99106
}
100107

101108
return proposal;

com.reprezen.swagedit.core/src/com/reprezen/swagedit/core/assist/StyledCompletionProposal.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ public class StyledCompletionProposal
3434
private final String description;
3535
/** Lower-cased prefix - content assist typeahead should be case-insensitive */
3636
private final String prefix;
37+
private final String selection;
3738

3839
public StyledCompletionProposal(String replacement, StyledString label, String prefix, String description,
39-
int offset) {
40+
int offset, String selection) {
4041
this.label = label;
4142
this.replacementString = replacement;
43+
this.selection = selection == null ? "" : selection;
4244
this.prefix = prefix != null ? prefix.toLowerCase() : null;
4345
this.replacementOffset = offset;
4446
this.description = description;
@@ -82,10 +84,9 @@ public Point getSelection(IDocument document) {
8284
offset = replacementOffset - prefix.length();
8385
}
8486
}
85-
86-
int cursorPosition = offset + replacementString.length();
87-
88-
return new Point(cursorPosition, 0);
87+
int replacementIndex = !"".equals(selection) ? replacementString.indexOf(selection) : -1;
88+
int selectionStart = offset + (replacementIndex < 0 ? replacementString.length() : replacementIndex);
89+
return new Point(selectionStart, selection.length());
8990
}
9091

9192
@Override

com.reprezen.swagedit.core/src/com/reprezen/swagedit/core/assist/contexts/ComponentContextType.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import java.util.Collection;
1414

1515
import com.fasterxml.jackson.core.JsonPointer;
16-
import com.fasterxml.jackson.databind.ObjectMapper;
17-
import com.fasterxml.jackson.databind.node.ObjectNode;
16+
import com.fasterxml.jackson.databind.JsonNode;
17+
import com.reprezen.swagedit.core.json.references.JsonReference;
1818
import com.reprezen.swagedit.core.model.AbstractNode;
1919
import com.reprezen.swagedit.core.model.Model;
2020
import com.reprezen.swagedit.core.schema.ComplexTypeDefinition;
@@ -23,13 +23,13 @@
2323

2424
public class ComponentContextType extends ContextType {
2525

26-
private final ObjectNode componentRef;
27-
26+
private final String componentRef;
27+
2828
public ComponentContextType(String value, String label, String componentSchemaPath) {
2929
super(value, label);
30-
componentRef = new ObjectMapper().createObjectNode().put("$ref", "#/definitions/" + componentSchemaPath);
30+
componentRef = "#/definitions/" + componentSchemaPath;
3131
}
32-
32+
3333
protected String getReferencePointerString() {
3434
return "/definitions/reference/properties/$ref";
3535
}
@@ -73,12 +73,17 @@ protected boolean isReferenceToComponent(Model model, JsonPointer pointer) {
7373
if (parentType instanceof ComplexTypeDefinition) {
7474
Collection<TypeDefinition> types = ((ComplexTypeDefinition) parentType).getComplexTypes();
7575
for (TypeDefinition type : types) {
76-
if (componentRef.equals(type.getContent())) {
76+
if (hasRefToComponent(type.getContent())) {
7777
return true;
7878
}
7979
}
8080
}
81-
return componentRef.equals(parentType.getContent());
81+
return hasRefToComponent(parentType.getContent());
82+
}
83+
84+
private boolean hasRefToComponent(JsonNode content) {
85+
return content.hasNonNull(JsonReference.PROPERTY)
86+
&& componentRef.equals(content.get(JsonReference.PROPERTY).asText());
8287
}
8388

8489
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2016 ModelSolv, Inc. and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* ModelSolv, Inc. - initial API and implementation and/or initial documentation
10+
*******************************************************************************/
11+
package com.reprezen.swagedit.core.preferences;
12+
13+
import org.dadacoalition.yedit.preferences.PreferenceConstants;
14+
import org.eclipse.jface.preference.IPreferenceStore;
15+
16+
public class KaiZenPreferencesUtils {
17+
public static int getTabWidth() {
18+
IPreferenceStore prefs = org.dadacoalition.yedit.Activator.getDefault().getPreferenceStore();
19+
return prefs.getInt(PreferenceConstants.SPACES_PER_TAB);
20+
}
21+
}

com.reprezen.swagedit.core/src/com/reprezen/swagedit/core/quickfix/QuickFixer.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
*******************************************************************************/
1111
package com.reprezen.swagedit.core.quickfix;
1212

13+
import static com.reprezen.swagedit.core.preferences.KaiZenPreferencesUtils.getTabWidth;
14+
1315
import java.util.regex.Matcher;
1416
import java.util.regex.Pattern;
1517

16-
import org.dadacoalition.yedit.preferences.PreferenceConstants;
1718
import org.eclipse.core.resources.IMarker;
1819
import org.eclipse.core.runtime.CoreException;
19-
import org.eclipse.jface.preference.IPreferenceStore;
2020
import org.eclipse.jface.text.BadLocationException;
2121
import org.eclipse.jface.text.IDocument;
2222
import org.eclipse.jface.text.IRegion;
@@ -83,10 +83,6 @@ protected String getIndent(IDocument document, int line) throws BadLocationExcep
8383
return definitionIndent + Strings.repeat(" ", getTabWidth());
8484
}
8585

86-
private int getTabWidth() {
87-
IPreferenceStore prefs = org.dadacoalition.yedit.Activator.getDefault().getPreferenceStore();
88-
return prefs.getInt(PreferenceConstants.SPACES_PER_TAB);
89-
}
9086
}
9187

9288
}

com.reprezen.swagedit.core/src/com/reprezen/swagedit/core/schema/ComplexTypeDefinition.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.fasterxml.jackson.core.JsonPointer;
2020
import com.fasterxml.jackson.databind.JsonNode;
2121
import com.fasterxml.jackson.databind.node.ArrayNode;
22+
import com.google.common.collect.Iterables;
2223
import com.reprezen.swagedit.core.model.AbstractNode;
2324

2425
/**
@@ -139,4 +140,10 @@ public boolean validate(AbstractNode valueNode) {
139140

140141
return isValid;
141142
}
143+
144+
@Override
145+
public String getLabel() {
146+
TypeDefinition firstType = Iterables.getFirst(complexTypes, null);
147+
return firstType!=null ? firstType.getLabel() : null;
148+
}
142149
}

com.reprezen.swagedit.core/src/com/reprezen/swagedit/core/schema/CompositeSchema.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ protected String baseURI(String href) {
138138
return href.startsWith("#") ? null : href.split("#")[0];
139139
}
140140

141-
protected JsonPointer pointer(String href) {
141+
public static JsonPointer pointer(String href) {
142142
if (href.startsWith("#")) {
143143
return JsonPointer.compile(href.substring(1));
144144
} else if (href.startsWith("/")) {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2017 ModelSolv, Inc. and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* ModelSolv, Inc. - initial API and implementation and/or initial documentation
10+
*******************************************************************************/
11+
package com.reprezen.swagedit.core.schema;
12+
13+
import static com.google.common.collect.Iterators.transform;
14+
15+
import java.util.Iterator;
16+
17+
import com.fasterxml.jackson.databind.JsonNode;
18+
import com.fasterxml.jackson.databind.node.ArrayNode;
19+
import com.google.common.base.Function;
20+
import com.google.common.base.Joiner;
21+
22+
public class JsonSchemaUtils {
23+
public static String getHumanFriendlyText(JsonNode swaggerSchemaNode, final String defaultValue) {
24+
String schemaTitle = getSchemaTitle(swaggerSchemaNode);
25+
if (schemaTitle != null) {
26+
return schemaTitle;
27+
}
28+
// nested array
29+
if (swaggerSchemaNode.get("items") != null) {
30+
return getHumanFriendlyText(swaggerSchemaNode.get("items"), defaultValue);
31+
}
32+
// "$ref":"#/definitions/headerParameterSubSchema"
33+
JsonNode ref = swaggerSchemaNode.get("$ref");
34+
if (ref != null) {
35+
return getLabelForRef(ref.asText());
36+
}
37+
// Auxiliary oneOf in "oneOf": [ { "$ref": "#/definitions/securityRequirement" }]
38+
JsonNode oneOf = swaggerSchemaNode.get("oneOf");
39+
if (oneOf != null) {
40+
if (oneOf instanceof ArrayNode) {
41+
ArrayNode arrayNode = (ArrayNode) oneOf;
42+
if (arrayNode.size() > 0) {
43+
Iterator<String> labels = transform(arrayNode.elements(), new Function<JsonNode, String>() {
44+
45+
@Override
46+
public String apply(JsonNode el) {
47+
return getHumanFriendlyText(el, defaultValue);
48+
}
49+
});
50+
return "[" + Joiner.on(", ").join(labels) + "]";
51+
}
52+
}
53+
}
54+
return defaultValue;
55+
}
56+
57+
public static String getSchemaTitle(JsonNode swaggerSchemaNode) {
58+
JsonNode title = swaggerSchemaNode.get("title");
59+
if (title != null) {
60+
return title.asText();
61+
}
62+
return null;
63+
}
64+
65+
public static String getLabelForRef(String refValue) {
66+
return refValue.substring(refValue.lastIndexOf("/") + 1);
67+
}
68+
}

com.reprezen.swagedit.core/src/com/reprezen/swagedit/core/schema/ReferenceTypeDefinition.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,18 @@ public TypeDefinition getPropertyType(String property) {
6868
public boolean validate(AbstractNode valueNode) {
6969
return resolve().validate(valueNode);
7070
}
71+
72+
@Override
73+
public String getLabel() {
74+
String label = JsonSchemaUtils.getSchemaTitle(content);
75+
if (label != null) {
76+
return label;
77+
}
78+
// $ref=schemaOrReference
79+
if (resolve() instanceof ComplexTypeDefinition) {
80+
return resolve().getLabel();
81+
}
82+
// $ref=schema
83+
return JsonSchemaUtils.getLabelForRef(content.get(JsonReference.PROPERTY).asText());
84+
}
7185
}

0 commit comments

Comments
 (0)