Search in sources :

Example 1 with PositionTracker

use of org.eclipse.search2.internal.ui.text.PositionTracker 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 2 with PositionTracker

use of org.eclipse.search2.internal.ui.text.PositionTracker in project eclipse.platform.text by eclipse.

the class AbstractTextSearchViewPage method getCurrentMatchLocation.

/**
 * Returns the current location of the match. This takes possible
 * modifications of the file into account. Therefore the result may
 * differ from the position information that can be obtained directly
 * off the match.
 * @param match the match to get the position for.
 * @return the current position of the match.
 *
 * @since 3.2
 */
public IRegion getCurrentMatchLocation(Match match) {
    PositionTracker tracker = InternalSearchUI.getInstance().getPositionTracker();
    int offset, length;
    Position pos = tracker.getCurrentPosition(match);
    if (pos == null) {
        offset = match.getOffset();
        length = match.getLength();
    } else {
        offset = pos.getOffset();
        length = pos.getLength();
    }
    return new Region(offset, length);
}
Also used : PositionTracker(org.eclipse.search2.internal.ui.text.PositionTracker) Position(org.eclipse.jface.text.Position) IRegion(org.eclipse.jface.text.IRegion) Region(org.eclipse.jface.text.Region)

Aggregations

Position (org.eclipse.jface.text.Position)2 PositionTracker (org.eclipse.search2.internal.ui.text.PositionTracker)2 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)1 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)1 IDocument (org.eclipse.jface.text.IDocument)1 IRegion (org.eclipse.jface.text.IRegion)1 Region (org.eclipse.jface.text.Region)1 TextEditChangeGroup (org.eclipse.ltk.core.refactoring.TextEditChangeGroup)1 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)1 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)1 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)1 TextEditGroup (org.eclipse.text.edits.TextEditGroup)1