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));
}
}
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);
}
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);
}
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());
}
}
}
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;
}
Aggregations