Java Code Examples for org.eclipse.jface.text.IDocument#getNumberOfLines()
The following examples show how to use
org.eclipse.jface.text.IDocument#getNumberOfLines() .
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: TextViewerJoinLinesAction.java From xtext-eclipse with Eclipse Public License 2.0 | 6 votes |
/** * Joins several text lines to one line. * * @param document the document * @param startLine the start line * @param endLine the end line * @return the new caret offset * @throws BadLocationException if the document is accessed with wrong line or offset */ private int joinLines(IDocument document, int startLine, int endLine) throws BadLocationException { if (startLine == document.getNumberOfLines() - 1) { // do nothing because we are in the last line return -1; } if (startLine == endLine) endLine++; // append join with the next line StringBuffer buffer= new StringBuffer(); for (int line= startLine; line <= endLine; line++) { buffer.append(trim(document, line, line == startLine)); if (line != endLine) buffer.append(fJoint); } int startLineOffset= document.getLineOffset(startLine); int endLineOffset= document.getLineOffset(endLine) + document.getLineLength(endLine) - getLineDelimiterLength(document, endLine); String replaceString= buffer.toString(); document.replace(startLineOffset, endLineOffset - startLineOffset, replaceString); return startLineOffset + replaceString.length(); }
Example 2
Source File: ToggleCommentAction.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * Returns the index of the first line whose start offset is in the given text range. * * @param region the text range in characters where to find the line * @param document The document * @return the first line whose start index is in the given range, -1 if there is no such line */ private int getFirstCompleteLineOfRegion(IRegion region, IDocument document) { try { final int startLine= document.getLineOfOffset(region.getOffset()); int offset= document.getLineOffset(startLine); if (offset >= region.getOffset()) return startLine; final int nextLine= startLine + 1; if (nextLine == document.getNumberOfLines()) return -1; offset= document.getLineOffset(nextLine); return (offset > region.getOffset() + region.getLength() ? -1 : nextLine); } catch (BadLocationException x) { // should not happen JavaPlugin.log(x); } return -1; }
Example 3
Source File: CountLinesBuffer.java From e4macs with Eclipse Public License 1.0 | 6 votes |
/** * @see com.mulgasoft.emacsplus.commands.EmacsPlusNoEditHandler#transform(org.eclipse.ui.texteditor.ITextEditor, org.eclipse.jface.text.IDocument, org.eclipse.jface.text.ITextSelection, org.eclipse.core.commands.ExecutionEvent) */ @Override protected int transform(ITextEditor editor, IDocument document, ITextSelection currentSelection, ExecutionEvent event) throws BadLocationException { int offset = currentSelection.getOffset(); IRegion lineInfo = document.getLineInformationOfOffset(offset); int lineTotal = document.getNumberOfLines(); int lineNumber = document.getLineOfOffset(offset); // only include current, if at beginning of line if (offset != lineInfo.getOffset()) { ++lineNumber; } if (document.getLength() == offset) { lineNumber = lineTotal; } setCmdResult(new Integer(lineTotal)); EmacsPlusUtils.showMessage(editor, String.format(COUNT_BUFFER, lineTotal, lineNumber, lineTotal - lineNumber), false); return super.transform(editor, document, currentSelection, event); }
Example 4
Source File: FileUtils.java From Pydev with Eclipse Public License 1.0 | 6 votes |
public static String getPythonFileEncoding(IDocument document, String fileLocation) throws PyUnsupportedEncodingException { List<String> first2Lines = new ArrayList<String>(2); switch (document.getNumberOfLines()) { case 0: break; case 1: first2Lines.add(TextSelectionUtils.getLine(document, 0)); break; default: // 2 or more first2Lines.add(TextSelectionUtils.getLine(document, 0)); first2Lines.add(TextSelectionUtils.getLine(document, 1)); break; } return getPythonFileEncoding(first2Lines, fileLocation); }
Example 5
Source File: TextSelectionUtils.java From Pydev with Eclipse Public License 1.0 | 6 votes |
/** * Deletes a line from the document * @param i */ public static void deleteLine(IDocument doc, int i) { try { IRegion lineInformation = doc.getLineInformation(i); int offset = lineInformation.getOffset(); int length = -1; if (doc.getNumberOfLines() > i) { int nextLineOffset = doc.getLineInformation(i + 1).getOffset(); length = nextLineOffset - offset; } else { length = lineInformation.getLength(); } if (length > -1) { doc.replace(offset, length, ""); } } catch (BadLocationException e) { Log.log(e); } }
Example 6
Source File: KillLineHandler.java From e4macs with Eclipse Public License 1.0 | 5 votes |
/** * @see com.mulgasoft.emacsplus.commands.EmacsPlusCmdHandler#transform(ITextEditor, IDocument, * ITextSelection, ExecutionEvent) */ @Override protected int transform(ITextEditor editor, IDocument document, ITextSelection currentSelection, ExecutionEvent event) throws BadLocationException { int uArg = getUniversalCount(); int offset = getCursorOffset(editor, currentSelection); int offsetLine = document.getLineOfOffset(offset); if (uArg == 1) { try { // gnu emacs: If the variable `kill-whole-line' is non-`nil', `C-k' at the very // beginning of a line kills the entire line including the following newline. boolean killWhole = isKillWholeLine() && (offset == document.getLineOffset(offsetLine)); executeCommand(killWhole ? CUT_LINE : CUT_LINE_TO_END, null, editor); } catch (Exception e) {} } else { try { // flag us as a kill command KillRing.getInstance().setKill(IEmacsPlusCommandDefinitionIds.KILL_LINE, false); int lastOffset = offset; // note that line numbers start from 0 int maxLine = document.getNumberOfLines() - 1; int endLine = uArg + document.getLineOfOffset(offset); // if range includes eof if (endLine >= maxLine) { // delete through to last character lastOffset = document.getLineOffset(maxLine) + document.getLineLength(maxLine); } else { // delete by whole lines lastOffset = document.getLineOffset(Math.min(Math.max(endLine, 0), maxLine)); } updateText(document, ((lastOffset >= offset ? offset : lastOffset)), Math.abs(lastOffset - offset), EMPTY_STR); } finally { // clear kill command flag KillRing.getInstance().setKill(null, false); } } return NO_OFFSET; }
Example 7
Source File: JavaFormatter.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private void indent(IDocument document) throws BadLocationException, MalformedTreeException { // first line int offset= document.getLineOffset(0); document.replace(offset, 0, CodeFormatterUtil.createIndentString(fInitialIndentLevel, fProject)); // following lines int lineCount= document.getNumberOfLines(); IndentUtil.indentLines(document, new LineRange(1, lineCount - 1), fProject, null); }
Example 8
Source File: JavaDocAutoIndentStrategy.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Guesses if the command operates within a newly created Javadoc comment or not. * If in doubt, it will assume that the Javadoc is new. * * @param document the document * @param commandOffset the command offset * @return <code>true</code> if the comment should be closed, <code>false</code> if not */ private boolean isNewComment(IDocument document, int commandOffset) { try { int lineIndex= document.getLineOfOffset(commandOffset) + 1; if (lineIndex >= document.getNumberOfLines()) return true; IRegion line= document.getLineInformation(lineIndex); ITypedRegion partition= TextUtilities.getPartition(document, fPartitioning, commandOffset, false); int partitionEnd= partition.getOffset() + partition.getLength(); if (line.getOffset() >= partitionEnd) return false; if (document.getLength() == partitionEnd) return true; // partition goes to end of document - probably a new comment String comment= document.get(partition.getOffset(), partition.getLength()); if (comment.indexOf("/*", 2) != -1) //$NON-NLS-1$ return true; // enclosed another comment -> probably a new comment return false; } catch (BadLocationException e) { return false; } }
Example 9
Source File: PropertyFileDocumentModel.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private void parsePropertyDocument(IDocument document) { fKeyValuePairs = new ArrayList<KeyValuePairModell>(); SimpleLineReader reader = new SimpleLineReader(document); int offset = 0; String line = reader.readLine(); int leadingWhiteSpaces = 0; while (line != null) { if (!SimpleLineReader.isCommentOrWhiteSpace(line)) { int idx = getIndexOfSeparationCharacter(line); if (idx != -1) { String key= line.substring(0, idx); String trimmedKey= key.trim(); String value= line.substring(idx + 1); String trimmedValue= Strings.trimLeadingTabsAndSpaces(value); int length= key.length() + 1 + value.length(); fKeyValuePairs.add(new KeyValuePairModell(trimmedKey, trimmedValue, offset, length, leadingWhiteSpaces)); leadingWhiteSpaces = 0; } } else { leadingWhiteSpaces += line.length(); } offset += line.length(); line = reader.readLine(); } int lastLine= document.getNumberOfLines() - 1; boolean needsNewLine= false; try { needsNewLine= !(document.getLineLength(lastLine) == 0); } catch (BadLocationException ignore) { // treat last line having no new line } LastKeyValuePair lastKeyValuePair = new LastKeyValuePair(offset, needsNewLine); fKeyValuePairs.add(lastKeyValuePair); }
Example 10
Source File: KillLineHandler.java From e4macs with Eclipse Public License 1.0 | 5 votes |
/** * When called from a console context, will use ST.CUT * * @see com.mulgasoft.emacsplus.commands.ConsoleCmdHandler#consoleDispatch(TextConsoleViewer, * IConsoleView, ExecutionEvent) */ public Object consoleDispatch(TextConsoleViewer viewer, IConsoleView activePart, ExecutionEvent event) { if (viewer.isEditable()) { IDocument doc = viewer.getDocument(); StyledText st = viewer.getTextWidget(); int offset = st.getCaretOffset(); try { IRegion info = doc.getLineInformationOfOffset(offset); int noffset = info.getOffset() + info.getLength(); if (offset == noffset) { int line = doc.getLineOfOffset(offset); if (++line < doc.getNumberOfLines()) { noffset = doc.getLineOffset(line); if (noffset == doc.getLength()) { noffset = offset; } } } if (offset != noffset) { st.redraw(); st.setSelection(offset, noffset); KillRing.getInstance().setKill(CUT_LINE_TO_END, false); return super.consoleDispatch(viewer, activePart, event); } viewer.refresh(); } catch (BadLocationException e) { } } return null; }
Example 11
Source File: TextSelectionUtils.java From Pydev with Eclipse Public License 1.0 | 5 votes |
/** * Adds a line to the document. * * @param doc the document * @param endLineDelim the delimiter that should be used * @param contents what should be added (the end line delimiter may be added before or after those contents * (depending on what are the current contents of the document). * @param afterLine the contents should be added after the line specified here. */ public static Tuple<Integer, String> getOffsetAndContentsToAddLine(IDocument doc, String endLineDelim, String contents, int afterLine) { try { int offset = -1; if (doc.getNumberOfLines() > afterLine) { offset = doc.getLineInformation(afterLine + 1).getOffset(); } else { offset = doc.getLineInformation(afterLine).getOffset(); } if (doc.getNumberOfLines() - 1 == afterLine) { contents = endLineDelim + contents; } if (!contents.endsWith(endLineDelim)) { contents += endLineDelim; } if (offset >= 0) { return new Tuple<>(offset, contents); } } catch (BadLocationException e) { Log.log(e); } return null; }
Example 12
Source File: JSDocAutoIndentStrategy.java From typescript.java with MIT License | 5 votes |
/** * Guesses if the command operates within a newly created javadoc comment or * not. If in doubt, it will assume that the javadoc is new. * * @param document * the document * @param commandOffset * the command offset * @return <code>true</code> if the comment should be closed, * <code>false</code> if not */ private boolean isNewComment(IDocument document, int commandOffset) { try { int lineIndex = document.getLineOfOffset(commandOffset) + 1; if (lineIndex >= document.getNumberOfLines()) return true; IRegion line = document.getLineInformation(lineIndex); ITypedRegion partition = TextUtilities.getPartition(document, fPartitioning, commandOffset, false); int partitionEnd = partition.getOffset() + partition.getLength(); if (line.getOffset() >= partitionEnd) return false; if (document.getLength() == partitionEnd) return true; // partition goes to end of document - probably a // new comment String comment = document.get(partition.getOffset(), partition.getLength()); if (comment.indexOf("/*", 2) != -1) //$NON-NLS-1$ return true; // enclosed another comment -> probably a new // comment return false; } catch (BadLocationException e) { return false; } }
Example 13
Source File: SmartSemicolonAutoEditStrategy.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Returns <code>true</code> if the document command is applied on a multi * line selection, <code>false</code> otherwise. * * @param document the document * @param command the command * @return <code>true</code> if <code>command</code> is a multiline command */ private boolean isMultilineSelection(IDocument document, DocumentCommand command) { try { return document.getNumberOfLines(command.offset, command.length) > 1; } catch (BadLocationException e) { // ignore return false; } }
Example 14
Source File: DefaultJavaFoldingStructureProvider.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Aligns <code>region</code> to start and end at a line offset. The region's start is * decreased to the next line offset, and the end offset increased to the next line start or the * end of the document. <code>null</code> is returned if <code>region</code> is * <code>null</code> itself or does not comprise at least one line delimiter, as a single line * cannot be folded. * * @param region the region to align, may be <code>null</code> * @param ctx the folding context * @return a region equal or greater than <code>region</code> that is aligned with line * offsets, <code>null</code> if the region is too small to be foldable (e.g. covers * only one line) */ protected final IRegion alignRegion(IRegion region, FoldingStructureComputationContext ctx) { if (region == null) return null; IDocument document= ctx.getDocument(); try { int start= document.getLineOfOffset(region.getOffset()); int end= document.getLineOfOffset(region.getOffset() + region.getLength()); if (start >= end) return null; int offset= document.getLineOffset(start); int endOffset; if (document.getNumberOfLines() > end + 1) endOffset= document.getLineOffset(end + 1); else endOffset= document.getLineOffset(end) + document.getLineLength(end); return new Region(offset, endOffset - offset); } catch (BadLocationException x) { // concurrent modification return null; } }
Example 15
Source File: NAppendCommentHandler.java From e4macs with Eclipse Public License 1.0 | 5 votes |
protected int transform(ITextEditor editor, IDocument document, ITextSelection currentSelection, ExecutionEvent event) throws BadLocationException { int currentLine = currentSelection.getStartLine(); currentLine = currentLine + 1; if (currentLine > document.getNumberOfLines()) { throw new BadLocationException(); } IRegion reg = document.getLineInformation(currentLine); ITextSelection newSelection = new TextSelection(document, reg .getOffset(), 0); return super.transform(editor, document, newSelection, event); }
Example 16
Source File: FixCompletionProposal.java From Pydev with Eclipse Public License 1.0 | 5 votes |
@Override public void apply(IDocument document) { try { document.replace(fReplacementOffset, fReplacementLength, fReplacementString); if (lineToRemove >= 0 && lineToRemove <= document.getNumberOfLines()) { IRegion lineInformation = document.getLineInformation(lineToRemove); document.replace(lineInformation.getOffset(), lineInformation.getLength(), ""); } } catch (BadLocationException x) { // ignore } }
Example 17
Source File: WhitespaceHelper.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
protected String calculateTrailingWhitespace(IDocument document) { try { IRegion line = document.getLineInformationOfOffset(offset + length); if (line != null) { String lineSuffix = document.get(offset + length, line.getLength() - (offset + length - line.getOffset())); if (isEmpty(lineSuffix.trim())) { length += lineSuffix.length(); int lineNr = document.getLineOfOffset(offset + length); if (lineNr < document.getNumberOfLines() - 1) { IRegion lineInformation = document.getLineInformation(lineNr + 1); if (!isEmpty(document.get(lineInformation.getOffset(), lineInformation.getLength()).trim())) { return lineSeparator; } } } else { for (int i = 0; i < lineSuffix.length(); ++i) { if (Character.isWhitespace(lineSuffix.charAt(i))) ++length; else break; } return lineSeparator + lineSeparator; } } } catch (BadLocationException e) { LOG.error("Error calculating trailing whitespace", e); } return null; }
Example 18
Source File: BrowseKillRingHandler.java From e4macs with Eclipse Public License 1.0 | 5 votes |
/** * Navigate up or down the ring entry by entry * * @param viewer the viewer on the console * @param dir FORWARD or BACKWARD */ private void browseRing(TextConsoleViewer viewer, int dir) { IDocument doc = viewer.getDocument(); StyledText st = viewer.getTextWidget(); if (doc != null && st != null) { int lines = doc.getNumberOfLines(); int off = st.getCaretOffset(); try { int l = doc.getLineOfOffset(off); KilledText okill = offsetHash.get(doc.getLineOffset(l)); KilledText nkill = null; int noff = -1; while ((l = l+dir) > -1 && l < lines){ off = doc.getLineOffset(l); KilledText tkill = offsetHash.get(off); if (nkill == null) { if (tkill != null && tkill != okill) { nkill = offsetHash.get(off); noff = off; if (dir == FORWARD) { break; } } } else { if (tkill != null && tkill != nkill){ break; } else { noff = off; } } } if (noff > -1) { st.setCaretOffset(noff); viewer.revealRange(noff, 0); } }catch (BadLocationException e) { } } }
Example 19
Source File: DocumentLineList.java From tm4e with Eclipse Public License 1.0 | 5 votes |
public DocumentLineList(IDocument document) { this.document = document; this.listener = new InternalListener(); document.addDocumentListener(listener); for (int i = 0; i < document.getNumberOfLines(); i++) { addLine(i); } }
Example 20
Source File: AbapGitStagingView.java From ADT_Frontend with MIT License | 4 votes |
/** * Validates the UI inputs */ protected int validateInputs() { int errorCode = 0; boolean valid = true; //check commit message String commitMessage = this.commitMessageTextViewer.getTextWidget().getText(); IDocument commitMessageDocument = new Document(commitMessage); if (commitMessageDocument.getNumberOfLines() > 1) { try { IRegion lineInfo = commitMessageDocument.getLineInformation(1); if (lineInfo.getLength() > 0) { valid = false; errorCode = 1; setWarnings(Messages.AbapGitStaging_commit_message_second_line_not_empty); } } catch (BadLocationException e) { AbapGitUIPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, AbapGitUIPlugin.PLUGIN_ID, e.getMessage(), e)); } } //check author String authorText = this.authorText.getText(); if (getAuthorFromUIText(authorText) == null) { setWarnings(Messages.AbapGitStaging_invalid_author); valid = false; errorCode = 2; } //check committer String committerText = this.committerText.getText(); if (getCommitterFromUIText(committerText) == null) { setWarnings(Messages.AbapGitStaging_invalid_committer); valid = false; errorCode = 3; } if (valid) { hideWarnings(); } return errorCode; }