Java Code Examples for org.eclipse.core.filebuffers.ITextFileBufferManager#connect()
The following examples show how to use
org.eclipse.core.filebuffers.ITextFileBufferManager#connect() .
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: JavadocContentAccess2.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
/** * 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; }
Example 2
Source File: JavadocContentAccess2.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
/** * Reads the content of the java.io.File. * * @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 getFileContent(File file) throws CoreException { String content = null; ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager(); IPath fullPath = new Path(file.getAbsolutePath()); manager.connect(fullPath, LocationKind.LOCATION, null); try { ITextFileBuffer buffer = manager.getTextFileBuffer(fullPath, LocationKind.LOCATION); if (buffer != null) { content = buffer.getDocument().get(); } } finally { manager.disconnect(fullPath, LocationKind.LOCATION, null); } return content; }
Example 3
Source File: JsonRpcHelpers.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
/** * 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 4
Source File: CleanUpPostSaveListener.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
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 5
Source File: DocumentAdapter.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private void initialize() { ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager(); try { if (fFileStore != null) { manager.connectFileStore(fFileStore, new NullProgressMonitor()); fTextFileBuffer= manager.getFileStoreTextFileBuffer(fFileStore); } else { manager.connect(fPath, fLocationKind, new NullProgressMonitor()); fTextFileBuffer= manager.getTextFileBuffer(fPath, fLocationKind); } fDocument= fTextFileBuffer.getDocument(); } catch (CoreException x) { fDocument= manager.createEmptyDocument(fPath, fLocationKind); if (fDocument instanceof ISynchronizable) ((ISynchronizable)fDocument).setLockObject(new Object()); } fDocument.addDocumentListener(this); fIsClosed= false; }
Example 6
Source File: JavadocContentAccess2.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * 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; }
Example 7
Source File: JavadocContentAccess2.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * Reads the content of the java.io.File. * * @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 getFileContent(File file) throws CoreException { String content= null; ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager(); IPath fullPath= new Path(file.getAbsolutePath()); manager.connect(fullPath, LocationKind.LOCATION, null); try { ITextFileBuffer buffer= manager.getTextFileBuffer(fullPath, LocationKind.LOCATION); if (buffer != null) { content= buffer.getDocument().get(); } } finally { manager.disconnect(fullPath, LocationKind.LOCATION, null); } return content; }
Example 8
Source File: DocumentAdapter.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
public DocumentAdapter(IOpenable owner, IFile file) { fOwner = owner; fFile = file; fBufferListeners = new ArrayList<>(3); fIsClosed = false; ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager(); try { manager.connect(file.getFullPath(), LocationKind.IFILE, null); fTextFileBuffer= manager.getTextFileBuffer(file.getFullPath(), LocationKind.IFILE); } catch (CoreException e) { } }
Example 9
Source File: DeletePackageFragmentRootChange.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private static String getFileContents(IFile file) throws CoreException { ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager(); IPath path= file.getFullPath(); manager.connect(path, LocationKind.IFILE, new NullProgressMonitor()); try { return manager.getTextFileBuffer(path, LocationKind.IFILE).getDocument().get(); } finally { manager.disconnect(path, LocationKind.IFILE, new NullProgressMonitor()); } }
Example 10
Source File: TypeScriptResourceUtil.java From typescript.java with MIT License | 5 votes |
/** * 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 11
Source File: DeletePackageFragmentRootChange.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static String getFileContents(IFile file) throws CoreException { ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager(); IPath path= file.getFullPath(); manager.connect(path, LocationKind.IFILE, new NullProgressMonitor()); try { return manager.getTextFileBuffer(path, LocationKind.IFILE).getDocument().get(); } finally { manager.disconnect(path, LocationKind.IFILE, new NullProgressMonitor()); } }
Example 12
Source File: ModelReconcilationTest.java From goclipse with Eclipse Public License 1.0 | 5 votes |
public BufferFixture(ITextFileBufferManager fbm, IPath path, LocationKind locationKind, FixtureSourceModelManager sourceModelMgr) throws CoreException { this.path = path; this.locationKind = locationKind; this.sourceModelMgr = sourceModelMgr; this.fbm = assertNotNull(fbm); fbm.connect(path, locationKind, pm); buffer = fbm.getTextFileBuffer(path, locationKind); }
Example 13
Source File: AddJavaDocStubOperation.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private void addJavadocComments(IProgressMonitor monitor) throws CoreException { ICompilationUnit cu= fMembers[0].getCompilationUnit(); ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager(); IPath path= cu.getPath(); manager.connect(path, LocationKind.IFILE, new SubProgressMonitor(monitor, 1)); try { IDocument document= manager.getTextFileBuffer(path, LocationKind.IFILE).getDocument(); String lineDelim= TextUtilities.getDefaultLineDelimiter(document); MultiTextEdit edit= new MultiTextEdit(); for (int i= 0; i < fMembers.length; i++) { IMember curr= fMembers[i]; int memberStartOffset= getMemberStartOffset(curr, document); String comment= null; switch (curr.getElementType()) { case IJavaElement.TYPE: comment= createTypeComment((IType) curr, lineDelim); break; case IJavaElement.FIELD: comment= createFieldComment((IField) curr, lineDelim); break; case IJavaElement.METHOD: comment= createMethodComment((IMethod) curr, lineDelim); break; } if (comment == null) { StringBuffer buf= new StringBuffer(); buf.append("/**").append(lineDelim); //$NON-NLS-1$ buf.append(" *").append(lineDelim); //$NON-NLS-1$ buf.append(" */").append(lineDelim); //$NON-NLS-1$ comment= buf.toString(); } else { if (!comment.endsWith(lineDelim)) { comment= comment + lineDelim; } } final IJavaProject project= cu.getJavaProject(); IRegion region= document.getLineInformationOfOffset(memberStartOffset); String line= document.get(region.getOffset(), region.getLength()); String indentString= Strings.getIndentString(line, project); String indentedComment= Strings.changeIndent(comment, 0, project, indentString, lineDelim); edit.addChild(new InsertEdit(memberStartOffset, indentedComment)); monitor.worked(1); } edit.apply(document); // apply all edits } catch (BadLocationException e) { throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e)); } finally { manager.disconnect(path, LocationKind.IFILE,new SubProgressMonitor(monitor, 1)); } }
Example 14
Source File: ReplaceRefactoring.java From Pydev with Eclipse Public License 1.0 | 4 votes |
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; }