Skip to content

Commit

Permalink
Added Qute CodeAction(s) for similar text suggestions for UnknownProp…
Browse files Browse the repository at this point in the history
…erty

Signed-off-by: Alexander Chen <alchen@redhat.com>
  • Loading branch information
Alexander Chen committed Aug 4, 2022
1 parent 03b06df commit eb2918c
Showing 1 changed file with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
import static com.redhat.qute.ls.commons.CodeActionFactory.createCodeActionWithData;

import java.text.MessageFormat;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.Diagnostic;
Expand All @@ -29,15 +32,18 @@
import com.redhat.qute.ls.api.QuteTemplateGenerateMissingJavaMember;
import com.redhat.qute.ls.commons.BadLocationException;
import com.redhat.qute.parser.expression.Part;
import com.redhat.qute.parser.expression.PropertyPart;
import com.redhat.qute.parser.template.Node;
import com.redhat.qute.parser.template.Template;
import com.redhat.qute.project.datamodel.JavaDataModelCache;
import com.redhat.qute.services.diagnostics.QuteErrorCode;
import com.redhat.qute.services.nativemode.JavaTypeAccessibiltyRule;
import com.redhat.qute.services.nativemode.JavaTypeFilter;
import com.redhat.qute.settings.SharedSettings;

/**
* Code actions for {@link QuteErrorCode#UnknownProperty}.
*
*
* @author Angelo ZERR
*
*/
Expand Down Expand Up @@ -70,12 +76,14 @@ public void doCodeActions(CodeActionRequest request, List<CompletableFuture<Void
QuteTemplateGenerateMissingJavaMember resolver = request.getResolver();
SharedSettings sharedSettings = request.getSharedSettings();

Part propertyPart = (Part) request.getCoveredNode();
Part part = (Part) node;
ResolvedJavaTypeInfo baseResolvedType = request.getJavaTypeOfCoveredNode(javaCache);

if (baseResolvedType != null) {
doCodeActionToCreateProperty(propertyPart, baseResolvedType, template, diagnostic, resolver,
sharedSettings, codeActionResolveFutures, codeActions);
doCodeActionToCreateProperty(part, baseResolvedType, template, diagnostic, resolver, sharedSettings,
codeActionResolveFutures, codeActions);

doCodeActionsForSimilarValues((PropertyPart) part, template, diagnostic, baseResolvedType, codeActions);
}

} catch (BadLocationException e) {
Expand Down Expand Up @@ -153,4 +161,46 @@ private static void doCodeActionToCreateProperty(Part propertyPart, ResolvedJava

}

private void doCodeActionsForSimilarValues(PropertyPart part, Template template, Diagnostic diagnostic,
ResolvedJavaTypeInfo baseResolvedType, List<CodeAction> codeActions) throws BadLocationException {
Collection<String> availableValues = collectAvailableValuesForPropertyPart(part, template, baseResolvedType);
doCodeActionsForSimilarValues(part, availableValues, template, diagnostic, codeActions);
}

private Collection<String> collectAvailableValuesForPropertyPart(PropertyPart node, Template template,
ResolvedJavaTypeInfo baseResolvedType) {
Collection<String> availableValues = new HashSet<>();
String projectUri = template.getProjectUri();

List<String> javaFieldNames = baseResolvedType.getFields().stream().map(x -> x.getName())
.collect(Collectors.toList());
for (String fieldName : javaFieldNames) {
availableValues.add(fieldName);
}

JavaTypeFilter filter = javaCache.getJavaTypeFilter(projectUri, null);
JavaTypeAccessibiltyRule javaTypeAccessibility = filter.getJavaTypeAccessibility(baseResolvedType,
template.getJavaTypesSupportedInNativeMode());

if (!isIgnoreSuperclasses(baseResolvedType, javaTypeAccessibility, filter)) {
List<String> extendedTypes = baseResolvedType.getExtendedTypes();
if (extendedTypes != null) {
for (String extendedType : extendedTypes) {
ResolvedJavaTypeInfo resolvedExtendedType = javaCache.resolveJavaType(extendedType, projectUri)
.getNow(null);
if (resolvedExtendedType != null) {
collectAvailableValuesForPropertyPart(node, template, resolvedExtendedType);
}
}
}
}

return availableValues;
}

private static boolean isIgnoreSuperclasses(ResolvedJavaTypeInfo baseType,
JavaTypeAccessibiltyRule javaTypeAccessibility, JavaTypeFilter filter) {
return filter != null && filter.isIgnoreSuperclasses(baseType, javaTypeAccessibility);
}

}

0 comments on commit eb2918c

Please sign in to comment.