use of com.intellij.openapi.editor.CaretModel in project intellij-plugins by JetBrains.
the class FlexLiveTemplatesTest method testPublicFunctionWithTypingInMxml.
public void testPublicFunctionWithTypingInMxml() throws Exception {
doTest("pf", "mxml", segmentIndex -> {
final CaretModel caretModel = getEditor().getCaretModel();
switch(segmentIndex) {
case 0:
type("functionName");
caretModel.moveToOffset(caretModel.getOffset() + 1);
break;
case 1:
type("param:Object");
caretModel.moveToOffset(caretModel.getOffset() + 1);
break;
case 2:
type("String");
caretModel.moveToOffset(caretModel.getOffset() + 1);
break;
}
});
}
use of com.intellij.openapi.editor.CaretModel in project intellij-community by JetBrains.
the class UnicodeUnescapeIntention method processIntention.
@Override
protected void processIntention(Editor editor, @NotNull PsiElement element) {
final SelectionModel selectionModel = editor.getSelectionModel();
if (selectionModel.hasSelection()) {
// does not check if Unicode 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 = indexOfUnicodeEscape(text, anchor + 1);
if (index < 0) {
break;
}
replacement.append(text.substring(anchor, index));
int hexStart = index + 1;
while (text.charAt(hexStart) == 'u') {
hexStart++;
}
anchor = hexStart + 4;
final int c = Integer.parseInt(text.substring(hexStart, anchor), 16);
replacement.appendCodePoint(c);
}
replacement.append(text.substring(anchor, textLength));
document.replaceString(start, end, replacement);
} else {
final CaretModel caretModel = editor.getCaretModel();
final Document document = editor.getDocument();
final int lineNumber = document.getLineNumber(caretModel.getOffset());
final int lineStartOffset = document.getLineStartOffset(lineNumber);
final String line = document.getText(new TextRange(lineStartOffset, document.getLineEndOffset(lineNumber)));
final int column = caretModel.getLogicalPosition().column;
final int index1 = indexOfUnicodeEscape(line, column);
final int index2 = indexOfUnicodeEscape(line, column + 1);
// if the caret is between two unicode escapes, replace the one to the right
final int escapeStart = index2 == column ? index2 : index1;
int hexStart = escapeStart + 1;
while (line.charAt(hexStart) == 'u') {
hexStart++;
}
final char c = (char) Integer.parseInt(line.substring(hexStart, hexStart + 4), 16);
if (Character.isHighSurrogate(c)) {
hexStart += 4;
if (line.charAt(hexStart++) == '\\' && line.charAt(hexStart++) == 'u') {
while (line.charAt(hexStart) == 'u') hexStart++;
final char d = (char) Integer.parseInt(line.substring(hexStart, hexStart + 4), 16);
document.replaceString(lineStartOffset + escapeStart, lineStartOffset + hexStart + 4, String.valueOf(new char[] { c, d }));
return;
}
} else if (Character.isLowSurrogate(c)) {
if (escapeStart >= 6 && StringUtil.isHexDigit(line.charAt(escapeStart - 1)) && StringUtil.isHexDigit(line.charAt(escapeStart - 2)) && StringUtil.isHexDigit(line.charAt(escapeStart - 3)) && StringUtil.isHexDigit(line.charAt(escapeStart - 4))) {
int i = escapeStart - 5;
while (i > 0 && line.charAt(i) == 'u') i--;
if (line.charAt(i) == '\\' && (i == 0 || line.charAt(i - 1) != '\\')) {
final char b = (char) Integer.parseInt(line.substring(escapeStart - 4, escapeStart), 16);
document.replaceString(lineStartOffset + i, lineStartOffset + hexStart + 4, String.valueOf(new char[] { b, c }));
return;
}
}
}
document.replaceString(lineStartOffset + escapeStart, lineStartOffset + hexStart + 4, String.valueOf(c));
}
}
use of com.intellij.openapi.editor.CaretModel in project intellij-community by JetBrains.
the class FlipExpressionIntention method processIntention.
@Override
protected void processIntention(Editor editor, @NotNull PsiElement element) {
final CaretModel caretModel = editor.getCaretModel();
final int offset = caretModel.getOffset();
super.processIntention(editor, element);
caretModel.moveToOffset(offset);
}
use of com.intellij.openapi.editor.CaretModel in project intellij-community by JetBrains.
the class FoldingProcessingOnDocumentModificationTest method testUnexpectedClassLevelJavadocExpandingOnClassSignatureChange.
public void testUnexpectedClassLevelJavadocExpandingOnClassSignatureChange() throws IOException {
// Inspired by IDEA-61275
String text = "/**\n" + " * This is a test comment\n" + " */\n" + "public <caret>class Test {\n" + "}";
init(text, TestFileType.JAVA);
CaretModel caretModel = myEditor.getCaretModel();
int caretOffset = caretModel.getOffset();
assertEquals(caretOffset, caretModel.getOffset());
updateFoldRegions();
toggleFoldRegionState(getFoldRegion(0), false);
type('a');
updateFoldRegions();
assertEquals(caretOffset + 1, caretModel.getOffset());
assertEquals(1, myEditor.getFoldingModel().getAllFoldRegions().length);
FoldRegion foldRegion = getFoldRegion(0);
assertFalse(foldRegion.isExpanded());
}
use of com.intellij.openapi.editor.CaretModel in project intellij-community by JetBrains.
the class SuppressIntentionAction method getElement.
@Nullable
private static PsiElement getElement(@NotNull Editor editor, @NotNull PsiFile file) {
CaretModel caretModel = editor.getCaretModel();
int position = caretModel.getOffset();
return file.findElementAt(position);
}
Aggregations