Java Code Examples for org.eclipse.core.filebuffers.ITextFileBuffer#getDocument()

The following examples show how to use org.eclipse.core.filebuffers.ITextFileBuffer#getDocument() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonRpcHelpers.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Returns an {@link IDocument} for the given {@link IFile}.
 *
 * @param file an {@link IFile}
 * @return a document with the contents of the file,
 * or <code>null</code> if the file can not be opened.
 */
public static IDocument toDocument(IFile file) {
	if (file != null && file.isAccessible()) {
		IPath path = file.getFullPath();
		ITextFileBufferManager fileBufferManager = FileBuffers.getTextFileBufferManager();
		LocationKind kind = LocationKind.IFILE;
		try {
			fileBufferManager.connect(path, kind, new NullProgressMonitor());
			ITextFileBuffer fileBuffer = fileBufferManager.getTextFileBuffer(path, kind);
			if (fileBuffer != null) {
				return fileBuffer.getDocument();
			}
		} catch (CoreException e) {
			JavaLanguageServerPlugin.logException("Failed to convert "+ file +"  to an IDocument", e);
		} finally {
			try {
				fileBufferManager.disconnect(path, kind, new NullProgressMonitor());
			} catch (CoreException slurp) {
				//Don't care
			}
		}
	}
	return null;
}
 
Example 2
Source File: CleanUpPostSaveListener.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private long getDocumentStamp(IFile file, IProgressMonitor monitor) throws CoreException {
 final ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
 final IPath path= file.getFullPath();

 monitor.beginTask("", 2); //$NON-NLS-1$

 ITextFileBuffer buffer= null;
 try {
 	manager.connect(path, LocationKind.IFILE, new SubProgressMonitor(monitor, 1));
  buffer= manager.getTextFileBuffer(path, LocationKind.IFILE);
	    IDocument document= buffer.getDocument();

	    if (document instanceof IDocumentExtension4) {
			return ((IDocumentExtension4)document).getModificationStamp();
		} else {
			return file.getModificationStamp();
		}
 } finally {
 	if (buffer != null)
 		manager.disconnect(path, LocationKind.IFILE, new SubProgressMonitor(monitor, 1));
 	monitor.done();
 }
}
 
Example 3
Source File: PyChange.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
public long getModificationStamp(IResource resource) {
    if (!(resource instanceof IFile)) {
        return resource.getModificationStamp();
    }
    IFile file = (IFile) resource;
    ITextFileBuffer buffer = getBuffer(file);
    if (buffer == null) {
        return file.getModificationStamp();
    } else {
        IDocument document = buffer.getDocument();
        if (document instanceof IDocumentExtension4) {
            return ((IDocumentExtension4) document).getModificationStamp();
        } else {
            return file.getModificationStamp();
        }
    }
}
 
Example 4
Source File: TextSearchVisitor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private IDocument getOpenDocument(IFile file, Map<IFile, IDocument> documentsInEditors) {
	IDocument document= documentsInEditors.get(file);
	if (document == null) {
		ITextFileBufferManager bufferManager= FileBuffers.getTextFileBufferManager();
		ITextFileBuffer textFileBuffer= bufferManager.getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
		if (textFileBuffer != null) {
			document= textFileBuffer.getDocument();
		}
	}
	return document;
}
 
Example 5
Source File: TypeScriptResourceUtil.java    From typescript.java with MIT License 5 votes vote down vote up
/**
 * Returns the {@link IDocument} from the given file and null if it's not
 * possible.
 */
public static IDocument getDocument(IPath location) {
	ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();

	boolean connected = false;
	try {
		ITextFileBuffer buffer = manager.getTextFileBuffer(location, LocationKind.NORMALIZE);
		if (buffer == null) {
			// no existing file buffer..create one
			manager.connect(location, LocationKind.NORMALIZE, new NullProgressMonitor());
			connected = true;
			buffer = manager.getTextFileBuffer(location, LocationKind.NORMALIZE);
			if (buffer == null) {
				return null;
			}
		}

		return buffer.getDocument();
	} catch (CoreException ce) {
		TypeScriptCorePlugin.logError(ce, "Error while getting document from file");
		return null;
	} finally {
		if (connected) {
			try {
				manager.disconnect(location, LocationKind.NORMALIZE, new NullProgressMonitor());
			} catch (CoreException e) {
				TypeScriptCorePlugin.logError(e, "Error while getting document from file");
			}
		}
	}
}
 
Example 6
Source File: FileUtils.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @return null if it was unable to get the document from the path (this may happen if it was not refreshed).
 * Or the document that represents the file
 */
public static IDocument getDocFromPath(IPath path) {
    ITextFileBuffer buffer = getBufferFromPath(path);
    if (buffer != null) {
        return buffer.getDocument();
    }
    return null;
}
 
Example 7
Source File: ChangedLinesComputer.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Return the lines which have changed in the given buffer since the last save occurred. 
 * 
 * @param buffer the buffer to compare contents from
 * @param monitor to report progress to
 * @return the regions of the changed lines or null if something went wrong.
 */
public static int[] calculateChangedLines(final ITextFileBuffer buffer, final IProgressMonitor monitor)
        throws CoreException {
    int[] result = null;

    try {
        monitor.beginTask("Calculating changed lines", 20);
        IFileStore fileStore = buffer.getFileStore();

        ITextFileBufferManager fileBufferManager = FileBuffers.createTextFileBufferManager();
        fileBufferManager.connectFileStore(fileStore, getSubProgressMonitor(monitor, 15));
        try {
            IDocument currentDocument = buffer.getDocument();
            IDocument oldDocument = ((ITextFileBuffer) fileBufferManager.getFileStoreFileBuffer(fileStore))
                    .getDocument();

            result = getChangedLines(oldDocument, currentDocument);
        } finally {
            fileBufferManager.disconnectFileStore(fileStore, getSubProgressMonitor(monitor, 5));
            monitor.done();
        }
    } catch (Exception e) {
        Log.log(e);
        return null;
    }

    return result;
}
 
Example 8
Source File: ReplaceRefactoring.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
private TextChange createFileChange(IFile file, Pattern pattern, Collection<Match> matches,
        RefactoringStatus resultingStatus, Collection<MatchGroup> matchGroups)
                throws PatternSyntaxException, CoreException {
    PositionTracker tracker = InternalSearchUI.getInstance().getPositionTracker();

    TextFileChange change = new SynchronizedTextFileChange(MessageFormat.format(
            SearchMessages.ReplaceRefactoring_group_label_change_for_file, file.getName()), file);
    change.setEdit(new MultiTextEdit());

    ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
    manager.connect(file.getFullPath(), LocationKind.IFILE, null);
    try {
        ITextFileBuffer textFileBuffer = manager.getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
        if (textFileBuffer == null) {
            resultingStatus
                    .addError(MessageFormat.format(SearchMessages.ReplaceRefactoring_error_accessing_file_buffer,
                            file.getName()));
            return null;
        }
        IDocument document = textFileBuffer.getDocument();
        String lineDelimiter = TextUtilities.getDefaultLineDelimiter(document);

        for (Iterator<Match> iterator = matches.iterator(); iterator.hasNext();) {
            Match match = iterator.next();
            int offset = match.getOffset();
            int length = match.getLength();
            Position currentPosition = tracker.getCurrentPosition(match);
            if (currentPosition != null) {
                offset = currentPosition.offset;
                if (length != currentPosition.length) {
                    resultingStatus.addError(MessageFormat.format(
                            SearchMessages.ReplaceRefactoring_error_match_content_changed, file.getName()));
                    continue;
                }
            }

            String originalText = getOriginalText(document, offset, length);
            if (originalText == null) {
                resultingStatus.addError(MessageFormat.format(
                        SearchMessages.ReplaceRefactoring_error_match_content_changed, file.getName()));
                continue;
            }

            String replacementString = computeReplacementString(pattern, originalText, fReplaceString,
                    lineDelimiter);
            if (replacementString == null) {
                resultingStatus.addError(MessageFormat.format(
                        SearchMessages.ReplaceRefactoring_error_match_content_changed, file.getName()));
                continue;
            }

            ReplaceEdit replaceEdit = new ReplaceEdit(offset, length, replacementString);
            change.addEdit(replaceEdit);
            TextEditChangeGroup textEditChangeGroup = new TextEditChangeGroup(change, new TextEditGroup(
                    SearchMessages.ReplaceRefactoring_group_label_match_replace, replaceEdit));
            change.addTextEditChangeGroup(textEditChangeGroup);
            matchGroups.add(new MatchGroup(textEditChangeGroup, match));
        }
    } finally {
        manager.disconnect(file.getFullPath(), LocationKind.IFILE, null);
    }
    return change;
}