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 selectComment(int caretPos) { IDocument doc = fText.getDocument(); int startPos, endPos; try { int pos = caretPos; char c = ' '; while (pos >= 0) { c = doc.getChar(pos); if (c == '\\') { pos -= 2; continue; } if (c == Character.LINE_SEPARATOR || c == '\"') break; --pos; } if (c != '\"') return false; startPos = pos; pos = caretPos; int length = doc.getLength(); c = ' '; while (pos < length) { c = doc.getChar(pos); if (c == Character.LINE_SEPARATOR || c == '\"') break; ++pos; } if (c != '\"') return false; endPos = pos; int offset = startPos + 1; int len = endPos - offset; fText.setSelectedRange(offset, len); return true; } catch (BadLocationException x) { } return false; }
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(); } } }
/** @see IPainter#paint(int) */ public final void paint(final int reason) { Point selection = mSourceViewer.getSelectedRange(); if (selection.y > 0) { deactivate(true); return; } SexpNavigator explorer = mEditor.getExplorer(); boolean backward = true; boolean closeToParen = false; int offset = selection.x; IDocument document = mEditor.getDocument(); try { char previousChar = '\0'; char nextChar = '\0'; if (selection.x > 0) previousChar = document.getChar(selection.x - 1); if (selection.x > 0 && SchemeScannerUtilities.isClosingParenthesis(previousChar) && SchemeTextUtilities.getPartition(document, selection.x - 1).getType() == IDocument.DEFAULT_CONTENT_TYPE) { closeToParen = true; } else { nextChar = document.getChar(selection.x); if (selection.x < document.getLength() - 1 && SchemeScannerUtilities.isOpeningParenthesis(nextChar) && SchemeTextUtilities.getPartition(document, selection.x).getType() == IDocument.DEFAULT_CONTENT_TYPE) { closeToParen = true; backward = false; } } if (closeToParen && backward && explorer.backwardSexpression(selection.x)) { offset = explorer.getListStart(); char matchingChar = document.getChar(offset); mMismatch = SchemeScannerUtilities.getParenthesisType(previousChar) != SchemeScannerUtilities.getParenthesisType(matchingChar); } else { if (closeToParen && !backward && explorer.forwardSexpression(selection.x)) { offset = explorer.getSexpEnd() - 1; char matchingChar = document.getChar(offset); mMismatch = SchemeScannerUtilities.getParenthesisType(nextChar) != SchemeScannerUtilities.getParenthesisType(matchingChar); } else { deactivate(true); return; } } } catch (BadLocationException exception) { deactivate(true); return; } if (mIsActive) { // only if different if (offset != mBracketPosition.getOffset()) { // remove old highlighting handleDrawRequest(null); // update position mBracketPosition.isDeleted = false; mBracketPosition.offset = offset; mBracketPosition.length = 1; // apply new highlighting handleDrawRequest(null); } } else { mIsActive = true; mBracketPosition.isDeleted = false; mBracketPosition.offset = offset; mBracketPosition.length = 1; mTextWidget.addPaintListener(this); mPositionManager.managePosition(mBracketPosition); handleDrawRequest(null); } }