Search in sources :

Example 1 with LogicalPosition

use of com.intellij.openapi.editor.LogicalPosition in project intellij-community by JetBrains.

the class PyEditingTest method testUncommentWithSpace.

public void testUncommentWithSpace() throws Exception {
    // PY-980
    myFixture.configureByFile("/editing/uncommentWithSpace.before.py");
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(0, 1));
    PlatformTestUtil.invokeNamedAction(IdeActions.ACTION_COMMENT_LINE);
    myFixture.checkResultByFile("/editing/uncommentWithSpace.after.py", true);
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition)

Example 2 with LogicalPosition

use of com.intellij.openapi.editor.LogicalPosition in project intellij-community by JetBrains.

the class AddEncodingQuickFix method applyFix.

public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
    final PsiElement element = descriptor.getPsiElement();
    final PsiFile file = element.getContainingFile();
    if (file == null)
        return;
    PsiElement firstLine = file.getFirstChild();
    if (firstLine instanceof PsiComment && firstLine.getText().startsWith("#!")) {
        firstLine = firstLine.getNextSibling();
    }
    final LanguageLevel languageLevel = LanguageLevel.forElement(file);
    final String commentText = String.format(PyEncodingUtil.ENCODING_FORMAT_PATTERN[myEncodingFormatIndex], myDefaultEncoding);
    final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
    PsiComment encodingComment = elementGenerator.createFromText(languageLevel, PsiComment.class, commentText);
    encodingComment = (PsiComment) file.addBefore(encodingComment, firstLine);
    final FileEditor fileEditor = FileEditorManager.getInstance(project).getSelectedEditor(element.getContainingFile().getVirtualFile());
    if (fileEditor instanceof TextEditor) {
        if (encodingComment.getNextSibling() == null || !encodingComment.getNextSibling().textContains('\n')) {
            file.addAfter(elementGenerator.createFromText(languageLevel, PsiWhiteSpace.class, "\n"), encodingComment);
        }
        final Editor editor = ((TextEditor) fileEditor).getEditor();
        final Document document = editor.getDocument();
        final int insertedLineNumber = document.getLineNumber(encodingComment.getTextOffset());
        editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(insertedLineNumber + 1, 0));
    }
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) FileEditor(com.intellij.openapi.fileEditor.FileEditor) Document(com.intellij.openapi.editor.Document) PsiComment(com.intellij.psi.PsiComment) TextEditor(com.intellij.openapi.fileEditor.TextEditor) LanguageLevel(com.jetbrains.python.psi.LanguageLevel) PsiFile(com.intellij.psi.PsiFile) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) Editor(com.intellij.openapi.editor.Editor) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 3 with LogicalPosition

use of com.intellij.openapi.editor.LogicalPosition in project ideavim by JetBrains.

the class MarkGroup method updateMarkFromInsert.

/**
   * This updates all the marks for a file whenever text is inserted into the file. If the line that contains a mark
   * that is after the start of the insertion point, shift the mark by the number of new lines added.
   *
   * @param editor      The editor that was updated
   * @param marks       The editor's marks
   * @param insStartOff The insertion point
   * @param insLength   The length of the insertion
   */
public static void updateMarkFromInsert(@Nullable Editor editor, @Nullable HashMap<Character, Mark> marks, int insStartOff, int insLength) {
    if (marks != null && marks.size() > 0 && editor != null) {
        int insEndOff = insStartOff + insLength;
        LogicalPosition insStart = editor.offsetToLogicalPosition(insStartOff);
        LogicalPosition insEnd = editor.offsetToLogicalPosition(insEndOff);
        if (logger.isDebugEnabled())
            logger.debug("mark insert. insStart = " + insStart + ", insEnd = " + insEnd);
        int lines = insEnd.line - insStart.line;
        if (lines == 0)
            return;
        for (Mark mark : marks.values()) {
            if (logger.isDebugEnabled())
                logger.debug("mark = " + mark);
            // Shift the mark if the insertion began on a line prior to the marked line.
            if (insStart.line < mark.getLogicalLine()) {
                mark.setLogicalLine(mark.getLogicalLine() + lines);
                if (logger.isDebugEnabled())
                    logger.debug("Shifting mark by " + lines + " lines");
            }
        }
    }
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) Mark(com.maddyhome.idea.vim.common.Mark)

Example 4 with LogicalPosition

use of com.intellij.openapi.editor.LogicalPosition in project ideavim by JetBrains.

the class MarkGroup method updateMarkFromDelete.

/**
   * This updates all the marks for a file whenever text is deleted from the file. If the line that contains a mark
   * is completely deleted then the mark is deleted too. If the deleted text is before the marked line, the mark is
   * moved up by the number of deleted lines.
   *
   * @param editor      The modified editor
   * @param marks       The editor's marks to update
   * @param delStartOff The offset within the editor where the deletion occurred
   * @param delLength   The length of the deleted text
   */
public static void updateMarkFromDelete(@Nullable Editor editor, @Nullable HashMap<Character, Mark> marks, int delStartOff, int delLength) {
    // Skip all this work if there are no marks
    if (marks != null && marks.size() > 0 && editor != null) {
        // Calculate the logical position of the start and end of the deleted text
        int delEndOff = delStartOff + delLength - 1;
        LogicalPosition delStart = editor.offsetToLogicalPosition(delStartOff);
        LogicalPosition delEnd = editor.offsetToLogicalPosition(delEndOff + 1);
        if (logger.isDebugEnabled())
            logger.debug("mark delete. delStart = " + delStart + ", delEnd = " + delEnd);
        // Now analyze each mark to determine if it needs to be updated or removed
        for (Character ch : marks.keySet()) {
            Mark mark = marks.get(ch);
            if (logger.isDebugEnabled())
                logger.debug("mark = " + mark);
            // proper number of lines.
            if (delEnd.line < mark.getLogicalLine()) {
                int lines = delEnd.line - delStart.line;
                if (logger.isDebugEnabled())
                    logger.debug("Shifting mark by " + lines + " lines");
                mark.setLogicalLine(mark.getLogicalLine() - lines);
            } else // If the deleted text begins before the mark and ends after the mark then it may be shifted or deleted
            if (delStart.line <= mark.getLogicalLine() && delEnd.line >= mark.getLogicalLine()) {
                int markLineStartOff = EditorHelper.getLineStartOffset(editor, mark.getLogicalLine());
                int markLineEndOff = EditorHelper.getLineEndOffset(editor, mark.getLogicalLine(), true);
                Command command = CommandState.getInstance(editor).getCommand();
                // If text is being changed from the start of the mark line (a special case for mark deletion)
                boolean changeFromMarkLineStart = command != null && command.getType() == Command.Type.CHANGE && delStartOff == markLineStartOff;
                // If the marked line is completely within the deleted text, remove the mark (except the special case)
                if (delStartOff <= markLineStartOff && delEndOff >= markLineEndOff && !changeFromMarkLineStart) {
                    VimPlugin.getMark().removeMark(ch, mark);
                    logger.debug("Removed mark");
                } else // on a line prior to the marked line (which means the deletion must end on the marked line).
                if (delStart.line < mark.getLogicalLine()) {
                    // shift mark
                    mark.setLogicalLine(delStart.line);
                    if (logger.isDebugEnabled())
                        logger.debug("Shifting mark to line " + delStart.line);
                }
            }
        }
    }
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) Command(com.maddyhome.idea.vim.command.Command) Mark(com.maddyhome.idea.vim.common.Mark)

Example 5 with LogicalPosition

use of com.intellij.openapi.editor.LogicalPosition in project ideavim by JetBrains.

the class MarkGroup method addJump.

private void addJump(@NotNull Editor editor, int offset, boolean reset) {
    final VirtualFile vf = EditorData.getVirtualFile(editor);
    if (vf == null) {
        return;
    }
    LogicalPosition lp = editor.offsetToLogicalPosition(offset);
    Jump jump = new Jump(lp.line, lp.column, vf.getPath());
    final String filename = jump.getFilename();
    for (int i = 0; i < jumps.size(); i++) {
        Jump j = jumps.get(i);
        if (filename != null && filename.equals(j.getFilename()) && j.getLogicalLine() == jump.getLogicalLine()) {
            jumps.remove(i);
            break;
        }
    }
    jumps.add(jump);
    if (reset) {
        jumpSpot = -1;
    } else {
        jumpSpot++;
    }
    if (jumps.size() > SAVE_JUMP_COUNT) {
        jumps.remove(0);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LogicalPosition(com.intellij.openapi.editor.LogicalPosition) Jump(com.maddyhome.idea.vim.common.Jump)

Aggregations

LogicalPosition (com.intellij.openapi.editor.LogicalPosition)106 Document (com.intellij.openapi.editor.Document)16 PsiElement (com.intellij.psi.PsiElement)14 Editor (com.intellij.openapi.editor.Editor)12 TextRange (com.intellij.openapi.util.TextRange)11 VirtualFile (com.intellij.openapi.vfs.VirtualFile)10 EditorEx (com.intellij.openapi.editor.ex.EditorEx)7 CaretModel (com.intellij.openapi.editor.CaretModel)6 NotNull (org.jetbrains.annotations.NotNull)6 RelativePoint (com.intellij.ui.awt.RelativePoint)5 JSFile (com.intellij.lang.javascript.psi.JSFile)4 EditorColorsManager (com.intellij.openapi.editor.colors.EditorColorsManager)4 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)4 Project (com.intellij.openapi.project.Project)4 PsiFile (com.intellij.psi.PsiFile)4 PsiReference (com.intellij.psi.PsiReference)4 PsiMultiReference (com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference)4 Mark (com.maddyhome.idea.vim.common.Mark)4 HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)3 EditorWindow (com.intellij.injected.editor.EditorWindow)3