use of org.eclipse.core.filebuffers.ITextFileBuffer in project eclipse.platform.text by eclipse.
the class ReplaceRefactoring method createFileChange.
private TextChange createFileChange(IFile file, Pattern pattern, Set<FileMatch> matches, RefactoringStatus resultingStatus, Collection<MatchGroup> matchGroups) throws PatternSyntaxException, CoreException {
PositionTracker tracker = InternalSearchUI.getInstance().getPositionTracker();
TextFileChange change = new TextFileChange(Messages.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(Messages.format(SearchMessages.ReplaceRefactoring_error_accessing_file_buffer, file.getName()));
return null;
}
IDocument document = textFileBuffer.getDocument();
String lineDelimiter = TextUtilities.getDefaultLineDelimiter(document);
for (FileMatch match : matches) {
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(Messages.format(SearchMessages.ReplaceRefactoring_error_match_content_changed, file.getName()));
continue;
}
}
String originalText = getOriginalText(document, offset, length);
if (originalText == null) {
resultingStatus.addError(Messages.format(SearchMessages.ReplaceRefactoring_error_match_content_changed, file.getName()));
continue;
}
String replacementString = PatternConstructor.interpretReplaceEscapes(fReplaceString, originalText, lineDelimiter);
replacementString = computeReplacementString(pattern, document, offset, replacementString);
if (replacementString == null) {
resultingStatus.addError(Messages.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;
}
use of org.eclipse.core.filebuffers.ITextFileBuffer in project eclipse.platform.text by eclipse.
the class PositionTrackerTest method checkInsertAtZero.
private void checkInsertAtZero(AbstractTextSearchResult result, IFile file) throws PartInitException, BadLocationException {
Match[] matches = result.getMatches(file);
int[] originalStarts = new int[matches.length];
for (int i = 0; i < originalStarts.length; i++) {
originalStarts[i] = matches[i].getOffset();
}
try {
SearchTestPlugin.openTextEditor(SearchPlugin.getActivePage(), file);
ITextFileBuffer fb = FileBuffers.getTextFileBufferManager().getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
IDocument doc = fb.getDocument();
doc.replace(0, 0, "Test");
for (int i = 0; i < originalStarts.length; i++) {
Position currentPosition = InternalSearchUI.getInstance().getPositionTracker().getCurrentPosition(matches[i]);
assertNotNull(currentPosition);
assertEquals(originalStarts[i] + "Test".length(), currentPosition.getOffset());
}
} finally {
SearchPlugin.getActivePage().closeAllEditors(false);
}
}
use of org.eclipse.core.filebuffers.ITextFileBuffer in project eclipse.platform.text by eclipse.
the class LineConversionTest method testConvertToCharacter.
public void testConvertToCharacter() throws Exception {
SearchTestPlugin.openTextEditor(SearchPlugin.getActivePage(), fFile);
ITextFileBuffer fb = FileBuffers.getTextFileBufferManager().getTextFileBuffer(fFile.getFullPath(), LocationKind.IFILE);
IDocument doc = fb.getDocument();
Position p1 = new Position(2, 1);
Position p2 = PositionTracker.convertToCharacterPosition(p1, doc);
// assertEquals(LINE_THREE, doc.get(p2.getOffset(), p2.getLength()));
assertEquals(p1, PositionTracker.convertToLinePosition(p2, doc));
p1 = new Position(0, 1);
p2 = PositionTracker.convertToCharacterPosition(p1, doc);
assertEquals(LINE_ONE, doc.get(p2.getOffset(), p2.getLength()));
assertEquals(p1, PositionTracker.convertToLinePosition(p2, doc));
p1 = new Position(1, 1);
p2 = PositionTracker.convertToCharacterPosition(p1, doc);
assertEquals(LINE_TWO, doc.get(p2.getOffset(), p2.getLength()));
assertEquals(p1, PositionTracker.convertToLinePosition(p2, doc));
p1 = new Position(0, 0);
p2 = PositionTracker.convertToCharacterPosition(p1, doc);
assertEquals("", doc.get(p2.getOffset(), p2.getLength()));
assertEquals(p1, PositionTracker.convertToLinePosition(p2, doc));
}
use of org.eclipse.core.filebuffers.ITextFileBuffer in project eclipse.platform.text by eclipse.
the class FileStoreFileBufferFunctions method test12_1.
/*
* Test IFileBufferListener#bufferContentAboutToBeReplaced/replaced
*/
@Test
public void test12_1() throws Exception {
class Listener extends FileBufferListener {
public IFileBuffer preBuffer, postBuffer;
public int preCount, postCount;
@Override
public void bufferContentAboutToBeReplaced(IFileBuffer buffer) {
++preCount;
preBuffer = buffer;
}
@Override
public void bufferContentReplaced(IFileBuffer buffer) {
++postCount;
postBuffer = buffer;
}
}
Listener listener = new Listener();
fManager.addFileBufferListener(listener);
try {
ITextFileBuffer fileBuffer = fManager.getFileStoreTextFileBuffer(fFileStore);
assertTrue(listener.preCount == 0 && listener.postCount == 0 && listener.preBuffer == null && listener.postBuffer == null);
fManager.connectFileStore(fFileStore, null);
try {
fileBuffer = fManager.getFileStoreTextFileBuffer(fFileStore);
IDocument document = fileBuffer.getDocument();
document.replace(0, 0, "prefix");
fileBuffer.revert(null);
assertTrue(listener.preCount == 1);
assertTrue(listener.preBuffer == fileBuffer);
assertTrue(listener.postCount == 1);
assertTrue(listener.postBuffer == 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 FileStoreFileBufferFunctions method test18.
/*
* Test annotation model existence.
* ATTENTION: This test is only effective in a workspace that contains the "org.eclipse.ui.editors" bundle.
*/
@Test
public void test18() throws Exception {
fManager.connectFileStore(fFileStore, null);
try {
ITextFileBuffer buffer = fManager.getFileStoreTextFileBuffer(fFileStore);
assertNotNull(buffer);
Class<IAnnotationModel> clazz = getAnnotationModelClass();
if (clazz != null) {
IAnnotationModel model = buffer.getAnnotationModel();
assertTrue(clazz.isInstance(model));
}
} finally {
fManager.disconnectFileStore(fFileStore, null);
}
}
Aggregations