use of org.eclipse.core.filebuffers.ITextFileBuffer in project eclipse.platform.text by eclipse.
the class FileStoreFileBufferFunctions method test17_3.
/*
* Test IFileBufferListener#stateChanging for internal changes
*/
@Test
public void test17_3() throws Exception {
class Listener extends FileBufferListener {
public IFileBuffer buffer;
public int count;
@Override
public void stateChanging(IFileBuffer buf) {
++count;
this.buffer = buf;
}
}
Listener listener = new Listener();
fManager.addFileBufferListener(listener);
try {
ITextFileBuffer fileBuffer = fManager.getFileStoreTextFileBuffer(fFileStore);
assertTrue(listener.count == 0 && listener.buffer == null);
fManager.connectFileStore(fFileStore, null);
try {
fileBuffer = fManager.getFileStoreTextFileBuffer(fFileStore);
IDocument document = fileBuffer.getDocument();
document.replace(0, 0, "prefix");
fileBuffer.commit(null, true);
assertTrue(listener.count == 1);
assertTrue(listener.buffer == fileBuffer);
} finally {
fManager.disconnectFileStore(fFileStore, null);
}
} finally {
fManager.removeFileBufferListener(listener);
}
}
use of org.eclipse.core.filebuffers.ITextFileBuffer in project eclipse.platform.text by eclipse.
the class FileStoreFileBuffersForNonExistingWorkspaceFiles method testBug118199_fixed.
@Test
public void testBug118199_fixed() throws Exception {
IPath location = getPath();
IFile file = getProject().getWorkspace().getRoot().getFileForLocation(location);
if (file == null) {
// $NON-NLS-1$ //$NON-NLS-2$
throw new IOException("File '" + location + "' can not be found.");
}
IPath path = file.getFullPath();
assertFalse(file.exists());
fManager.connect(path, LocationKind.IFILE, null);
try {
ITextFileBuffer buffer = fManager.getTextFileBuffer(path, LocationKind.IFILE);
buffer.getDocument().set("test");
buffer.commit(null, false);
} finally {
fManager.disconnect(path, LocationKind.IFILE, null);
}
assertTrue(file.exists());
}
use of org.eclipse.core.filebuffers.ITextFileBuffer in project linuxtools by eclipse.
the class GNUHyperlinkDetector method detectHyperlinks.
/**
* Detector using RuleBasedScanner.
*/
@Override
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
if (documentLocation == null) {
ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
ITextFileBuffer buffer = bufferManager.getTextFileBuffer(textViewer.getDocument());
if (buffer == null) {
return null;
}
documentLocation = buffer.getLocation().removeLastSegments(1);
}
IDocument thisDoc = textViewer.getDocument();
GNUHyperlinkScanner scanner = new GNUHyperlinkScanner();
ITypedRegion partitionInfo = null;
try {
partitionInfo = thisDoc.getPartition(region.getOffset());
} catch (org.eclipse.jface.text.BadLocationException e1) {
e1.printStackTrace();
return null;
}
scanner.setRange(thisDoc, partitionInfo.getOffset(), partitionInfo.getLength());
Token tmpToken = (Token) scanner.nextToken();
String tokenStr = (String) tmpToken.getData();
if (tokenStr == null) {
return null;
}
// null.
while (region.getOffset() < scanner.getTokenOffset() || region.getOffset() > scanner.getOffset() || tokenStr.equals("_other")) {
tmpToken = (Token) scanner.nextToken();
tokenStr = (String) tmpToken.getData();
if (tokenStr == null)
return null;
}
Region tokenRegion = new Region(scanner.getTokenOffset(), scanner.getTokenLength());
String line = "";
try {
line = thisDoc.get(tokenRegion.getOffset(), tokenRegion.getLength());
} catch (org.eclipse.jface.text.BadLocationException e1) {
e1.printStackTrace();
return null;
}
// process file link
if (tokenStr.equals(GNUHyperlinkScanner.FILE_NAME)) {
Region pathRegion = null;
int lineOffset = 0;
// cut "* " if necessary
if (line.startsWith("* ")) {
lineOffset = 2;
line = line.substring(2);
}
pathRegion = new Region(tokenRegion.getOffset() + lineOffset, line.length());
if (documentLocation == null)
return null;
// Replace any escape characters added to name
line = line.replaceAll("\\\\(.)", "$1");
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IFile fileLoc = (IFile) root.findMember(documentLocation.append(line));
if (fileLoc != null && fileLoc.exists()) {
return new IHyperlink[] { new FileHyperlink(pathRegion, fileLoc) };
}
}
return null;
}
use of org.eclipse.core.filebuffers.ITextFileBuffer in project eclipse-pmd by acanda.
the class JavaQuickFix method fixMarkersInFile.
/**
* Fixes all provided markers in a file.
*
* @param markers The markers to fix. There is at least one marker in this collection and all markers can be fixed
* by this quick fix.
*/
protected void fixMarkersInFile(final IFile file, final List<IMarker> markers, final IProgressMonitor monitor) {
monitor.subTask(file.getFullPath().toOSString());
final Optional<ICompilationUnit> optionalCompilationUnit = getCompilationUnit(file);
if (!optionalCompilationUnit.isPresent()) {
return;
}
final ICompilationUnit compilationUnit = optionalCompilationUnit.get();
ITextFileBufferManager bufferManager = null;
final IPath path = compilationUnit.getPath();
try {
bufferManager = FileBuffers.getTextFileBufferManager();
bufferManager.connect(path, LocationKind.IFILE, null);
final ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path, LocationKind.IFILE);
final IDocument document = textFileBuffer.getDocument();
final IAnnotationModel annotationModel = textFileBuffer.getAnnotationModel();
final ASTParser astParser = ASTParser.newParser(AST.JLS4);
astParser.setKind(ASTParser.K_COMPILATION_UNIT);
astParser.setResolveBindings(needsTypeResolution());
astParser.setSource(compilationUnit);
final SubProgressMonitor parserMonitor = new SubProgressMonitor(monitor, 100);
final CompilationUnit ast = (CompilationUnit) astParser.createAST(parserMonitor);
parserMonitor.done();
startFixingMarkers(ast);
final Map<?, ?> options = compilationUnit.getJavaProject().getOptions(true);
for (final IMarker marker : markers) {
try {
final MarkerAnnotation annotation = getMarkerAnnotation(annotationModel, marker);
// if the annotation is null it means that is was deleted by a previous quick fix
if (annotation != null) {
final Optional<T> node = getNodeFinder(annotationModel.getPosition(annotation)).findNode(ast);
if (node.isPresent()) {
final boolean isSuccessful = fixMarker(node.get(), document, options);
if (isSuccessful) {
marker.delete();
}
}
}
} finally {
monitor.worked(100);
}
}
finishFixingMarkers(ast, document, options);
// commit changes to underlying file if it is not opened in an editor
if (!isEditorOpen(file)) {
final SubProgressMonitor commitMonitor = new SubProgressMonitor(monitor, 100);
textFileBuffer.commit(commitMonitor, false);
commitMonitor.done();
} else {
monitor.worked(100);
}
} catch (CoreException | MalformedTreeException | BadLocationException e) {
// TODO: log error
// PMDPlugin.getDefault().error("Error processing quickfix", e);
} finally {
if (bufferManager != null) {
try {
bufferManager.disconnect(path, LocationKind.IFILE, null);
} catch (final CoreException e) {
// TODO: log error
// PMDPlugin.getDefault().error("Error processing quickfix", e);
}
}
}
}
use of org.eclipse.core.filebuffers.ITextFileBuffer in project che by eclipse.
the class CodeRefactoringUtil method getIndentationLevel.
public static int getIndentationLevel(ASTNode node, ICompilationUnit unit) throws CoreException {
IPath fullPath = unit.getCorrespondingResource().getFullPath();
try {
FileBuffers.getTextFileBufferManager().connect(fullPath, LocationKind.IFILE, new NullProgressMonitor());
ITextFileBuffer buffer = FileBuffers.getTextFileBufferManager().getTextFileBuffer(fullPath, LocationKind.IFILE);
try {
IRegion region = buffer.getDocument().getLineInformationOfOffset(node.getStartPosition());
return Strings.computeIndentUnits(buffer.getDocument().get(region.getOffset(), region.getLength()), unit.getJavaProject());
} catch (BadLocationException exception) {
JavaPlugin.log(exception);
}
return 0;
} finally {
FileBuffers.getTextFileBufferManager().disconnect(fullPath, LocationKind.IFILE, new NullProgressMonitor());
}
}
Aggregations