Search in sources :

Example 76 with ITextFileBuffer

use of org.eclipse.core.filebuffers.ITextFileBuffer in project che by eclipse.

the class TextFileBufferManager method getTextFileBuffer.

//
//	/*
//	 * @see org.eclipse.core.filebuffers.ITextFileBufferManager#getFileStoreTextFileBuffer(org.eclipse.core.filesystem.IFileStore)
//	 * @since 3.3
//	 */
//	public ITextFileBuffer getFileStoreTextFileBuffer(IFileStore fileStore) {
//		Assert.isLegal(fileStore != null);
//		return (ITextFileBuffer)getFileStoreFileBuffer(fileStore);
//	}
/*
	 * @see org.eclipse.core.filebuffers.ITextFileBufferManager#getTextFileBuffer(org.eclipse.jface.text.IDocument)
	 * @since 3.3
	 */
public ITextFileBuffer getTextFileBuffer(IDocument document) {
    Assert.isLegal(document != null);
    Iterator iter;
    synchronized (fFilesBuffers) {
        iter = new ArrayList(fFilesBuffers.values()).iterator();
    }
    while (iter.hasNext()) {
        Object buffer = iter.next();
        if (buffer instanceof ITextFileBuffer) {
            ITextFileBuffer textFileBuffer = (ITextFileBuffer) buffer;
            if (textFileBuffer.getDocument() == document) {
                if (!((AbstractFileBuffer) textFileBuffer).isDisconnected())
                    return textFileBuffer;
                return null;
            }
        }
    }
    synchronized (fFileStoreFileBuffers) {
        iter = new ArrayList(fFileStoreFileBuffers.values()).iterator();
    }
    while (iter.hasNext()) {
        Object buffer = iter.next();
        if (buffer instanceof ITextFileBuffer) {
            ITextFileBuffer textFileBuffer = (ITextFileBuffer) buffer;
            if (textFileBuffer.getDocument() == document) {
                if (!((AbstractFileBuffer) textFileBuffer).isDisconnected())
                    return textFileBuffer;
            }
        }
    }
    return null;
}
Also used : Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer)

Example 77 with ITextFileBuffer

use of org.eclipse.core.filebuffers.ITextFileBuffer in project che by eclipse.

the class JavadocContentAccess2 method getIFileContent.

/**
     * Reads the content of the IFile.
     *
     * @param file the file whose content has to be read
     * @return the content of the file
     * @throws CoreException if the file could not be successfully connected or disconnected
     */
private static String getIFileContent(IFile file) throws CoreException {
    String content = null;
    ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
    IPath fullPath = file.getFullPath();
    manager.connect(fullPath, LocationKind.IFILE, null);
    try {
        ITextFileBuffer buffer = manager.getTextFileBuffer(fullPath, LocationKind.IFILE);
        if (buffer != null) {
            content = buffer.getDocument().get();
        }
    } finally {
        manager.disconnect(fullPath, LocationKind.IFILE, null);
    }
    return content;
}
Also used : IPath(org.eclipse.core.runtime.IPath) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer)

Example 78 with ITextFileBuffer

use of org.eclipse.core.filebuffers.ITextFileBuffer in project che by eclipse.

the class CodeAssist method computeAssistProposals.

@SuppressWarnings("unchecked")
public Proposals computeAssistProposals(IJavaProject project, String fqn, int offset, List<Problem> problems) throws CoreException {
    ICompilationUnit compilationUnit;
    IType type = project.findType(fqn);
    if (type == null) {
        return null;
    }
    if (type.isBinary()) {
        throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.CORE_EXCEPTION, "Can't calculate Quick Assist on binary file"));
    } else {
        compilationUnit = type.getCompilationUnit();
    }
    IBuffer buffer = compilationUnit.getBuffer();
    ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
    bufferManager.connect(compilationUnit.getPath(), LocationKind.IFILE, new NullProgressMonitor());
    ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(compilationUnit.getPath(), LocationKind.IFILE);
    IDocument document = textFileBuffer.getDocument();
    TextViewer viewer = new TextViewer(document, new Point(offset, 0));
    AssistContext context = new AssistContext(compilationUnit, offset, 0);
    ArrayList proposals = new ArrayList<>();
    JavaCorrectionProcessor.collectProposals(context, problems, true, true, proposals);
    return convertProposals(offset, compilationUnit, viewer, proposals);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JavaModelException(org.eclipse.jdt.core.JavaModelException) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) ArrayList(java.util.ArrayList) JavaModelStatus(org.eclipse.jdt.internal.core.JavaModelStatus) Point(org.eclipse.swt.graphics.Point) IBuffer(org.eclipse.jdt.core.IBuffer) IType(org.eclipse.jdt.core.IType) TextViewer(org.eclipse.che.jdt.javaeditor.TextViewer) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IDocument(org.eclipse.jface.text.IDocument)

Example 79 with ITextFileBuffer

use of org.eclipse.core.filebuffers.ITextFileBuffer in project AutoRefactor by JnRouvignac.

the class ApplyRefactoringsJob method applyRefactoring.

private void applyRefactoring(ICompilationUnit compilationUnit, AggregateASTVisitor refactoringToApply, JavaProjectOptions options, SubMonitor 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 80 with ITextFileBuffer

use of org.eclipse.core.filebuffers.ITextFileBuffer in project eclipse.platform.text by eclipse.

the class SpellCheckDocumentListener method documentChanged.

@Override
public void documentChanged(final DocumentEvent event) {
    if (this.lastJob != null) {
        this.lastJob.cancel();
    }
    this.lastJob = new Job("Spellcheck") {

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            ITextFileBuffer iTextFileBuffer = ITextFileBufferManager.DEFAULT.getTextFileBuffer(event.getDocument());
            if (iTextFileBuffer == null) {
                return Status.CANCEL_STATUS;
            }
            IAnnotationModel model = iTextFileBuffer.getAnnotationModel();
            String text = event.getDocument().get();
            int commentStart = text.indexOf("<comment>");
            if (commentStart < 0) {
                return Status.OK_STATUS;
            }
            commentStart += "<comment>".length();
            int commentEnd = text.indexOf("</comment>", commentStart);
            if (commentEnd <= commentStart) {
                return Status.OK_STATUS;
            }
            Region region = new Region(commentStart, commentEnd - commentStart);
            service.check(event.getDocument(), new Region[] { region }, new SpellingContext(), new ISpellingProblemCollector() {

                private Map<SpellingAnnotation, Position> annotations = new HashMap<>();

                @Override
                public void endCollecting() {
                    Set<SpellingAnnotation> previous = new HashSet<>();
                    model.getAnnotationIterator().forEachRemaining(annotation -> {
                        if (annotation instanceof SpellingAnnotation) {
                            previous.add((SpellingAnnotation) annotation);
                        }
                    });
                    if (model instanceof IAnnotationModelExtension) {
                        ((IAnnotationModelExtension) model).replaceAnnotations(previous.toArray(new SpellingAnnotation[previous.size()]), annotations);
                    } else {
                        previous.forEach(model::removeAnnotation);
                        annotations.forEach(model::addAnnotation);
                    }
                }

                @Override
                public void beginCollecting() {
                }

                @Override
                public void accept(SpellingProblem problem) {
                    this.annotations.put(new SpellingAnnotation(problem), new Position(problem.getOffset(), problem.getLength()));
                }
            }, monitor);
            return Status.OK_STATUS;
        }
    };
    this.lastJob.setUser(false);
    this.lastJob.setPriority(Job.DECORATE);
    // set a delay before reacting to user action to handle continuous typing
    this.lastJob.schedule(500);
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Position(org.eclipse.jface.text.Position) ISpellingProblemCollector(org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) SpellingProblem(org.eclipse.ui.texteditor.spelling.SpellingProblem) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) SpellingAnnotation(org.eclipse.ui.texteditor.spelling.SpellingAnnotation) SpellingContext(org.eclipse.ui.texteditor.spelling.SpellingContext) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) Region(org.eclipse.jface.text.Region) Job(org.eclipse.core.runtime.jobs.Job) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)133 Test (org.junit.Test)85 IDocument (org.eclipse.jface.text.IDocument)54 IFileBuffer (org.eclipse.core.filebuffers.IFileBuffer)37 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)35 IPath (org.eclipse.core.runtime.IPath)31 IFile (org.eclipse.core.resources.IFile)18 BadLocationException (org.eclipse.jface.text.BadLocationException)15 IFileStore (org.eclipse.core.filesystem.IFileStore)9 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)9 CoreException (org.eclipse.core.runtime.CoreException)8 Position (org.eclipse.jface.text.Position)8 IFileInfo (org.eclipse.core.filesystem.IFileInfo)7 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)7 Match (org.eclipse.search.ui.text.Match)7 IFolder (org.eclipse.core.resources.IFolder)5 IStatus (org.eclipse.core.runtime.IStatus)5 Path (org.eclipse.core.runtime.Path)5 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)5 Status (org.eclipse.core.runtime.Status)4