Search in sources :

Example 36 with Position

use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.

the class PositionTracker method dirtyStateChanged.

@Override
public void dirtyStateChanged(IFileBuffer buffer, boolean isDirty) {
    if (isDirty)
        return;
    final int[] trackCount = new int[1];
    doForExistingMatchesIn(buffer, new IFileBufferMatchOperation() {

        @Override
        public void run(ITextFileBuffer textBuffer, Match match) {
            trackCount[0]++;
            Position pos = fMatchesToPositions.get(match);
            if (pos != null) {
                if (pos.isDeleted()) {
                    AbstractTextSearchResult result = fMatchesToSearchResults.get(match);
                    // might be that the containing element has been removed.
                    if (result != null) {
                        result.removeMatch(match);
                    }
                    untrackPosition(textBuffer, match);
                } else {
                    if (match.getBaseUnit() == Match.UNIT_LINE) {
                        try {
                            pos = convertToLinePosition(pos, textBuffer.getDocument());
                        } catch (BadLocationException e) {
                            SearchPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, SearchPlugin.getID(), 0, e.getLocalizedMessage(), e));
                        }
                    }
                    match.setOffset(pos.getOffset());
                    match.setLength(pos.getLength());
                }
            }
        }
    });
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) Position(org.eclipse.jface.text.Position) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) AbstractTextSearchResult(org.eclipse.search.ui.text.AbstractTextSearchResult) BadLocationException(org.eclipse.jface.text.BadLocationException) Match(org.eclipse.search.ui.text.Match)

Example 37 with Position

use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.

the class PositionTracker method trackPosition.

private void trackPosition(AbstractTextSearchResult result, ITextFileBuffer fb, Match match) {
    int offset = match.getOffset();
    int length = match.getLength();
    if (offset < 0 || length < 0)
        return;
    try {
        IDocument doc = fb.getDocument();
        Position position = new Position(offset, length);
        if (match.getBaseUnit() == Match.UNIT_LINE) {
            position = convertToCharacterPosition(position, doc);
        }
        doc.addPosition(position);
        fMatchesToSearchResults.put(match, result);
        fMatchesToPositions.put(match, position);
        addFileBufferMapping(fb, match);
    } catch (BadLocationException e) {
        // the match is outside the document
        result.removeMatch(match);
    }
}
Also used : Position(org.eclipse.jface.text.Position) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 38 with Position

use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.

the class PositionTracker method getCurrentPosition.

public Position getCurrentPosition(Match match) {
    Position pos = fMatchesToPositions.get(match);
    if (pos == null)
        return pos;
    AbstractTextSearchResult result = fMatchesToSearchResults.get(match);
    if (match.getBaseUnit() == Match.UNIT_LINE && result != null) {
        ITextFileBuffer fb = getTrackedFileBuffer(result, match.getElement());
        if (fb != null) {
            IDocument doc = fb.getDocument();
            try {
                pos = convertToLinePosition(pos, doc);
            } catch (BadLocationException e) {
            }
        }
    }
    return pos;
}
Also used : Position(org.eclipse.jface.text.Position) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) AbstractTextSearchResult(org.eclipse.search.ui.text.AbstractTextSearchResult) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 39 with Position

use of org.eclipse.jface.text.Position 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 40 with Position

use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.

the class AnnotationManagerTest method testAddAnnotation.

@Test
public void testAddAnnotation() throws Exception {
    NewSearchUI.runQueryInForeground(null, fQuery1);
    AbstractTextSearchResult result = (AbstractTextSearchResult) fQuery1.getSearchResult();
    Object[] files = result.getElements();
    try {
        for (int i = 0; i < files.length; i++) {
            IFile file = (IFile) files[i];
            ITextEditor editor = (ITextEditor) SearchTestPlugin.openTextEditor(SearchPlugin.getActivePage(), file);
            IAnnotationModel annotationModel = editor.getDocumentProvider().getAnnotationModel(editor.getEditorInput());
            annotationModel.getAnnotationIterator();
            HashSet<Position> positions = new HashSet<>();
            for (Iterator<Annotation> iter = annotationModel.getAnnotationIterator(); iter.hasNext(); ) {
                Annotation annotation = iter.next();
                if (annotation.getType().equals(fAnnotationTypeLookup.getAnnotationType(NewSearchUI.SEARCH_MARKER, IMarker.SEVERITY_INFO))) {
                    positions.add(annotationModel.getPosition(annotation));
                }
            }
            Match[] matches = result.getMatches(file);
            for (int j = 0; j < matches.length; j++) {
                Position position = new Position(matches[j].getOffset(), matches[j].getLength());
                // $NON-NLS-1$
                assertTrue("position not found at: " + j, positions.remove(position));
            }
            assertEquals(0, positions.size());
        }
    } finally {
        SearchPlugin.getActivePage().closeAllEditors(false);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) Position(org.eclipse.jface.text.Position) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) AbstractTextSearchResult(org.eclipse.search.ui.text.AbstractTextSearchResult) Annotation(org.eclipse.jface.text.source.Annotation) FileMatch(org.eclipse.search.internal.ui.text.FileMatch) Match(org.eclipse.search.ui.text.Match) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Position (org.eclipse.jface.text.Position)421 BadLocationException (org.eclipse.jface.text.BadLocationException)145 Annotation (org.eclipse.jface.text.source.Annotation)110 IDocument (org.eclipse.jface.text.IDocument)80 ArrayList (java.util.ArrayList)75 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)57 IRegion (org.eclipse.jface.text.IRegion)55 Test (org.junit.Test)54 HashMap (java.util.HashMap)49 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)46 Point (org.eclipse.swt.graphics.Point)40 List (java.util.List)39 Iterator (java.util.Iterator)31 TypedPosition (org.eclipse.jface.text.TypedPosition)31 Region (org.eclipse.jface.text.Region)27 IFile (org.eclipse.core.resources.IFile)22 IAnnotationModelExtension (org.eclipse.jface.text.source.IAnnotationModelExtension)21 ProjectionAnnotation (org.eclipse.jface.text.source.projection.ProjectionAnnotation)21 Map (java.util.Map)15 CoreException (org.eclipse.core.runtime.CoreException)15