private String lineNumberToString(int lineNumber, @NotNull Editor editor) { final int lineCount = editor.getDocument().getLineCount(); final int digitsCount = (int)Math.ceil(Math.log10(lineCount)); return StringHelper.leftJustify("" + lineNumber, digitsCount, ' '); }
@Override public void actionPerformed(@NotNull AnActionEvent e) { DataContext dataContext = e.getDataContext(); Editor editor = getEditor(dataContext); if (editor == null) return; InlayModel inlayModel = editor.getInlayModel(); Document document = editor.getDocument(); int lineCount = document.getLineCount(); for (int i = random.nextInt(10); i < lineCount; ) { int offset = document.getLineStartOffset(i); // Mostly above boolean above = random.nextInt(10) > 3; // Mostly do one, but occasionally throw in a bunch int count = random.nextInt(10) > 7 ? random.nextInt(5) : 1; for (int j = 0; j < count; j++) { float factor = Math.max(1.75f * random.nextFloat(), 0.9f); String text = String.format("---------- %s line %d ----------", above ? "above" : "below", i + 1); inlayModel.addBlockElement(offset, true, above, 0, new MyBlockRenderer(factor, text)); } // Every 10 lines +/- 3 lines i += 10 + (random.nextInt(6) - 3); } }
/** * Gets the number of actual lines in the file * * @param editor The editor * @return The file line count */ public static int getLineCount(@NotNull final Editor editor) { int len = editor.getDocument().getLineCount(); if (editor.getDocument().getTextLength() > 0 && editor.getDocument().getCharsSequence().charAt(editor.getDocument().getTextLength() - 1) == '\n') { len--; } return len; }
@NotNull private LineRange getLineRange(@NotNull Editor editor, @NotNull Caret caret, @NotNull DataContext context, @NotNull ExCommand cmd) { final LineRange range = cmd.getLineRange(editor, caret, context); final LineRange normalizedRange; // Something like "30,20sort" gets converted to "20,30sort" if (range.getEndLine() < range.getStartLine()) { normalizedRange = new LineRange(range.getEndLine(), range.getStartLine()); } else { normalizedRange = range; } // If we don't have a range, we either have "sort", a selection, or a block if (normalizedRange.getEndLine() - normalizedRange.getStartLine() == 0) { // If we have a selection. final SelectionModel selectionModel = editor.getSelectionModel(); if (selectionModel.hasSelection()) { final int start = selectionModel.getSelectionStart(); final int end = selectionModel.getSelectionEnd(); final int startLine = editor.offsetToLogicalPosition(start).line; final int endLine = editor.offsetToLogicalPosition(end).line; return new LineRange(startLine, endLine); } // If we have a generic selection, i.e. "sort" entire document else { return new LineRange(0, editor.getDocument().getLineCount() - 1); } } return normalizedRange; }
private static int getDocumentLineCount(final Document document) { try { final int lineCount = document.getLineCount(); return lineCount == 0 ? 1 : lineCount; } catch (final Exception e) { return 1; } }
private int getDocumentLineCount(final Document document) { try { final int lineCount = document.getLineCount(); return lineCount == 0 ? 1 : lineCount; } catch (final NullPointerException e) { return 1; } }
public PerlFormattingContext(@NotNull PsiElement element, @NotNull CodeStyleSettings settings) { mySettings = settings.getCommonSettings(PerlLanguage.INSTANCE); myPerlSettings = settings.getCustomSettings(PerlCodeStyleSettings.class); mySpacingBuilder = createSpacingBuilder(); PsiFile containingFile = element.getContainingFile(); myDocument = containingFile == null ? null : containingFile.getViewProvider().getDocument(); myCommentsLines = myDocument == null ? new BitSet() : new BitSet(myDocument.getLineCount()); }
private static TextRange getIssueTextRange(PsiFile file, Document doc, IssueLocation issueLocation) throws NoMatchException { int ijStartLine = issueLocation.getStartLine() - 1; int ijEndLine = issueLocation.getEndLine() - 1; int lineCount = doc.getLineCount(); if (ijStartLine >= doc.getLineCount()) { throw new NoMatchException("Start line number (" + ijStartLine + ") larger than lines in file: " + lineCount); } if (ijEndLine >= doc.getLineCount()) { throw new NoMatchException("End line number (" + ijStartLine + ") larger than lines in file: " + lineCount); } int rangeEnd = findEndLineOffset(doc, ijEndLine, issueLocation.getEndLineOffset()); int rangeStart = findStartLineOffset(file, doc, ijStartLine, issueLocation.getStartLineOffset(), rangeEnd); if (rangeEnd < rangeStart) { throw new NoMatchException("Invalid text range (start: " + rangeStart + ", end: " + rangeEnd); } return new TextRange(rangeStart, rangeEnd); }
if (currentLine < document.getLineCount() - 1) { final PsiComment nextLineComment = PsiTreeUtil.findElementOfClassAtRange( file, for (int i = currentLine + 1; i < document.getLineCount(); i++) { int start = document.getLineStartOffset(i); int end = document.getLineEndOffset(i);
public void addComment(Editor editor, ChangeInfo changeInfo, String revisionId, Project project, Comment comment) { if (editor == null) return; MarkupModel markup = editor.getMarkupModel(); RangeHighlighter rangeHighlighter = null; if (comment.range != null) { rangeHighlighter = highlightRangeComment(comment.range, editor, project); } int lineCount = markup.getDocument().getLineCount(); int line = (comment.line != null ? comment.line : 0) - 1; if (line < 0) { line = 0; } if (line > lineCount - 1) { line = lineCount - 1; } if (line >= 0) { final RangeHighlighter highlighter = markup.addLineHighlighter(line, HighlighterLayer.ERROR + 1, null); CommentGutterIconRenderer iconRenderer = new CommentGutterIconRenderer( this, editor, gerritUtil, gerritSettings, addCommentActionBuilder, comment, changeInfo, revisionId, highlighter, rangeHighlighter); highlighter.setGutterIconRenderer(iconRenderer); } }
protected void doTestConsoleFilter(@NotNull com.intellij.execution.filters.Filter filter) { initWithFileSmart(); Document document = getEditor().getDocument(); int lines = document.getLineCount();