public static int characterPositionToOffset(@NotNull final Editor editor, @NotNull final CharacterPosition pos) { return editor.getDocument().getLineStartOffset(normalizeLine(editor, pos.line)) + pos.column; }
/** * Gets the offset of the start of the line containing the supplied offset * * @param editor The editor * @param offset The offset within the line * @return The offset of the line start */ public static int getLineStartForOffset(@NotNull final Editor editor, final int offset) { LogicalPosition pos = editor.offsetToLogicalPosition(normalizeOffset(editor, offset)); return editor.getDocument().getLineStartOffset(pos.line); }
@NotNull private static Optional<Integer> offset(@NotNull String path, @NotNull Matcher matcher) { VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(path); Optional<Integer> offset = Optional.empty(); if (virtualFile != null) { Document document = FileDocumentManager.getInstance().getDocument(virtualFile); if (document != null) { int lineNumber = Integer.parseInt(matcher.group("lineNumber")); int start = document.getLineStartOffset(lineNumber - 1); @Nullable String formattedColumnNumber = matcher.group("columnNumber"); if (formattedColumnNumber != null) { int column = Integer.parseInt(formattedColumnNumber) - 1; offset = Optional.of(start + column); } else { offset = Optional.of(start); } } } return offset; }
@NotNull public static CharacterPosition offsetToCharacterPosition(@NotNull final Editor editor, final int offset) { int line = editor.getDocument().getLineNumber(normalizeOffset(editor, offset)); int col = offset - editor.getDocument().getLineStartOffset(line); return new CharacterPosition(line, col); }
/** * Returns the offset of the start of the requested line. * * @param editor The editor * @param line The logical line to get the start offset for. * @return 0 if line is < 0, file size of line is bigger than file, else the start offset for the line */ public static int getLineStartOffset(@NotNull final Editor editor, final int line) { if (line < 0) { return 0; } else if (line >= getLineCount(editor)) { return getFileSize(editor); } else { return editor.getDocument().getLineStartOffset(line); } }
@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); } }
/** * Converts a logical line number to a visual line number. Several logical lines can map to the same * visual line when there are collapsed fold regions. * * @param editor The editor * @param line The logical line number to convert * @return The visual line number */ public static int logicalLineToVisualLine(@NotNull final Editor editor, final int line) { if (editor instanceof EditorImpl) { // This is faster than simply calling Editor#logicalToVisualPosition return ((EditorImpl) editor).offsetToVisualLine(editor.getDocument().getLineStartOffset(line)); } return editor.logicalToVisualPosition(new LogicalPosition(line, 0)).line; }
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); }
int col = editor.getCaretModel().getOffset() - doc.getLineStartOffset(lline);
@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; }
int col = editor.getCaretModel().getOffset() - doc.getLineStartOffset(lp.line); int endoff = doc.getLineEndOffset(lp.line); if (doc.getCharsSequence().charAt(endoff) == '\n') { endoff--; int ecol = endoff - doc.getLineStartOffset(lp.line); LogicalPosition elp = editor.offsetToLogicalPosition(endoff);
private boolean firstOnLine(@NotNull ASTNode node) { Document document = document(node.getPsi()); assert document != null; int nodeStartOffset = node.getStartOffset(); int lineNumber = document.getLineNumber(nodeStartOffset); int lineStartOffset = document.getLineStartOffset(lineNumber); String prefix = document.getText(new TextRange(lineStartOffset, nodeStartOffset)); return isWhitespace(prefix); }
VisualPosition position = editors[0].getSelectionModel().getSelectionStartPosition(); if (position != null) { offset = document.getLineStartOffset(position.line);
@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; }
document.insertString(lbraceEndOffset, StringUtil.repeat("\n", 2 - lineDiff)); int offsetToMove = document.getLineStartOffset(startLine + 1); context.getEditor().getCaretModel().moveToOffset(offsetToMove); CodeStyleManager.getInstance(context.getProject()).adjustLineIndent(document, offsetToMove);