diff --git a/src/main/java/com/couchbase/intellij/searchworkbench/contributor/BooleanCbsContributor.java b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/BooleanCbsContributor.java index e3ed70f0..7bbeeb30 100644 --- a/src/main/java/com/couchbase/intellij/searchworkbench/contributor/BooleanCbsContributor.java +++ b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/BooleanCbsContributor.java @@ -1,5 +1,6 @@ package com.couchbase.intellij.searchworkbench.contributor; +import com.intellij.codeInsight.completion.CompletionResultSet; import com.intellij.json.psi.JsonObject; import com.intellij.json.psi.JsonProperty; @@ -14,7 +15,7 @@ public boolean accept(String parentKey) { } @Override - public void contributeKey(String parentKey, JsonObject jsonObject, List contributors) { + public void contributeKey(String parentKey, JsonObject jsonObject, List contributors, CompletionResultSet result) { List existingKeys = jsonObject.getPropertyList().stream() .map(JsonProperty::getName) .toList(); diff --git a/src/main/java/com/couchbase/intellij/searchworkbench/contributor/CBSCodeCompletionContributor.java b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/CBSCodeCompletionContributor.java index dcd97b67..4edba478 100644 --- a/src/main/java/com/couchbase/intellij/searchworkbench/contributor/CBSCodeCompletionContributor.java +++ b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/CBSCodeCompletionContributor.java @@ -96,7 +96,7 @@ protected void addCompletions(@NotNull CompletionParameters parameters, for (CBSContributor contributor : contributors) { if (contributor.accept(type)) { if (isKey) { - contributor.contributeKey(type, jsonObject, suggestions); + contributor.contributeKey(type, jsonObject, suggestions, result); } else { contributor.contributeValue(jsonObject, attributeName, suggestions); } diff --git a/src/main/java/com/couchbase/intellij/searchworkbench/contributor/CBSContributor.java b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/CBSContributor.java index 83053838..30a2359e 100644 --- a/src/main/java/com/couchbase/intellij/searchworkbench/contributor/CBSContributor.java +++ b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/CBSContributor.java @@ -1,5 +1,6 @@ package com.couchbase.intellij.searchworkbench.contributor; +import com.intellij.codeInsight.completion.CompletionResultSet; import com.intellij.json.psi.JsonObject; import java.util.List; @@ -8,7 +9,7 @@ public interface CBSContributor { boolean accept(String key); - void contributeKey(String parentKey, JsonObject jsonObject, List contributors); + void contributeKey(String parentKey, JsonObject jsonObject, List contributors, CompletionResultSet result); void contributeValue(JsonObject jsonObject, String attributeKey, List contributors); } diff --git a/src/main/java/com/couchbase/intellij/searchworkbench/contributor/CBSTemplateDef.java b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/CBSTemplateDef.java new file mode 100644 index 00000000..19178e1a --- /dev/null +++ b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/CBSTemplateDef.java @@ -0,0 +1,16 @@ +package com.couchbase.intellij.searchworkbench.contributor; + +import lombok.AllArgsConstructor; +import lombok.Data; + +import java.util.List; + +@Data +@AllArgsConstructor +public class CBSTemplateDef { + + private String key; + private String desc; + + private List attrs; +} diff --git a/src/main/java/com/couchbase/intellij/searchworkbench/contributor/CBSTemplates.java b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/CBSTemplates.java new file mode 100644 index 00000000..e3fd39cc --- /dev/null +++ b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/CBSTemplates.java @@ -0,0 +1,423 @@ +package com.couchbase.intellij.searchworkbench.contributor; + +import com.intellij.codeInsight.lookup.LookupElement; +import com.intellij.codeInsight.lookup.LookupElementBuilder; +import com.intellij.codeInsight.template.Template; +import com.intellij.codeInsight.template.TemplateManager; +import com.intellij.codeInsight.template.impl.TextExpression; +import com.intellij.icons.AllIcons; +import com.intellij.openapi.editor.Document; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; + +import java.util.List; + +public class CBSTemplates { + + public static final String RADIUS_QUERY = "radius_query"; + + public static final String RECTANGLE_QUERY = "rectangle_query"; + public static final String POINT_GEO_QUERY = "point_geo_query"; + public static final String LINESTRING_GEO_QUERY = "linestring_geo_query"; + public static final String POLYGON_GEO_QUERY = "polygon_geo_query"; + + public static LookupElement getPointGeoJsonTemplate(List existingKeys) { + return getGeoTemplate(POINT_GEO_QUERY, "Point GeoJSON Query", "Point", existingKeys); + } + + public static LookupElement getLineStringTemplate(List existingKeys) { + return getGeoTemplate(LINESTRING_GEO_QUERY, "LineString GeoJSON Query", "LineString", existingKeys); + } + + public static LookupElement getPolygonTemplate(List existingKeys) { + return getGeoTemplate(POLYGON_GEO_QUERY, "Polygon GeoJSON Query", "Polygon", existingKeys); + } + + public static LookupElement getMultiPointTemplate(List existingKeys) { + return getGeoTemplate("multi_point_geo_query", "MultiPoint GeoJSON Query", "MultiPoint", existingKeys); + } + + public static LookupElement getMultiLineStringTemplate(List existingKeys) { + return getGeoTemplate("multi_linestring_geo_query", "MultiLineString GeoJSON Query", "MultiLineString", existingKeys); + } + + public static LookupElement getMultiPolygonTemplate(List existingKeys) { + return getGeoTemplate("multi_polygon_geo_query", "MultiPolygon GeoJSON Query", "MultiPolygon", existingKeys); + } + + public static LookupElement getEnvelopeTemplate(List existingKeys) { + return getGeoTemplate("envelope_geo_query", "Envelope GeoJSON Query", "Envelope", existingKeys); + } + + public static LookupElement getCircleTemplate(List existingKeys) { + return getGeoTemplate("circle_geo_query", "Circle GeoJSON Query", "Circle", existingKeys); + } + + public static LookupElement getGeometryCollectionTemplate(List existingKeys) { + return getGeoTemplate("geometry_col_geo_query", "Geometry Collection GeoJSON Query", "GeometryCollection", existingKeys); + } + + + public static LookupElement getGeoTemplate(String key, String desc, String type, List existingKeys) { + return LookupElementBuilder.create(key) + .withTypeText(desc) + .withBoldness(true) + .withIcon(AllIcons.Json.Object) + .withInsertHandler((context, item) -> { + Editor editor = context.getEditor(); + Project project = context.getProject(); + + int startOffset = editor.getCaretModel().getOffset() - key.length(); + + Document document = editor.getDocument(); + document.deleteString(startOffset, startOffset + key.length()); + + TemplateManager templateManager = TemplateManager.getInstance(project); + Template template = templateManager.createTemplate(key, "JSON"); + template.setToReformat(true); + + boolean needsComma = false; + + if (!existingKeys.contains("field")) { + template.addTextSegment("\"field\" : "); + template.addVariable("field", new TextExpression(""), true); + needsComma = true; + } + + if (!existingKeys.contains("geometry")) { + if (needsComma) { + template.addTextSegment(",\n"); + } + template.addTextSegment("\"geometry\": {\n"); + template.addTextSegment("\"shape\" : { \n"); + template.addTextSegment("\"type\" : \"" + type + "\", \n"); + + if ("GeometryCollection".equals(type)) { + template.addTextSegment("\"geometries\" : [ \n"); + template.addVariable("geometries", new TextExpression(""), true); + template.addTextSegment("]\n"); + } else { + template.addTextSegment("\"coordinates\" :"); + template.addVariable("coordinates", new TextExpression(""), true); + } + + if ("Circle".equals(type)) { + template.addTextSegment(",\n \"radius\":"); + template.addVariable("radius", new TextExpression(""), true); + } + template.addTextSegment("},\n"); + template.addTextSegment("\"relation\" : "); + template.addVariable("relation", new TextExpression(""), true); + template.addTextSegment("\n}"); + } + + TemplateManager.getInstance(project).startTemplate(editor, template); + }); + } + + + public static LookupElement getMustTemplate() { + return LookupElementBuilder.create("must") + .withTypeText("Must boolean query template") + .withBoldness(true) + .withIcon(AllIcons.Json.Object) + .withInsertHandler((context, item) -> { + Editor editor = context.getEditor(); + Project project = context.getProject(); + + int startOffset = editor.getCaretModel().getOffset() - "must".length(); + + Document document = editor.getDocument(); + document.deleteString(startOffset, startOffset + "must".length()); + + TemplateManager templateManager = TemplateManager.getInstance(project); + Template template = templateManager.createTemplate("must", "JSON"); + template.setToReformat(true); + + + template.addTextSegment("\"must\" : {\n \"conjuncts\":[ \n{"); + template.addVariable("filter", new TextExpression(""), true); + template.addTextSegment("}\n]\n}"); + TemplateManager.getInstance(project).startTemplate(editor, template); + }); + } + + public static LookupElement getShouldTemplate() { + return LookupElementBuilder.create("should") + .withTypeText("Should boolean query template") + .withBoldness(true) + .withIcon(AllIcons.Json.Object) + .withInsertHandler((context, item) -> { + Editor editor = context.getEditor(); + Project project = context.getProject(); + + int startOffset = editor.getCaretModel().getOffset() - "should".length(); + + Document document = editor.getDocument(); + document.deleteString(startOffset, startOffset + "should".length()); + + TemplateManager templateManager = TemplateManager.getInstance(project); + Template template = templateManager.createTemplate("should", "JSON"); + template.setToReformat(true); + + + template.addTextSegment("\"should\" : {\n \"disjuncts\":[ \n{"); + template.addVariable("filter", new TextExpression(""), true); + template.addTextSegment("}\n]\n}"); + TemplateManager.getInstance(project).startTemplate(editor, template); + }); + } + + public static LookupElement getMustNotTemplate() { + return LookupElementBuilder.create("must_not") + .withTypeText("MustNot boolean query template") + .withBoldness(true) + .withIcon(AllIcons.Json.Object) + .withInsertHandler((context, item) -> { + Editor editor = context.getEditor(); + Project project = context.getProject(); + + int startOffset = editor.getCaretModel().getOffset() - "must_not".length(); + + Document document = editor.getDocument(); + document.deleteString(startOffset, startOffset + "must_not".length()); + + TemplateManager templateManager = TemplateManager.getInstance(project); + Template template = templateManager.createTemplate("must_not", "JSON"); + template.setToReformat(true); + + + template.addTextSegment("\"must_not\" : {\n \"disjuncts\":[ \n{"); + template.addVariable("filter", new TextExpression(""), true); + template.addTextSegment("}\n]\n}"); + TemplateManager.getInstance(project).startTemplate(editor, template); + }); + } + + public static LookupElement getRectangleTemplate(List existingKeys) { + return LookupElementBuilder.create(RECTANGLE_QUERY) + .withTypeText("Rectangle-Based Geopoint Query") + .withBoldness(true) + .withIcon(AllIcons.Json.Object) + .withInsertHandler((context, item) -> { + Editor editor = context.getEditor(); + Project project = context.getProject(); + + int startOffset = editor.getCaretModel().getOffset() - RECTANGLE_QUERY.length(); + + Document document = editor.getDocument(); + document.deleteString(startOffset, startOffset + RECTANGLE_QUERY.length()); + + TemplateManager templateManager = TemplateManager.getInstance(project); + Template template = templateManager.createTemplate(RECTANGLE_QUERY, "JSON"); + template.setToReformat(true); + + boolean needsComma = false; + + if (!existingKeys.contains("field")) { + template.addTextSegment("\"field\" : "); + template.addVariable("field", new TextExpression(""), true); + needsComma = true; + } + + if (!existingKeys.contains("top_left")) { + if (needsComma) { + template.addTextSegment(",\n"); + } + template.addTextSegment("\"top_left\": {"); + template.addTextSegment("\"lon\" : "); + template.addVariable("lon", new TextExpression(""), true); + template.addTextSegment(",\n"); + template.addTextSegment("\"lat\" : "); + template.addVariable("lat", new TextExpression(""), true); + template.addTextSegment("\n}"); + } + + if (!existingKeys.contains("bottom_right")) { + if (needsComma) { + template.addTextSegment(",\n"); + } + template.addTextSegment("\"bottom_right\": {"); + template.addTextSegment("\"lon\" : "); + template.addVariable("lon", new TextExpression(""), true); + template.addTextSegment(",\n"); + template.addTextSegment("\"lat\" : "); + template.addVariable("lat", new TextExpression(""), true); + template.addTextSegment("\n}"); + } + + TemplateManager.getInstance(project).startTemplate(editor, template); + }); + } + + public static LookupElement getRadiusTemplate(List existingKeys) { + return LookupElementBuilder.create(RADIUS_QUERY) + .withTypeText("Distance/Radius-Based Geopoint Query") + .withBoldness(true) + .withIcon(AllIcons.Json.Object) + .withInsertHandler((context, item) -> { + Editor editor = context.getEditor(); + Project project = context.getProject(); + + int startOffset = editor.getCaretModel().getOffset() - RADIUS_QUERY.length(); + + Document document = editor.getDocument(); + document.deleteString(startOffset, startOffset + RADIUS_QUERY.length()); + + TemplateManager templateManager = TemplateManager.getInstance(project); + Template template = templateManager.createTemplate(RADIUS_QUERY, "JSON"); + template.setToReformat(true); + + boolean needsComma = false; + + if (!existingKeys.contains("field")) { + template.addTextSegment("\"field\" : "); + template.addVariable("field", new TextExpression(""), true); + needsComma = true; + } + + if (!existingKeys.contains("distance")) { + if (needsComma) { + template.addTextSegment(",\n"); + } + template.addTextSegment("\"distance\" : "); + template.addVariable("distance", new TextExpression(""), true); + needsComma = true; + } + + if (!existingKeys.contains("location")) { + if (needsComma) { + template.addTextSegment(",\n"); + } + template.addTextSegment("\"location\": {"); + template.addTextSegment("\"lon\" : "); + template.addVariable("lon", new TextExpression(""), true); + template.addTextSegment(",\n"); + template.addTextSegment("\"lat\" : "); + template.addVariable("lat", new TextExpression(""), true); + template.addTextSegment("\n}"); + } + + TemplateManager.getInstance(project).startTemplate(editor, template); + }); + } + + + public static LookupElement getGenericTemplate(String key, String desc, List attrs) { + return LookupElementBuilder.create(key) + .withTypeText(desc) + .withBoldness(true) + .withIcon(AllIcons.Json.Object) + .withInsertHandler((context, item) -> { + Editor editor = context.getEditor(); + Project project = context.getProject(); + + int startOffset = editor.getCaretModel().getOffset() - key.length(); + + Document document = editor.getDocument(); + document.deleteString(startOffset, startOffset + key.length()); + + TemplateManager templateManager = TemplateManager.getInstance(project); + Template template = templateManager.createTemplate(key, "JSON"); + template.setToReformat(true); + + for (int i = 0; i < attrs.size(); i++) { + + template.addTextSegment("\"" + attrs.get(i) + "\" : "); + template.addVariable(attrs.get(i), new TextExpression(""), true); + //isn't the last in the value + if (attrs.size() > 1 && i < attrs.size() - 1) { + template.addTextSegment(",\n"); + } + } + TemplateManager.getInstance(project).startTemplate(editor, template); + }); + } + + + public static LookupElement getQueryTemplate(List existingKeys) { + return LookupElementBuilder.create("query") + .withTypeText("Query string query syntax") + .withBoldness(true) + .withIcon(AllIcons.Json.Object) + .withInsertHandler((context, item) -> { + Editor editor = context.getEditor(); + Project project = context.getProject(); + + int startOffset = editor.getCaretModel().getOffset() - "query".length(); + + Document document = editor.getDocument(); + document.deleteString(startOffset, startOffset + "query".length()); + + TemplateManager templateManager = TemplateManager.getInstance(project); + Template template = templateManager.createTemplate("query", "JSON"); + template.setToReformat(true); + + if (!existingKeys.contains("query")) { + template.addTextSegment("\"query\" : \""); + template.addVariable("query", new TextExpression(""), true); + template.addTextSegment("\""); + } + + TemplateManager.getInstance(project).startTemplate(editor, template); + }); + } + + + public static LookupElement getConjunctsTemplate(List existingKeys) { + return LookupElementBuilder.create("conjuncts") + .withTypeText("Query conjunction") + .withBoldness(true) + .withIcon(AllIcons.Json.Object) + .withInsertHandler((context, item) -> { + Editor editor = context.getEditor(); + Project project = context.getProject(); + + int startOffset = editor.getCaretModel().getOffset() - "conjuncts".length(); + + Document document = editor.getDocument(); + document.deleteString(startOffset, startOffset + "conjuncts".length()); + + TemplateManager templateManager = TemplateManager.getInstance(project); + Template template = templateManager.createTemplate("conjuncts", "JSON"); + template.setToReformat(true); + + if (!existingKeys.contains("conjuncts")) { + template.addTextSegment("\"conjuncts\" : [ \n {\n"); + template.addVariable("conjuncts", new TextExpression(""), true); + template.addTextSegment("\n}\n]"); + } + + TemplateManager.getInstance(project).startTemplate(editor, template); + }); + } + + public static LookupElement getDisjunctsTemplate(List existingKeys) { + return LookupElementBuilder.create("disjuncts") + .withTypeText("Query disjunction") + .withBoldness(true) + .withIcon(AllIcons.Json.Object) + .withInsertHandler((context, item) -> { + Editor editor = context.getEditor(); + Project project = context.getProject(); + + int startOffset = editor.getCaretModel().getOffset() - "disjuncts".length(); + + Document document = editor.getDocument(); + document.deleteString(startOffset, startOffset + "disjuncts".length()); + + TemplateManager templateManager = TemplateManager.getInstance(project); + Template template = templateManager.createTemplate("disjuncts", "JSON"); + template.setToReformat(true); + + if (!existingKeys.contains("disjuncts")) { + template.addTextSegment("\"disjuncts\" : [ \n {\n"); + template.addVariable("disjuncts", new TextExpression(""), true); + template.addTextSegment("\n}\n]"); + } + + TemplateManager.getInstance(project).startTemplate(editor, template); + }); + } + +} diff --git a/src/main/java/com/couchbase/intellij/searchworkbench/contributor/ConsistencyCbsContributor.java b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/ConsistencyCbsContributor.java index 5effaa92..97e6c6c5 100644 --- a/src/main/java/com/couchbase/intellij/searchworkbench/contributor/ConsistencyCbsContributor.java +++ b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/ConsistencyCbsContributor.java @@ -1,5 +1,6 @@ package com.couchbase.intellij.searchworkbench.contributor; +import com.intellij.codeInsight.completion.CompletionResultSet; import com.intellij.json.psi.JsonObject; import java.util.Arrays; @@ -15,7 +16,7 @@ public boolean accept(String parentKey) { } @Override - public void contributeKey(String parentKey, JsonObject jsonObject, List contributors) { + public void contributeKey(String parentKey, JsonObject jsonObject, List contributors, CompletionResultSet result) { ContributorUtil.suggestMissing(jsonObject, keys, contributors); } diff --git a/src/main/java/com/couchbase/intellij/searchworkbench/contributor/CtlCbsContributor.java b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/CtlCbsContributor.java index 052b9038..4a631128 100644 --- a/src/main/java/com/couchbase/intellij/searchworkbench/contributor/CtlCbsContributor.java +++ b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/CtlCbsContributor.java @@ -1,5 +1,6 @@ package com.couchbase.intellij.searchworkbench.contributor; +import com.intellij.codeInsight.completion.CompletionResultSet; import com.intellij.json.psi.JsonObject; import java.util.Arrays; @@ -15,7 +16,7 @@ public boolean accept(String parentKey) { } @Override - public void contributeKey(String parentKey, JsonObject jsonObject, List contributors) { + public void contributeKey(String parentKey, JsonObject jsonObject, List contributors, CompletionResultSet result) { ContributorUtil.suggestMissing(jsonObject, keys, contributors); } diff --git a/src/main/java/com/couchbase/intellij/searchworkbench/contributor/GeometryCbsContributor.java b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/GeometryCbsContributor.java index b2e4fe5a..2f7ed71d 100644 --- a/src/main/java/com/couchbase/intellij/searchworkbench/contributor/GeometryCbsContributor.java +++ b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/GeometryCbsContributor.java @@ -1,5 +1,6 @@ package com.couchbase.intellij.searchworkbench.contributor; +import com.intellij.codeInsight.completion.CompletionResultSet; import com.intellij.json.psi.JsonObject; import java.util.Arrays; @@ -15,7 +16,7 @@ public boolean accept(String parentKey) { } @Override - public void contributeKey(String parentKey, JsonObject jsonObject, List contributors) { + public void contributeKey(String parentKey, JsonObject jsonObject, List contributors, CompletionResultSet result) { ContributorUtil.suggestMissing(jsonObject, keys, contributors); } diff --git a/src/main/java/com/couchbase/intellij/searchworkbench/contributor/HighlightCbsContributor.java b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/HighlightCbsContributor.java index 00e03726..4ff6a38e 100644 --- a/src/main/java/com/couchbase/intellij/searchworkbench/contributor/HighlightCbsContributor.java +++ b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/HighlightCbsContributor.java @@ -1,5 +1,6 @@ package com.couchbase.intellij.searchworkbench.contributor; +import com.intellij.codeInsight.completion.CompletionResultSet; import com.intellij.json.psi.JsonObject; import java.util.Arrays; @@ -15,7 +16,7 @@ public boolean accept(String parentKey) { } @Override - public void contributeKey(String parentKey, JsonObject jsonObject, List contributors) { + public void contributeKey(String parentKey, JsonObject jsonObject, List contributors, CompletionResultSet result) { ContributorUtil.suggestMissing(jsonObject, keys, contributors); } diff --git a/src/main/java/com/couchbase/intellij/searchworkbench/contributor/KnnCbsContributor.java b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/KnnCbsContributor.java index b7bda4db..a5e1847b 100644 --- a/src/main/java/com/couchbase/intellij/searchworkbench/contributor/KnnCbsContributor.java +++ b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/KnnCbsContributor.java @@ -1,5 +1,6 @@ package com.couchbase.intellij.searchworkbench.contributor; +import com.intellij.codeInsight.completion.CompletionResultSet; import com.intellij.json.psi.JsonObject; import java.util.Arrays; @@ -15,7 +16,7 @@ public boolean accept(String parentKey) { } @Override - public void contributeKey(String parentKey, JsonObject jsonObject, List contributors) { + public void contributeKey(String parentKey, JsonObject jsonObject, List contributors, CompletionResultSet result) { ContributorUtil.suggestMissing(jsonObject, keys, contributors); } diff --git a/src/main/java/com/couchbase/intellij/searchworkbench/contributor/LocationCbsContributor.java b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/LocationCbsContributor.java index 885e0ffa..314aae10 100644 --- a/src/main/java/com/couchbase/intellij/searchworkbench/contributor/LocationCbsContributor.java +++ b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/LocationCbsContributor.java @@ -1,5 +1,6 @@ package com.couchbase.intellij.searchworkbench.contributor; +import com.intellij.codeInsight.completion.CompletionResultSet; import com.intellij.json.psi.JsonObject; import java.util.Arrays; @@ -15,7 +16,7 @@ public boolean accept(String parentKey) { } @Override - public void contributeKey(String parentKey, JsonObject jsonObject, List contributors) { + public void contributeKey(String parentKey, JsonObject jsonObject, List contributors, CompletionResultSet result) { ContributorUtil.suggestMissing(jsonObject, keys, contributors); } diff --git a/src/main/java/com/couchbase/intellij/searchworkbench/contributor/QueryCbsContributor.java b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/QueryCbsContributor.java index b0c7609a..a7f17500 100644 --- a/src/main/java/com/couchbase/intellij/searchworkbench/contributor/QueryCbsContributor.java +++ b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/QueryCbsContributor.java @@ -1,5 +1,6 @@ package com.couchbase.intellij.searchworkbench.contributor; +import com.intellij.codeInsight.completion.CompletionResultSet; import com.intellij.json.psi.JsonObject; import com.intellij.json.psi.JsonProperty; @@ -12,6 +13,7 @@ public class QueryCbsContributor implements CBSContributor { public static final List compound = Arrays.asList("disjuncts", "conjuncts"); public static final List query = Arrays.asList("boost", "conjuncts"); public static final List match = Arrays.asList("match", "analyzer", "operator", "boost", "fuzziness", "prefix_length", "field"); + public static final List match_phrase = Arrays.asList("match_phrase", "analyzer", "operator", "boost", "fuzziness", "prefix_length", "field"); public static final List bool = Arrays.asList("bool", "boost", "field"); public static final List prefix = Arrays.asList("prefix", "boost", "field"); public static final List regexp = Arrays.asList("regexp", "boost", "field"); @@ -44,6 +46,7 @@ public class QueryCbsContributor implements CBSContributor { allQueryKeys.addAll(compound); allQueryKeys.addAll(query); allQueryKeys.addAll(match); + allQueryKeys.addAll(match_phrase); allQueryKeys.addAll(bool); allQueryKeys.addAll(prefix); allQueryKeys.addAll(regexp); @@ -66,7 +69,7 @@ public boolean accept(String parentKey) { } @Override - public void contributeKey(String parentKey, JsonObject jsonObject, List contributors) { + public void contributeKey(String parentKey, JsonObject jsonObject, List contributors, CompletionResultSet result) { List tempKeys = new ArrayList<>(jsonObject.getPropertyList().stream() .map(JsonProperty::getName) @@ -81,123 +84,195 @@ public void contributeKey(String parentKey, JsonObject jsonObject, List if (boolQueries.stream().anyMatch(existingKeys::contains)) { contributors.addAll(boolQueries.stream().filter(e -> !existingKeys.contains(e)).toList()); - } else if (compound.stream().anyMatch(existingKeys::contains)) { - contributors.addAll(compound.stream().filter(e -> !existingKeys.contains(e)).toList()); - } else if (!existingKeys.contains("query")) { + addBooleanTemplates(existingKeys, result); + } else if (!existingKeys.contains("query") && !compound.stream().anyMatch(existingKeys::contains)) { HashSet suggestions = new HashSet<>(); if (!existingKeys.contains("field")) { suggestions.add("field"); } if (existingKeys.isEmpty()) { + result.addElement(CBSTemplates.getQueryTemplate(existingKeys)); suggestions.add("query"); + addBooleanTemplates(existingKeys, result); + result.addElement(CBSTemplates.getConjunctsTemplate(existingKeys)); + result.addElement(CBSTemplates.getDisjunctsTemplate(existingKeys)); } - suggestions.addAll(getMatchContributors(existingKeys)); - suggestions.addAll(getBoolContributors(existingKeys)); - suggestions.addAll(getPrefixContributors(existingKeys)); - suggestions.addAll(getRegexpContributors(existingKeys)); - suggestions.addAll(getTermContributors(existingKeys)); - suggestions.addAll(getTermsContributors(existingKeys)); - suggestions.addAll(getWildcardContributors(existingKeys)); - suggestions.addAll(getNumericContributors(existingKeys)); - suggestions.addAll(getDateContributors(existingKeys)); - suggestions.addAll(getCidrContributors(existingKeys)); - suggestions.addAll(getRadiusContributors(existingKeys)); - suggestions.addAll(getRectangleContributors(existingKeys)); - suggestions.addAll(getPolygonContributors(existingKeys)); - suggestions.addAll(getGeometryContributors(existingKeys)); - - - List suggestions2 = suggestions.stream().filter(e -> !existingKeys.contains(e)).toList(); - System.out.println(suggestions2); - contributors.addAll(suggestions2); + suggestions.addAll(getMatchContributors(existingKeys, result)); + suggestions.addAll(getMatchPhraseContributors(existingKeys, result)); + suggestions.addAll(getBoolContributors(existingKeys, result)); + suggestions.addAll(getPrefixContributors(existingKeys, result)); + suggestions.addAll(getRegexpContributors(existingKeys, result)); + suggestions.addAll(getTermContributors(existingKeys, result)); + suggestions.addAll(getTermsContributors(existingKeys, result)); + suggestions.addAll(getWildcardContributors(existingKeys, result)); + suggestions.addAll(getNumericContributors(existingKeys, result)); + suggestions.addAll(getDateContributors(existingKeys, result)); + suggestions.addAll(getCidrContributors(existingKeys, result)); + suggestions.addAll(getRadiusContributors(existingKeys, result)); + suggestions.addAll(getRectangleContributors(existingKeys, result)); + suggestions.addAll(getPolygonContributors(existingKeys, result)); + suggestions.addAll(getGeometryContributors(existingKeys, result)); + + contributors.addAll(suggestions.stream().filter(e -> !existingKeys.contains(e)).toList()); } } + public void addBooleanTemplates(List existingKeys, CompletionResultSet result) { + if (!existingKeys.contains("must")) { + result.addElement(CBSTemplates.getMustTemplate()); + } + + if (!existingKeys.contains("must_not")) { + result.addElement(CBSTemplates.getMustNotTemplate()); + } + + if (!existingKeys.contains("should")) { + result.addElement(CBSTemplates.getShouldTemplate()); + } + } - private List getGeometryContributors(List existingKeys) { - return genericContributor(geometry, existingKeys); + private List getGeometryContributors(List existingKeys, CompletionResultSet result) { + if (geometry.containsAll(existingKeys)) { + result.addElement(CBSTemplates.getPointGeoJsonTemplate(existingKeys)); + result.addElement(CBSTemplates.getLineStringTemplate(existingKeys)); + result.addElement(CBSTemplates.getPolygonTemplate(existingKeys)); + result.addElement(CBSTemplates.getMultiPointTemplate(existingKeys)); + result.addElement(CBSTemplates.getMultiLineStringTemplate(existingKeys)); + result.addElement(CBSTemplates.getMultiPolygonTemplate(existingKeys)); + result.addElement(CBSTemplates.getEnvelopeTemplate(existingKeys)); + result.addElement(CBSTemplates.getCircleTemplate(existingKeys)); + result.addElement(CBSTemplates.getGeometryCollectionTemplate(existingKeys)); + } + + return genericContributor(geometry, existingKeys, result); + } + + private List getPolygonContributors(List existingKeys, CompletionResultSet result) { + return genericContributor(polygon, existingKeys, result, + getSingleTemplate("polygon_query", "Polygon-Based Geopoint Queries\n", new ArrayList<>(polygon))); } - private List getPolygonContributors(List existingKeys) { - return genericContributor(polygon, existingKeys); + private List getRectangleContributors(List existingKeys, CompletionResultSet result) { + if (rectangle.containsAll(existingKeys)) { + result.addElement(CBSTemplates.getRectangleTemplate(existingKeys)); + } + return genericContributor(rectangle, existingKeys, result); } - private List getRectangleContributors(List existingKeys) { - return genericContributor(rectangle, existingKeys); + private List getRadiusContributors(List existingKeys, CompletionResultSet result) { + if (radius.containsAll(existingKeys)) { + result.addElement(CBSTemplates.getRadiusTemplate(existingKeys)); + } + return genericContributor(radius, existingKeys, result); } - private List getRadiusContributors(List existingKeys) { - return genericContributor(radius, existingKeys); + private List genericContributor(List keys, List existingKeys, CompletionResultSet result) { + return genericContributor(keys, existingKeys, result, new ArrayList<>()); } - private List genericContributor(List keys, List existingKeys) { + private List genericContributor(List keys, List existingKeys, + CompletionResultSet result, List templateDefs) { if (keys.containsAll(existingKeys)) { + if (!templateDefs.isEmpty()) { + for (CBSTemplateDef def : templateDefs) { + def.getAttrs().removeAll(existingKeys); + def.getAttrs().remove("boost"); + result.addElement(CBSTemplates.getGenericTemplate(def.getKey(), def.getDesc(), def.getAttrs())); + } + } + return keys; } else { return new ArrayList<>(); } } - private List getMatchContributors(List existingKeys) { - return genericContributor(match, existingKeys); + private List getMatchContributors(List existingKeys, CompletionResultSet result) { + List templates = Arrays.asList( + new CBSTemplateDef("match_query", "Simple match query search", new ArrayList<>(Arrays.asList("field", "match"))), + new CBSTemplateDef("match_query_all", "Match query search with all attributes", new ArrayList<>(match)) + ); + + return genericContributor(match, existingKeys, result, templates); } - private List getBoolContributors(List existingKeys) { - return genericContributor(bool, existingKeys); + private List getMatchPhraseContributors(List existingKeys, CompletionResultSet result) { + List templates = Arrays.asList( + new CBSTemplateDef("match_phrase_query", "Simple match phrase query search", new ArrayList<>(Arrays.asList("field", "match_phrase"))), + new CBSTemplateDef("match_phrase_query_all", "Match phrase query search with all attributes", new ArrayList<>(match_phrase)) + ); + + return genericContributor(match_phrase, existingKeys, result, templates); } - private List getCidrContributors(List existingKeys) { - return genericContributor(cidr, existingKeys); + private List getBoolContributors(List existingKeys, CompletionResultSet result) { + return genericContributor(bool, existingKeys, result, + getSingleTemplate("bool_query", "Boolean query", new ArrayList<>(bool))); } - private List getPrefixContributors(List existingKeys) { - return genericContributor(prefix, existingKeys); + private List getCidrContributors(List existingKeys, CompletionResultSet result) { + return genericContributor(cidr, existingKeys, result, + getSingleTemplate("cidr_query", "CIDR query", new ArrayList<>(cidr))); } - private List getRegexpContributors(List existingKeys) { - return genericContributor(regexp, existingKeys); + private List getPrefixContributors(List existingKeys, CompletionResultSet result) { + return genericContributor(prefix, existingKeys, result, + getSingleTemplate("prefix_query", "Prefix query", new ArrayList<>(prefix))); } - private List getTermContributors(List existingKeys) { - return genericContributor(term, existingKeys); + private List getRegexpContributors(List existingKeys, CompletionResultSet result) { + return genericContributor(regexp, existingKeys, result, + getSingleTemplate("regex_query", "Regex query", new ArrayList<>(regexp))); } - private List getTermsContributors(List existingKeys) { - return genericContributor(terms, existingKeys); + private List getTermContributors(List existingKeys, CompletionResultSet result) { + return genericContributor(term, existingKeys, result, + getSingleTemplate("term_query", "Term query", new ArrayList<>(term))); } - private List getWildcardContributors(List existingKeys) { - return genericContributor(wildcard, existingKeys); + private List getTermsContributors(List existingKeys, CompletionResultSet result) { + return genericContributor(terms, existingKeys, result, + getSingleTemplate("terms_query", "Terms query", new ArrayList<>(terms))); } - private List getNumericContributors(List existingKeys) { - return genericContributor(numeric, existingKeys); + private List getWildcardContributors(List existingKeys, CompletionResultSet result) { + return genericContributor(wildcard, existingKeys, result, + getSingleTemplate("wildcard_query", "Wildcard query", new ArrayList<>(wildcard))); } - private List getDateContributors(List existingKeys) { - return genericContributor(date, existingKeys); + private List getSingleTemplate(String key, String desc, List fields) { + return Arrays.asList( + new CBSTemplateDef(key, desc, new ArrayList<>(fields)) + ); } + private List getNumericContributors(List existingKeys, CompletionResultSet result) { + List templates = Arrays.asList( + new CBSTemplateDef("numeric_range", "Simple numeric range search", new ArrayList<>(Arrays.asList("field", "start", "end"))), + new CBSTemplateDef("numeric_range_all", "Data numeric search with all attributes", new ArrayList<>(numeric)) + ); + return genericContributor(numeric, existingKeys, result, templates); + } - @Override - public void contributeValue(JsonObject jsonObject, String attributeKey, List contributors) { + private List getDateContributors(List existingKeys, CompletionResultSet result) { + List templates = Arrays.asList( + new CBSTemplateDef("date_range", "Simple data range search", new ArrayList<>(Arrays.asList("field", "start", "end"))), + new CBSTemplateDef("date_range_all", "Data range search with all attributes", new ArrayList<>(date)) + ); + return genericContributor(date, existingKeys, result, templates); } - //ContributorUtil.suggestMissing(jsonObject, keys, contributors); + @Override + public void contributeValue(JsonObject jsonObject, String attributeKey, List contributors) { + if ("operator".equals(attributeKey)) { + contributors.add("or"); + contributors.add("and"); + } + } } -// @Override -// public void contributeValue(JsonObject jsonObject, String attributeKey, List contributors) { -//// if ("level".equals(attributeKey)) { -//// contributors.add("at_plus"); -//// contributors.add("not_bounded"); -//// } else if ("results".equals(attributeKey)) { -//// contributors.add("complete"); -//// } -// } - diff --git a/src/main/java/com/couchbase/intellij/searchworkbench/contributor/ShapeCbsContributor.java b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/ShapeCbsContributor.java index ee96f5ec..3f3c6007 100644 --- a/src/main/java/com/couchbase/intellij/searchworkbench/contributor/ShapeCbsContributor.java +++ b/src/main/java/com/couchbase/intellij/searchworkbench/contributor/ShapeCbsContributor.java @@ -1,5 +1,6 @@ package com.couchbase.intellij.searchworkbench.contributor; +import com.intellij.codeInsight.completion.CompletionResultSet; import com.intellij.json.psi.JsonObject; import com.intellij.json.psi.JsonProperty; import com.intellij.json.psi.JsonStringLiteral; @@ -15,7 +16,7 @@ public boolean accept(String parentKey) { } @Override - public void contributeKey(String parentKey, JsonObject jsonObject, List contributors) { + public void contributeKey(String parentKey, JsonObject jsonObject, List contributors, CompletionResultSet result) { String type = null; for (JsonProperty property : jsonObject.getPropertyList()) { diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index b6a38adf..e4584505 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -117,7 +117,6 @@ -