Java Code Examples for org.eclipse.jface.text.IFindReplaceTarget#getSelection()

The following examples show how to use org.eclipse.jface.text.IFindReplaceTarget#getSelection() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ParagraphHandler.java    From e4macs with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Find the next occurrence of empty line(s)
 * Will match all valid lines except single ^$
 *   
 * @param editor
 * @param forward true if moving forward
 * @return -1 if not found, else offset of match in widget coords
 */
protected int findNext(ITextEditor editor, boolean forward) {

	IFindReplaceTarget frt = getTarget(editor);
	Point selection = frt.getSelection();
	// don't move past eob
	int offset = Math.min(MarkUtils.getCharCount(editor), Math.max(0,MarkUtils.getCaretOffset(editor) + (forward ? 1 : -2)));
	int result = ((IFindReplaceTargetExtension3)frt).findAndSelect(offset, PARAGRAPH_START, forward, false, false, true);		
	if (result != -1) {
		if ((forward && offset > result) || (!forward && offset < result)) {
			// reset if we've wrapped around in the search
			MarkUtils.setWidgetSelection(editor, selection.x, selection.x + selection.y);
			result = -1;
		}
	}
	return result;
}
 
Example 2
Source File: SearchReplaceMinibuffer.java    From e4macs with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Refine target search to not proceed beyond a region limit, if present
 *  
 * @see com.mulgasoft.emacsplus.minibuffer.SearchMinibuffer#findTarget(org.eclipse.jface.text.IFindReplaceTarget, java.lang.String, int, boolean)
 */
protected int findTarget(IFindReplaceTarget target, String searchStr, int searchIndex, boolean forward) {
	int result = WRAP_INDEX;
	StyledText text = getTextWidget();		
	try {
		text.setRedraw(false);
		result = super.findTarget(target,searchStr,searchIndex,forward);
		// if present, check if we've gone past the end of the region 
		if (getLimit() != WRAP_INDEX && result != WRAP_INDEX) {
			int pos = MarkUtils.widget2ModelOffset(getViewer(), result);
			// include length of search result in check
			if (pos + target.getSelection().y > getLimit()) {
				result = WRAP_INDEX;
				// restore last position the region
				setSelection(searchIndex);
			}
		}
	} finally {
		text.setRedraw(true);
	}
	return result;
}
 
Example 3
Source File: QuickXFind.java    From xds-ide with Eclipse Public License 1.0 5 votes vote down vote up
private static SearchRegion getSearchRegion(IFindReplaceTarget target, IEditorPart editorPart) 
    {
        SearchRegion searchRegion = null;
        
        String text = target.getSelectionText();
        Point range = target.getSelection();
        if (range.y > 0) {
            searchRegion = new SearchRegion(text, range.x, range.y);
        }
        else if (editorPart instanceof ITextEditor) {
            ISelection selection = ((ITextEditor) editorPart).getSelectionProvider().getSelection();
            if (selection instanceof ITextSelection) {
                int offset = ((ITextSelection)selection).getOffset();
                IDocumentProvider provider = ((ITextEditor) editorPart).getDocumentProvider();
                IEditorInput input = editorPart.getEditorInput();
                if ((provider != null) && (input != null)) {
                    IDocument document = provider.getDocument(input);
                    if (document != null) {
                        searchRegion = getSearchRegion(document, offset);
                        if (searchRegion != null) {
//                            ITextSelection textSelection = new TextSelection(
//                                document, searchRegion.offset, searchRegion.length
//                            );
//                            ((ITextEditor) editorPart).getSelectionProvider().setSelection(textSelection);
                            searchRegion = new SearchRegion(
                                searchRegion.text, 
                                searchRegion.offset + (range.x - offset), 
                                searchRegion.length  
                            );
                        }
                    }
                }
            }
        }
        
        return searchRegion;
    }
 
Example 4
Source File: SearchMinibuffer.java    From e4macs with Eclipse Public License 1.0 5 votes vote down vote up
protected Point getSelection() {
	Point result = null;
	IFindReplaceTarget target = getTarget();
	if (target != null) {
		result = target.getSelection();
	}
	return result;
}
 
Example 5
Source File: ParagraphHandler.java    From e4macs with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Find the next occurrence of empty line consisting of ^$
 * Since findNext will find multiple sequential ^$'s, we only need find one
 * 
 * @param editor
 * @param forward
 * @return offset of match in widget coords
 */
protected int findNextEmpty(ITextEditor editor, boolean forward) {
	IFindReplaceTarget frt = getTarget(editor);
	Point selection = frt.getSelection();
	int count = MarkUtils.getCharCount(editor);
	int coffset = MarkUtils.getCaretOffset(editor); 
	// don't move past EOB
	int offset = Math.min(count,coffset + (forward ? 1 : -1)); //(forward ? selection.x + selection.y+1 : selection.x-2);	
	int next = 0;
	int prev = -1;
	do {
		next = ((IFindReplaceTargetExtension3)frt).findAndSelect(offset, REGEX_BOL_HACK, forward, false, false, true);
		if (next == -1 || prev == next){
			// we've run out of buffer
			offset = (forward ? count : 0); 
			MarkUtils.setWidgetSelection(editor, offset, offset);
			return offset;
		} else if (forward && offset > next) {
			// protect against overrun
			MarkUtils.setWidgetSelection(editor, count, count);
			return count;
		} else if (!forward && offset < next) {
			// protect against overrun
			MarkUtils.setWidgetSelection(editor, 0, 0);
			return 0;
		} else {
			selection = frt.getSelection();	
			// Eclipse internals (frda) return different regions depending on whether the file has a 
			// single or double line delimiter e.g. \r\n or \n.  In the former it returns a 0 length
			// region, and in the latter a length of 1
			if (selection.y == 0 || (!forward && selection.x == 0)) {
				// found it, or reached the top
				return selection.x;
			} if (selection.y == 1) {
				// check for single line delimiter 
				char c = frt.getSelectionText().charAt(0);
				if (c == '\n' || c == '\r'){
					return selection.x;
				}
			}
			// the widget count could change if a folded region expands automatically
			count = MarkUtils.getCharCount(editor);
			// don't move past EOB				
			offset = Math.min(count,(forward ? selection.x + selection.y+1 : selection.x-1));
			prev = next;
		}
	} while (next != -1);
	return next;
}
 
Example 6
Source File: SearchReplaceMinibuffer.java    From e4macs with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Perform one replacement
 * 
 * @return the replacement index
 */
private boolean replaceIt() {
	boolean result = false;
	boolean forward = true;
	if (!checkPaused()) { 
		findCount++;
		try {
			result = true;
			int index = getSearchOffset();
			IFindReplaceTarget target = getTarget();
			int fpos = findTarget(target,getSearchStr(), index, forward);
			if (fpos > -1) {
				boolean initial = false;
				boolean all = false;
				String replacer = replaceStr;
				if (!isCaseSensitive() && replacer.length() > 0) {
					// Preserve case using the emacs definition
					// - Preserving case means that if the string matched is all caps, or capitalized,
					//   then its replacement is upper cased or capitalized.)
					String replacee = target.getSelectionText().trim();
					if (replacee != null && replacee.length() > 0) {
						initial = Character.isUpperCase(replacee.charAt(0));
						all = initial;
						if (initial) {
							for (int i = 1; i < replacee.length(); i++) {
								if (!Character.isUpperCase(replacee.charAt(i))) {
									all = false;
									break;
								}
							}
						}
					}
				}
				int adjust = 0;
				if (all || initial) {
					caseReplace(replacer,index,all);
				} else {
					if (isRegexLD()) {
						adjust = replacer.length();	// prevents repetitious find of same EOL
						// now we need the offset in model coords
						ITextSelection sel = (ITextSelection)getEditor().getSelectionProvider().getSelection();
						// use document 'insert' instead of broken target replace
						getDocument().replace(sel.getOffset(),0,replacer);
						// search uses cursor position - s/r is always forward
						MarkUtils.setCursorOffset(getEditor(), sel.getOffset()+adjust);
					} else {
						((IFindReplaceTargetExtension3)target).replaceSelection(replacer, isRegexp());
					}
				}
				Point p = target.getSelection();
				int rIndex = p.x + p.y + adjust;
				setSearchOffset(rIndex);					
			}
		} catch (Exception e) {
			setResultString(e.getLocalizedMessage(), true);
			finish();
			beep();
		}
	}
	return result;
}