Search in sources :

Example 56 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project sts4 by spring-projects.

the class ValueCompletionProcessor method provideCompletions.

@Override
public Collection<ICompletionProposal> provideCompletions(ASTNode node, Annotation annotation, ITypeBinding type, int offset, IDocument doc) {
    List<ICompletionProposal> result = new ArrayList<>();
    try {
        FuzzyMap<PropertyInfo> index = indexProvider.getIndex(doc);
        // case: @Value(<*>)
        if (node == annotation && doc.get(offset - 1, 2).endsWith("()")) {
            List<Match<PropertyInfo>> matches = findMatches("", index);
            for (Match<PropertyInfo> match : matches) {
                DocumentEdits edits = new DocumentEdits(doc);
                edits.replace(offset, offset, "\"${" + match.data.getId() + "}\"");
                ValuePropertyKeyProposal proposal = new ValuePropertyKeyProposal(edits, match.data.getId(), match.data.getName(), null);
                result.add(proposal);
            }
        } else // case: @Value(prefix<*>)
        if (node instanceof SimpleName && node.getParent() instanceof Annotation) {
            computeProposalsForSimpleName(node, result, offset, doc, index);
        } else // case: @Value(value=<*>)
        if (node instanceof SimpleName && node.getParent() instanceof MemberValuePair && "value".equals(((MemberValuePair) node.getParent()).getName().toString())) {
            computeProposalsForSimpleName(node, result, offset, doc, index);
        } else // case: @Value("prefix<*>")
        if (node instanceof StringLiteral && node.getParent() instanceof Annotation) {
            if (node.toString().startsWith("\"") && node.toString().endsWith("\"")) {
                computeProposalsForStringLiteral(node, result, offset, doc, index);
            }
        } else // case: @Value(value="prefix<*>")
        if (node instanceof StringLiteral && node.getParent() instanceof MemberValuePair && "value".equals(((MemberValuePair) node.getParent()).getName().toString())) {
            if (node.toString().startsWith("\"") && node.toString().endsWith("\"")) {
                computeProposalsForStringLiteral(node, result, offset, doc, index);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
Also used : DocumentEdits(org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) Annotation(org.eclipse.jdt.core.dom.Annotation) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException) Match(org.springframework.ide.vscode.commons.util.FuzzyMap.Match) MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) PropertyInfo(org.springframework.ide.vscode.boot.metadata.PropertyInfo)

Example 57 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project sts4 by spring-projects.

the class WebfluxRouterSymbolProvider method extractPath.

private WebfluxRouteElement[] extractPath(MethodInvocation routerInvocation, TextDocument doc) {
    WebfluxPathFinder pathFinder = new WebfluxPathFinder(routerInvocation, doc);
    List<?> arguments = routerInvocation.arguments();
    for (Object argument : arguments) {
        if (argument != null && argument instanceof ASTNode) {
            ((ASTNode) argument).accept(pathFinder);
        }
    }
    List<WebfluxRouteElement> path = pathFinder.getPath();
    extractNestedValue(routerInvocation, path, (methodInvocation) -> {
        IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
        String methodName = methodBinding.getName();
        try {
            if (WebfluxUtils.REQUEST_PREDICATE_PATH_METHOD.equals(methodName)) {
                StringLiteral stringLiteral = WebfluxUtils.extractStringLiteralArgument(methodInvocation);
                if (stringLiteral != null) {
                    Range range = doc.toRange(stringLiteral.getStartPosition(), stringLiteral.getLength());
                    return new WebfluxRouteElement(stringLiteral.getLiteralValue(), range);
                }
            }
        } catch (BadLocationException e) {
        // ignore
        }
        return null;
    });
    return (WebfluxRouteElement[]) path.toArray(new WebfluxRouteElement[path.size()]);
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Range(org.eclipse.lsp4j.Range) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException)

Example 58 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project fabric8 by jboss-fuse.

the class AddSwaggerAnnotationMojo method addSwaggerApiAnnotation.

private void addSwaggerApiAnnotation(CompilationUnit unit, AnnotationVisitor visitor, File file, Document document) throws JavaModelException, IllegalArgumentException, MalformedTreeException, BadLocationException, IOException {
    AST ast = unit.getAST();
    ASTRewrite rewriter = ASTRewrite.create(ast);
    ListRewrite listRewrite = rewriter.getListRewrite(unit, CompilationUnit.TYPES_PROPERTY);
    NormalAnnotation normalAnnotation = rewriter.getAST().newNormalAnnotation();
    Name name = ast.newName("com.wordnik.swagger.annotations.Api");
    normalAnnotation.setTypeName(name);
    MemberValuePair memberValuePair = ast.newMemberValuePair();
    memberValuePair.setName(ast.newSimpleName("value"));
    StringLiteral stringLiteral = ast.newStringLiteral();
    String rootPath = visitor.getRootPath();
    rootPath = rootPath.substring(1, rootPath.length() - 1);
    if (rootPath.endsWith("/")) {
        rootPath = rootPath.substring(0, rootPath.length() - 1);
    }
    stringLiteral.setLiteralValue(rootPath);
    memberValuePair.setValue(stringLiteral);
    normalAnnotation.values().add(memberValuePair);
    memberValuePair = ast.newMemberValuePair();
    memberValuePair.setName(ast.newSimpleName("description"));
    stringLiteral = ast.newStringLiteral();
    stringLiteral.setLiteralValue("Operations about " + visitor.getRestServiceClass());
    memberValuePair.setValue(stringLiteral);
    normalAnnotation.values().add(memberValuePair);
    listRewrite.insertAt(normalAnnotation, 0, null);
    for (MethodDeclaration method : visitor.getRestMethod()) {
        listRewrite = rewriter.getListRewrite(method, MethodDeclaration.MODIFIERS2_PROPERTY);
        normalAnnotation = rewriter.getAST().newNormalAnnotation();
        name = ast.newName("com.wordnik.swagger.annotations.ApiOperation");
        normalAnnotation.setTypeName(name);
        memberValuePair = ast.newMemberValuePair();
        memberValuePair.setName(ast.newSimpleName("value"));
        stringLiteral = ast.newStringLiteral();
        stringLiteral.setLiteralValue(method.getName().toString());
        memberValuePair.setValue(stringLiteral);
        normalAnnotation.values().add(memberValuePair);
        Javadoc doc = method.getJavadoc();
        String comment = null;
        if (doc != null) {
            comment = method.getJavadoc().toString();
        }
        if (comment != null && comment.length() > 0) {
            // add notes from method java doc
            memberValuePair = ast.newMemberValuePair();
            memberValuePair.setName(ast.newSimpleName("notes"));
            stringLiteral = ast.newStringLiteral();
            stringLiteral.setLiteralValue(comment);
            memberValuePair.setValue(stringLiteral);
            normalAnnotation.values().add(memberValuePair);
        }
        listRewrite.insertAt(normalAnnotation, 0, null);
        listRewrite = rewriter.getListRewrite((ASTNode) ((List) method.getStructuralProperty(MethodDeclaration.PARAMETERS_PROPERTY)).get(0), SingleVariableDeclaration.MODIFIERS2_PROPERTY);
        normalAnnotation = rewriter.getAST().newNormalAnnotation();
        name = ast.newName("com.wordnik.swagger.annotations.ApiParam");
        normalAnnotation.setTypeName(name);
        ((VariableDeclaration) ((List) method.getStructuralProperty(MethodDeclaration.PARAMETERS_PROPERTY)).get(0)).getName();
        memberValuePair = ast.newMemberValuePair();
        memberValuePair.setName(ast.newSimpleName("value"));
        stringLiteral = ast.newStringLiteral();
        stringLiteral.setLiteralValue(((VariableDeclaration) ((List) method.getStructuralProperty(MethodDeclaration.PARAMETERS_PROPERTY)).get(0)).getName().toString());
        memberValuePair.setValue(stringLiteral);
        normalAnnotation.values().add(memberValuePair);
        listRewrite.insertAt(normalAnnotation, 0, null);
    }
    TextEdit edits = rewriter.rewriteAST(document, null);
    edits.apply(document);
    FileUtils.writeStringToFile(file, document.get());
}
Also used : AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Name(org.eclipse.jdt.core.dom.Name) MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) TextEdit(org.eclipse.text.edits.TextEdit) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration)

Example 59 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project jbosstools-hibernate by jbosstools.

the class HQLQuickAssistProcessor method getAssists.

public IJavaCompletionProposal[] getAssists(final IInvocationContext context, IProblemLocation[] locations) throws CoreException {
    IJavaCompletionProposal[] result = new IJavaCompletionProposal[0];
    if (!hasAssists(context))
        return result;
    ASTNode coveringNode = context.getCoveringNode();
    if (!(coveringNode instanceof StringLiteral)) {
        return result;
    }
    final StringLiteral stringLiteral = (StringLiteral) coveringNode;
    String contents = stringLiteral.getLiteralValue();
    result = new IJavaCompletionProposal[1];
    result[0] = new ExternalActionQuickAssistProposal(contents, EclipseImages.getImage(ImageConstants.HQL_EDITOR), JdtUiMessages.HQLQuickAssistProcessor_copy_to_hql_editor, context) {

        public void apply(IDocument document) {
            IEditorPart editorPart = Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
            ITextEditor[] textEditors = OpenMappingUtils.getTextEditors(editorPart);
            if (textEditors.length == 0)
                return;
            Point position = new Point(stringLiteral.getStartPosition() + 1, stringLiteral.getLength() - 2);
            new SaveQueryEditorListener(textEditors[0], getName(), getContents(), position, SaveQueryEditorListener.HQLEditor);
        }
    };
    return result;
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IEditorPart(org.eclipse.ui.IEditorPart) Point(org.eclipse.swt.graphics.Point) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal) IDocument(org.eclipse.jface.text.IDocument)

Example 60 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project eclipse.jdt.ls by eclipse.

the class CompletionHandler method isCompletionForConstructor.

/**
 * Check whether the completion is triggered for constructors: "new |"
 * @param params completion parameters
 * @param unit completion unit
 * @param monitor progress monitor
 * @throws JavaModelException
 */
private boolean isCompletionForConstructor(CompletionParams params, ICompilationUnit unit, IProgressMonitor monitor) throws JavaModelException {
    Position pos = params.getPosition();
    int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), pos.getLine(), pos.getCharacter());
    if (offset < 4) {
        return false;
    }
    String content = unit.getSource();
    if (content == null) {
        return false;
    }
    String triggerWord = content.substring(offset - 4, offset);
    if (!"new ".equals(triggerWord)) {
        return false;
    }
    CompilationUnit root = SharedASTProviderCore.getAST(unit, SharedASTProviderCore.WAIT_ACTIVE_ONLY, monitor);
    if (root == null || monitor.isCanceled()) {
        return false;
    }
    ASTNode node = NodeFinder.perform(root, offset - 4, 0);
    if (node instanceof StringLiteral || node instanceof SimpleName) {
        return false;
    }
    return true;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) Position(org.eclipse.lsp4j.Position) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Aggregations

StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)74 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)31 Expression (org.eclipse.jdt.core.dom.Expression)22 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)20 CastExpression (org.eclipse.jdt.core.dom.CastExpression)19 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)18 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)16 ASTNode (org.eclipse.jdt.core.dom.ASTNode)12 SimpleName (org.eclipse.jdt.core.dom.SimpleName)11 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)11 CharacterLiteral (org.eclipse.jdt.core.dom.CharacterLiteral)10 ArrayList (java.util.ArrayList)9 AST (org.eclipse.jdt.core.dom.AST)9 BooleanLiteral (org.eclipse.jdt.core.dom.BooleanLiteral)9 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)9 MemberValuePair (org.eclipse.jdt.core.dom.MemberValuePair)9 NumberLiteral (org.eclipse.jdt.core.dom.NumberLiteral)9 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)9 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)8 NormalAnnotation (org.eclipse.jdt.core.dom.NormalAnnotation)8