Search in sources :

Example 1 with TextDocument

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) {
    }
}
Also used : TextDocument(com.redhat.qute.ls.commons.TextDocument) ObjectPart(com.redhat.qute.parser.expression.ObjectPart) Expression(com.redhat.qute.parser.template.Expression) Position(org.eclipse.lsp4j.Position) Node(com.redhat.qute.parser.template.Node) CodeAction(org.eclipse.lsp4j.CodeAction) JsonObject(com.google.gson.JsonObject) BadLocationException(com.redhat.qute.ls.commons.BadLocationException)

Example 2 with TextDocument

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);
}
Also used : JavaDataModelCache(com.redhat.qute.project.datamodel.JavaDataModelCache) TextDocument(com.redhat.qute.ls.commons.TextDocument) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Position(org.eclipse.lsp4j.Position) QuteProjectRegistry(com.redhat.qute.project.QuteProjectRegistry) MockQuteProjectRegistry(com.redhat.qute.project.MockQuteProjectRegistry) CodeAction(org.eclipse.lsp4j.CodeAction) QuteLanguageService(com.redhat.qute.services.QuteLanguageService) Range(org.eclipse.lsp4j.Range) MockQuteProjectRegistry(com.redhat.qute.project.MockQuteProjectRegistry) Template(com.redhat.qute.parser.template.Template)

Aggregations

TextDocument (com.redhat.qute.ls.commons.TextDocument)2 CodeAction (org.eclipse.lsp4j.CodeAction)2 Position (org.eclipse.lsp4j.Position)2 JsonObject (com.google.gson.JsonObject)1 BadLocationException (com.redhat.qute.ls.commons.BadLocationException)1 ObjectPart (com.redhat.qute.parser.expression.ObjectPart)1 Expression (com.redhat.qute.parser.template.Expression)1 Node (com.redhat.qute.parser.template.Node)1 Template (com.redhat.qute.parser.template.Template)1 MockQuteProjectRegistry (com.redhat.qute.project.MockQuteProjectRegistry)1 QuteProjectRegistry (com.redhat.qute.project.QuteProjectRegistry)1 JavaDataModelCache (com.redhat.qute.project.datamodel.JavaDataModelCache)1 QuteLanguageService (com.redhat.qute.services.QuteLanguageService)1 CodeActionContext (org.eclipse.lsp4j.CodeActionContext)1 Range (org.eclipse.lsp4j.Range)1