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