use of com.intellij.openapi.editor.CaretModel in project intellij-community by JetBrains.
the class CurrentLineMarker method attach.
public void attach(EditorSource editorSource) {
if (myEditor != null)
hide();
myEditor = editorSource.getEditor();
if (myEditor == null)
return;
final CaretModel caretModel = myEditor.getCaretModel();
caretModel.addCaretListener(this);
editorSource.addDisposable(new Disposable() {
public void dispose() {
caretModel.removeCaretListener(CurrentLineMarker.this);
}
});
}
use of com.intellij.openapi.editor.CaretModel in project intellij-community by JetBrains.
the class ReplaceOctalEscapeWithUnicodeEscapeIntention method processIntention.
@Override
protected void processIntention(Editor editor, @NotNull PsiElement element) {
final SelectionModel selectionModel = editor.getSelectionModel();
if (selectionModel.hasSelection()) {
// does not check if octal escape is inside char or string literal (garbage in, garbage out)
final Document document = editor.getDocument();
final int start = selectionModel.getSelectionStart();
final int end = selectionModel.getSelectionEnd();
final String text = document.getText(new TextRange(start, end));
final int textLength = end - start;
final StringBuilder replacement = new StringBuilder(textLength);
int anchor = 0;
while (true) {
final int index = indexOfOctalEscape(text, anchor + 1);
if (index < 0) {
break;
}
replacement.append(text.substring(anchor, index));
anchor = appendUnicodeEscape(text, index, replacement);
}
replacement.append(text.substring(anchor, textLength));
document.replaceString(start, end, replacement);
} else if (element instanceof PsiLiteralExpression) {
final PsiLiteralExpression literalExpression = (PsiLiteralExpression) element;
final String text = literalExpression.getText();
final CaretModel model = editor.getCaretModel();
final int offset = model.getOffset() - literalExpression.getTextOffset();
final StringBuilder newLiteralText = new StringBuilder();
final int index1 = indexOfOctalEscape(text, offset);
final int index2 = indexOfOctalEscape(text, offset + 1);
final int escapeStart = index2 == offset ? index2 : index1;
newLiteralText.append(text.substring(0, escapeStart));
final int escapeEnd = appendUnicodeEscape(text, escapeStart, newLiteralText);
newLiteralText.append(text.substring(escapeEnd, text.length()));
PsiReplacementUtil.replaceExpression(literalExpression, newLiteralText.toString());
}
}
use of com.intellij.openapi.editor.CaretModel in project intellij-community by JetBrains.
the class CreateSubclassAction method isAvailable.
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
final CaretModel caretModel = editor.getCaretModel();
final int position = caretModel.getOffset();
PsiElement element = file.findElementAt(position);
PsiClass psiClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
if (psiClass == null || psiClass.isAnnotationType() || psiClass.isEnum() || psiClass instanceof PsiAnonymousClass || psiClass.hasModifierProperty(PsiModifier.FINAL)) {
return false;
}
VirtualFile virtualFile = PsiUtilCore.getVirtualFile(psiClass);
if (virtualFile == null || virtualFile.getFileType() == ScratchFileType.INSTANCE) {
return false;
}
if (!isSupportedLanguage(psiClass))
return false;
final PsiMethod[] constructors = psiClass.getConstructors();
if (constructors.length > 0) {
boolean hasNonPrivateConstructor = false;
for (PsiMethod constructor : constructors) {
if (!constructor.hasModifierProperty(PsiModifier.PRIVATE)) {
hasNonPrivateConstructor = true;
break;
}
}
if (!hasNonPrivateConstructor)
return false;
}
PsiElement lBrace = psiClass.getLBrace();
if (lBrace == null)
return false;
if (element.getTextOffset() >= lBrace.getTextOffset())
return false;
TextRange declarationRange = HighlightNamesUtil.getClassDeclarationTextRange(psiClass);
final TextRange elementTextRange = element.getTextRange();
if (!declarationRange.contains(elementTextRange)) {
if (!(element instanceof PsiWhiteSpace) || (declarationRange.getStartOffset() != elementTextRange.getEndOffset() && declarationRange.getEndOffset() != elementTextRange.getStartOffset())) {
return false;
}
}
myText = getTitle(psiClass);
return true;
}
use of com.intellij.openapi.editor.CaretModel in project intellij-community by JetBrains.
the class ExtractClassHandler 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 CaretModel caretModel = editor.getCaretModel();
final int position = caretModel.getOffset();
final PsiElement element = file.findElementAt(position);
final PsiMember selectedMember = PsiTreeUtil.getParentOfType(element, PsiMember.class, true);
if (selectedMember == null) {
//todo
return;
}
PsiClass containingClass = selectedMember.getContainingClass();
if (containingClass == null && selectedMember instanceof PsiClass) {
containingClass = (PsiClass) selectedMember;
}
final String cannotRefactorMessage = getCannotRefactorMessage(containingClass);
if (cannotRefactorMessage != null) {
CommonRefactoringUtil.showErrorHint(project, editor, RefactorJBundle.message("cannot.perform.the.refactoring") + cannotRefactorMessage, ExtractClassProcessor.REFACTORING_NAME, getHelpID());
return;
}
new ExtractClassDialog(containingClass, selectedMember).show();
}
use of com.intellij.openapi.editor.CaretModel in project intellij-community by JetBrains.
the class CreateLocalVarFromInstanceofAction method invoke.
@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) {
PsiInstanceOfExpression instanceOfExpression = getInstanceOfExpressionAtCaret(editor, file);
assert instanceOfExpression.getContainingFile() == file : instanceOfExpression.getContainingFile() + "; file=" + file;
try {
final PsiStatement statementInside = isNegated(instanceOfExpression) ? null : getExpressionStatementInside(file, editor, instanceOfExpression.getOperand());
PsiDeclarationStatement decl = createLocalVariableDeclaration(instanceOfExpression, statementInside);
if (decl == null)
return;
decl = (PsiDeclarationStatement) CodeStyleManager.getInstance(project).reformat(decl);
decl = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(decl);
PsiLocalVariable localVariable = (PsiLocalVariable) decl.getDeclaredElements()[0];
TemplateBuilderImpl builder = new TemplateBuilderImpl(localVariable);
builder.setEndVariableAfter(localVariable.getNameIdentifier());
Template template = generateTemplate(project, localVariable.getInitializer(), localVariable.getType());
Editor newEditor = CreateFromUsageBaseFix.positionCursor(project, file, localVariable.getNameIdentifier());
if (newEditor == null)
return;
TextRange range = localVariable.getNameIdentifier().getTextRange();
newEditor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
CreateFromUsageBaseFix.startTemplate(newEditor, template, project, new TemplateEditingAdapter() {
@Override
public void templateFinished(Template template, boolean brokenOff) {
ApplicationManager.getApplication().runWriteAction(() -> {
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
CaretModel caretModel = editor.getCaretModel();
PsiElement elementAt = file.findElementAt(caretModel.getOffset());
PsiDeclarationStatement declarationStatement = PsiTreeUtil.getParentOfType(elementAt, PsiDeclarationStatement.class);
if (declarationStatement != null) {
caretModel.moveToOffset(declarationStatement.getTextRange().getEndOffset());
}
new EnterAction().actionPerformed(editor, DataManager.getInstance().getDataContext());
});
}
});
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
Aggregations