Search in sources :

Example 1 with Document

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

the class StubUtility method fixEmptyVariables.

// remove lines for empty variables
private static String fixEmptyVariables(TemplateBuffer buffer, String[] variables) throws MalformedTreeException, BadLocationException {
    IDocument doc = new Document(buffer.getString());
    int nLines = doc.getNumberOfLines();
    MultiTextEdit edit = new MultiTextEdit();
    HashSet<Integer> removedLines = new HashSet<Integer>();
    for (int i = 0; i < variables.length; i++) {
        // look if Javadoc tags have to be added
        TemplateVariable position = findVariable(buffer, variables[i]);
        if (position == null || position.getLength() > 0) {
            continue;
        }
        int[] offsets = position.getOffsets();
        for (int k = 0; k < offsets.length; k++) {
            int line = doc.getLineOfOffset(offsets[k]);
            IRegion lineInfo = doc.getLineInformation(line);
            int offset = lineInfo.getOffset();
            String str = doc.get(offset, lineInfo.getLength());
            if (Strings.containsOnlyWhitespaces(str) && nLines > line + 1 && removedLines.add(new Integer(line))) {
                int nextStart = doc.getLineOffset(line + 1);
                edit.addChild(new DeleteEdit(offset, nextStart - offset));
            }
        }
    }
    edit.apply(doc, 0);
    return doc.get();
}
Also used : TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) DeleteEdit(org.eclipse.text.edits.DeleteEdit) IDocument(org.eclipse.jface.text.IDocument) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) IRegion(org.eclipse.jface.text.IRegion) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with Document

use of org.eclipse.jface.text.Document 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 3 with Document

use of org.eclipse.jface.text.Document 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 4 with Document

use of org.eclipse.jface.text.Document 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 Document

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

the class DocumentAdapter method initialize.

private void initialize() {
    //			}
    if (fDocument == null) {
        if (fFile != null) {
            try (InputStream inputStream = fFile.getContents()) {
                fDocument = new Document(IoUtil.readStream(inputStream));
            } catch (IOException | CoreException e) {
                JavaPlugin.log(e);
                fDocument = new Document("");
            }
        } else {
            fDocument = new Document("");
        }
    }
    //			fDocument = fTextFileBuffer.getDocument();
    //		} catch (CoreException x) {
    //			fDocument = manager.createEmptyDocument(fPath, fLocationKind);
    //			if (fDocument instanceof ISynchronizable)
    //				((ISynchronizable)fDocument).setLockObject(new Object());
    //		}
    fDocument.addDocumentListener(this);
    fIsClosed = false;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) InputStream(java.io.InputStream) IOException(java.io.IOException) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

Document (org.eclipse.jface.text.Document)59 IDocument (org.eclipse.jface.text.IDocument)48 BadLocationException (org.eclipse.jface.text.BadLocationException)26 TextEdit (org.eclipse.text.edits.TextEdit)16 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)9 CoreException (org.eclipse.core.runtime.CoreException)8 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 IRegion (org.eclipse.jface.text.IRegion)8 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)8 TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)8 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)7 TemplateException (org.eclipse.jface.text.templates.TemplateException)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)6 CodeTemplateContext (org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext)6 Template (org.eclipse.jface.text.templates.Template)6 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)6 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)6 InputStream (java.io.InputStream)5 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)5 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)5