Skip to content

Commit

Permalink
Ignore 'UndefinedObject' problem code action should appear only if LSP
Browse files Browse the repository at this point in the history
client can support update configuration.

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Apr 20, 2023
1 parent f185bd5 commit 3576bda
Show file tree
Hide file tree
Showing 14 changed files with 491 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,24 @@ public void update(List<TextDocumentContentChangeEvent> changes) {
TextDocumentContentChangeEvent changeEvent = changes.get(i);
Range range = changeEvent.getRange();
int length = 0;

int startOffset = -1;
if (range != null) {
length = changeEvent.getRangeLength().intValue();
if (changeEvent.getRangeLength() != null) {
// rangeLength is defined, use it
length = changeEvent.getRangeLength().intValue();
} else {
// rangeLength is not defined, compute it
startOffset = offsetAt(range.getStart());
int endOffset = offsetAt(range.getEnd());
length = endOffset - startOffset;
}
} else {
// range is optional and if not given, the whole file content is replaced
length = buffer.length();
range = new Range(positionAt(0), positionAt(length));
}
String text = changeEvent.getText();
int startOffset = offsetAt(range.getStart());
startOffset = startOffset != 1 ? startOffset : offsetAt(range.getStart());
buffer.replace(startOffset, startOffset + length, text);
lineTracker.replace(startOffset, length, text);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.redhat.qute.parser.template.Node;
import com.redhat.qute.parser.template.Template;
import com.redhat.qute.project.QuteProject;
import com.redhat.qute.services.commands.QuteClientCommandConstants;
import com.redhat.qute.services.diagnostics.QuteErrorCode;

/**
Expand Down Expand Up @@ -55,10 +56,13 @@ public void doCodeActions(CodeActionRequest request, List<CompletableFuture<Void
// CodeAction(s) to replace text with similar suggestions
doCodeActionsForSimilarValues(part, template, diagnostic, codeActions);

// CodeAction to set validation severity to ignore
doCodeActionToSetIgnoreSeverity(template, diagnostic, QuteErrorCode.UndefinedNamespace, codeActions,
UNDEFINED_NAMESPACE_SEVERITY_SETTING);

boolean canUpdateConfiguration = request.getSharedSettings().getCommandCapabilities()
.isCommandSupported(QuteClientCommandConstants.COMMAND_CONFIGURATION_UPDATE);
if (canUpdateConfiguration) {
// CodeAction to set validation severity to ignore
doCodeActionToSetIgnoreSeverity(template, diagnostic, QuteErrorCode.UndefinedNamespace, codeActions,
UNDEFINED_NAMESPACE_SEVERITY_SETTING);
}
} catch (BadLocationException e) {
LOGGER.log(Level.SEVERE, "Creation of undefined namespace code action failed", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.redhat.qute.project.datamodel.ExtendedDataModelParameter;
import com.redhat.qute.project.datamodel.ExtendedDataModelTemplate;
import com.redhat.qute.project.datamodel.resolvers.ValueResolver;
import com.redhat.qute.services.commands.QuteClientCommandConstants;
import com.redhat.qute.services.diagnostics.QuteErrorCode;
import com.redhat.qute.utils.StringUtils;
import com.redhat.qute.utils.UserTagUtils;
Expand Down Expand Up @@ -83,9 +84,13 @@ public void doCodeActions(CodeActionRequest request, List<CompletableFuture<Void
// CodeAction to append ?? to object to make it optional
doCodeActionToAddOptionalSuffix(template, diagnostic, codeActions);

// CodeAction to set validation severity to ignore
doCodeActionToSetIgnoreSeverity(template, diagnostic, QuteErrorCode.UndefinedObject, codeActions,
UNDEFINED_OBJECT_SEVERITY_SETTING);
boolean canUpdateConfiguration = request.getSharedSettings().getCommandCapabilities()
.isCommandSupported(QuteClientCommandConstants.COMMAND_CONFIGURATION_UPDATE);
if (canUpdateConfiguration) {
// CodeAction to set validation severity to ignore
doCodeActionToSetIgnoreSeverity(template, diagnostic, QuteErrorCode.UndefinedObject, codeActions,
UNDEFINED_OBJECT_SEVERITY_SETTING);
}

} catch (BadLocationException e) {
LOGGER.log(Level.SEVERE, "Creation of undefined object code action failed", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public class QuteAssert {
*/;

public static final int SECTION_SNIPPET_SIZE = 15 /* #each, #for, ... #fragment ... */ + USER_TAG_SIZE;

public static final int RESOLVERS_SIZE = 9;

public static String getFileUri(String templateFile) {
Expand Down Expand Up @@ -606,7 +606,7 @@ private static void testCodeLensFor(String value, String fileUri, String templat
template.setTemplateId(templateId);

QuteLanguageService languageService = new QuteLanguageService(projectRegistry);
SharedSettings sharedSettings = createSharedSettings();
SharedSettings sharedSettings = createSharedSettings(false);
List<? extends CodeLens> actual = languageService.getCodeLens(template, sharedSettings, () -> {
}).get();
assertCodeLens(actual, expected);
Expand Down Expand Up @@ -647,7 +647,7 @@ private static void testInlayHintFor(String value, String fileUri, String templa
Template template = createTemplate(value, fileUri, projectUri, templateBaseDir, projectRegistry);
template.setTemplateId(templateId);

SharedSettings settings = createSharedSettings();
SharedSettings settings = createSharedSettings(false);
if (inlayHintSettings != null) {
settings.getInlayHintSettings().update(inlayHintSettings);
}
Expand Down Expand Up @@ -689,9 +689,14 @@ public static void assertInlayHint(List<? extends InlayHint> actual, InlayHint..

// ------------------- CodeAction assert

public static void testCodeActionsWithConfigurationUpdateFor(String value, Diagnostic diagnostic, CodeAction... expected)
throws Exception {
testCodeActionsFor(value, diagnostic, createSharedSettings(true), expected);
}

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

public static void testCodeActionsFor(String value, Diagnostic diagnostic, SharedSettings settings,
Expand Down Expand Up @@ -1022,12 +1027,16 @@ private static Template createTemplate(String value, String fileUri, String proj
return template;
}

private static SharedSettings createSharedSettings() {
public static SharedSettings createSharedSettings(boolean withConfigurationUpdate) {
SharedSettings sharedSettings = new SharedSettings();
CommandCapabilities commandCapabilities = new CommandCapabilities();
CommandKindCapabilities kinds = new CommandKindCapabilities(
Arrays.asList(QuteClientCommandConstants.COMMAND_JAVA_DEFINITION,
QuteClientCommandConstants.COMMAND_SHOW_REFERENCES));
withConfigurationUpdate ?
Arrays.asList(QuteClientCommandConstants.COMMAND_JAVA_DEFINITION,
QuteClientCommandConstants.COMMAND_SHOW_REFERENCES,
QuteClientCommandConstants.COMMAND_CONFIGURATION_UPDATE) :
Arrays.asList(QuteClientCommandConstants.COMMAND_JAVA_DEFINITION,
QuteClientCommandConstants.COMMAND_SHOW_REFERENCES));
commandCapabilities.setCommandKind(kinds);
sharedSettings.getCommandCapabilities().setCapabilities(commandCapabilities);
return sharedSettings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static com.redhat.qute.QuteAssert.d;
import static com.redhat.qute.QuteAssert.te;
import static com.redhat.qute.QuteAssert.testCodeActionsFor;
import static com.redhat.qute.QuteAssert.testCodeActionsWithConfigurationUpdateFor;
import static com.redhat.qute.QuteAssert.testDiagnosticsFor;

import org.eclipse.lsp4j.Diagnostic;
Expand All @@ -27,7 +28,8 @@
import com.redhat.qute.services.diagnostics.QuteErrorCode;

/**
* Test code action for similar text suggestions for {@link QuteErrorCode#UndefinedNamespace}.
* Test code action for similar text suggestions for
* {@link QuteErrorCode#UndefinedNamespace}.
*
*/
public class QuteCodeActionForSimilarTextSuggestionsForUndefinedNamespaceTest {
Expand All @@ -42,12 +44,26 @@ public void similarTextSuggestionQuickFixForUndefinedNamespace() throws Exceptio

testDiagnosticsFor(template, d);
testCodeActionsFor(template, d, //
ca(d, te(0, 1, 0, 5, "inject")));
testCodeActionsWithConfigurationUpdateFor(template, d, //
ca(d, te(0, 1, 0, 5, "inject")), //
ca(d, c("Ignore `UndefinedNamespace` problem.", //
QuteClientCommandConstants.COMMAND_CONFIGURATION_UPDATE, //
"qute.validation.undefinedNamespace.severity", //
"test.qute", //
ConfigurationItemEditType.update, "ignore", //
d)),
ca(d, c("Exclude this file from validation.", //
QuteClientCommandConstants.COMMAND_CONFIGURATION_UPDATE, //
"qute.validation.excluded", //
"test.qute", //
ConfigurationItemEditType.add, "test.qute", //
d)),
ca(d, c("Disable Qute validation for the `qute-quickstart` project.", //
QuteClientCommandConstants.COMMAND_CONFIGURATION_UPDATE, //
"qute.validation.enabled", //
"test.qute", //
ConfigurationItemEditType.update, false, //
d)));
}

Expand All @@ -60,13 +76,26 @@ public void noSimilarTextSuggestionQuickFixForUndefinedNamespace() throws Except
DiagnosticSeverity.Warning);

testDiagnosticsFor(template, d);
testCodeActionsFor(template, d, //
testCodeActionsFor(template, d);
testCodeActionsWithConfigurationUpdateFor(template, d, //
ca(d, c("Ignore `UndefinedNamespace` problem.", //
QuteClientCommandConstants.COMMAND_CONFIGURATION_UPDATE, //
"qute.validation.undefinedNamespace.severity", //
"test.qute", //
ConfigurationItemEditType.update, "ignore", //
d)));
d)),
ca(d, c("Exclude this file from validation.", //
QuteClientCommandConstants.COMMAND_CONFIGURATION_UPDATE, //
"qute.validation.excluded", //
"test.qute", //
ConfigurationItemEditType.add, "test.qute", //
d)),
ca(d, c("Disable Qute validation for the `qute-quickstart` project.", //
QuteClientCommandConstants.COMMAND_CONFIGURATION_UPDATE, //
"qute.validation.enabled", //
"test.qute", //
ConfigurationItemEditType.update, false, //
d)));
}

}
Loading

0 comments on commit 3576bda

Please sign in to comment.