protected void copyIndentation(final IDocument d, final DocumentCommand c) { // CODE KOPIERT AUS SUPER-KLASSE DA DORT PRIVATE // == DefaultIndentLineAutoEditStrategy.autoIndentAfterNewLine(IDocument d, DocumentCommand c) if (c.offset == -1 || d.getLength() == 0) return; try { // find start of line int p = (c.offset == d.getLength() ? c.offset - 1 : c.offset); IRegion info = d.getLineInformationOfOffset(p); int start = info.getOffset(); // find white spaces int end = findEndOfWhiteSpace(d, start, c.offset); StringBuffer buf = new StringBuffer(c.text); if (end > start) { // append to input buf.append(d.get(start, end - start)); } c.text = buf.toString(); } catch (BadLocationException excp) { // stop work } }
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; }
/** * Creates a new document event. * * @param doc the changed document */ public DocumentEvent(IDocument doc, int eventKind, Object eventInfo) { assert doc != null; fDocument = doc; fEventKind = eventKind; fEventInfo = eventInfo; fModificationStamp = fDocument.getModificationStamp(); }
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; }
private void addFoldingRegions( Set<Position> regions, IDocumentElementNode[] nodes, IDocument document) throws BadLocationException { for (int i = 0; i < nodes.length; i++) { IDocumentElementNode element = nodes[i]; int startLine = document.getLineOfOffset(element.getOffset()); int endLine = document.getLineOfOffset(element.getOffset() + element.getLength()); if (startLine < endLine) { int start = document.getLineOffset(startLine); int end = document.getLineOffset(endLine) + document.getLineLength(endLine); Position position = new Position(start, end - start); regions.add(position); fPositionToElement.put(position, element); } IDocumentElementNode[] children = element.getChildNodes(); if (children != null) { addFoldingRegions(regions, children, document); } } }
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(); } } }
void fix(IDocument document) { getFixes(); if (!Common.isEmptyArray(mFixes)) { try { for (int i = 0; i < mFixes.length; i++) { INILine line = mFixes[i].getLine(); if (line == null) { mFixes[i].setPosition(new Position(0, 0)); } else { mFixes[i].setPosition(line.getParent().getChildPosition(line)); } } Arrays.sort(mFixes, cReversePositionComparator); for (int i = 0; i < mFixes.length; i++) { Position position = mFixes[i].getPosition(); document.replace(position.offset, position.length, mFixes[i].getText()); } } catch (BadLocationException e) { e.printStackTrace(); } } }
/** @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); } }
@Override public ICompletionProposal[] computeQuickAssistProposals( IQuickAssistInvocationContext invocationContext) { IAnnotationModel amodel = invocationContext.getSourceViewer().getAnnotationModel(); IDocument doc = invocationContext.getSourceViewer().getDocument(); int offset = invocationContext.getOffset(); Iterator<?> it = amodel.getAnnotationIterator(); TreeSet<ICompletionProposal> proposalSet = new TreeSet<>( new Comparator<Object>() { @Override public int compare(Object o1, Object o2) { if (o1 instanceof ICompletionProposal && o2 instanceof ICompletionProposal) { ICompletionProposal proposal1 = (ICompletionProposal) o1; ICompletionProposal proposal2 = (ICompletionProposal) o2; return proposal1 .getDisplayString() .compareToIgnoreCase(proposal2.getDisplayString()); } return 0; } }); while (it.hasNext()) { Object key = it.next(); if (!(key instanceof SimpleMarkerAnnotation)) { if (key instanceof SpellingAnnotation) { SpellingAnnotation annotation = (SpellingAnnotation) key; if (amodel.getPosition(annotation).overlapsWith(offset, 1)) { ICompletionProposal[] proposals = annotation.getSpellingProblem().getProposals(); for (ICompletionProposal proposal : proposals) { proposalSet.add(proposal); } } } continue; } SimpleMarkerAnnotation annotation = (SimpleMarkerAnnotation) key; populateDataModelForAnnotation(annotation); IMarker marker = annotation.getMarker(); IMarkerResolution[] mapping = fResMap.get(marker); if (mapping != null) { Position pos = amodel.getPosition(annotation); try { int line = doc.getLineOfOffset(pos.getOffset()); int start = pos.getOffset(); String delim = doc.getLineDelimiter(line); int delimLength = delim != null ? delim.length() : 0; int end = doc.getLineLength(line) + start - delimLength; if (offset >= start && offset <= end) { for (IMarkerResolution markerResolution : mapping) { PDECompletionProposal proposal = new PDECompletionProposal(markerResolution, pos, marker); if (!proposalSet.contains(proposal)) { proposalSet.add(proposal); } } } } catch (BadLocationException e) { } } } return proposalSet.toArray(new ICompletionProposal[proposalSet.size()]); }