public boolean openHyperLink() { String localiz = getLocalization(); if (localiz == null) { return false; } else if (fBase.getUnderlyingResource() == null) { return false; } else if (fElement.length() == 0 || fElement.charAt(0) != '%') { return false; } IProject proj = fBase.getUnderlyingResource().getProject(); IFile file = proj.getFile(localiz + ".properties"); // $NON-NLS-1$ if (!file.exists()) return false; try { IEditorPart editor = IDE.openEditor(PDEPlugin.getActivePage(), file); if (!(editor instanceof TextEditor)) return false; TextEditor tEditor = (TextEditor) editor; IDocument doc = tEditor.getDocumentProvider().getDocument(tEditor.getEditorInput()); if (doc == null) return false; try { String key = fElement.substring(1); int keyLen = key.length(); int length = doc.getLength(); int start = 0; IRegion region = null; FindReplaceDocumentAdapter docSearch = new FindReplaceDocumentAdapter(doc); while ((region = docSearch.find(start, key, true, false, false, false)) != null) { int offset = region.getOffset(); if (offset > 0) { // check for newline before char c = doc.getChar(offset - 1); if (c != '\n' && c != '\r') { start += keyLen; continue; } } if (offset + keyLen < length) { // check for whitespace / assign symbol after char c = doc.getChar(offset + keyLen); if (!Character.isWhitespace(c) && c != '=' && c != ':') { start += keyLen; continue; } } tEditor.selectAndReveal(offset, keyLen); break; } } catch (BadLocationException e) { PDEPlugin.log(e); } } catch (PartInitException e) { return false; } return true; }
protected boolean selectWord(int caretPos) { IDocument doc = fText.getDocument(); int startPos, endPos; try { int pos = caretPos; char c; while (pos >= 0) { c = doc.getChar(pos); if (!Character.isJavaIdentifierPart(c)) break; --pos; } startPos = pos; pos = caretPos; int length = doc.getLength(); while (pos < length) { c = doc.getChar(pos); if (!Character.isJavaIdentifierPart(c)) break; ++pos; } endPos = pos; selectRange(startPos, endPos); return true; } catch (BadLocationException x) { } return false; }
/** * Der vorhergehende character, wobei whitespace zeichen uebersprungen werden, entspricht dem * uebergebenen character. * * @param document * @param offset * @param c * @return * @throws BadLocationException */ private boolean previousCharIs(final IDocument document, final int offset, final int c) throws BadLocationException { if (offset <= 0) { return false; } int curOffset = offset; while (curOffset > 0) { char docChar = document.getChar(--curOffset); if (!Character.isWhitespace(docChar)) { return docChar == c; } } return false; }
/** * Ueberspringen von Whitespace-Zeichen, bis Dokumentanfang oder nicht Whitespace gefunden wurde * * @param document * @param offset * @return */ private int skipWhitespace(final IDocument document, final int offset) throws BadLocationException { if (offset <= 0) { return 0; } int curOffset = offset; while (true) { if (!Character.isWhitespace(document.getChar(--curOffset))) { return curOffset + 1; } if (curOffset == 0) { return curOffset; } } }
private boolean isFunctionCallProposal(final IDocument document, final int offset) throws BadLocationException { if (offset <= 0) { return false; } int curOffset = offset; while (true) { char c = document.getChar(--curOffset); if (c == '(') { return true; } if (curOffset == 0 || Character.isWhitespace(c)) { return false; } } }
/** * Berechnet das Wort das vor dem offset liegt. Ist das Zeichen vor dem Wort ein Leerzeichen, (, ) * wird der leere String zurueckgegeben. * * @param document * @param offset * @return */ @Nonnull private String computePreviousWord(final IDocument document, final int offset) throws BadLocationException { if (offset <= 0) { // <= 0, da auf jeden Fall offset -1 vor zugriff return ""; } int curOffset = offset; StringBuilder prefix = new StringBuilder(); while (true) { char c = document.getChar(--curOffset); if (Character.isWhitespace(c) || c == '(' || c == ')' || c == '\'') { return prefix.reverse().toString(); } prefix.append(c); if (curOffset == 0) { return prefix.reverse().toString(); } } }