Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CodeAction to disable validation #535

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,25 @@ public static CodeAction createFile(String title, String docURI, String content,
*/
public static CodeAction createCommand(String title, String commandId, List<Object> commandParams,
Diagnostic diagnostic) {
return createCommand(title, commandId, commandParams, Collections.singletonList(diagnostic));
}

/**
* Makes a CodeAction to call a command from the available server commands.
*
* @param title The displayed name of the CodeAction
* @param commandId The id of the given command to add as CodeAction
* @param commandParams The document URI of the document the command is called
* on
* @param diagnostics The diagnostic list that this CodeAction will fix
*/
public static CodeAction createCommand(String title, String commandId, List<Object> commandParams,
List<Diagnostic> diagnostics) {
CodeAction codeAction = new CodeAction(title);
Command command = new Command(title, commandId, commandParams);
codeAction.setCommand(command);
codeAction.setDiagnostics(Collections.singletonList(diagnostic));
codeAction.setDiagnostics(diagnostics);
codeAction.setKind(CodeActionKind.QuickFix);

return codeAction;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
*******************************************************************************/
package com.redhat.qute.services;

import static com.redhat.qute.ls.commons.CodeActionFactory.createCommand;
import static com.redhat.qute.services.diagnostics.QuteDiagnosticContants.DIAGNOSTIC_DATA_ITERABLE;
import static com.redhat.qute.services.diagnostics.QuteDiagnosticContants.DIAGNOSTIC_DATA_NAME;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;

Expand All @@ -29,11 +31,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.ConfigurationItemEdit;
import com.redhat.qute.ls.commons.client.ConfigurationItemEditType;
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.commands.QuteClientCommandConstants;
import com.redhat.qute.services.diagnostics.QuteErrorCode;
import com.redhat.qute.settings.SharedSettings;
import com.redhat.qute.utils.QutePositionUtility;
Expand All @@ -46,15 +51,37 @@
*/
class QuteCodeActions {

private static final String DECLARE_UNDEFINED_VARIABLE_MESSAGE = "Declare `{0}` with parameter declaration.";
private static final String DECLARE_UNDEFINED_VARIABLE_TITLE = "Declare `{0}` with parameter declaration.";

// Enable/Disable Qute validation

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

private static final String DISABLE_VALIDATION_TITLE = "Disable Qute validation.";

public CompletableFuture<List<CodeAction>> doCodeActions(Template template, CodeActionContext context, Range range,
SharedSettings sharedSettings) {
List<CodeAction> codeActions = new ArrayList<>();
if (context.getDiagnostics() != null) {
for (Diagnostic diagnostic : context.getDiagnostics()) {
List<Diagnostic> diagnostics = context.getDiagnostics();
if (diagnostics != null && !diagnostics.isEmpty()) {
boolean canUpdateConfiguration = sharedSettings.getCommandCapabilities()
.isCommandSupported(QuteClientCommandConstants.COMMAND_CONFIGURATION_UPDATE);
if (canUpdateConfiguration) {
// For each error, we provide the following quick fix:
//
// "Disable Qute validation."
//
// which will update the setting on client side to disable the Qute validation.
doCodeActionToDisableValidation(diagnostics, codeActions);
}
for (Diagnostic diagnostic : diagnostics) {
if (QuteErrorCode.UndefinedVariable.isQuteErrorCode(diagnostic.getCode())) {
// Manage code action for undefined variable
// The following Qute template:
// {undefinedVariable}
//
// will provide a quickfix like:
//
// Declare `undefinedVariable` with parameter declaration."
doCodeActionsForUndefinedVariable(template, diagnostic, codeActions);
}
}
Expand Down Expand Up @@ -88,7 +115,7 @@ private void doCodeActionsForUndefinedVariable(Template template, Diagnostic dia
TextDocument document = template.getTextDocument();
String lineDelimiter = document.lineDelimiter(0);

String title = MessageFormat.format(DECLARE_UNDEFINED_VARIABLE_MESSAGE, varName);
String title = MessageFormat.format(DECLARE_UNDEFINED_VARIABLE_TITLE, varName);

Position position = new Position(0, 0);

Expand All @@ -103,15 +130,37 @@ private void doCodeActionsForUndefinedVariable(Template template, Diagnostic dia
insertText.append("}");
insertText.append(lineDelimiter);

CodeAction insertParameterDeclaration = CodeActionFactory.insert(title, position, insertText.toString(),
document, diagnostic);
codeActions.add(insertParameterDeclaration);
CodeAction insertParameterDeclarationQuickFix = CodeActionFactory.insert(title, position,
insertText.toString(), document, diagnostic);
codeActions.add(insertParameterDeclarationQuickFix);
}

} catch (BadLocationException e) {

}
}

public void doCodeActionToDisableValidation(List<Diagnostic> diagnostics, List<CodeAction> codeActions) {
CodeAction disableValidationQuickFix = createConfigurationUpdateCodeAction(DISABLE_VALIDATION_TITLE,
QUTE_VALIDATION_ENABLED_SECTION, false, ConfigurationItemEditType.update, diagnostics);
codeActions.add(disableValidationQuickFix);
}

/**
* Create the configuration update (done on client side) quick fix.
*
* @param title the displayed name of the QuickFix.
* @param sectionName the section name of the settings to update.
* @param item the section value of the settings to update.
* @param editType the configuration edit type.
* @param diagnostic the diagnostic list that this CodeAction will fix.
*
* @return the configuration update (done on client side) quick fix.
*/
private static CodeAction createConfigurationUpdateCodeAction(String title, String sectionName, Object sectionValue,
ConfigurationItemEditType editType, List<Diagnostic> diagnostics) {
ConfigurationItemEdit configItemEdit = new ConfigurationItemEdit(sectionName, editType, sectionValue);
return createCommand(title, QuteClientCommandConstants.COMMAND_CONFIGURATION_UPDATE,
Collections.singletonList(configItemEdit), diagnostics);
}
}
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.services.commands.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
Expand Up @@ -7,24 +7,33 @@
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package com.redhat.qute.ls.commons.client;
package com.redhat.qute.services.commands;

/**
* Commonly used client commands
* Qute command IDs available on LSP client side.
*
* @author Angelo ZERR
*
*/
public class CommandKind {
public class QuteClientCommandConstants {

private CommandKind() {
private QuteClientCommandConstants() {
}

/**
* Client command to open URI
* Client command to open Qute template by file Uri.
*/
public static final String COMMAND_OPEN_URI = "qute.command.open.uri";

/**
* Client command to go to the definition of Java data model (field, method,
* method invocation of "data" method).
*/
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 @@ -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.services.commands.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,14 +66,14 @@
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.CommandKindCapabilities;
import com.redhat.qute.parser.template.Template;
import com.redhat.qute.parser.template.TemplateParser;
import com.redhat.qute.project.MockQuteProjectRegistry;
import com.redhat.qute.project.QuteProjectRegistry;
import com.redhat.qute.project.datamodel.JavaDataModelCache;
import com.redhat.qute.services.QuteLanguageService;
import com.redhat.qute.services.commands.QuteClientCommandConstants;
import com.redhat.qute.services.diagnostics.IQuteErrorCode;
import com.redhat.qute.services.diagnostics.ResolvingJavaTypeContext;
import com.redhat.qute.settings.QuteCompletionSettings;
Expand Down Expand Up @@ -506,7 +506,8 @@ 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 Expand Up @@ -545,11 +546,16 @@ public static void assertCodeLens(List<? extends CodeLens> actual, CodeLens... e

public static void testCodeActionsFor(String value, Diagnostic diagnostic, CodeAction... expected)
throws Exception {
testCodeActionsFor(value, diagnostic, FILE_URI, PROJECT_URI, TEMPLATE_BASE_DIR, expected);
testCodeActionsFor(value, diagnostic, new SharedSettings(), expected);
}

public static void testCodeActionsFor(String value, Diagnostic diagnostic, String fileUri, String projectUri,
String templateBaseDir, CodeAction... expected) throws Exception {
public static void testCodeActionsFor(String value, Diagnostic diagnostic, SharedSettings settings,
CodeAction... expected) throws Exception {
testCodeActionsFor(value, diagnostic, FILE_URI, PROJECT_URI, TEMPLATE_BASE_DIR, settings, expected);
}

private static void testCodeActionsFor(String value, Diagnostic diagnostic, String fileUri, String projectUri,
String templateBaseDir, SharedSettings settings, CodeAction... expected) throws Exception {
int offset = value.indexOf('|');
Range range = null;

Expand All @@ -572,7 +578,7 @@ public static void testCodeActionsFor(String value, Diagnostic diagnostic, Strin
Template template = createTemplate(value, fileUri, projectUri, templateBaseDir, projectRegistry);
QuteLanguageService languageService = new QuteLanguageService(new JavaDataModelCache(projectRegistry));

List<CodeAction> actual = languageService.doCodeActions(template, context, range, new SharedSettings()).get();
List<CodeAction> actual = languageService.doCodeActions(template, context, range, settings).get();
assertCodeActions(actual, expected);
}

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.services.commands.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));
}
}
Loading