private boolean doRemoveMatch(Match match) { boolean existed= false; List matches= (List) fElementsToMatches.get(match.getElement()); if (matches != null) { existed= matches.remove(match); if (matches.isEmpty()) fElementsToMatches.remove(match.getElement()); } return existed; }
public static Match findExactMatch(IDocument document, Match match, IEditorPart editor) { if (match.getOffset() == -1 && match.getBaseUnit() == Match.UNIT_LINE) return new Match(match.getElement(), Match.UNIT_CHARACTER, 0, 0); IPluginObject element = (IPluginObject) match.getElement(); String name = null; String value = null; IRegion region = null; // since Extension and Extension point matches don't contain line #'s, we need handle them differently (by trying to find matches in UI model) if (editor instanceof ManifestEditor && (element instanceof IPluginExtension || element instanceof IPluginExtensionPoint)) { region = getAttributeMatch((ManifestEditor) editor, element, document); } else { if (element instanceof IPluginImport) { name = "plugin"; //$NON-NLS-1$ value = ((IPluginImport) element).getId(); } else if (element instanceof IPlugin) { name = "id"; //$NON-NLS-1$ value = ((IPlugin) element).getId(); } else if (element instanceof IFragment) { name = "id"; //$NON-NLS-1$ value = ((IFragment) element).getId(); } region = getAttributeRegionForLine(document, name, value, match.getOffset()); } if (region != null) { return new Match(element, Match.UNIT_CHARACTER, region.getOffset(), region.getLength()); } return match; }
private boolean updateFilterState(Match match) { MatchFilter[] matchFilters = getActiveMatchFilters(); if (matchFilters == null) { return false; // do nothing, no change } boolean oldState = match.isFiltered(); for (int i = 0; i < matchFilters.length; i++) { if (matchFilters[i].filters(match)) { match.setFiltered(true); return !oldState; } } match.setFiltered(false); return oldState; }
private IMarker createMarker(int pos, int lineNumber, Match match, IFile file, ITextEditor textEditor) throws CoreException { if( file == null ) return null; IMarker marker = null; marker = file.createMarker(NewSearchUI.SEARCH_MARKER); marker.setAttribute(IMarker.TRANSIENT, true); marker.setAttribute(IMarker.MESSAGE, match.getElement()); MarkerUtilities.setLineNumber(marker, lineNumber); MarkerUtilities.setCharStart(marker, pos + match.getOffset()); MarkerUtilities.setCharEnd(marker, pos + match.getOffset() + match.getLength()); return marker; }
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); } }
public static IEditorPart open(Match match, boolean activate) { IEditorPart editorPart = null; editorPart = ManifestEditor.open(match.getElement(), true); if (editorPart != null && editorPart instanceof ManifestEditor) { ManifestEditor editor = (ManifestEditor) editorPart; if (match.getBaseUnit() != AttributesMatch.UNIT_ATTRIBUTE_SEARCH_PATTERN) { IDocument doc = editor.getDocument(match); if (doc != null) { Match exact = findExactMatch(doc, match, editor); editor.openToSourcePage(match.getElement(), exact.getOffset(), exact.getLength()); } } else { // open to extensions page and initialize filter with search result's pattern text IFormPage page = editor.setActivePage(ExtensionsPage.PAGE_ID); IManagedForm form = page.getManagedForm(); IFormPart parts[] = form.getParts(); if (parts != null && parts.length > 0) { ExtensionsSection section = (ExtensionsSection) parts[0]; String searchPattern = ((AttributesMatch) match).getSearchPattern(); Text filterText = section.getFormFilteredTree().getFilterControl(); if (!filterText.getText().equals(searchPattern)) { section.setBypassFilterDelay(true); // force immediate job run filterText.setText(searchPattern); } } } } return editorPart; }
@Override public void addHighlights(Match[] matches) { HashMap<Annotation, Position> map= new HashMap<>(matches.length); for (Match match : matches) { int offset= match.getOffset(); int length= match.getLength(); if (offset >= 0 && length >= 0) { Position position= createPosition(match); if (position != null) { Annotation annotation= match.isFiltered() ? new Annotation(SearchPlugin.FILTERED_SEARCH_ANNOTATION_TYPE, true, null) : new Annotation(SearchPlugin.SEARCH_ANNOTATION_TYPE, true, null); fMatchesToAnnotations.put(match, annotation); map.put(annotation, position); } } } addAnnotations(map); }
private IMarker createMarker(Match match) throws CoreException { Position position= InternalSearchUI.getInstance().getPositionTracker().getCurrentPosition(match); if (position == null) { if (match.getOffset() < 0 || match.getLength() < 0) return null; position= new Position(match.getOffset(), match.getLength()); } else { // need to clone position, can't have it twice in a document. position= new Position(position.getOffset(), position.getLength()); } IMarker marker= match.isFiltered() ? fFile.createMarker(SearchPlugin.FILTERED_SEARCH_MARKER) : fFile.createMarker(NewSearchUI.SEARCH_MARKER); HashMap attributes= new HashMap(4); if (match.getBaseUnit() == Match.UNIT_CHARACTER) { attributes.put(IMarker.CHAR_START, new Integer(position.getOffset())); attributes.put(IMarker.CHAR_END, new Integer(position.getOffset()+position.getLength())); } else { attributes.put(IMarker.LINE_NUMBER, new Integer(position.getOffset())); } marker.setAttributes(attributes); return marker; }
public Position getCurrentPosition(Match match) { Position pos= (Position)fMatchesToPositions.get(match); if (pos == null) return pos; AbstractTextSearchResult result= (AbstractTextSearchResult) 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; }
@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()); } } } });
@Override public void setOffset(int offset) { if (fOriginalLocation == null) { // remember the original location before changing it fOriginalLocation= new Region(getOffset(), getLength()); } super.setOffset(offset); }
@Override public void setLength(int length) { if (fOriginalLocation == null) { // remember the original location before changing it fOriginalLocation= new Region(getOffset(), getLength()); } super.setLength(length); }
if (matches[i].isFiltered()) matches[i]= null; else
protected void searchInFile(final WorkspaceFile file) { for (final char[] search : m_searchKeys) { int pos = 0; int index; while ((index = CharOperation.indexOf(search, file.content(), true, pos)) >= 0) { if (file.inWorkspace().isPresent()) { final Match match = new Match(file.inWorkspace().get(), index, search.length); final String key = String.valueOf(search, 1, search.length - 2); // remove starting and ending quotes acceptNlsKeyMatch(key, match); } pos = index + search.length; } } }
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); } }
private void gotoCurrentMarker() { if (fMarkers.size() > 0) { Match marker= getCurrentMarker(); Control focusControl= getShell().getDisplay().getFocusControl(); try { selectEntry(marker); ITextEditor editor= null; if (NewSearchUI.reuseEditor()) editor= openEditorReuse(marker); else editor= openEditorNoReuse(marker); Position p= InternalSearchUI.getInstance().getPositionTracker().getCurrentPosition(marker); if (p != null) editor.selectAndReveal(p.getOffset(), p.getLength()); else editor.selectAndReveal(marker.getOffset(), marker.getLength()); if (focusControl != null && !focusControl.isDisposed()) focusControl.setFocus(); } catch (PartInitException e) { String message= Messages.format(SearchMessages.ReplaceDialog_error_unable_to_open_text_editor, ((IFile)marker.getElement()).getName()); MessageDialog.openError(getParentShell(), getDialogTitle(), message); } } }
public void addHighlights(Match[] matches) { HashMap map= new HashMap(matches.length); for (int i= 0; i < matches.length; i++) { int offset= matches[i].getOffset(); int length= matches[i].getLength(); if (offset >= 0 && length >= 0) { Position position= createPosition(matches[i]); if (position != null) { Annotation annotation= matches[i].isFiltered() ? new Annotation(SearchPlugin.FILTERED_SEARCH_ANNOTATION_TYPE, true, null) : new Annotation(SearchPlugin.SEARCH_ANNOTATION_TYPE, true, null); fMatchesToAnnotations.put(matches[i], annotation); map.put(annotation, position); } } } addAnnotations(map); }
private IMarker createMarker(Match match) throws CoreException { Position position= InternalSearchUI.getInstance().getPositionTracker().getCurrentPosition(match); if (position == null) { if (match.getOffset() < 0 || match.getLength() < 0) return null; position= new Position(match.getOffset(), match.getLength()); } else { // need to clone position, can't have it twice in a document. position= new Position(position.getOffset(), position.getLength()); } IMarker marker= match.isFiltered() ? fFile.createMarker(SearchPlugin.FILTERED_SEARCH_MARKER) : fFile.createMarker(NewSearchUI.SEARCH_MARKER); HashMap<String, Integer> attributes= new HashMap<>(4); if (match.getBaseUnit() == Match.UNIT_CHARACTER) { attributes.put(IMarker.CHAR_START, Integer.valueOf(position.getOffset())); attributes.put(IMarker.CHAR_END, Integer.valueOf(position.getOffset()+position.getLength())); } else { attributes.put(IMarker.LINE_NUMBER, Integer.valueOf(position.getOffset())); } marker.setAttributes(attributes); return marker; }