Search in sources :

Example 56 with ITextFileBuffer

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;
}
Also used : ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) Position(org.eclipse.jface.text.Position) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) TextEditChangeGroup(org.eclipse.ltk.core.refactoring.TextEditChangeGroup) PositionTracker(org.eclipse.search2.internal.ui.text.PositionTracker) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) TextEditGroup(org.eclipse.text.edits.TextEditGroup) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) IDocument(org.eclipse.jface.text.IDocument)

Example 57 with ITextFileBuffer

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);
    }
}
Also used : Position(org.eclipse.jface.text.Position) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IDocument(org.eclipse.jface.text.IDocument) Match(org.eclipse.search.ui.text.Match)

Example 58 with ITextFileBuffer

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));
}
Also used : Position(org.eclipse.jface.text.Position) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IDocument(org.eclipse.jface.text.IDocument)

Example 59 with ITextFileBuffer

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);
    }
}
Also used : IFileBuffer(org.eclipse.core.filebuffers.IFileBuffer) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IDocument(org.eclipse.jface.text.IDocument) Test(org.junit.Test)

Example 60 with ITextFileBuffer

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);
    }
}
Also used : ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Test(org.junit.Test)

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