use of com.intellij.openapi.editor.CaretModel in project Perl5-IDEA by Camelcade.
the class PerlEnterHeredocClosingHandler method postProcessEnter.
@Override
public Result postProcessEnter(@NotNull PsiFile file, @NotNull Editor editor, @NotNull DataContext dataContext) {
if (!file.getLanguage().is(PerlLanguage.INSTANCE)) {
return Result.Continue;
}
final CaretModel caretModel = editor.getCaretModel();
int offset = caretModel.getOffset();
PsiElement currentElement = file.findElementAt(offset);
if (currentElement != null && (!(currentElement.getParent() instanceof PerlHeredocElementImpl) || !Perl5CodeInsightSettings.getInstance().HEREDOC_AUTO_INSERTION)) {
return Result.Continue;
}
LogicalPosition currentPosition = caretModel.getLogicalPosition();
final int enterLine = currentPosition.line - 1;
final int currentOffset = caretModel.getOffset();
if (enterLine <= -1 || currentOffset <= 0) {
return Result.Continue;
}
final Document document = editor.getDocument();
final PsiDocumentManager manager = PsiDocumentManager.getInstance(file.getProject());
manager.commitDocument(document);
final int lineStartOffset = document.getLineStartOffset(enterLine);
PsiElement firstLineElement = file.findElementAt(lineStartOffset);
if (firstLineElement == null) {
return Result.Continue;
}
HeredocCollector collector = new HeredocCollector(currentOffset - 1);
PerlPsiUtil.iteratePsiElementsRight(firstLineElement, collector);
SmartPsiElementPointer<PerlHeredocOpener> lastOpenerPointer = null;
for (SmartPsiElementPointer<PerlHeredocOpener> currentOpenerPointer : collector.getResult()) {
PerlHeredocOpener currentOpener = currentOpenerPointer.getElement();
if (currentOpener == null) {
// System.err.println("Opener invalidated on reparse");
return Result.Continue;
}
String openerName = currentOpener.getName();
boolean emptyOpener = StringUtil.isEmpty(openerName);
PsiReference inboundReference = ReferencesSearch.search(currentOpener).findFirst();
if (inboundReference != null) {
boolean falseAlarm = false;
PsiElement run = inboundReference.getElement().getPrevSibling();
while (run instanceof PsiWhiteSpace) {
run = run.getPrevSibling();
}
if (run instanceof PerlHeredocElementImpl) {
Pattern openerPattern = EMPTY_OPENER_PATTERN;
if (!emptyOpener) {
openerPattern = Pattern.compile("<<~?(\\s*)(?:" + "\"" + openerName + "\"" + "|" + "`" + openerName + "`" + "|" + "'" + openerName + "'" + "|" + "\\\\" + openerName + "|" + openerName + ")");
}
falseAlarm = openerPattern.matcher(run.getNode().getChars()).find();
}
if (// looks like overlapping heredocs
falseAlarm) {
inboundReference = null;
} else {
lastOpenerPointer = currentOpenerPointer;
}
}
if (// disclosed marker
inboundReference == null) {
int addOffset;
String closeMarker = "\n" + openerName + "\n";
if (// first one
lastOpenerPointer == null) {
addOffset = currentOffset;
} else // sequentional
{
PerlHeredocOpener lastOpener = lastOpenerPointer.getElement();
if (lastOpener == null) {
return Result.Continue;
}
PsiReference lastOpenerReference = ReferencesSearch.search(lastOpener).findFirst();
if (lastOpenerReference != null) {
PsiElement element = lastOpenerReference.getElement();
addOffset = element.getTextRange().getEndOffset();
if (!emptyOpener) {
closeMarker = "\n" + closeMarker;
}
} else {
return Result.Continue;
}
}
document.insertString(addOffset, closeMarker);
manager.commitDocument(document);
CodeStyleManager.getInstance(file.getProject()).reformatRange(file, addOffset, addOffset + closeMarker.length());
currentOpener = currentOpenerPointer.getElement();
if (currentOpener == null) {
return Result.Continue;
}
inboundReference = ReferencesSearch.search(currentOpener).findFirst();
if (inboundReference != null) {
lastOpenerPointer = currentOpenerPointer;
} else {
return Result.Continue;
}
}
}
return Result.Continue;
}
use of com.intellij.openapi.editor.CaretModel in project Perl5-IDEA by Camelcade.
the class PerlTypedHandler method beforeCharTyped.
@NotNull
@Override
public Result beforeCharTyped(char c, @NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file, @NotNull FileType fileType) {
CaretModel caretModel = editor.getCaretModel();
int currentOffset = caretModel.getOffset();
Document document = editor.getDocument();
CharSequence documentSequence = document.getCharsSequence();
if (currentOffset > documentSequence.length()) {
return Result.CONTINUE;
}
EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
HighlighterIterator iterator = highlighter.createIterator(currentOffset);
IElementType nextTokenType = iterator.atEnd() ? null : iterator.getTokenType();
char nextChar = currentOffset == documentSequence.length() ? 0 : documentSequence.charAt(currentOffset);
if (c == '<' && nextTokenType == QUOTE_DOUBLE_CLOSE && nextChar == '>' && currentOffset > 0 && documentSequence.charAt(currentOffset - 1) == '<' && (currentOffset < 3 || PerlEditorUtil.getPreviousTokenType(highlighter.createIterator(currentOffset - 2)) != RESERVED_QQ)) {
document.replaceString(currentOffset, currentOffset + 1, "<");
caretModel.moveToOffset(currentOffset + 1);
return Result.STOP;
}
if (QUOTE_CLOSE_FIRST_ANY.contains(nextTokenType) && c == nextChar) {
caretModel.moveToOffset(currentOffset + 1);
return Result.STOP;
}
if (c == ':' && nextTokenType == PACKAGE && Perl5CodeInsightSettings.getInstance().AUTO_INSERT_COLON) {
document.insertString(currentOffset, "::");
caretModel.moveToOffset(currentOffset + 2);
AutoPopupController.getInstance(project).scheduleAutoPopup(editor);
return Result.STOP;
}
if (c == ' ') {
Result result = tryToAddFatComma(editor, file, currentOffset);
if (result != null) {
return result;
}
}
return Result.CONTINUE;
}
use of com.intellij.openapi.editor.CaretModel in project Perl5-IDEA by Camelcade.
the class PodTypedHandler method beforeCharTyped.
@Override
public Result beforeCharTyped(char c, Project project, Editor editor, PsiFile file, FileType fileType) {
if (file instanceof PodFile) {
if (c == '<') {
CaretModel caretModel = editor.getCaretModel();
int offset = caretModel.getOffset() - 1;
PsiElement element = file.findElementAt(offset);
IElementType elementType = element == null ? null : element.getNode().getElementType();
String text = element == null ? null : element.getText();
if (elementType == POD_IDENTIFIER && text != null) {
if (text.length() == 1 && StringUtil.containsChar(POD_COMMANDS, text.charAt(0))) {
EditorModificationUtil.insertStringAtCaret(editor, ">", false, false, 0);
}
} else if (elementType == POD_ANGLE_LEFT || POD_COMMANDS_TOKENSET.contains(elementType)) {
// noinspection ConstantConditions
extendAngles(element.getParent());
caretModel.moveToOffset(offset + 2);
return Result.STOP;
}
} else if (// '>'
c == '>') {
CaretModel caretModel = editor.getCaretModel();
int offset = caretModel.getOffset();
PsiElement element = file.findElementAt(offset);
IElementType elementType = element == null ? null : element.getNode().getElementType();
int extraOffset = 0;
if (elementType != POD_ANGLE_RIGHT) {
offset--;
element = file.findElementAt(offset);
elementType = element == null ? null : element.getNode().getElementType();
extraOffset++;
}
if (elementType == POD_ANGLE_RIGHT) {
// noinspection ConstantConditions
caretModel.moveToOffset(offset + extendAngles(element.getParent()) + extraOffset);
return Result.STOP;
}
}
}
return super.beforeCharTyped(c, project, editor, file, fileType);
}
use of com.intellij.openapi.editor.CaretModel in project android by JetBrains.
the class CreateTypedResourceFileAction method doCreateAndNavigate.
PsiElement[] doCreateAndNavigate(String newName, PsiDirectory directory, String rootTagName, boolean chooseTagName, boolean navigate) throws Exception {
final XmlFile file = AndroidResourceUtil.createFileResource(newName, directory, rootTagName, myResourceType.getName(), myValuesResourceFile);
if (navigate) {
doNavigate(file);
}
if (chooseTagName) {
XmlDocument document = file.getDocument();
if (document != null) {
XmlTag rootTag = document.getRootTag();
if (rootTag != null) {
final Project project = file.getProject();
final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
if (editor != null) {
CaretModel caretModel = editor.getCaretModel();
caretModel.moveToOffset(rootTag.getTextOffset() + 1);
XmlTagInplaceRenamer.rename(editor, rootTag);
}
}
}
}
return new PsiElement[] { file };
}
use of com.intellij.openapi.editor.CaretModel in project intellij-plugins by JetBrains.
the class DartIntroduceHandler method performAction.
public void performAction(DartIntroduceOperation operation) {
final PsiFile file = operation.getFile();
if (!CommonRefactoringUtil.checkReadOnlyStatus(file)) {
return;
}
final Editor editor = operation.getEditor();
if (editor.getSettings().isVariableInplaceRenameEnabled()) {
final TemplateState templateState = TemplateManagerImpl.getTemplateState(operation.getEditor());
if (templateState != null && !templateState.isFinished()) {
return;
}
}
PsiElement element1 = null;
PsiElement element2 = null;
final SelectionModel selectionModel = editor.getSelectionModel();
if (selectionModel.hasSelection()) {
element1 = file.findElementAt(selectionModel.getSelectionStart());
element2 = file.findElementAt(selectionModel.getSelectionEnd() - 1);
if (element1 instanceof PsiWhiteSpace) {
int startOffset = element1.getTextRange().getEndOffset();
element1 = file.findElementAt(startOffset);
}
if (element2 instanceof PsiWhiteSpace) {
int endOffset = element2.getTextRange().getStartOffset();
element2 = file.findElementAt(endOffset - 1);
}
} else {
if (smartIntroduce(operation)) {
return;
}
final CaretModel caretModel = editor.getCaretModel();
final Document document = editor.getDocument();
int lineNumber = document.getLineNumber(caretModel.getOffset());
if ((lineNumber >= 0) && (lineNumber < document.getLineCount())) {
element1 = file.findElementAt(document.getLineStartOffset(lineNumber));
element2 = file.findElementAt(document.getLineEndOffset(lineNumber) - 1);
}
}
final Project project = operation.getProject();
if (element1 == null || element2 == null) {
showCannotPerformError(project, editor);
return;
}
element1 = DartRefactoringUtil.getSelectedExpression(project, file, element1, element2);
if (element1 == null) {
showCannotPerformError(project, editor);
return;
}
if (!checkIntroduceContext(file, editor, element1)) {
return;
}
operation.setElement(element1);
performActionOnElement(operation);
}
Aggregations