Search in sources :

Example 16 with Document

use of org.eclipse.jface.text.Document in project flux 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 17 with Document

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

the class CUCorrectionProposal method createTextChange.

protected TextChange createTextChange() throws CoreException {
    ICompilationUnit cu = getCompilationUnit();
    String name = getName();
    TextChange change;
    if (!cu.getResource().exists()) {
        String source;
        try {
            source = cu.getSource();
        } catch (JavaModelException e) {
            JavaPlugin.log(e);
            // empty
            source = new String();
        }
        Document document = new Document(source);
        document.setInitialLineDelimiter(StubUtility.getLineDelimiterUsed(cu));
        change = new DocumentChange(name, document);
    } else {
        CompilationUnitChange cuChange = new CompilationUnitChange(name, cu);
        cuChange.setSaveMode(TextFileChange.LEAVE_DIRTY);
        change = cuChange;
    }
    TextEdit rootEdit = new MultiTextEdit();
    change.setEdit(rootEdit);
    // initialize text change
    IDocument document = change.getCurrentDocument(new NullProgressMonitor());
    addEdits(document, rootEdit);
    return change;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JavaModelException(org.eclipse.jdt.core.JavaModelException) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) TextChange(org.eclipse.ltk.core.refactoring.TextChange) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) DocumentChange(org.eclipse.ltk.core.refactoring.DocumentChange) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) IDocument(org.eclipse.jface.text.IDocument) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 18 with Document

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

the class StubUtility method getMethodComment.

/*
	 * Don't use this method directly, use CodeGeneration.
	 * @see org.eclipse.jdt.ui.CodeGeneration#getMethodComment(ICompilationUnit, String, String, String[], String[], String, String[], IMethod, String)
	 */
public static String getMethodComment(ICompilationUnit cu, String typeName, String methodName, String[] paramNames, String[] excTypeSig, String retTypeSig, String[] typeParameterNames, IMethod target, boolean delegate, String lineDelimiter) throws CoreException {
    String templateName = CodeTemplateContextType.METHODCOMMENT_ID;
    if (retTypeSig == null) {
        templateName = CodeTemplateContextType.CONSTRUCTORCOMMENT_ID;
    } else if (target != null) {
        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, methodName);
    if (retTypeSig != null) {
        context.setVariable(CodeTemplateContextType.RETURN_TYPE, Signature.toString(retTypeSig));
    }
    if (target != null) {
        String targetTypeName = target.getDeclaringType().getFullyQualifiedName('.');
        String[] targetParamTypeNames = getParameterTypeNamesForSeeTag(target);
        if (delegate)
            context.setVariable(CodeTemplateContextType.SEE_TO_TARGET_TAG, getSeeTag(targetTypeName, methodName, targetParamTypeNames));
        else
            context.setVariable(CodeTemplateContextType.SEE_TO_OVERRIDDEN_TAG, getSeeTag(targetTypeName, methodName, targetParamTypeNames));
    }
    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 document = new Document(str);
    String[] exceptionNames = new String[excTypeSig.length];
    for (int i = 0; i < excTypeSig.length; i++) {
        exceptionNames[i] = Signature.toString(excTypeSig[i]);
    }
    String returnType = retTypeSig != null ? Signature.toString(retTypeSig) : null;
    int[] tagOffsets = position.getOffsets();
    for (int i = tagOffsets.length - 1; i >= 0; i--) {
        // from last to first
        try {
            insertTag(document, tagOffsets[i], position.getLength(), paramNames, exceptionNames, returnType, typeParameterNames, false, lineDelimiter);
        } catch (BadLocationException e) {
            throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
        }
    }
    return document.get();
}
Also used : TemplateException(org.eclipse.jface.text.templates.TemplateException) 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 19 with Document

use of org.eclipse.jface.text.Document in project generator by mybatis.

the class ExistingJavaFileVisitorTest method testRegularClass.

@Test
public void testRegularClass() throws Exception {
    InputStream resource = getClass().getResourceAsStream("/org/mybatis/generator/eclipse/core/tests/merge/resources/AwfulTable.java.src");
    String source = getResourceAsString(resource);
    IDocument document = new Document(source);
    CompilationUnit cu = getCompilationUnitFromSource(source);
    cu.recordModifications();
    ExistingJavaFileVisitor visitor = new ExistingJavaFileVisitor(MergeConstants.OLD_ELEMENT_TAGS);
    // delete all the old generated stuff
    cu.accept(visitor);
    assertThat(visitor.getTypeDeclaration(), is(notNullValue()));
    assertThat(visitor.getTypeDeclaration().isInterface(), is(false));
    assertThat(visitor.getTypeDeclaration().getName().getFullyQualifiedName(), is("AwfulTable"));
    // generate a new compilation unit that is stripped of old stuff
    TextEdit textEdit = cu.rewrite(document, null);
    textEdit.apply(document);
    CompilationUnitSummary summary = getCompilationUnitSummaryFromSource(document.get());
    assertThat(summary, hasImportCount(1));
    assertThat(summary, hasClass("AwfulTable", withSuperClass("AwfulTableKey")));
    assertThat(summary, hasClass("AwfulTable", withSuperInterfaceCount(0)));
    assertThat(summary, hasClass("AwfulTable", withMethodCount(0)));
    assertThat(summary, hasClass("AwfulTable", withFieldCount(0)));
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) InputStream(java.io.InputStream) TextEdit(org.eclipse.text.edits.TextEdit) CompilationUnitSummary(org.mybatis.generator.eclipse.tests.harness.summary.CompilationUnitSummary) Utilities.getResourceAsString(org.mybatis.generator.eclipse.tests.harness.Utilities.getResourceAsString) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) ExistingJavaFileVisitor(org.mybatis.generator.eclipse.core.merge.ExistingJavaFileVisitor) IDocument(org.eclipse.jface.text.IDocument) Test(org.junit.Test)

Example 20 with Document

use of org.eclipse.jface.text.Document in project generator by mybatis.

the class ExistingJavaFileVisitorTest method testComplexClass.

@Test
public void testComplexClass() throws Exception {
    InputStream resource = getClass().getResourceAsStream("/org/mybatis/generator/eclipse/core/tests/merge/resources/AwfulTableExample.java.src");
    String source = getResourceAsString(resource);
    IDocument document = new Document(source);
    CompilationUnit cu = getCompilationUnitFromSource(source);
    cu.recordModifications();
    ExistingJavaFileVisitor visitor = new ExistingJavaFileVisitor(MergeConstants.OLD_ELEMENT_TAGS);
    // delete all the old generated stuff
    cu.accept(visitor);
    assertThat(visitor.getTypeDeclaration(), is(notNullValue()));
    assertThat(visitor.getTypeDeclaration().isInterface(), is(false));
    assertThat(visitor.getTypeDeclaration().getName().getFullyQualifiedName(), is("AwfulTableExample"));
    assertThat(visitor.containsInnerClass("Criteria"), is(true));
    // generate a new compilation unit that is stripped of old stuff
    TextEdit textEdit = cu.rewrite(document, null);
    textEdit.apply(document);
    CompilationUnitSummary summary = getCompilationUnitSummaryFromSource(document.get());
    assertThat(summary, hasImportCount(2));
    assertThat(summary, hasClass("AwfulTableExample", withSuperClass(nullValue())));
    assertThat(summary, hasClass("AwfulTableExample", withSuperInterfaceCount(0)));
    assertThat(summary, hasClass("AwfulTableExample", withMethodCount(0)));
    assertThat(summary, hasClass("AwfulTableExample", withFieldCount(0)));
    assertThat(summary, hasClass("AwfulTableExample", withClass("Criteria")));
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) InputStream(java.io.InputStream) TextEdit(org.eclipse.text.edits.TextEdit) CompilationUnitSummary(org.mybatis.generator.eclipse.tests.harness.summary.CompilationUnitSummary) Utilities.getResourceAsString(org.mybatis.generator.eclipse.tests.harness.Utilities.getResourceAsString) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) ExistingJavaFileVisitor(org.mybatis.generator.eclipse.core.merge.ExistingJavaFileVisitor) IDocument(org.eclipse.jface.text.IDocument) Test(org.junit.Test)

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