WEB-37864 Support JSON-schema based code completion for all linters/tools we support for yaml/plain js formats

This commit is contained in:
Anton Lobov
2019-03-20 10:50:24 +01:00
parent c9380139ed
commit 62f95b942d
3 changed files with 10 additions and 4 deletions
@@ -37,6 +37,7 @@ public interface JsonLikePsiWalker {
boolean requiresNameQuotes();
default boolean requiresValueQuotes() { return true; }
boolean allowsSingleQuotes();
default boolean isValidIdentifier(String string, Project project) { return true; }
boolean hasMissingCommaAfter(@NotNull PsiElement element);
@@ -160,7 +160,7 @@ public class JsonSchemaCompletionContributor extends CompletionContributor {
myResultConsumer = resultConsumer;
myVariants = new HashSet<>();
myWalker = JsonLikePsiWalker.getWalker(myPosition, myRootSchema);
myWrapInQuotes = myWalker != null && myWalker.requiresNameQuotes() && !(position.getParent() instanceof JsonStringLiteral);
myWrapInQuotes = !(position.getParent() instanceof JsonStringLiteral);
myInsideStringLiteral = position.getParent() instanceof JsonStringLiteral;
}
@@ -392,7 +392,8 @@ public class JsonSchemaCompletionContributor extends CompletionContributor {
@SuppressWarnings("SameParameterValue") @Nullable final String description,
@Nullable final String altText,
@Nullable InsertHandler<LookupElement> handler) {
LookupElementBuilder builder = LookupElementBuilder.create(!myWrapInQuotes ? StringUtil.unquoteString(key) : key);
String unquoted = StringUtil.unquoteString(key);
LookupElementBuilder builder = LookupElementBuilder.create(!shouldWrapInQuotes(unquoted) ? unquoted : key);
if (altText != null) {
builder = builder.withPresentableText(altText);
}
@@ -405,13 +406,17 @@ public class JsonSchemaCompletionContributor extends CompletionContributor {
myVariants.add(builder);
}
private boolean shouldWrapInQuotes(String key) {
return myWrapInQuotes && myWalker != null && (myWalker.requiresNameQuotes() || !myWalker.isValidIdentifier(key, myProject));
}
private void addPropertyVariant(@NotNull String key,
@NotNull JsonSchemaObject jsonSchemaObject,
boolean hasValue,
boolean insertComma) {
final Collection<JsonSchemaObject> variants = new JsonSchemaResolver(myProject, jsonSchemaObject).resolve();
jsonSchemaObject = ObjectUtils.coalesce(ContainerUtil.getFirstItem(variants), jsonSchemaObject);
key = !myWrapInQuotes ? key : StringUtil.wrapWithDoubleQuote(key);
key = !shouldWrapInQuotes(key) ? key : StringUtil.wrapWithDoubleQuote(key);
LookupElementBuilder builder = LookupElementBuilder.create(key);
final String typeText = JsonSchemaDocumentationProvider.getBestDocumentation(true, jsonSchemaObject);
@@ -28,7 +28,7 @@ public class JsonSchemaPerformanceTest extends JsonSchemaHeavyAbstractTest {
}
public void testSwaggerHighlighting() {
doPerformanceTest(8000, "swagger");
doPerformanceTest(8300, "swagger");
}
public void testTsLintSchema() {