Skip to content

Commit

Permalink
CodeAction to disable validation
Browse files Browse the repository at this point in the history
Fixes redhat-developer#531

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Jan 12, 2022
1 parent da79fdb commit 0d9e039
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
* @author Angelo ZERR
*
*/
public class CommandKind {
public class QuteClientCommandConstants {

private CommandKind() {
private QuteClientCommandConstants() {
}

/**
Expand All @@ -27,4 +27,9 @@ private CommandKind() {

public static final String COMMAND_JAVA_DEFINITION = "qute.command.java.definition";

/**
* Client command to update client configuration settings
*/
public static final String COMMAND_CONFIGURATION_UPDATE = "qute.command.configuration.update";

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
import com.redhat.qute.ls.commons.BadLocationException;
import com.redhat.qute.ls.commons.CodeActionFactory;
import com.redhat.qute.ls.commons.TextDocument;
import com.redhat.qute.ls.commons.client.QuteClientCommandConstants;
import com.redhat.qute.parser.expression.ObjectPart;
import com.redhat.qute.parser.template.Expression;
import com.redhat.qute.parser.template.Node;
import com.redhat.qute.parser.template.NodeKind;
import com.redhat.qute.parser.template.Template;
import com.redhat.qute.services.diagnostics.QuteErrorCode;
import com.redhat.qute.services.diagnostics.TemplateCodeActionFactory;
import com.redhat.qute.settings.SharedSettings;
import com.redhat.qute.utils.QutePositionUtility;

Expand All @@ -52,7 +54,12 @@ public CompletableFuture<List<CodeAction>> doCodeActions(Template template, Code
SharedSettings sharedSettings) {
List<CodeAction> codeActions = new ArrayList<>();
if (context.getDiagnostics() != null) {
boolean canUpdateConfiguration = sharedSettings.getCommandCapabilities()
.isCommandSupported(QuteClientCommandConstants.COMMAND_CONFIGURATION_UPDATE);
for (Diagnostic diagnostic : context.getDiagnostics()) {
if (canUpdateConfiguration) {
codeActions.add(TemplateCodeActionFactory.createValidationEnabledCodeAction(false, diagnostic));
}
if (QuteErrorCode.UndefinedVariable.isQuteErrorCode(diagnostic.getCode())) {
// Manage code action for undefined variable
doCodeActionsForUndefinedVariable(template, diagnostic, codeActions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*******************************************************************************/
package com.redhat.qute.services;

import static com.redhat.qute.ls.commons.client.CommandKind.COMMAND_JAVA_DEFINITION;
import static com.redhat.qute.ls.commons.client.QuteClientCommandConstants.COMMAND_JAVA_DEFINITION;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,12 @@ private void validateWithRealQuteParser(Template template, List<Diagnostic> diag
Position start = new Position(line, e.getOrigin().getLineCharacterStart() - 1);
Position end = new Position(line, e.getOrigin().getLineCharacterEnd() - 1);
Range range = new Range(start, end);
Diagnostic diagnostic = createDiagnostic(range, message, QuteErrorCode.SyntaxError);
Diagnostic diagnostic = createDiagnostic(range, message, DiagnosticSeverity.Error,
QuteErrorCode.SyntaxError);
diagnostics.add(diagnostic);
}
}

private Diagnostic createDiagnostic(Range range, String message, QuteErrorCode errorCode) {
return new Diagnostic(range, message, DiagnosticSeverity.Error, QUTE_SOURCE,
errorCode != null ? errorCode.getCode() : null);
}

private void validateDataModel(Node parent, Template template, ResolvingJavaTypeContext resolvingJavaTypeContext,
ResolutionContext currentContext, List<Diagnostic> diagnostics) {
ResolutionContext previousContext = currentContext;
Expand Down Expand Up @@ -795,7 +791,13 @@ private ResolvedJavaTypeInfo validateIterable(Part part, Section ownerSection,
private static Diagnostic createDiagnostic(Range range, DiagnosticSeverity severity, IQuteErrorCode errorCode,
Object... arguments) {
String message = errorCode.getMessage(arguments);
return new Diagnostic(range, message, severity, QUTE_SOURCE, errorCode.getCode());
return createDiagnostic(range, message, severity, errorCode);
}

private static Diagnostic createDiagnostic(Range range, String message, DiagnosticSeverity severity,
IQuteErrorCode errorCode) {
Diagnostic diagnostic = new Diagnostic(range, message, severity, QUTE_SOURCE,
errorCode != null ? errorCode.getCode() : null);
return diagnostic;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*******************************************************************************
* Copyright (c) 2022 Red Hat Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package com.redhat.qute.services.diagnostics;

import java.util.Collections;

import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.CodeActionKind;
import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.Diagnostic;

import com.redhat.qute.ls.commons.client.ConfigurationItemEdit;
import com.redhat.qute.ls.commons.client.ConfigurationItemEditType;
import com.redhat.qute.ls.commons.client.QuteClientCommandConstants;

/**
* Specific code action factory for Qute.
*
* @author Angelo ZERR
*
*/
public class TemplateCodeActionFactory {

// Enable/Disable Qute validation

private static final String UNASSIGNED_EXCLUDED_SECTION = "quarkus.tools.qute.validation.enabled";

private static final String UNASSIGNED_EXCLUDE_CODE_ACTION_TITLE = "Do you want disable Qute validation?";

private static final String UNASSIGNED_EXCLUDE_COMMAND_TITLE = "Disable Qute validation";

/**
* Returns a code action for <code>diagnostic</code> that causes
* <code>item</code> to be added to
* <code>microprofile.tools.validation.unassigned.excluded</code> client
* configuration
*
* @param item the item to add to the client configuration array
* @param diagnostic the diagnostic for the <code>CodeAction</code>
* @return a code action that causes <code>item</code> to be added to
* <code>microprofile.tools.validation.unassigned.excluded</code> client
* configuration
*/
public static CodeAction createValidationEnabledCodeAction(boolean enabled, Diagnostic diagnostic) {
String codeActionTitle = UNASSIGNED_EXCLUDE_CODE_ACTION_TITLE;
String commandTitle = UNASSIGNED_EXCLUDE_COMMAND_TITLE;
ConfigurationItemEditType editType = ConfigurationItemEditType.update;
return createConfigurationUpdateCodeAction(codeActionTitle, commandTitle, UNASSIGNED_EXCLUDED_SECTION, editType,
enabled, diagnostic);
}

private static CodeAction createConfigurationUpdateCodeAction(String codeActionTitle, String commandTitle,
String section, ConfigurationItemEditType editType, Object item, Diagnostic diagnostic) {
CodeAction updateCodeAction = new CodeAction(codeActionTitle);

ConfigurationItemEdit configItemEdit = new ConfigurationItemEdit(section, editType, item);

Command command = new Command(commandTitle, QuteClientCommandConstants.COMMAND_CONFIGURATION_UPDATE,
Collections.singletonList(configItemEdit));
updateCodeAction.setCommand(command);
updateCodeAction.setKind(CodeActionKind.QuickFix);
updateCodeAction.setDiagnostics(Collections.singletonList(diagnostic));
return updateCodeAction;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public CommandCapabilities getCapabilities() {
* Returns <code>true</code> if the client supports the <code>commandKind</code>
* command. Otherwise, returns <code>false</code>.
*
* See {@link com.redhat.qute.ls.commons.client.CommandKind}
* See {@link com.redhat.qute.ls.commons.client.QuteClientCommandConstants}
*
* @param commandKind the command kind to check for
* @return <code>true</code> if the client supports the <code>commandKind</code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
import com.redhat.qute.ls.commons.BadLocationException;
import com.redhat.qute.ls.commons.TextDocument;
import com.redhat.qute.ls.commons.client.CommandCapabilities;
import com.redhat.qute.ls.commons.client.CommandKind;
import com.redhat.qute.ls.commons.client.QuteClientCommandConstants;
import com.redhat.qute.ls.commons.client.CommandKindCapabilities;
import com.redhat.qute.parser.template.Template;
import com.redhat.qute.parser.template.TemplateParser;
Expand Down Expand Up @@ -506,7 +506,7 @@ public static void testCodeLensFor(String value, String fileUri, String projectU
QuteLanguageService languageService = new QuteLanguageService(new JavaDataModelCache(projectRegistry));
SharedSettings sharedSettings = new SharedSettings();
CommandCapabilities commandCapabilities = new CommandCapabilities();
CommandKindCapabilities kinds = new CommandKindCapabilities(Arrays.asList(CommandKind.COMMAND_JAVA_DEFINITION));
CommandKindCapabilities kinds = new CommandKindCapabilities(Arrays.asList(QuteClientCommandConstants.COMMAND_JAVA_DEFINITION));
commandCapabilities.setCommandKind(kinds);
sharedSettings.getCommandCapabilities().setCapabilities(commandCapabilities);
List<? extends CodeLens> actual = languageService.getCodeLens(template, sharedSettings, () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import org.junit.jupiter.api.Test;

import com.redhat.qute.ls.commons.client.CommandKind;
import com.redhat.qute.ls.commons.client.QuteClientCommandConstants;

/**
* Tests for Qute code lens.
Expand All @@ -39,19 +39,19 @@ public void checkedTemplateMatching() throws Exception {
// Display information about data model (classes and parameters) as codelens.
String value = "";
testCodeLensFor(value, "src/main/resources/templates/ItemResource/items.qute.html", //
cl(r(0, 0, 0, 0), "ItemResource$Templates#items(...)", CommandKind.COMMAND_JAVA_DEFINITION), //
cl(r(0, 0, 0, 0), "items : List<Item>", CommandKind.COMMAND_JAVA_DEFINITION));
cl(r(0, 0, 0, 0), "ItemResource$Templates#items(...)", QuteClientCommandConstants.COMMAND_JAVA_DEFINITION), //
cl(r(0, 0, 0, 0), "items : List<Item>", QuteClientCommandConstants.COMMAND_JAVA_DEFINITION));

testCodeLensFor(value, "src/main/resources/templates/ItemResource/items", //
cl(r(0, 0, 0, 0), "ItemResource$Templates#items(...)", CommandKind.COMMAND_JAVA_DEFINITION), //
cl(r(0, 0, 0, 0), "items : List<Item>", CommandKind.COMMAND_JAVA_DEFINITION));
cl(r(0, 0, 0, 0), "ItemResource$Templates#items(...)", QuteClientCommandConstants.COMMAND_JAVA_DEFINITION), //
cl(r(0, 0, 0, 0), "items : List<Item>", QuteClientCommandConstants.COMMAND_JAVA_DEFINITION));

testCodeLensFor(value, "src/main/resources/templates/ItemResource/items.qute", //
cl(r(0, 0, 0, 0), "ItemResource$Templates#items(...)", CommandKind.COMMAND_JAVA_DEFINITION), //
cl(r(0, 0, 0, 0), "items : List<Item>", CommandKind.COMMAND_JAVA_DEFINITION));
cl(r(0, 0, 0, 0), "ItemResource$Templates#items(...)", QuteClientCommandConstants.COMMAND_JAVA_DEFINITION), //
cl(r(0, 0, 0, 0), "items : List<Item>", QuteClientCommandConstants.COMMAND_JAVA_DEFINITION));

testCodeLensFor(value, "src/main/resources/templates/ItemResource/items.html", //
cl(r(0, 0, 0, 0), "ItemResource$Templates#items(...)", CommandKind.COMMAND_JAVA_DEFINITION), //
cl(r(0, 0, 0, 0), "items : List<Item>", CommandKind.COMMAND_JAVA_DEFINITION));
cl(r(0, 0, 0, 0), "ItemResource$Templates#items(...)", QuteClientCommandConstants.COMMAND_JAVA_DEFINITION), //
cl(r(0, 0, 0, 0), "items : List<Item>", QuteClientCommandConstants.COMMAND_JAVA_DEFINITION));
}
}

0 comments on commit 0d9e039

Please sign in to comment.