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 } }
/** * Returns the variable and function names at the current line, or <code>null</code> if none. * * @param part text editor * @param selection text selection * @return the variable and function names at the current line, or <code>null</code> if none. The * array has two elements, the first is the variable name, the second is the function name. */ protected String[] getVariableAndFunctionName(IWorkbenchPart part, ISelection selection) { ITextEditor editor = getEditor(part); if (editor != null && selection instanceof ITextSelection) { ITextSelection textSelection = (ITextSelection) selection; IDocumentProvider documentProvider = editor.getDocumentProvider(); try { documentProvider.connect(this); IDocument document = documentProvider.getDocument(editor.getEditorInput()); IRegion region = document.getLineInformationOfOffset(textSelection.getOffset()); String string = document.get(region.getOffset(), region.getLength()).trim(); if (string.startsWith("var ")) { // $NON-NLS-1$ String varName = string.substring(4).trim(); String fcnName = getFunctionName( document, varName, document.getLineOfOffset(textSelection.getOffset())); return new String[] {varName, fcnName}; } } catch (CoreException e) { } catch (BadLocationException e) { } finally { documentProvider.disconnect(this); } } return null; }
/* * @see org.eclipse.jface.text.IPainter#paint(int) */ public void paint(int reason) { IDocument document = fTextViewer.getDocument(); if (document == null) { deactivate(false); return; } if (!fIsActive) { fIsActive = true; fTextWidget.addPaintListener(this); redrawAll(); } else if (reason == CONFIGURATION || reason == INTERNAL) { redrawAll(); } else if (reason == TEXT_CHANGE) { // redraw current line only try { IRegion lineRegion = document.getLineInformationOfOffset(getDocumentOffset(fTextWidget.getCaretOffset())); int widgetOffset = getWidgetOffset(lineRegion.getOffset()); int charCount = fTextWidget.getCharCount(); int redrawLength = Math.min(lineRegion.getLength(), charCount - widgetOffset); if (widgetOffset >= 0 && redrawLength > 0) { fTextWidget.redrawRange(widgetOffset, redrawLength, true); } } catch (BadLocationException e) { // ignore } } }
/** * Checks if the angular bracket at <code>offset</code> is a type parameter bracket. * * @param offset the offset of the opening bracket * @param document the document * @param scanner a java heuristic scanner on <code>document</code> * @return <code>true</code> if the bracket is part of a type parameter, <code>false</code> * otherwise * @since 3.1 */ private boolean isTypeParameterBracket( int offset, IDocument document, PHPHeuristicScanner scanner) { /* * type parameter come after braces (closing or opening), semicolons, or * after a Type name (heuristic: starts with capital character, or after * a modifier keyword in a method declaration (visibility, static, * synchronized, final) */ try { final IRegion line = document.getLineInformationOfOffset(offset); final int prevToken = scanner.previousToken(offset - 1, line.getOffset()); final int prevTokenOffset = scanner.getPosition() + 1; final String previous = prevToken == Symbols.TokenEOF ? null : document.get(prevTokenOffset, offset - prevTokenOffset).trim(); if (prevToken == Symbols.TokenLBRACE || prevToken == Symbols.TokenRBRACE || prevToken == Symbols.TokenSEMICOLON || prevToken == Symbols.TokenSYNCHRONIZED || prevToken == Symbols.TokenSTATIC || (prevToken == Symbols.TokenIDENT && isTypeParameterIntroducer(previous)) || prevToken == Symbols.TokenEOF) { return true; } } catch (final BadLocationException e) { return false; } return false; }
/** * Returns the position of the first non whitespace char in the current line. * * @param doc * @param cursorOffset * @return position of the first character of the line (returned as an absolute offset) * @throws BadLocationException */ public static int getFirstCharPosition(IDocument doc, int cursorOffset) throws BadLocationException { IRegion region; region = doc.getLineInformationOfOffset(cursorOffset); int offset = region.getOffset(); return offset + getFirstCharRelativePosition(doc, cursorOffset); }
private String getRelativeIndent(int offset) { int indent = getStringOrCommentIndent(offset); try { IRegion lineInfo = document.getLineInformationOfOffset(offset); StringBuilder result = new StringBuilder(); int lineOffset = lineInfo.getOffset(); for (int i = lineOffset; i < lineOffset + indent; i++) { char ch = document.getChar(i); if (ch != ' ' && ch != '\t') { return ""; } } for (int i = lineOffset + indent; ; ) { char ch = document.getChar(i++); if (ch == ' ' || ch == '\t') { result.append(ch); } else { break; } } return result.toString(); } catch (BadLocationException e) { e.printStackTrace(); return ""; } }
/** @see IPresentationDamager#getDamageRegion(ITypedRegion, DocumentEvent, boolean) */ public IRegion getDamageRegion( final ITypedRegion partition, final DocumentEvent event, final boolean documentPartitioningChanged) { if (!documentPartitioningChanged) { try { final IRegion info = fDocument.getLineInformationOfOffset(event.getOffset()); final int start = Math.max(partition.getOffset(), info.getOffset()); int end = event.getOffset() + (event.getText() == null ? event.getLength() : event.getText().length()); if ((info.getOffset() <= end) && (end <= info.getOffset() + info.getLength())) { // optimize the case of the same line end = info.getOffset() + info.getLength(); } else { end = endOfLineOf(end); } end = Math.min(partition.getOffset() + partition.getLength(), end); return new Region(start, end - start); } catch (final BadLocationException x) { } } return partition; }
static IRegion getCurrentArgumentRegion(IDocument document, int loc, int index, int startOfArgs) throws BadLocationException { IRegion li = document.getLineInformationOfOffset(loc); int endOfLine = li.getOffset() + li.getLength(); int offset = findCharCount(index, document, loc + startOfArgs, endOfLine, ",;", "", true) + 1; if (offset > 0 && document.getChar(offset) == ' ') { offset++; } int nextOffset = findCharCount(index + 1, document, loc + startOfArgs, endOfLine, ",;", "", true); int middleOffset = findCharCount(1, document, offset, nextOffset, "=", "", true) + 1; if (middleOffset > 0 && document.getChar(middleOffset) == '>') { middleOffset++; } while (middleOffset > 0 && document.getChar(middleOffset) == ' ') { middleOffset++; } if (middleOffset > offset && middleOffset < nextOffset) { offset = middleOffset; } if (nextOffset == -1) { nextOffset = offset; } return new Region(offset, nextOffset - offset); }
private IRegion getLineRegionAtOffset(final IDocument document, final int offset) { try { return document.getLineInformationOfOffset(offset); } catch (final BadLocationException ex) { FeatureEditorPlugin.instance().error("Could not find line for offset " + offset, ex); } return null; }
/** * Goes to first char of the line. * * @param doc * @param cursorOffset */ protected void gotoFirstChar(IDocument doc, int cursorOffset) { try { IRegion region = doc.getLineInformationOfOffset(cursorOffset); int offset = region.getOffset(); setCaretPosition(offset); } catch (BadLocationException e) { beep(e); } }
public static String searchIndentation(final IDocument document, final int offset) { try { final IRegion region = document.getLineInformationOfOffset(offset); final String lineContent = document.get(region.getOffset(), region.getLength()); return searchIndentation(lineContent); } catch (final BadLocationException e) { return ""; //$NON-NLS-1$ } }
/** @return the current column that is selected from the cursor. */ public int getCursorColumn() { try { int absoluteOffset = getAbsoluteCursorOffset(); IRegion region = doc.getLineInformationOfOffset(absoluteOffset); return absoluteOffset - region.getOffset(); } catch (BadLocationException e) { throw new RuntimeException(e); } }
/** * @param offset the offset we want info on * @return a tuple with the line, col of the passed offset in the document */ public Tuple<Integer, Integer> getLineAndCol(int offset) { try { IRegion region = doc.getLineInformationOfOffset(offset); int line = doc.getLineOfOffset(offset); int col = offset - region.getOffset(); return new Tuple<Integer, Integer>(line, col); } catch (BadLocationException e) { throw new RuntimeException(e); } }
private boolean isCommentLine(IDocument d, int offset) throws BadLocationException { IRegion reg = d.getLineInformationOfOffset(offset); String line = d.get(reg.getOffset(), reg.getLength()); String trLine = line.trim(); if (trLine.length() > 2) { String com = trLine.substring(0, 2); if (com.equalsIgnoreCase("--")) return true; return false; } return false; }
private static IRegion getRegionOfInterest(ITextEditor editor, int invocationLocation) throws BadLocationException { IDocumentProvider documentProvider = editor.getDocumentProvider(); if (documentProvider == null) { return null; } IDocument document = documentProvider.getDocument(editor.getEditorInput()); if (document == null) { return null; } return document.getLineInformationOfOffset(invocationLocation); }
/** * Returns the indentation level at the position of code completion. * * @return the indentation level at the position of the code completion */ private int getIndentation() { int start = getStart(); IDocument document = getDocument(); try { IRegion region = document.getLineInformationOfOffset(start); String lineContent = document.get(region.getOffset(), region.getLength()); IJavaProject project = getJavaProject(); return Strings.computeIndentUnits(lineContent, project); } catch (BadLocationException e) { return 0; } }
private String getLineIndentation(IDocument document, int offset) throws BadLocationException { // find start of line int adjustedOffset = (offset == document.getLength() ? offset - 1 : offset); IRegion line = document.getLineInformationOfOffset(adjustedOffset); int start = line.getOffset(); // find white spaces int end = findEndOfWhiteSpace(document, start, offset); return document.get(start, end - start); }
/** * Creates the errors that are related to the mixed indentation. * * @param monitor */ private static void createMixedErrorMessages( IDocument doc, IAnalysisPreferences analysisPrefs, ArrayList<IMessage> ret, List<Tuple3<String, Integer, Boolean>> errorsAre, String errorMsg, char errorChar, IProgressMonitor monitor) { for (Tuple3<String, Integer, Boolean> indentation : errorsAre) { if (monitor.isCanceled()) { return; } Integer offset = indentation.o2; int startLine = PySelection.getLineOfOffset(doc, offset) + 1; IRegion region; try { region = doc.getLineInformationOfOffset(offset); int startCol = offset - region.getOffset() + 1; String indentationString = indentation.o1; int charIndex = indentationString.indexOf(errorChar); startCol += charIndex; // now, get the endCol int endCol = startCol; int indentationStringLen = indentationString.length(); // endCol starts at 1, but string access starts at 0 (so <= is needed) while (endCol <= indentationStringLen) { if (indentationString.charAt(endCol - 1) == errorChar) { endCol++; } else { break; } } ret.add( new Message( IAnalysisPreferences.TYPE_INDENTATION_PROBLEM, errorMsg, startLine, startLine, startCol, endCol, analysisPrefs)); } catch (BadLocationException e) { Log.log(e); } } }
protected int endOfLineOf(int offset) throws BadLocationException { IRegion info = document.getLineInformationOfOffset(offset); if (offset <= info.getOffset() + info.getLength()) return info.getOffset() + info.getLength(); int line = document.getLineOfOffset(offset); try { info = document.getLineInformation(line + 1); return info.getOffset() + info.getLength(); } catch (BadLocationException x) { return document.getLength(); } }
/** * Creates a region describing the text block (something that starts at the beginning of a line) * completely containing the current selection. * * @param selection The selection to use * @param document The document * @return the region describing the text block comprising the given selection */ private IRegion getTextBlockFromSelection(ITextSelection selection, IDocument document) { try { IRegion line = document.getLineInformationOfOffset(selection.getOffset()); int length = selection.getLength() == 0 ? line.getLength() : selection.getLength() + (selection.getOffset() - line.getOffset()); return new Region(line.getOffset(), length); } catch (BadLocationException x) { // should not happen // JavaPlugin.log(x); } return null; }
private boolean hasIdentifierToTheLeft(IDocument document, int offset) { try { int start = offset; IRegion startLine = document.getLineInformationOfOffset(start); int minStart = startLine.getOffset(); while (start != minStart && XPathTextEditor.this.fWhitespaceDetector.isWhitespace(document.getChar(start - 1))) --start; return start != minStart && Character.isJavaIdentifierPart(document.getChar(start - 1)); } catch (BadLocationException e) { return true; } }
private boolean hasCharacterToTheRight(IDocument document, int offset, char character) { try { int end = offset; IRegion endLine = document.getLineInformationOfOffset(end); int maxEnd = endLine.getOffset() + endLine.getLength(); while (end != maxEnd && XPathTextEditor.this.fWhitespaceDetector.isWhitespace(document.getChar(end))) ++end; return end != maxEnd && document.getChar(end) == character; } catch (BadLocationException e) { // be conservative return true; } }
/** * Computes and returns the indentation for a javadoc line. The line must be inside a javadoc * comment. * * @param document the document * @param line the line in document * @param scanner the scanner * @param partition the comment partition * @return the indent, or <code>null</code> if not computable * @throws BadLocationException */ private static String computeJavadocIndent( IDocument document, int line, JavaHeuristicScanner scanner, ITypedRegion partition) throws BadLocationException { if (line == 0) // impossible - the first line is never inside a javadoc comment return null; // don't make any assumptions if the line does not start with \s*\* - it might be // commented out code, for which we don't want to change the indent final IRegion lineInfo = document.getLineInformation(line); final int lineStart = lineInfo.getOffset(); final int lineLength = lineInfo.getLength(); final int lineEnd = lineStart + lineLength; int nonWS = scanner.findNonWhitespaceForwardInAnyPartition(lineStart, lineEnd); if (nonWS == JavaHeuristicScanner.NOT_FOUND || document.getChar(nonWS) != '*') { if (nonWS == JavaHeuristicScanner.NOT_FOUND) return document.get(lineStart, lineLength); return document.get(lineStart, nonWS - lineStart); } // take the indent from the previous line and reuse IRegion previousLine = document.getLineInformation(line - 1); int previousLineStart = previousLine.getOffset(); int previousLineLength = previousLine.getLength(); int previousLineEnd = previousLineStart + previousLineLength; StringBuffer buf = new StringBuffer(); int previousLineNonWS = scanner.findNonWhitespaceForwardInAnyPartition(previousLineStart, previousLineEnd); if (previousLineNonWS == JavaHeuristicScanner.NOT_FOUND || document.getChar(previousLineNonWS) != '*') { // align with the comment start if the previous line is not an asterix line previousLine = document.getLineInformationOfOffset(partition.getOffset()); previousLineStart = previousLine.getOffset(); previousLineLength = previousLine.getLength(); previousLineEnd = previousLineStart + previousLineLength; previousLineNonWS = scanner.findNonWhitespaceForwardInAnyPartition(previousLineStart, previousLineEnd); if (previousLineNonWS == JavaHeuristicScanner.NOT_FOUND) previousLineNonWS = previousLineEnd; // add the initial space // TODO this may be controlled by a formatter preference in the future buf.append(' '); } String indentation = document.get(previousLineStart, previousLineNonWS - previousLineStart); buf.insert(0, indentation); return buf.toString(); }
private void autoEditAfterNewLine(IDocument document, DocumentCommand command) { if (command.offset == -1 || document.getLength() == 0) { return; } try { int p = command.offset == document.getLength() ? command.offset - 1 : command.offset; IRegion info = document.getLineInformationOfOffset(p); int start = info.getOffset(); StringBuffer buf = new StringBuffer(command.text); int end = findEndOfWhiteSpaceAfter(document, start, command.offset); String lineSpaces = (end > start) ? document.get(start, end - start) : ""; buf.append(lineSpaces); if (isAfterOpenBrace(document, command.offset - 1, start)) { buf.append( IndenterUtil.createWhiteSpace(1, 0, TextUtilities.getDefaultLineDelimiter(document))); if (isBeforeCloseBrace(document, command.offset, info.getOffset() + info.getLength())) { command.shiftsCaret = false; command.caretOffset = command.offset + buf.length(); buf.append(command.text); buf.append(lineSpaces); } command.text = buf.toString(); } else { int indent = computeIndentCount(document, command.offset); if (isBeforeCloseBrace(document, command.offset, info.getOffset() + info.getLength())) { indent--; } command.text += IndenterUtil.createWhiteSpace( indent, 0, TextUtilities.getDefaultLineDelimiter(document)); } } catch (BadLocationException e) { KotlinLogger.logAndThrow(e); } }
@Override protected void addEdits(IDocument document, TextEdit rootEdit) throws CoreException { try { String lineDelimiter = TextUtilities.getDefaultLineDelimiter(document); final IJavaProject project = getCompilationUnit().getJavaProject(); IRegion region = document.getLineInformationOfOffset(fInsertPosition); String lineContent = document.get(region.getOffset(), region.getLength()); String indentString = Strings.getIndentString(lineContent, project); String str = Strings.changeIndent(fComment, 0, project, indentString, lineDelimiter); InsertEdit edit = new InsertEdit(fInsertPosition, str); rootEdit.addChild(edit); if (fComment.charAt(fComment.length() - 1) != '\n') { rootEdit.addChild(new InsertEdit(fInsertPosition, lineDelimiter)); rootEdit.addChild(new InsertEdit(fInsertPosition, indentString)); } } catch (BadLocationException e) { throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e)); } }
/** * Are we in the first char of the line with the offset passed? * * @param doc * @param cursorOffset */ protected void isInFirstVisibleChar(IDocument doc, int cursorOffset) { try { IRegion region = doc.getLineInformationOfOffset(cursorOffset); int offset = region.getOffset(); String src = doc.get(offset, region.getLength()); if ("".equals(src)) { return; } int i = 0; while (i < src.length()) { if (!Character.isWhitespace(src.charAt(i))) { break; } i++; } setCaretPosition(offset + i - 1); } catch (BadLocationException e) { beep(e); return; } }
/** * Returns the position of the last non whitespace char in the current line. * * @param doc * @param cursorOffset * @return position of the last character of the line (returned as an absolute offset) * @throws BadLocationException */ protected int getLastCharPosition(IDocument doc, int cursorOffset) throws BadLocationException { IRegion region; region = doc.getLineInformationOfOffset(cursorOffset); int offset = region.getOffset(); String src = doc.get(offset, region.getLength()); int i = src.length(); boolean breaked = false; while (i > 0) { i--; // we have to break if we find a character that is not a whitespace or a tab. if (Character.isWhitespace(src.charAt(i)) == false && src.charAt(i) != '\t') { breaked = true; break; } } if (!breaked) { i--; } return (offset + i); }
/** * Returns a selection of the member in the given text selection, or the original selection if * none. * * @param part * @param selection * @return a structured selection of the member in the given text selection, or the original * selection if none * @exception CoreException if an exception occurs */ protected ISelection translateToMembers(IWorkbenchPart part, ISelection selection) throws CoreException { ITextEditor textEditor = getTextEditor(part); if (textEditor != null && selection instanceof ITextSelection) { ITextSelection textSelection = (ITextSelection) selection; IEditorInput editorInput = textEditor.getEditorInput(); IDocumentProvider documentProvider = textEditor.getDocumentProvider(); if (documentProvider == null) { throw new CoreException(Status.CANCEL_STATUS); } IDocument document = documentProvider.getDocument(editorInput); int offset = textSelection.getOffset(); if (document != null) { try { IRegion region = document.getLineInformationOfOffset(offset); int end = region.getOffset() + region.getLength(); while (Character.isWhitespace(document.getChar(offset)) && offset < end) { offset++; } } catch (BadLocationException e) { } } IMember m = null; IRubyScript root = getTypeRoot(editorInput); if (root != null) { synchronized (root) { root.reconcile(false, null, null); } IRubyElement e = root.getElementAt(offset); if (e instanceof IMember) { m = (IMember) e; } } if (m != null) { return new StructuredSelection(m); } } return selection; }
/** * Adds two spaces to the indentation of the previous line. * * @param d the document to work on * @param c the command to deal with */ private void autoIndentAfterNewLine(IDocument d, DocumentCommand c) { if (c.offset == -1 || d.getLength() == 0) return; try { String lastPartitionType = null; if (d instanceof IDocumentExtension3) { IDocumentExtension3 id3 = (IDocumentExtension3) d; lastPartitionType = id3.getDocumentPartitioner(SassEditor.SASS_PARTITIONING).getContentType(c.offset - 1); } // 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); // indent if we just finished an element StringBuffer buf = new StringBuffer(c.text); if (lastPartitionType != null && (lastPartitionType.equalsIgnoreCase(SassPartitionScanner.SASS_CLASS) || lastPartitionType.equalsIgnoreCase(SassPartitionScanner.SASS_ID))) { buf.append(" "); } if (end > start) { // append to input buf.append(d.get(start, end - start)); } c.text = buf.toString(); } catch (BadLocationException excp) { // stop work } }
private void AtlStringIndentAfterNewLine(IDocument document, DocumentCommand command) throws BadLocationException { ITypedRegion partition = TextUtilities.getPartition(document, fPartitioning, command.offset, true); int offset = partition.getOffset(); int length = partition.getLength(); if (command.offset == offset + length && document.getChar(offset + length - 1) == '\'') return; String indentation = getLineIndentation(document, command.offset); String delimiter = TextUtilities.getDefaultLineDelimiter(document); IRegion line = document.getLineInformationOfOffset(offset); String string = document.get(line.getOffset(), offset - line.getOffset()); if (string.trim().length() != 0) indentation += String.valueOf("\t\t"); IPreferenceStore preferenceStore = AtlUIPlugin.getDefault().getPreferenceStore(); if (isLineDelimiter(document, command.text)) command.text = "\' +" + command.text + indentation + "\'"; else if (command.text.length() > 1 && preferenceStore.getBoolean(AtlPreferenceConstants.TYPING_ESCAPE_STRINGS)) command.text = getModifiedText(command.text, indentation, delimiter); }