Search in sources :

Example 66 with IDocument

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

the class TestHelper method normalizeJavaSourceCode.

public static String normalizeJavaSourceCode(String source) {
    final CodeFormatter codeFormatter = createCodeFormatter(getJava7Options());
    final TextEdit edit = codeFormatter.format(K_COMPILATION_UNIT, // source to format
    source, // source to format
    0, // source to format
    source.length(), // initial indentation and line separator
    0, // initial indentation and line separator
    System.getProperty("line.separator"));
    try {
        final IDocument document = new Document(source);
        edit.apply(document);
        return document.get();
    } catch (MalformedTreeException e) {
        throw new RuntimeException(e);
    } catch (BadLocationException e) {
        throw new RuntimeException(e);
    }
}
Also used : CodeFormatter(org.eclipse.jdt.core.formatter.CodeFormatter) TextEdit(org.eclipse.text.edits.TextEdit) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) 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 67 with IDocument

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

the class ApplyRefactoringsJob method applyRefactoring.

private void applyRefactoring(ICompilationUnit compilationUnit, AggregateASTVisitor refactoringToApply, JavaProjectOptions options, IProgressMonitor monitor) throws Exception {
    final ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
    final IPath path = compilationUnit.getPath();
    final LocationKind locationKind = LocationKind.NORMALIZE;
    try {
        bufferManager.connect(path, locationKind, null);
        final ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path, locationKind);
        if (!textFileBuffer.isSynchronized()) {
            /*
                 * Cannot read the source when a file is not synchronized,
                 * Let's ignore this file to avoid problems when:
                 * - doing string manipulation with the source text
                 * - applying automated refactorings to such files
                 */
            environment.getLogger().error("File \"" + compilationUnit.getPath() + "\" is not synchronized with the file system." + " Automated refactorings will not be applied to it.");
            return;
        }
        final IDocument document = textFileBuffer.getDocument();
        applyRefactoring(document, compilationUnit, refactoringToApply, options, monitor);
    } finally {
        bufferManager.disconnect(path, locationKind, null);
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) LocationKind(org.eclipse.core.filebuffers.LocationKind) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IDocument(org.eclipse.jface.text.IDocument)

Example 68 with IDocument

use of org.eclipse.jface.text.IDocument in project ow by vtst.

the class AbstractCompletionProposal method apply.

@Override
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
    String replacementString = getReplacementString(trigger);
    try {
        IDocument document = viewer.getDocument();
        int replacementOffset = context.getInvocationOffset() - context.getPrefixLength();
        document.replace(replacementOffset, context.getPrefixLength() + offset - context.getInvocationOffset(), replacementString);
        regionToSelect = new Point(context.getInvocationOffset() + replacementString.length() - context.getPrefixLength(), 0);
        setUpLinkedMode(document, replacementOffset, getFragments());
    } catch (BadLocationException e) {
        assert false;
    }
}
Also used : StyledString(org.eclipse.jface.viewers.StyledString) Point(org.eclipse.swt.graphics.Point) IDocument(org.eclipse.jface.text.IDocument) Point(org.eclipse.swt.graphics.Point) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 69 with IDocument

use of org.eclipse.jface.text.IDocument in project ow by vtst.

the class AbstractEditorRegistry method addEditor.

private synchronized void addEditor(ITextEditor textEditor) {
    IEditorInput editorInput = textEditor.getEditorInput();
    if (!(editorInput instanceof IFileEditorInput))
        return;
    IFile file = ((IFileEditorInput) editorInput).getFile();
    IDocument document = textEditor.getDocumentProvider().getDocument(editorInput);
    if (file == null || document == null)
        return;
    editorToFile.put(textEditor, file);
    editorToDocument.put(textEditor, document);
    boolean newlyOpenedFile = fileToEditors.put(file, textEditor);
    if (documentToEditors.put(document, textEditor)) {
        DocumentListener listener = makeDocumentListener(document);
        documentToListener.put(document, listener);
        document.addDocumentListener(listener);
        documentToFile.put(document, file);
    }
    if (newlyOpenedFile) {
        triggerFileOpenListener(file);
    }
}
Also used : IDocumentListener(org.eclipse.jface.text.IDocumentListener) NullDocumentListener(net.vtst.ow.eclipse.js.closure.util.listeners.NullDocumentListener) IFile(org.eclipse.core.resources.IFile) IFileEditorInput(org.eclipse.ui.IFileEditorInput) IEditorInput(org.eclipse.ui.IEditorInput) IDocument(org.eclipse.jface.text.IDocument)

Example 70 with IDocument

use of org.eclipse.jface.text.IDocument in project ow by vtst.

the class CompilationUnitProviderFromEclipseIFile method lastModified.

@Override
public long lastModified() {
    JavaScriptEditorRegistry editorRegistry = OwJsClosurePlugin.getDefault().getEditorRegistry();
    IDocument document = editorRegistry.getDocument(file);
    if (document == null)
        return file.getModificationStamp();
    else {
        // Be also careful to the listener.
        return editorRegistry.getLastModificationTime(document);
    }
}
Also used : IDocument(org.eclipse.jface.text.IDocument)

Aggregations

IDocument (org.eclipse.jface.text.IDocument)488 BadLocationException (org.eclipse.jface.text.BadLocationException)195 Document (org.eclipse.jface.text.Document)118 Test (org.junit.Test)93 IRegion (org.eclipse.jface.text.IRegion)72 Point (org.eclipse.swt.graphics.Point)63 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)54 Position (org.eclipse.jface.text.Position)51 ArrayList (java.util.ArrayList)44 CoreException (org.eclipse.core.runtime.CoreException)39 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)30 IPath (org.eclipse.core.runtime.IPath)26 IFile (org.eclipse.core.resources.IFile)25 IStatus (org.eclipse.core.runtime.IStatus)25 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)24 TextEdit (org.eclipse.text.edits.TextEdit)23 Region (org.eclipse.jface.text.Region)22 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)22 LinkedPositionGroup (org.eclipse.jface.text.link.LinkedPositionGroup)21 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)20