Search in sources :

Example 21 with CaretModel

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

the class SmartIndentingBackspaceHandler method doBeforeCharDeleted.

@Override
protected void doBeforeCharDeleted(char c, PsiFile file, Editor editor) {
    Project project = file.getProject();
    Document document = editor.getDocument();
    CharSequence charSequence = document.getImmutableCharSequence();
    CaretModel caretModel = editor.getCaretModel();
    int caretOffset = caretModel.getOffset();
    LogicalPosition pos = caretModel.getLogicalPosition();
    int lineStartOffset = document.getLineStartOffset(pos.line);
    int beforeWhitespaceOffset = CharArrayUtil.shiftBackward(charSequence, caretOffset - 1, " \t") + 1;
    if (beforeWhitespaceOffset != lineStartOffset) {
        myReplacement = null;
        return;
    }
    PsiDocumentManager.getInstance(project).commitDocument(document);
    CodeStyleFacade codeStyleFacade = CodeStyleFacade.getInstance(project);
    myReplacement = codeStyleFacade.getLineIndent(document, lineStartOffset);
    if (myReplacement == null) {
        return;
    }
    int tabSize = codeStyleFacade.getTabSize(file.getFileType());
    int targetColumn = getWidth(myReplacement, tabSize);
    int endOffset = CharArrayUtil.shiftForward(charSequence, caretOffset, " \t");
    LogicalPosition logicalPosition = caretOffset < endOffset ? editor.offsetToLogicalPosition(endOffset) : pos;
    int currentColumn = logicalPosition.column;
    if (currentColumn > targetColumn) {
        myStartOffset = lineStartOffset;
    } else if (logicalPosition.line == 0) {
        myStartOffset = 0;
        myReplacement = "";
    } else {
        int prevLineEndOffset = document.getLineEndOffset(logicalPosition.line - 1);
        myStartOffset = CharArrayUtil.shiftBackward(charSequence, prevLineEndOffset - 1, " \t") + 1;
        if (myStartOffset != document.getLineStartOffset(logicalPosition.line - 1)) {
            int spacing = CodeStyleManager.getInstance(project).getSpacing(file, endOffset);
            myReplacement = StringUtil.repeatSymbol(' ', Math.max(0, spacing));
        }
    }
}
Also used : CodeStyleFacade(com.intellij.codeStyle.CodeStyleFacade) Project(com.intellij.openapi.project.Project) LogicalPosition(com.intellij.openapi.editor.LogicalPosition) CaretModel(com.intellij.openapi.editor.CaretModel) Document(com.intellij.openapi.editor.Document)

Example 22 with CaretModel

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

the class WrapReturnValueHandler method invoke.

public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) {
    final ScrollingModel scrollingModel = editor.getScrollingModel();
    scrollingModel.scrollToCaret(ScrollType.MAKE_VISIBLE);
    final PsiElement element = CommonDataKeys.PSI_ELEMENT.getData(dataContext);
    PsiMethod selectedMethod = null;
    if (element instanceof PsiMethod) {
        selectedMethod = (PsiMethod) element;
    } else {
        final CaretModel caretModel = editor.getCaretModel();
        final int position = caretModel.getOffset();
        PsiElement selectedElement = file.findElementAt(position);
        while (selectedElement != null) {
            if (selectedElement instanceof PsiMethod) {
                selectedMethod = (PsiMethod) selectedElement;
                break;
            }
            selectedElement = selectedElement.getParent();
        }
    }
    if (selectedMethod == null) {
        CommonRefactoringUtil.showErrorHint(project, editor, RefactorJBundle.message("cannot.perform.the.refactoring") + RefactorJBundle.message("the.caret.should.be.positioned.at.the.name.of.the.method.to.be.refactored"), REFACTORING_NAME, this.getHelpID());
        return;
    }
    invoke(project, selectedMethod, editor);
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) ScrollingModel(com.intellij.openapi.editor.ScrollingModel)

Example 23 with CaretModel

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

the class JavadocHelper method navigate.

/**
   * Tries to navigate caret at the given editor to the target position inserting missing white spaces if necessary.
   * 
   * @param position  target caret position
   * @param editor    target editor
   * @param project   target project
   */
@SuppressWarnings("MethodMayBeStatic")
public void navigate(@NotNull LogicalPosition position, @NotNull Editor editor, @NotNull final Project project) {
    final Document document = editor.getDocument();
    final CaretModel caretModel = editor.getCaretModel();
    final int endLineOffset = document.getLineEndOffset(position.line);
    final LogicalPosition endLinePosition = editor.offsetToLogicalPosition(endLineOffset);
    if (endLinePosition.column < position.column && !editor.getSettings().isVirtualSpace() && !editor.isViewer()) {
        final String toInsert = StringUtil.repeat(" ", position.column - endLinePosition.column);
        ApplicationManager.getApplication().runWriteAction(() -> {
            document.insertString(endLineOffset, toInsert);
            PsiDocumentManager.getInstance(project).commitDocument(document);
        });
    }
    caretModel.moveToLogicalPosition(position);
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) CaretModel(com.intellij.openapi.editor.CaretModel) Document(com.intellij.openapi.editor.Document)

Example 24 with CaretModel

use of com.intellij.openapi.editor.CaretModel in project intellij-plugins by JetBrains.

the class HbTypedHandler method adjustMustacheFormatting.

/**
   * When appropriate, adjusts the formatting for some 'staches, particularily close 'staches
   * and simple inverses ("{{^}}" and "{{else}}")
   */
private static void adjustMustacheFormatting(Project project, int offset, Editor editor, PsiFile file, FileViewProvider provider) {
    if (!HbConfig.isFormattingEnabled()) {
        // formatting disabled; nothing to do
        return;
    }
    PsiElement elementAtCaret = provider.findElementAt(offset - 1, HbLanguage.class);
    PsiElement closeOrSimpleInverseParent = PsiTreeUtil.findFirstParent(elementAtCaret, true, element -> element != null && (element instanceof HbSimpleInverse || element instanceof HbCloseBlockMustache));
    // run the formatter if the user just completed typing a SIMPLE_INVERSE or a CLOSE_BLOCK_STACHE
    if (closeOrSimpleInverseParent != null) {
        // grab the current caret position (AutoIndentLinesHandler is about to mess with it)
        PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
        CaretModel caretModel = editor.getCaretModel();
        CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
        codeStyleManager.adjustLineIndent(file, editor.getDocument().getLineStartOffset(caretModel.getLogicalPosition().line));
    }
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) CaretModel(com.intellij.openapi.editor.CaretModel) PsiElement(com.intellij.psi.PsiElement)

Example 25 with CaretModel

use of com.intellij.openapi.editor.CaretModel in project android by JetBrains.

the class AndroidThemePreviewToolWindowManager method processFileEditorChange.

/**
   * Method called when the current XML editor focus changes to a new one.
   *
   * @param newEditor the new editor, or null if no editor is selected.
   */
private void processFileEditorChange(@Nullable final TextEditor newEditor) {
    if (myToolWindow == null) {
        return;
    }
    if (myActiveEditor == newEditor) {
        return;
    }
    myToolWindowUpdateQueue.cancelAllUpdates();
    if (myActiveEditor != null) {
        myActiveEditor.getEditor().getCaretModel().removeCaretListener(myCaretListener);
        myActiveEditor.getEditor().getDocument().removeDocumentListener(myDocumentListener);
        myActiveEditor = null;
    }
    boolean available = false;
    if (newEditor != null && isApplicableEditor(newEditor)) {
        myActiveEditor = newEditor;
        CaretModel caretModel = myActiveEditor.getEditor().getCaretModel();
        caretModel.addCaretListener(myCaretListener);
        Document document = myActiveEditor.getEditor().getDocument();
        document.addDocumentListener(myDocumentListener);
        PsiFile psiFile = PsiDocumentManager.getInstance(myProject).getPsiFile(document);
        Configuration configuration = getBestConfiguration(psiFile);
        if (configuration != null) {
            if (myThemeEditorContext == null) {
                myThemeEditorContext = new ThemeEditorContext(configuration);
            } else {
                myThemeEditorContext.setConfiguration(configuration);
            }
            // Check if there is a theme at the current offset before enabling the preview
            if (getThemeAtEditorOffset(document, caretModel.getOffset()) != null) {
                available = true;
                updatePreview();
            }
        }
    }
    myToolWindow.setAvailable(available, null);
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) Configuration(com.android.tools.idea.configurations.Configuration) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) RestrictedConfiguration(com.android.tools.idea.editors.theme.qualifiers.RestrictedConfiguration) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document)

Aggregations

CaretModel (com.intellij.openapi.editor.CaretModel)57 Document (com.intellij.openapi.editor.Document)23 PsiElement (com.intellij.psi.PsiElement)14 SelectionModel (com.intellij.openapi.editor.SelectionModel)10 TextRange (com.intellij.openapi.util.TextRange)9 Editor (com.intellij.openapi.editor.Editor)8 Project (com.intellij.openapi.project.Project)7 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)6 Nullable (org.jetbrains.annotations.Nullable)6 EditorEx (com.intellij.openapi.editor.ex.EditorEx)5 IElementType (com.intellij.psi.tree.IElementType)5 LookupElement (com.intellij.codeInsight.lookup.LookupElement)3 EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)3 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)3 PsiFile (com.intellij.psi.PsiFile)3 List (java.util.List)3 InsertionContext (com.intellij.codeInsight.completion.InsertionContext)2 TemplateState (com.intellij.codeInsight.template.impl.TemplateState)2 Caret (com.intellij.openapi.editor.Caret)2 ScrollingModel (com.intellij.openapi.editor.ScrollingModel)2