Search in sources :

Example 56 with Document

use of org.eclipse.jface.text.Document in project AutoRefactor by JnRouvignac.

the class AllRefactoringRulesTest method testRefactoring0.

private void testRefactoring0() throws Exception {
    final File samplesDir = new File(SAMPLES_ALL_BASE_DIR);
    final File sampleIn = new File(samplesDir, "samples_in/" + sampleName);
    assertTrue(sampleName + ": sample in file " + sampleIn + " should exist", sampleIn.exists());
    final File sampleOut = new File(samplesDir, "samples_out/" + sampleName);
    assertTrue(sampleName + ": sample out file " + sampleOut + " should exist", sampleOut.exists());
    final String sampleInSource = readAll(sampleIn);
    final String sampleOutSource = readAll(sampleOut);
    final IPackageFragment packageFragment = JavaCoreHelper.getPackageFragment(PACKAGE_NAME);
    final ICompilationUnit cu = packageFragment.createCompilationUnit(sampleName, sampleInSource, true, null);
    cu.getBuffer().setContents(sampleInSource);
    cu.save(null, true);
    final IDocument doc = new Document(sampleInSource);
    new ApplyRefactoringsJob(null, null, TEST_ENVIRONMENT).applyRefactoring(doc, cu, new AggregateASTVisitor(AllRefactoringRules.getAllRefactoringRules()), newJavaProjectOptions(Release.javaSE("1.7.0"), 4), new NullProgressMonitor());
    final String actual = normalizeJavaSourceCode(doc.get().replaceAll("samples_in", "samples_out"));
    final String expected = normalizeJavaSourceCode(sampleOutSource);
    assertEquals(sampleName + ": wrong output;", expected, actual);
}
Also used : ApplyRefactoringsJob(org.autorefactor.refactoring.ApplyRefactoringsJob) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) File(java.io.File) IDocument(org.eclipse.jface.text.IDocument)

Example 57 with Document

use of org.eclipse.jface.text.Document in project tdi-studio-se by Talend.

the class TalendJavaSourceViewer method createViewerForComponent.

public static ISourceViewer createViewerForComponent(Composite composite, int styles, CompilationUnitEditor editor, List<Variable> variableList, String uniqueName, String codePart) {
    //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    String selectedPart = "[" + uniqueName + " " + codePart + " ] start";
    IDocument originalDocument = editor.getDocumentProvider().getDocument(editor.getEditorInput());
    int documentOffset = -1;
    //$NON-NLS-1$
    String globalFields = "";
    //$NON-NLS-1$
    String localFields = "";
    //$NON-NLS-1$
    globalFields = "\tprivate static java.util.Properties context = new java.util.Properties();\n";
    //$NON-NLS-1$
    globalFields += "\tprivate static final java.util.Map<String, Object> globalMap = new java.util.HashMap<String, Object>();\n";
    IDocument newDoc = new Document();
    boolean checkCode = false;
    FindReplaceDocumentAdapter frda = null;
    // hywang modified for bug 9180
    String documentText = originalDocument.get();
    if (documentText != null && !documentText.equals("") && documentText.length() > 0) {
        //$NON-NLS-1$
        frda = new FindReplaceDocumentAdapter(originalDocument);
    }
    try {
        Region region = null;
        if (frda != null) {
            region = (Region) frda.find(0, selectedPart, true, false, false, false);
        }
        if (region != null) {
            checkCode = true;
            documentOffset = region.getOffset();
            documentOffset = originalDocument.getLineOffset(originalDocument.getLineOfOffset(documentOffset) + 2);
            JavaCompletionProcessor processor = new JavaCompletionProcessor(editor, new ContentAssistant(), IDocument.DEFAULT_CONTENT_TYPE);
            boolean globalFieldsDone = false;
            //$NON-NLS-1$
            globalFields = "";
            String className = editor.getPartName().substring(0, editor.getPartName().indexOf('.') + 1);
            ICompletionProposal[] proposals = processor.computeCompletionProposals(editor.getViewer(), documentOffset);
            for (ICompletionProposal curProposal : proposals) {
                if (curProposal instanceof JavaCompletionProposal) {
                    JavaCompletionProposal javaProposal = ((JavaCompletionProposal) curProposal);
                    if (javaProposal.getJavaElement() instanceof SourceField) {
                        globalFieldsDone = true;
                        SourceField sourceField = (SourceField) javaProposal.getJavaElement();
                        //$NON-NLS-1$ //$NON-NLS-2$
                        globalFields += "\t" + sourceField.getSource() + "\n";
                    // System.out.println();
                    }
                    if (javaProposal.getJavaElement() == null && !globalFieldsDone) {
                        //$NON-NLS-1$
                        String[] str = javaProposal.getSortString().split(" ");
                        //$NON-NLS-1$
                        String variable = "";
                        for (int i = str.length - 1; i >= 0; i--) {
                            //$NON-NLS-1$
                            variable += str[i].length() == 0 ? " " : str[i];
                        }
                        if (variable.contains(className)) {
                            continue;
                        }
                        //$NON-NLS-1$
                        variable += ";";
                        //$NON-NLS-1$ //$NON-NLS-2$
                        localFields += "\t\t" + variable + "\n";
                    // System.out.println(variable);
                    }
                }
            }
        }
    } catch (BadLocationException e) {
        ExceptionHandler.process(e);
    } catch (JavaModelException e) {
        // e.printStackTrace();
        ExceptionHandler.process(e);
    }
    StringBuffer buff = new StringBuffer();
    //$NON-NLS-1$
    buff.append("package internal;\n\n");
    buff.append(getImports());
    //$NON-NLS-1$ //$NON-NLS-2$
    buff.append("public class " + VIEWER_CLASS_NAME + currentId + " {\n");
    buff.append(globalFields);
    //$NON-NLS-1$
    buff.append("\tpublic void myFunction(){\n");
    buff.append(localFields);
    documentOffset = buff.toString().length();
    //$NON-NLS-1$
    buff.append("\n\t\n}\n}");
    newDoc.set(buff.toString());
    return initializeViewer(composite, styles, checkCode, newDoc, documentOffset);
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) SourceField(org.eclipse.jdt.internal.core.SourceField) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) ContentAssistant(org.eclipse.jface.text.contentassist.ContentAssistant) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) Region(org.eclipse.jface.text.Region) JavaCompletionProcessor(org.eclipse.jdt.internal.ui.text.java.JavaCompletionProcessor) IDocument(org.eclipse.jface.text.IDocument) FindReplaceDocumentAdapter(org.eclipse.jface.text.FindReplaceDocumentAdapter) BadLocationException(org.eclipse.jface.text.BadLocationException) JavaCompletionProposal(org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal)

Example 58 with Document

use of org.eclipse.jface.text.Document in project Main by SpartanRefactoring.

the class ASTTestClassGenerator method addStaticImports.

public static String addStaticImports(ASTNode root, String importName, String simpleName) {
    if (!iz.compilationUnit(root)) {
        return root.toString();
    }
    ListRewrite lr = ASTRewrite.create(root.getAST()).getListRewrite(root, CompilationUnit.IMPORTS_PROPERTY);
    ImportDeclaration id = root.getAST().newImportDeclaration();
    id.setStatic(true);
    id.setName(root.getAST().newName(importName.split("\\.")));
    lr.insertLast(id, null);
    //irw.addStaticImport(importName, simpleName, false);
    try {
        IDocument doc = new Document(root.toString());
        TextEdit te = lr.getASTRewrite().rewriteAST(doc, null);
        // TextEdit te = irw.rewriteImports(new NullProgressMonitor()); //FIXME: @orenafek: Change null to something else...
        te.apply(doc);
        return doc.get();
    } catch (BadLocationException e) {
        note.bug(e);
    }
    //lr.insertLast(ImportDeclaration.
    return root.toString();
}
Also used : TextEdit(org.eclipse.text.edits.TextEdit) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 59 with Document

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

the class ComponentMarker method findAndMarkComponentAnnotations.

private static void findAndMarkComponentAnnotations(ICompilationUnit c) throws CoreException, JavaModelException {
    Document document = null;
    boolean found = false;
    String key = null;
    for (IType t : c.getTypes()) {
        for (IAnnotation annot : t.getAnnotations()) {
            if ("Component".equals(annot.getElementName())) {
                if (document == null)
                    document = new Document(c.getBuffer().getContents());
                found = true;
                key = getNameFromComponent(annot);
                int lineNumber;
                try {
                    lineNumber = document.getLineOfOffset(t.getSourceRange().getOffset()) + 1;
                    String message = key == null ? "OSGi Component" : key;
                    IMarker marker = c.getResource().createMarker(BndtoolsConstants.MARKER_COMPONENT);
                    marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_INFO);
                    marker.setAttribute(IMarker.MESSAGE, message);
                    marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);
                    marker.setAttribute(IMarker.LOCATION, "line " + lineNumber);
                } catch (BadLocationException e) {
                    logger.logError("Component Marker error", e);
                    lineNumber = -1;
                }
            }
        }
    }
    if (!found) {
        c.getResource().deleteMarkers(BndtoolsConstants.MARKER_COMPONENT, true, IResource.DEPTH_ONE);
    }
}
Also used : IAnnotation(org.eclipse.jdt.core.IAnnotation) IMarker(org.eclipse.core.resources.IMarker) Document(org.eclipse.jface.text.Document) BadLocationException(org.eclipse.jface.text.BadLocationException) IType(org.eclipse.jdt.core.IType)

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