Search in sources :

Example 51 with CaretModel

use of com.intellij.openapi.editor.CaretModel in project idea-handlebars by dmarcotte.

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, new Condition<PsiElement>() {

        @Override
        public boolean value(PsiElement element) {
            return 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 52 with CaretModel

use of com.intellij.openapi.editor.CaretModel in project Intellij-Plugin by getgauge.

the class CustomRenameHandlerTest method testShouldRenameWhenPsiFileIsNotPresentInDataContext.

@Test
public void testShouldRenameWhenPsiFileIsNotPresentInDataContext() throws Exception {
    CaretModel caretModel = mock(CaretModel.class);
    when(dataContext.getData(CommonDataKeys.PSI_ELEMENT.getName())).thenReturn(null);
    when(dataContext.getData(CommonDataKeys.EDITOR.getName())).thenReturn(editor);
    when(editor.getCaretModel()).thenReturn(caretModel);
    when(caretModel.getOffset()).thenReturn(0);
    when(dataContext.getData(CommonDataKeys.PSI_FILE.getName())).thenReturn(null);
    boolean isAvailable = new CustomRenameHandler().isAvailableOnDataContext(dataContext);
    assertFalse("Should rename when psi file is not present. Expected: false, Actual: true", isAvailable);
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) Test(org.junit.Test)

Example 53 with CaretModel

use of com.intellij.openapi.editor.CaretModel in project Intellij-Plugin by getgauge.

the class CustomRenameHandlerTest method testShouldRenameWhenElementIsNotPresentInDataContext.

@Test
public void testShouldRenameWhenElementIsNotPresentInDataContext() throws Exception {
    CaretModel caretModel = mock(CaretModel.class);
    PsiFile file = mock(PsiFile.class);
    when(dataContext.getData(CommonDataKeys.PSI_ELEMENT.getName())).thenReturn(null);
    when(dataContext.getData(CommonDataKeys.EDITOR.getName())).thenReturn(editor);
    when(editor.getCaretModel()).thenReturn(caretModel);
    when(caretModel.getOffset()).thenReturn(0);
    when(dataContext.getData(CommonDataKeys.PSI_FILE.getName())).thenReturn(file);
    when(file.findElementAt(0)).thenReturn(mock(SpecStepImpl.class));
    boolean isAvailable = new CustomRenameHandler().isAvailableOnDataContext(dataContext);
    assertTrue("Should rename when element is not present in DataContext. Expected: true, Actual: false", isAvailable);
    when(file.findElementAt(0)).thenReturn(mock(ConceptStepImpl.class));
    isAvailable = new CustomRenameHandler().isAvailableOnDataContext(dataContext);
    assertTrue("Should rename when element is not present in DataContext. Expected: true, Actual: false", isAvailable);
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) ConceptStepImpl(com.thoughtworks.gauge.language.psi.impl.ConceptStepImpl) SpecStepImpl(com.thoughtworks.gauge.language.psi.impl.SpecStepImpl) PsiFile(com.intellij.psi.PsiFile) Test(org.junit.Test)

Example 54 with CaretModel

use of com.intellij.openapi.editor.CaretModel in project Perl5-IDEA by Camelcade.

the class PerlBackspaceHandler method beforeCharDeleted.

@Override
public void beforeCharDeleted(char c, PsiFile file, Editor editor) {
    CaretModel caretModel = editor.getCaretModel();
    int currentOffset = caretModel.getOffset() - 1;
    if (currentOffset < 0) {
        return;
    }
    EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
    HighlighterIterator iterator = highlighter.createIterator(currentOffset);
    IElementType tokenToDelete = iterator.atEnd() ? null : iterator.getTokenType();
    if (QUOTE_OPEN_ANY.contains(tokenToDelete)) {
        PerlEditorUtil.moveToNextMeaningfulToken(iterator);
        if (iterator.atEnd()) {
            return;
        }
        IElementType nextTokenType = iterator.getTokenType();
        if (QUOTE_CLOSE_PAIRED.contains(nextTokenType)) {
            int startOffsetToDelete = currentOffset + 1;
            if (currentOffset > 0 && QUOTE_MIDDLE.contains(tokenToDelete)) {
                HighlighterIterator preQuoteIterator = PerlEditorUtil.moveToPreviousMeaningfulToken(highlighter.createIterator(currentOffset - 1));
                if (!preQuoteIterator.atEnd() && QUOTE_OPEN_ANY.contains(preQuoteIterator.getTokenType())) {
                    startOffsetToDelete = preQuoteIterator.getEnd();
                    caretModel.moveToOffset(preQuoteIterator.getEnd());
                }
            }
            editor.getDocument().deleteString(startOffsetToDelete, iterator.getEnd());
        }
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) CaretModel(com.intellij.openapi.editor.CaretModel) EditorEx(com.intellij.openapi.editor.ex.EditorEx) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 55 with CaretModel

use of com.intellij.openapi.editor.CaretModel in project Perl5-IDEA by Camelcade.

the class PodBackspaceHandler method charDeleted.

@Override
public boolean charDeleted(char c, PsiFile file, Editor editor) {
    if (file instanceof PodFile) {
        CaretModel caretModel = editor.getCaretModel();
        int offset = caretModel.getOffset();
        PsiElement element = file.findElementAt(offset);
        IElementType elementType = element == null ? null : element.getNode().getElementType();
        if (elementType == POD_ANGLE_RIGHT) {
            @SuppressWarnings("ConstantConditions") PsiElement formatterBlock = element.getParent();
            if (formatterBlock != null) {
                PsiElement openAngles = formatterBlock.getFirstChild();
                openAngles = openAngles == null ? null : openAngles.getNextSibling();
                if (openAngles != null) {
                    int openAnglesOffset = openAngles.getTextRange().getStartOffset();
                    editor.getDocument().deleteString(openAnglesOffset, openAnglesOffset + 1);
                    return true;
                }
            }
        } else if (elementType == POD_ANGLE_LEFT) {
            @SuppressWarnings("ConstantConditions") PsiElement formatterBlock = element.getParent();
            if (formatterBlock != null) {
                PsiElement closeAngles = formatterBlock.getLastChild();
                if (closeAngles != null) {
                    int closeAnglesOffset = closeAngles.getTextRange().getStartOffset();
                    editor.getDocument().deleteString(closeAnglesOffset - 1, closeAnglesOffset);
                    return true;
                }
            }
            return true;
        }
    }
    return false;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) CaretModel(com.intellij.openapi.editor.CaretModel) PodFile(com.perl5.lang.pod.parser.psi.PodFile) PsiElement(com.intellij.psi.PsiElement)

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