Search in sources :

Example 1 with BadLocationException

use of org.eclipse.jface.text.BadLocationException in project che by eclipse.

the class ASTNodes method asFormattedString.

public static String asFormattedString(ASTNode node, int indent, String lineDelim, Map<String, String> options) {
    String unformatted = asString(node);
    TextEdit edit = CodeFormatterUtil.format2(node, unformatted, indent, lineDelim, options);
    if (edit != null) {
        Document document = new Document(unformatted);
        try {
            edit.apply(document, TextEdit.NONE);
        } catch (BadLocationException e) {
            JavaPlugin.log(e);
        }
        return document.get();
    }
    // unknown node
    return unformatted;
}
Also used : TextEdit(org.eclipse.text.edits.TextEdit) Document(org.eclipse.jface.text.Document) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 2 with BadLocationException

use of org.eclipse.jface.text.BadLocationException in project che by eclipse.

the class StubUtility method getMethodComment.

/*
	 * Don't use this method directly, use CodeGeneration.
	 * This method should work with all AST levels.
	 * @see org.eclipse.jdt.ui.CodeGeneration#getMethodComment(ICompilationUnit, String, MethodDeclaration, boolean, String, String[], String)
	 */
public static String getMethodComment(ICompilationUnit cu, String typeName, MethodDeclaration decl, boolean isDeprecated, String targetName, String targetMethodDeclaringTypeName, String[] targetMethodParameterTypeNames, boolean delegate, String lineDelimiter) throws CoreException {
    boolean needsTarget = targetMethodDeclaringTypeName != null && targetMethodParameterTypeNames != null;
    String templateName = CodeTemplateContextType.METHODCOMMENT_ID;
    if (decl.isConstructor()) {
        templateName = CodeTemplateContextType.CONSTRUCTORCOMMENT_ID;
    } else if (needsTarget) {
        if (delegate)
            templateName = CodeTemplateContextType.DELEGATECOMMENT_ID;
        else
            templateName = CodeTemplateContextType.OVERRIDECOMMENT_ID;
    }
    Template template = getCodeTemplate(templateName, cu.getJavaProject());
    if (template == null) {
        return null;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
    context.setCompilationUnitVariables(cu);
    context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, typeName);
    context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, decl.getName().getIdentifier());
    if (!decl.isConstructor()) {
        context.setVariable(CodeTemplateContextType.RETURN_TYPE, ASTNodes.asString(getReturnType(decl)));
    }
    if (needsTarget) {
        if (delegate)
            context.setVariable(CodeTemplateContextType.SEE_TO_TARGET_TAG, getSeeTag(targetMethodDeclaringTypeName, targetName, targetMethodParameterTypeNames));
        else
            context.setVariable(CodeTemplateContextType.SEE_TO_OVERRIDDEN_TAG, getSeeTag(targetMethodDeclaringTypeName, targetName, targetMethodParameterTypeNames));
    }
    TemplateBuffer buffer;
    try {
        buffer = context.evaluate(template);
    } catch (BadLocationException e) {
        throw new CoreException(Status.CANCEL_STATUS);
    } catch (TemplateException e) {
        throw new CoreException(Status.CANCEL_STATUS);
    }
    if (buffer == null)
        return null;
    String str = buffer.getString();
    if (Strings.containsOnlyWhitespaces(str)) {
        return null;
    }
    // look if Javadoc tags have to be added
    TemplateVariable position = findVariable(buffer, CodeTemplateContextType.TAGS);
    if (position == null) {
        return str;
    }
    IDocument textBuffer = new Document(str);
    List<TypeParameter> typeParams = shouldGenerateMethodTypeParameterTags(cu.getJavaProject()) ? decl.typeParameters() : Collections.emptyList();
    String[] typeParamNames = new String[typeParams.size()];
    for (int i = 0; i < typeParamNames.length; i++) {
        TypeParameter elem = typeParams.get(i);
        typeParamNames[i] = elem.getName().getIdentifier();
    }
    List<SingleVariableDeclaration> params = decl.parameters();
    String[] paramNames = new String[params.size()];
    for (int i = 0; i < paramNames.length; i++) {
        SingleVariableDeclaration elem = params.get(i);
        paramNames[i] = elem.getName().getIdentifier();
    }
    String[] exceptionNames = getExceptionNames(decl);
    String returnType = null;
    if (!decl.isConstructor()) {
        returnType = ASTNodes.asString(getReturnType(decl));
    }
    int[] tagOffsets = position.getOffsets();
    for (int i = tagOffsets.length - 1; i >= 0; i--) {
        // from last to first
        try {
            insertTag(textBuffer, tagOffsets[i], position.getLength(), paramNames, exceptionNames, returnType, typeParamNames, isDeprecated, lineDelimiter);
        } catch (BadLocationException e) {
            throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
        }
    }
    return textBuffer.get();
}
Also used : TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) ITypeParameter(org.eclipse.jdt.core.ITypeParameter) TemplateException(org.eclipse.jface.text.templates.TemplateException) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) Template(org.eclipse.jface.text.templates.Template) CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) CoreException(org.eclipse.core.runtime.CoreException) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) BadLocationException(org.eclipse.jface.text.BadLocationException) IDocument(org.eclipse.jface.text.IDocument)

Example 3 with BadLocationException

use of org.eclipse.jface.text.BadLocationException in project che by eclipse.

the class StubUtility method evaluateTemplate.

private static String evaluateTemplate(CodeTemplateContext context, Template template, String[] fullLineVariables) throws CoreException {
    TemplateBuffer buffer;
    try {
        buffer = context.evaluate(template);
        if (buffer == null)
            return null;
        String str = fixEmptyVariables(buffer, fullLineVariables);
        if (Strings.containsOnlyWhitespaces(str)) {
            return null;
        }
        return str;
    } catch (BadLocationException e) {
        throw new CoreException(Status.CANCEL_STATUS);
    } catch (TemplateException e) {
        throw new CoreException(Status.CANCEL_STATUS);
    }
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) TemplateException(org.eclipse.jface.text.templates.TemplateException) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 4 with BadLocationException

use of org.eclipse.jface.text.BadLocationException in project che by eclipse.

the class StubUtility method getTypeComment.

/*
	 * Don't use this method directly, use CodeGeneration.
	 * @see org.eclipse.jdt.ui.CodeGeneration#getTypeComment(ICompilationUnit, String, String[], String)
	 */
public static String getTypeComment(ICompilationUnit cu, String typeQualifiedName, String[] typeParameterNames, String lineDelim) throws CoreException {
    Template template = getCodeTemplate(CodeTemplateContextType.TYPECOMMENT_ID, cu.getJavaProject());
    if (template == null) {
        return null;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelim);
    context.setCompilationUnitVariables(cu);
    context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, Signature.getQualifier(typeQualifiedName));
    context.setVariable(CodeTemplateContextType.TYPENAME, Signature.getSimpleName(typeQualifiedName));
    TemplateBuffer buffer;
    try {
        buffer = context.evaluate(template);
    } catch (BadLocationException e) {
        throw new CoreException(Status.CANCEL_STATUS);
    } catch (TemplateException e) {
        throw new CoreException(Status.CANCEL_STATUS);
    }
    String str = buffer.getString();
    if (Strings.containsOnlyWhitespaces(str)) {
        return null;
    }
    // look if Javadoc tags have to be added
    TemplateVariable position = findVariable(buffer, CodeTemplateContextType.TAGS);
    if (position == null) {
        return str;
    }
    IDocument document = new Document(str);
    int[] tagOffsets = position.getOffsets();
    for (int i = tagOffsets.length - 1; i >= 0; i--) {
        // from last to first
        try {
            insertTag(document, tagOffsets[i], position.getLength(), EMPTY, EMPTY, null, typeParameterNames, false, lineDelim);
        } catch (BadLocationException e) {
            throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
        }
    }
    return document.get();
}
Also used : CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) CoreException(org.eclipse.core.runtime.CoreException) TemplateException(org.eclipse.jface.text.templates.TemplateException) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException) IDocument(org.eclipse.jface.text.IDocument) Template(org.eclipse.jface.text.templates.Template)

Example 5 with BadLocationException

use of org.eclipse.jface.text.BadLocationException in project che by eclipse.

the class LinkedNamesAssistProposal method apply.

/* (non-Javadoc)
     * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer, char, int, int)
     */
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
    try {
        Point seletion = viewer.getSelectedRange();
        // get full ast
        CompilationUnit root = SharedASTProvider.getAST(fContext.getCompilationUnit(), SharedASTProvider.WAIT_YES, null);
        ASTNode nameNode = NodeFinder.perform(root, fNode.getStartPosition(), fNode.getLength());
        final int pos = fNode.getStartPosition();
        ASTNode[] sameNodes;
        if (nameNode instanceof SimpleName) {
            sameNodes = LinkedNodeFinder.findByNode(root, (SimpleName) nameNode);
        } else {
            sameNodes = new ASTNode[] { nameNode };
        }
        // sort for iteration order, starting with the node @ offset
        Arrays.sort(sameNodes, new Comparator<ASTNode>() {

            public int compare(ASTNode o1, ASTNode o2) {
                return rank(o1) - rank(o2);
            }

            /**
                 * Returns the absolute rank of an <code>ASTNode</code>. Nodes
                 * preceding <code>offset</code> are ranked last.
                 *
                 * @param node the node to compute the rank for
                 * @return the rank of the node with respect to the invocation offset
                 */
            private int rank(ASTNode node) {
                int relativeRank = node.getStartPosition() + node.getLength() - pos;
                if (relativeRank < 0)
                    return Integer.MAX_VALUE + relativeRank;
                else
                    return relativeRank;
            }
        });
        IDocument document = viewer.getDocument();
        LinkedPositionGroupImpl group = new LinkedPositionGroupImpl();
        for (int i = 0; i < sameNodes.length; i++) {
            ASTNode elem = sameNodes[i];
            RegionImpl region = new RegionImpl();
            region.setOffset(elem.getStartPosition());
            region.setLength(elem.getLength());
            group.addPositions(region);
        //				group.addPosition(new LinkedPosition(document, elem.getStartPosition(), elem.getLength(), i));
        }
        LinkedModeModelImpl model = new LinkedModeModelImpl();
        model.addGroups(group);
        //			model.forceInstall();
        model.setEscapePosition(offset);
        this.linkedModel = model;
        if (fContext instanceof AssistContext) {
        //				IEditorPart editor = ((AssistContext)fContext).getEditor();
        //				if (editor instanceof JavaEditor) {
        //					model.addLinkingListener(new EditorHighlightingSynchronizer((JavaEditor)editor));
        //				}
        }
        if (fValueSuggestion != null) {
            document.replace(nameNode.getStartPosition(), nameNode.getLength(), fValueSuggestion);
        //				IRegion selectedRegion = ui.getSelectedRegion();
        //				seletion = new Point(selectedRegion.getOffset(), fValueSuggestion.length());
        }
    //			viewer.setSelectedRange(seletion.x, seletion.y); // by default full word is selected, restore original selection
    } catch (BadLocationException e) {
        JavaPlugin.log(e);
    }
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) LinkedPositionGroupImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedPositionGroupImpl) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) SimpleName(org.eclipse.jdt.core.dom.SimpleName) LinkedModeModelImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedModeModelImpl) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) ASTNode(org.eclipse.jdt.core.dom.ASTNode) RegionImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.RegionImpl) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

BadLocationException (org.eclipse.jface.text.BadLocationException)133 IDocument (org.eclipse.jface.text.IDocument)58 IRegion (org.eclipse.jface.text.IRegion)43 Document (org.eclipse.jface.text.Document)26 Point (org.eclipse.swt.graphics.Point)17 CoreException (org.eclipse.core.runtime.CoreException)16 Position (org.eclipse.jface.text.Position)13 StyledString (org.eclipse.jface.viewers.StyledString)13 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)13 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)12 TemplateException (org.eclipse.jface.text.templates.TemplateException)12 TextEdit (org.eclipse.text.edits.TextEdit)12 UndoEdit (org.eclipse.text.edits.UndoEdit)10 Region (org.eclipse.jface.text.Region)9 ASTNode (org.eclipse.jdt.core.dom.ASTNode)8 ArrayList (java.util.ArrayList)7 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)7 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)6 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)6 DefaultLineTracker (org.eclipse.jface.text.DefaultLineTracker)6