/** * This method returns the line numbers of all the methods passed to it. * @param document The document with which the user is interacting. * @param vfMethods The list of methods for which the line numbers are required. * @return Map containing method names and their starting line numbers. */ private Map<String, Integer> getMethodLineNumbers(IDocument document, List<VFMethod> vfMethods) { FindReplaceDocumentAdapter findReplaceDocumentAdapter = new FindReplaceDocumentAdapter(document); TreeMap<String, Integer> result = new TreeMap<String, Integer>(); for (VFMethod method : vfMethods) { try { method.getSootMethod().getBytecodeSignature(); IRegion region = findReplaceDocumentAdapter.find(0, method.getSootMethod().getDeclaration().substring(0, method.getSootMethod().getDeclaration().indexOf('(')), true, true, false, false); if (region != null) { result.put(method.getSootMethod().getDeclaration(), document.getLineOfOffset(region.getOffset())); } } catch (BadLocationException e) { e.printStackTrace(); } } return MapUtil.sortByValue(result); }
@Test public void testHoverThrowable() throws Exception { String uriString = ClassFileUtil.getURI(project, "java.lang.Exception"); IClassFile classFile = JDTUtils.resolveClassFile(uriString); String contents = JavaLanguageServerPlugin.getContentProviderManager().getSource(classFile, monitor); IDocument document = new Document(contents); IRegion region = new FindReplaceDocumentAdapter(document).find(0, "Throwable", true, false, false, false); int offset = region.getOffset(); int line = document.getLineOfOffset(offset); int character = offset - document.getLineOffset(line); TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uriString); Position position = new Position(line, character); TextDocumentPositionParams params = new TextDocumentPositionParams(textDocument, position); Hover hover = handler.hover(params, monitor); assertNotNull(hover); assertTrue("Unexpected hover ", !hover.getContents().isEmpty()); }
public ArrayList<IRegion> findwordInAllFiles(IDocument doc, String word, boolean forwardSearch, boolean caseSensitive, boolean wholeWord, boolean regularExpressions) { ArrayList<IRegion> al = null; if (doc != null) { al = new ArrayList<>(); FindReplaceDocumentAdapter frda = new FindReplaceDocumentAdapter(doc); int start = 0; IRegion ir; try { while ((ir = frda.find(start, word, forwardSearch, caseSensitive, wholeWord, regularExpressions)) != null) { al.add(ir); start = ir.getOffset() + ir.getLength(); } } catch (BadLocationException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return al; }
/** * For an given id that is used in the document retrieves the four coordinates of it's first occurrence. * @param document * @param searchAdapter * @param idRegion * @return location coordinates in the sense of {@link Location} class (bl, bc, el, ec). * @throws CoreException on errors */ public static int[] calculateCoordinates(IDocument document, FindReplaceDocumentAdapter searchAdapter, String id) throws CoreException { try { IRegion foundId = searchAdapter.find(0, id, true, true, false, false); if (foundId == null) { return EMPTY_LOCATION; } else { // return the coordinates return regionToLocation(document, foundId, true); } } catch (BadLocationException e) { throw new CoreException(new Status(IStatus.ERROR, TLCActivator.PLUGIN_ID, "Error during detection of the id position in MC.tla.", e)); } }
public List<IRegion> searchOccurrences(String searchFor) { ArrayList<IRegion> lst = new ArrayList<IRegion>(); FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(this.doc); boolean regExSearch = false; boolean wholeWord = true; boolean caseSensitive = true; boolean forwardSearch = true; int startOffset = 0; try { while (true) { IRegion found = adapter.find(startOffset, searchFor, forwardSearch, caseSensitive, wholeWord, regExSearch); if (found == null) { break; } lst.add(found); startOffset = found.getOffset() + found.getLength(); } } catch (BadLocationException e) { Log.log(e); } return lst; }
private boolean initializePatternControl() { ISelection selection = getSelection(); if (selection instanceof ITextSelection && !selection.isEmpty() && ((ITextSelection) selection).getLength() > 0) { String text = ((ITextSelection) selection).getText(); if (text != null) { if (fIsRegExSearch) { fPattern.setText(FindReplaceDocumentAdapter.escapeForRegExPattern(text)); } else { fPattern.setText(insertEscapeChars(text)); } return true; } } return false; }
private Map<ProjectionAnnotation, Position> getAllSnippetsAnnotations() { Map<ProjectionAnnotation, Position> annotations = new HashMap<ProjectionAnnotation, Position>(); IDocument document = getDocument(); int curOffset = 0; FindReplaceDocumentAdapter frda = new FindReplaceDocumentAdapter(document); try { IRegion startRegion = frda.find(curOffset, "SNIPPET_START", true, false, false, false); //$NON-NLS-1$ while (startRegion != null && startRegion.getOffset() >= curOffset) { int startLine = document.getLineOfOffset(startRegion.getOffset()); int startOffset = document.getLineOffset(startLine); curOffset = startOffset + document.getLineLength(startLine); IRegion endRegion = frda.find(startRegion.getOffset(), "SNIPPET_END", true, false, false, false); //$NON-NLS-1$ if (endRegion != null) { int endLine = document.getLineOfOffset(endRegion.getOffset()); int endOffset = document.getLineOffset(endLine); endOffset += document.getLineLength(endLine); curOffset = endOffset; String text = document.get(startOffset, endOffset - startOffset); ProjectionAnnotation annotation = new ProjectionAnnotation(true); annotation.setText(text); annotation.setRangeIndication(true); annotations.put(annotation, new Position(startOffset, endOffset - startOffset)); } if (curOffset < document.getLength()) { startRegion = frda.find(curOffset, "SNIPPET_START", true, false, false, false); //$NON-NLS-1$ } } } catch (BadLocationException e) { } return annotations; }
private Map<String, Integer> getMethodLineNumbers(IDocument document, List<VFMethod> vfMethods) throws BadLocationException { FindReplaceDocumentAdapter findReplaceDocumentAdapter = new FindReplaceDocumentAdapter(document); TreeMap<String, Integer> result = new TreeMap<>(); for (VFMethod method : vfMethods) { method.getSootMethod().getBytecodeSignature(); IRegion region = findReplaceDocumentAdapter.find(0, method.getSootMethod().getDeclaration(), true, true, false, false); result.put(method.getSootMethod().getDeclaration(), document.getLineOfOffset(region.getOffset())); } return MapUtil.sortByValue(result); }
/** * This method returns the line numbers of all the methods passed to it. * @param document The document with which the user is interacting. * @param vfMethods The list of methods for which the line numbers are required. * @return Map containing method names and their starting line numbers. */ private Map<String, Integer> getMethodLineNumbers(IDocument document, List<VFMethod> vfMethods) { FindReplaceDocumentAdapter findReplaceDocumentAdapter = new FindReplaceDocumentAdapter(document); TreeMap<String, Integer> result = new TreeMap<>(); for (VFMethod method : vfMethods) { try { method.getSootMethod().getBytecodeSignature(); IRegion region = findReplaceDocumentAdapter.find(0, method.getSootMethod().getDeclaration(), true, true, false, false); result.put(method.getSootMethod().getDeclaration(), document.getLineOfOffset(region.getOffset())); } catch (BadLocationException e) { e.printStackTrace(); } } return MapUtil.sortByValue(result); }
/** * This method returns the offset of the method passed to it. * @param document The document user is interacting with. * @param method The method whose offset is required. * @return The offset of the method. */ private Integer getMethodOffset(IDocument document, VFMethod method) { FindReplaceDocumentAdapter findReplaceDocumentAdapter = new FindReplaceDocumentAdapter(document); try { IRegion region = findReplaceDocumentAdapter.find(0, FindReplaceDocumentAdapter.escapeForRegExPattern(method.getSootMethod().getDeclaration()), true, true, false, true); return region.getOffset(); } catch (BadLocationException e) { e.printStackTrace(); } return 0; }
private int getMethodLineNumber(IDocument document, VFMethod method) { FindReplaceDocumentAdapter findReplaceDocumentAdapter = new FindReplaceDocumentAdapter(document); try { method.getSootMethod().getBytecodeSignature(); IRegion region = findReplaceDocumentAdapter.find(0, method.getSootMethod().getDeclaration(), true, true, false, false); return document.getLineOfOffset(region.getOffset()); } catch (BadLocationException e) { e.printStackTrace(); } return -1; }
public static IRegion getDelimRegion( IDocument doc, int offset) throws BadLocationException{ IRegion regLine = doc.getLineInformationOfOffset(offset); FindReplaceDocumentAdapter finder = new FindReplaceDocumentAdapter(doc); int lineOff = regLine.getOffset(); IRegion regDelim = null; try { regDelim = finder.find(lineOff, AssistConstants.S_EQUAL, true, true, false, false); } catch (Exception ignore) {} return regDelim; }
/** * �滻�ı� * * @param word1 * Ҫ�滻���ַ��� * @param word2 * �滻Ϊ */ public int replace(IDocument doc, String word1, String word2, boolean forwardSearch, boolean caseSensitive, boolean wholeWord, boolean showmessage, boolean regularExpressions) { int i = 0; try { FindReplaceDocumentAdapter frda = new FindReplaceDocumentAdapter(doc); IRegion ir = new IRegion() { @Override public int getOffset() { // TODO Auto-generated method stub return 0; } @Override public int getLength() { // TODO Auto-generated method stub return 0; } }; while ((ir = frda.find(ir.getOffset() + 1, word1, true, caseSensitive, wholeWord, regularExpressions)) != null) { frda.replace(word2, regularExpressions); i++; } if (showmessage) black.ba.getMessageBox("����/�滻��Ϣ", "�滻��" + i + "��"); black.ba.setFileChanged(); } catch (BadLocationException e) { // TODO Auto-generated catch block e.printStackTrace(); } return i; }
private @Nullable IRegion replace(FindReplaceDocumentAdapter adapter) throws BadLocationException, PatternSyntaxException { disableReplaceButtons(true); updateHistory(replaceComboBox); String replaceString = nullToEmpty(replaceComboBox.getValue()); return adapter.replace(replaceString, isSet(regularExpression)); }
private IRegion findStartingAt(int startOffset, String findString, FindReplaceDocumentAdapter adapter) throws BadLocationException, PatternSyntaxException { boolean forwardSearch = true; boolean caseSensitive = isSet(this.caseSensitive); boolean wholeWord = isSet(this.wholeWord); boolean regExSearch = isSet(regularExpression); return adapter.find(startOffset, findString, forwardSearch, caseSensitive, wholeWord, regExSearch); }
@Override public void documentChanged(DocumentEvent event) { if (hyperlinks != null) { try { IDocument doc = event.getDocument(); Iterator<String> itr = hyperlinks.keySet().iterator(); int lastOffset = startingOffset; while (itr.hasNext()) { String txt = itr.next(); FindReplaceDocumentAdapter frda = new FindReplaceDocumentAdapter(doc); IRegion region = frda.find(lastOffset, txt, true, true, false, false); if (region != null) { HyperlinkInfo info = hyperlinks.get(txt); int linkOffset = region.getOffset(); int linkLength = region.getLength(); if (info.getOffsetInLink() > 0) { linkOffset += info.getOffsetInLink(); linkLength -= info.getOffsetInLink(); } if (info.getLinkLength() > 0) { linkLength = info.getLinkLength(); } if (linkOffset >= 0 && linkLength > 0 && info.getFileOffset() >= 0 && info.getFileLength() > 0) { FileLink fileLink = new FileLink(info.getFile(), null, info.getFileOffset(), info.getFileLength(), info.getFileLineNumber()); mc.addHyperlink(fileLink, linkOffset, linkLength); } // if (linkOffset > lastOffset) { // lastOffset = linkOffset; // } } } } catch (BadLocationException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { hyperlinks = null; } } }
/** * @see com.mulgasoft.emacsplus.commands.MinibufferExecHandler#doExecuteResult(org.eclipse.ui.texteditor.ITextEditor, java.lang.Object) */ @Override protected boolean doExecuteResult(ITextEditor editor, Object minibufferResult) { String msg = null; boolean isError = false; if (minibufferResult == null) { msg = String.format(msg, 0); } else { try { int count = 1; // cursor is already at the end of the first find int begin = getCursorOffset(editor); String searchStr = getSearchStr(minibufferResult); FindReplaceDocumentAdapter fda = new FindReplaceDocumentAdapter(getThisDocument(editor)); IRegion found = null; while ((found = getNextMatch(fda,begin,searchStr)) != null) { ++count; int tmp = found.getOffset() + found.getLength(); if (tmp != begin) { begin = tmp; } else { // offset should always move after match, but just in case msg = "Infinite loop on = " + searchStr; //$NON-NLS-1$ isError = true; break; } } if (msg == null) { msg = ((count == 1) ? OCCURRENCE : String.format(OCCURRENCES, count)); } } catch (BadLocationException e) { // shouldn't happen, but alert user if it does msg = BAD_LOCATION_ERROR; isError = true; } } asyncShowMessage(editor, msg, isError); return true; }
/** * Check if the given position is in the context of the given closure. This is done by checking * the braces balance, basically the number of braces when { means + 1 and } means -1 should not * become 0 before reaching the position. * * @param document * @param closureName * @param position * @return * @throws BadLocationException */ public static boolean isPositionInClosureContext(IDocument document, String closureName, int position) throws BadLocationException { FindReplaceDocumentAdapter searchAdapter = new FindReplaceDocumentAdapter(document); //we need the first occurrence searching backwards from the position. IRegion region = searchAdapter.find(position, closureName, false, true, true, false); //fail fast if (region == null) { return false; } //start looking at the contents. int currentPosition = region.getOffset() + region.getLength(); int balance = 0; while(currentPosition < position) { //get and increase the counter. char currentCharacter = searchAdapter.charAt(currentPosition++); if (currentCharacter == SCOPE_OPEN.charValue()) { balance++; } if (currentCharacter == SCOPE_CLOSE) { balance--; if (balance == 0) { //scope has ended or the script is not correctly formed. return false; } } } return true; }
private IRegion find( String text, String findString, int offset, boolean searchForward, boolean caseSensitive, boolean wholeWord, boolean regExSearch ) { Document document = new Document(text); FindReplaceDocumentAdapter documentAdapter = new FindReplaceDocumentAdapter(document); try { return documentAdapter.find(offset, findString, searchForward, caseSensitive, wholeWord, regExSearch); } catch ( BadLocationException argh ) { return null; } }
private boolean hasSnippetsModifications() { IDocument document = getDocument(); if (document == null) { return false; } int curNbAnnotations = oldAnnotations.size(); int actualNbAnnotations = 0; int curOffset = 0; FindReplaceDocumentAdapter frda = new FindReplaceDocumentAdapter(document); try { IRegion startRegion = frda.find(curOffset, "SNIPPET_START", true, false, false, false); //$NON-NLS-1$ while (startRegion != null && startRegion.getOffset() >= curOffset) { int startLine = document.getLineOfOffset(startRegion.getOffset()); int startOffset = document.getLineOffset(startLine); curOffset = startOffset + document.getLineLength(startLine); IRegion endRegion = frda.find(startRegion.getOffset(), "SNIPPET_END", true, false, false, false); //$NON-NLS-1$ if (endRegion != null) { actualNbAnnotations++; int endLine = document.getLineOfOffset(endRegion.getOffset()); int endOffset = document.getLineOffset(endLine); endOffset += document.getLineLength(endLine); curOffset = endOffset; boolean contains = false; String text = document.get(startOffset, endOffset - startOffset); for (ProjectionAnnotation annotation : oldAnnotations.keySet()) { Position pos = oldAnnotations.get(annotation); if (annotation.getText().equals(text) && (startOffset == pos.getOffset())) { contains = true; } } if (!contains) { return true; } } if (curOffset < document.getLength()) { startRegion = frda.find(curOffset, "SNIPPET_START", true, false, false, false); //$NON-NLS-1$ } } } catch (BadLocationException e) { } if (curNbAnnotations != actualNbAnnotations) { return true; } return false; }
private void setSourceViewer(SourceViewer sourceViewer) { this.sourceViewer = checkNotNull(sourceViewer, "sourceViewer == null!"); adapter = new FindReplaceDocumentAdapter(sourceViewer.getDocument()); disableReplaceButtons(true); sourceViewerSelected.set(true); }
/** burke 修改find/replace界面修改 不需要fuzzySearch*/ //private boolean fuzzySearch; public IRegion executeSearch(String findValue, String dataValue) { // 如果查找的字符和单元格中的数据长度为 0,,则直接返回没有找到。 if (findValue == null || findValue.length() == 0 || dataValue == null || dataValue.length() == 0) { return null; } Document doc = new Document(dataValue); FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(doc); IRegion region; try { if (startOffset == -1) { if (searchForward) { startOffset = 0; } else { startOffset = adapter.length() - 1; } } region = adapter.find(startOffset, findValue, searchForward, caseSensitive, wholeWord, regExSearch); while (region != null) { boolean inTag = false; for (int i = region.getOffset(); i < region.getOffset() + region.getLength(); i++) { Position tagRange = InnerTagUtil.getStyledTagRange(dataValue, i); if (tagRange != null) { if (searchForward) { if (tagRange.getOffset() + tagRange.getLength() == dataValue.length()) { return null; // 如果句首是一个标记,则直接返回 null,会继续查找上一个文本段。 } startOffset = tagRange.getOffset() + tagRange.getLength(); } else { if (tagRange.offset == 0) { return null; // 如果句首是一个标记,则直接返回 null,会继续查找上一个文本段。 } startOffset = tagRange.getOffset() - 1; } inTag = true; break; } } if (inTag) { region = adapter.find(startOffset, findValue, searchForward, caseSensitive, wholeWord, regExSearch); } else { break; } } return region; } catch (BadLocationException e) { return null; } }
@Override public void documentChanged(DocumentEvent event) { if (hyperlinks != null) { try { IDocument doc = event.getDocument(); Iterator<String> itr = hyperlinks.keySet().iterator(); int lastOffset = startingOffset; while (itr.hasNext()) { String txt = itr.next(); FindReplaceDocumentAdapter frda = new FindReplaceDocumentAdapter(doc); IRegion region = frda.find(lastOffset, txt, true, true, false, false); if (region != null) { HyperlinkInfo info = hyperlinks.get(txt); int linkOffset = region.getOffset(); int linkLength = region.getLength(); if (info.getOffsetInLink() > 0) { linkOffset += info.getOffsetInLink(); linkLength -= info.getOffsetInLink(); } if (info.getLinkLength() > 0) { linkLength = info.getLinkLength(); } if (linkOffset >= 0 && linkLength > 0 && info.getFileOffset() >= 0 && info.getFileLength() > 0) { IPath path = new Path(info.getLinkResource().toString()); IFile file = null; //project.getFile(path); // FileLink fileLink = new FileLink(file, null, info.getFileOffset(), info.getFileLength(), info.getFileLineNumber()); // mc.addHyperlink(fileLink, linkOffset, linkLength); } // if (linkOffset > lastOffset) { // lastOffset = linkOffset; // } } } } catch (BadLocationException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { hyperlinks = null; } } }
private static boolean addAddToBuildPropertiesProposal(final ICompilationUnit cu, final Name name, final String fullyQualifiedName, Collection<ICommandAccess> proposals) throws CoreException { IProject project= cu.getJavaProject().getProject(); final IFile buildProperties= project.getFile("build.properties"); //$NON-NLS-1$ boolean isBundle= project.hasNature("org.eclipse.pde.PluginNature"); //$NON-NLS-1$ if (!isBundle) return false; final String changeName= CorrectionMessages.UnresolvedElementsSubProcessor_add_annotation_bundle_description; final String buildPropertiesEntry= "additional.bundles = org.eclipse.jdt.annotation"; //$NON-NLS-1$ ChangeCorrectionProposal proposal= new ChangeCorrectionProposal(changeName, null, 0) { @Override protected Change createChange() throws CoreException { if (!buildProperties.exists()) { return new CreateFileChange(buildProperties.getFullPath(), buildPropertiesEntry, null); } else { TextFileChange change= new TextFileChange(changeName, buildProperties); change.setEdit(new MultiTextEdit()); ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager(); manager.connect(buildProperties.getFullPath(), LocationKind.IFILE, null); try { ITextFileBuffer textFileBuffer= manager.getTextFileBuffer(buildProperties.getFullPath(), LocationKind.IFILE); IDocument document= textFileBuffer.getDocument(); String lineDelim= TextUtilities.getDefaultLineDelimiter(document); IRegion match= new FindReplaceDocumentAdapter(document).find(0, "additional\\.bundles\\s*=\\s*", true, false, false, true); //$NON-NLS-1$ if (match != null) { StringBuilder buf= new StringBuilder("org.eclipse.jdt.annotation,\\").append(lineDelim); //$NON-NLS-1$ int spaces= match.getOffset() + match.getLength() - document.getLineOffset(document.getLineOfOffset(match.getOffset())); while (spaces-- > 0) buf.append(' '); change.addEdit(new InsertEdit(match.getOffset() + match.getLength(), buf.toString())); } else { String entry= buildPropertiesEntry + lineDelim; int len= document.getLength(); if (len > 0 && document.getLineInformation(document.getNumberOfLines() - 1).getLength() != 0) { entry= lineDelim + entry; } change.addEdit(new InsertEdit(len, entry)); } CompilationUnitChange addImportChange= createAddImportChange(cu, name, fullyQualifiedName); return new CompositeChange(changeName, new Change[] { change, addImportChange}); } catch (BadLocationException e) { JavaPlugin.log(e); return new NullChange(); } finally { manager.disconnect(buildProperties.getFullPath(), LocationKind.IFILE, null); } } } @Override public Object getAdditionalProposalInfo(IProgressMonitor monitor) { return Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_add_annotation_bundle_info, buildPropertiesEntry); } }; proposals.add(proposal); return true; }
/** * Mostly based on code from {@link org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedNamesAssistProposal} */ private void startEditing(ISourceViewer viewer) throws ExecutionException { Point selOffsetAndLen = viewer.getSelectedRange(); int selStart = CoordinatesUtil.fromOffsetAndLengthToStartAndEnd(selOffsetAndLen).x; IDocument document = viewer.getDocument(); try { String selectedText; if (selOffsetAndLen.y == 0) { // no characters selected String documentText = document.get(); Point wordOffsetAndLen = TextUtil.findWordSurrounding(documentText, selStart); if (wordOffsetAndLen != null) { selectedText = document.get(wordOffsetAndLen.x, wordOffsetAndLen.y); } else { IRegion selectedLine = document.getLineInformationOfOffset(selStart); selectedText = document.get(selectedLine.getOffset(), selectedLine.getLength()); } } else { selectedText = document.get(selOffsetAndLen.x, selOffsetAndLen.y); } LinkedPositionGroup linkedPositionGroup = new LinkedPositionGroup(); FindReplaceDocumentAdapter findReplaceAdaptor = new FindReplaceDocumentAdapter(document); IRegion matchingRegion = findReplaceAdaptor.find(0, selectedText, true, true, false, false); while (matchingRegion != null) { linkedPositionGroup.addPosition(new LinkedPosition(document, matchingRegion.getOffset(), matchingRegion .getLength())); matchingRegion = findReplaceAdaptor.find(matchingRegion.getOffset() + matchingRegion.getLength(), selectedText, true, true, false, false); } LinkedModeModel model = new LinkedModeModel(); model.addGroup(linkedPositionGroup); model.forceInstall(); LinkedModeUI ui = new EditorLinkedModeUI(model, viewer); ui.setExitPolicy(new DeleteBlockingExitPolicy(document)); ui.enter(); // by default the text being edited is selected so restore original selection viewer.setSelectedRange(selOffsetAndLen.x, selOffsetAndLen.y); } catch (BadLocationException e) { throw new ExecutionException("Editing failed", e); } }
private void startEditing(ISourceViewer viewer) throws ExecutionException { final Point selOffsetAndLen = viewer.getSelectedRange(); final IDocument document = viewer.getDocument(); try { final String searchText; final int candidateSearchOffset; final int selStart = CoordinatesUtil.fromOffsetAndLengthToStartAndEnd(selOffsetAndLen).x; if (selOffsetAndLen.y == 0) { // no characters selected final String documentText = document.get(); final Point wordOffsetAndLen = TextUtil.findWordSurrounding(documentText, selStart); if (wordOffsetAndLen != null) { searchText = document.get(wordOffsetAndLen.x, wordOffsetAndLen.y); candidateSearchOffset = wordOffsetAndLen.x; } else { final IRegion selectedLine = document.getLineInformationOfOffset(selStart); searchText = document.get(selectedLine.getOffset(), selectedLine.getLength()); candidateSearchOffset = selectedLine.getOffset(); } } else { searchText = document.get(selOffsetAndLen.x, selOffsetAndLen.y); candidateSearchOffset = selOffsetAndLen.x; } final int searchOffset; final List<IRegion> selections; final Point startingSelection; final SelectInProgress currentState = getCurrentState(); if (LinkedModeModel.getModel(document, 0) != null && currentState != null && selOffsetAndLen.equals(currentState.startingSelection) && searchText.equals(currentState.searchText)) { startingSelection = currentState.startingSelection; selections = new ArrayList<IRegion>(currentState.existingSelections); searchOffset = currentState.nextOffset; } else { startingSelection = selOffsetAndLen; selections = new ArrayList<IRegion>(); searchOffset = candidateSearchOffset; } final IRegion matchingRegion = new FindReplaceDocumentAdapter(document).find(searchOffset, searchText, true, true, false, false); if (matchingRegion != null) { selections.add(matchingRegion); if (selections.size() == 1) { // select the next occurrence too; only selecting the current cursor pos isn't useful final IRegion secondMatchingRegion = new FindReplaceDocumentAdapter(document).find( matchingRegion.getOffset() + matchingRegion.getLength(), searchText, true, true, false, false); if (secondMatchingRegion != null) { selections.add(secondMatchingRegion); } } if (selections.size() > 1) { final IRegion lastSelection = selections.get(selections.size() - 1); saveCurrentState(new SelectInProgress(startingSelection, searchText, selections, lastSelection.getOffset() + lastSelection.getLength())); startLinkedEdit(selections, viewer, selOffsetAndLen); } } } catch (BadLocationException e) { throw new ExecutionException("Editing failed", e); } }
/** * Find the next match in the document and return its region * * @param documentAdapter * @param begin the search here * @param regExp * @return the found region or null * @throws BadLocationException */ private IRegion getNextMatch(FindReplaceDocumentAdapter fda, int begin, String regExp) throws BadLocationException { return fda.find(begin, regExp, true, false, false, true); }
private void addMarkers(Set<String> deletedKeys) throws Exception { targetFile.deleteMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE); if (deletedKeys == null || deletedKeys.isEmpty()) { //do nothing return; } IDocumentProvider provider = new TextFileDocumentProvider(); provider.connect(targetFile); IDocument targetFileDocument = provider.getDocument(targetFile); FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(targetFileDocument); for (String deletedkey: deletedKeys) { IRegion region = adapter.find(0, deletedkey, true, false, false, false); IMarker marker = targetFile.createMarker(IMarker.PROBLEM); int lineNumber = targetFileDocument.getLineOfOffset(region.getOffset()); if (marker.exists()) { marker.setAttribute(IMarker.MESSAGE, "Property might not be used within mule configs."); marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH); marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING); marker.setAttribute(IMarker.LINE_NUMBER, lineNumber + 1); marker.setAttribute(IMarker.CHAR_START, region.getOffset()); marker.setAttribute(IMarker.CHAR_END, region.getOffset() + region.getLength()); } } provider.disconnect(targetFile); }