use of com.redhat.qute.ls.commons.TextDocument in project quarkus-ls by redhat-developer.
the class QuteCodeActions method doCodeActionsForUndefinedObject.
private static void doCodeActionsForUndefinedObject(Template template, Diagnostic diagnostic, QuteErrorCode errorCode, List<CodeAction> codeActions) {
try {
String varName = null;
boolean isIterable = false;
JsonObject data = (JsonObject) diagnostic.getData();
if (data != null) {
varName = data.get(DIAGNOSTIC_DATA_NAME).getAsString();
isIterable = data.get(DIAGNOSTIC_DATA_ITERABLE).getAsBoolean();
} else {
int offset = template.offsetAt(diagnostic.getRange().getStart());
Node node = template.findNodeAt(offset);
node = QutePositionUtility.findBestNode(offset, node);
if (node.getKind() == NodeKind.Expression) {
Expression expression = (Expression) node;
ObjectPart part = expression.getObjectPart();
if (part != null) {
varName = part.getPartName();
}
}
}
if (varName != null) {
TextDocument document = template.getTextDocument();
String lineDelimiter = document.lineDelimiter(0);
String title = MessageFormat.format(UNDEFINED_OBJECT_CODEACTION_TITLE, varName);
Position position = new Position(0, 0);
StringBuilder insertText = new StringBuilder("{@");
if (isIterable) {
insertText.append("java.util.List");
} else {
insertText.append("java.lang.String");
}
insertText.append(" ");
insertText.append(varName);
insertText.append("}");
insertText.append(lineDelimiter);
CodeAction insertParameterDeclarationQuickFix = CodeActionFactory.insert(title, position, insertText.toString(), document, diagnostic);
codeActions.add(insertParameterDeclarationQuickFix);
// CodeAction to set validation severity to ignore
doCodeActionToSetIgnoreSeverity(template, Collections.singletonList(diagnostic), errorCode, codeActions, UNDEFINED_OBJECT_SEVERITY_SETTING);
}
} catch (BadLocationException e) {
}
}
use of com.redhat.qute.ls.commons.TextDocument in project quarkus-ls by redhat-developer.
the class QuteAssert method testCodeActionsFor.
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;
if (offset != -1) {
value = value.substring(0, offset) + value.substring(offset + 1);
}
TextDocument document = new TextDocument(value.toString(), FILE_URI);
if (offset != -1) {
Position position = document.positionAt(offset);
range = new Range(position, position);
} else {
range = diagnostic.getRange();
}
CodeActionContext context = new CodeActionContext();
context.setDiagnostics(Arrays.asList(diagnostic));
QuteProjectRegistry projectRegistry = new MockQuteProjectRegistry();
Template template = createTemplate(value, fileUri, projectUri, templateBaseDir, projectRegistry);
QuteLanguageService languageService = new QuteLanguageService(new JavaDataModelCache(projectRegistry));
List<CodeAction> actual = languageService.doCodeActions(template, context, range, settings).get();
assertCodeActions(actual, expected);
}
Aggregations