/** * Gets the offset of the end of the line containing the supplied offset * * @param editor The editor * @param offset The offset within the line * @return The offset of the line end */ public static int getLineEndForOffset(@NotNull final Editor editor, final int offset) { LogicalPosition pos = editor.offsetToLogicalPosition(normalizeOffset(editor, offset)); return editor.getDocument().getLineEndOffset(pos.line); }
/** * Gets the number of characters on the specified logical line. This will be different than the number of visual * characters if there are "real" tabs in the line. * * @param editor The editor * @param line The logical line within the file * @return The number of characters in the specified line */ public static int getLineLength(@NotNull final Editor editor, final int line) { if (getLineCount(editor) == 0) { return 0; } else { return Math.max(0, editor.offsetToLogicalPosition(editor.getDocument().getLineEndOffset(line)).column); } }
public void autoIndentLines(@NotNull Editor editor, @NotNull Caret caret, @NotNull DataContext context, int count) { final int startLine = caret.getLogicalPosition().line; final int endLine = startLine + count - 1; if (endLine <= EditorHelper.getLineCount(editor)) { final TextRange range = new TextRange(caret.getOffset(), editor.getDocument().getLineEndOffset(endLine)); autoIndentRange(editor, caret, context, range); } }
/** * Returns the offset of the end of the requested line. * * @param editor The editor * @param line The logical line to get the end offset for * @param allowEnd True include newline * @return 0 if line is < 0, file size of line is bigger than file, else the end offset for the line */ public static int getLineEndOffset(@NotNull final Editor editor, final int line, final boolean allowEnd) { if (line < 0) { return 0; } else if (line >= getLineCount(editor)) { return getFileSize(editor, allowEnd); } else { return editor.getDocument().getLineEndOffset(line) - (allowEnd ? 0 : 1); } }
private static boolean isLineBreakpointAvailable(@NotNull VirtualFile file, int line, @NotNull Project project) { Document document = FileDocumentManager.getInstance().getDocument(file); if (document == null || document.getLineEndOffset(line) == document.getLineStartOffset(line)) return false; Checker canPutAtChecker = new Checker(); XDebuggerUtil.getInstance().iterateLine(project, document, line, canPutAtChecker); return canPutAtChecker.isLineBreakpointAvailable(); }
private static boolean isLineBreakpointAvailable(@NotNull VirtualFile file, int line, @NotNull Project project) { Document document = FileDocumentManager.getInstance().getDocument(file); if (document == null || document.getLineEndOffset(line) == document.getLineStartOffset(line)) { return false; } Checker canPutAtChecker = new Checker(); XDebuggerUtil.getInstance().iterateLine(project, document, line, canPutAtChecker); // Used to identify line comments, which cannot be correctly processed by the checker. if (document.getText(new TextRange(document.getLineStartOffset(line), document.getLineEndOffset(line))).trim() .startsWith(BALLERINA_LINE_COMMENT_PREFIX)) { return false; } return canPutAtChecker.isLineBreakpointAvailable(); }
/** * Sort range of text with a given comparator * * @param editor The editor to replace text in * @param range The range to sort * @param lineComparator The comparator to use to sort * @return true if able to sort the text, false if not */ public boolean sortRange(@NotNull Editor editor, @NotNull LineRange range, @NotNull Comparator<String> lineComparator) { final int startLine = range.getStartLine(); final int endLine = range.getEndLine(); final int count = endLine - startLine + 1; if (count < 2) { return false; } final int startOffset = editor.getDocument().getLineStartOffset(startLine); final int endOffset = editor.getDocument().getLineEndOffset(endLine); return sortTextRange(editor, startOffset, endOffset, lineComparator); }
private int doIndent(@NotNull Editor editor, @NotNull Caret caret, @NotNull DataContext context, int startOffset, int endOffset) { final int startLine = editor.offsetToLogicalPosition(startOffset).line; final int endLine = editor.offsetToLogicalPosition(endOffset - 1).line; final int startLineOffset = editor.getDocument().getLineStartOffset(startLine); final int endLineOffset = editor.getDocument().getLineEndOffset(endLine); VimPlugin.getChange().autoIndentRange(editor, caret, context, new TextRange(startLineOffset, endLineOffset)); return EditorHelper.getLineEndOffset(editor, endLine, true); }
@Override public void apply(@NotNull PsiFile file, @NotNull List<Issue> issueList, @NotNull AnnotationHolder holder) { if (issueList.size() > 0) { @Nullable Document document = file.getViewProvider().getDocument(); if (document != null) { String workingDirectory = ensureWorkingDirectory(file.getProject()); for (Issue issue : issueList) { int lineStartOffset = document.getLineStartOffset(issue.line); int start; int end; if (issue.column != null) { start = lineStartOffset + issue.column; end = start + 1; } else { start = lineStartOffset; end = document.getLineEndOffset(issue.line); } Annotation annotation = holder.createWarningAnnotation(new TextRange(start, end), issue.message); annotation.setAfterEndOfLine(end == start); issue.explanation.ifPresent(explanation -> { String toolTip = explanationToToolTip(explanation, workingDirectory); if (!toolTip.isEmpty()) { annotation.setTooltip(toolTip); } }); } } } }
public boolean execute(@NotNull Editor editor, @NotNull DataContext context, @NotNull ExCommand cmd) throws ExException { LineRange range = cmd.getLineRange(editor, context); CharSequence chars = editor.getDocument().getCharsSequence(); for (int l = range.getStartLine(); l <= range.getEndLine(); l++) { int start = editor.getDocument().getLineStartOffset(l); int end = editor.getDocument().getLineEndOffset(l); if (logger.isDebugEnabled()) { logger.debug("Line " + l + ", start offset=" + start + ", end offset=" + end); for (int i = start; i <= end; i++) { logger.debug("Offset " + i + ", char=" + chars.charAt(i) + ", lp=" + editor.offsetToLogicalPosition(i) + ", vp=" + editor.offsetToVisualPosition(i)); } } } return true; }
LogicalPosition lp = editor.getCaretModel().getLogicalPosition(); int col = editor.getCaretModel().getOffset() - doc.getLineStartOffset(lp.line); int endoff = doc.getLineEndOffset(lp.line); if (doc.getCharsSequence().charAt(endoff) == '\n') { endoff--;
@Override public boolean autoImportReferenceAtCursor(@NotNull Editor editor, @NotNull PsiFile file) { if (!file.getViewProvider().getLanguages().contains(GoLanguage.INSTANCE) || !DaemonListeners.canChangeFileSilently(file)) { return false; } int caretOffset = editor.getCaretModel().getOffset(); Document document = editor.getDocument(); int lineNumber = document.getLineNumber(caretOffset); int startOffset = document.getLineStartOffset(lineNumber); int endOffset = document.getLineEndOffset(lineNumber); List<PsiElement> elements = CollectHighlightsUtil.getElementsInRange(file, startOffset, endOffset); for (PsiElement element : elements) { if (element instanceof GoCompositeElement) { for (PsiReference reference : element.getReferences()) { GoImportPackageQuickFix fix = new GoImportPackageQuickFix(reference); if (fix.doAutoImportOrShowHint(editor, false)) { return true; } } } } return false; }
@Override public Result postProcessEnter(@NotNull PsiFile file, @NotNull Editor editor, @NotNull DataContext dataContext) { if (!file.getLanguage().is(BallerinaLanguage.INSTANCE)) { return Result.Continue; } // We need to save the file before checking. Otherwise issues can occur when we press enter in a string. Project project = file.getProject(); PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); // Checks whether the previous line starts with "#". LogicalPosition caretPos = editor.getCaretModel().getLogicalPosition(); int prevLine = caretPos.line - 1; String lineString = editor.getDocument().getText( new TextRange(editor.getDocument().getLineStartOffset(prevLine), editor.getDocument().getLineEndOffset(prevLine))); if (lineString.trim().startsWith("#")) { int newCol = lineString.indexOf("#"); String enteredText = editor.getDocument().getText( new TextRange(editor.getDocument().getLineStartOffset(caretPos.line), editor.getDocument().getLineEndOffset(caretPos.line))).trim(); editor.getDocument().deleteString(editor.getDocument().getLineStartOffset(caretPos.line), editor.getDocument().getLineEndOffset(caretPos.line)); editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(caretPos.line, 1)); enterNewLine(editor, enteredText, newCol); // Commit the document. PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); } return Result.Continue; }
if (type == SelectionType.LINE_WISE) { if (subMode == CommandState.SubMode.VISUAL_BLOCK) { startOffset = editor.getDocument().getLineEndOffset(endLines.get(index)) + 1;
if (column < MotionGroup.LAST_COLUMN && lineLength < column) { final String pad = EditorHelper.pad(editor, context, line, column); final int offset = editor.getDocument().getLineEndOffset(line); caret.moveToOffset(offset); insertText(editor, caret, pad);
final String pad = EditorHelper.pad(editor, context, logicalLine + i, repeatColumn); if (pad.length() > 0) { final int offset = editor.getDocument().getLineEndOffset(logicalLine + i); caret.moveToOffset(offset); insertText(editor, caret, pad);
int end = editor.getDocument().getLineEndOffset(line2);
private void removeEmptyLineIfNecessary(final Editor editor) { final int offset = editor.getCaretModel().getOffset(); final int lineNumber = editor.getDocument().getLineNumber(offset); final int lineStartOffset = editor.getDocument().getLineStartOffset(lineNumber); final int lineEndOffset = editor.getDocument().getLineEndOffset(lineNumber); final String lineContent = editor.getDocument().getText().substring(lineStartOffset, lineEndOffset); if ("".equals(lineContent.trim())) { final int endIndex = editor.getDocument().getText().length() > lineEndOffset ? lineEndOffset + 1 : lineEndOffset; editor.getDocument().deleteString(lineStartOffset, endIndex); } }
private void removeEmptyLineIfNecessary(final Editor editor) { final int offset = editor.getCaretModel().getOffset(); final int lineNumber = editor.getDocument().getLineNumber(offset); final int lineStartOffset = editor.getDocument().getLineStartOffset(lineNumber); final int lineEndOffset = editor.getDocument().getLineEndOffset(lineNumber); final String lineContent = editor.getDocument().getText().substring(lineStartOffset, lineEndOffset); if ("".equals(lineContent.trim())) { final int endIndex = editor.getDocument().getText().length() > lineEndOffset ? lineEndOffset + 1 : lineEndOffset; editor.getDocument().deleteString(lineStartOffset, endIndex); } }
@Override protected void run() throws Throwable { int position = document.getLineEndOffset(document.getLineNumber(anchorOffset)); document.insertString(position, text); } }.execute();