use of com.intellij.openapi.editor.LogicalPosition in project intellij-community by JetBrains.
the class PyEditingTest method testUncommentWithSpace.
public void testUncommentWithSpace() throws Exception {
// PY-980
myFixture.configureByFile("/editing/uncommentWithSpace.before.py");
myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(0, 1));
PlatformTestUtil.invokeNamedAction(IdeActions.ACTION_COMMENT_LINE);
myFixture.checkResultByFile("/editing/uncommentWithSpace.after.py", true);
}
use of com.intellij.openapi.editor.LogicalPosition in project intellij-community by JetBrains.
the class AddEncodingQuickFix method applyFix.
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
final PsiFile file = element.getContainingFile();
if (file == null)
return;
PsiElement firstLine = file.getFirstChild();
if (firstLine instanceof PsiComment && firstLine.getText().startsWith("#!")) {
firstLine = firstLine.getNextSibling();
}
final LanguageLevel languageLevel = LanguageLevel.forElement(file);
final String commentText = String.format(PyEncodingUtil.ENCODING_FORMAT_PATTERN[myEncodingFormatIndex], myDefaultEncoding);
final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
PsiComment encodingComment = elementGenerator.createFromText(languageLevel, PsiComment.class, commentText);
encodingComment = (PsiComment) file.addBefore(encodingComment, firstLine);
final FileEditor fileEditor = FileEditorManager.getInstance(project).getSelectedEditor(element.getContainingFile().getVirtualFile());
if (fileEditor instanceof TextEditor) {
if (encodingComment.getNextSibling() == null || !encodingComment.getNextSibling().textContains('\n')) {
file.addAfter(elementGenerator.createFromText(languageLevel, PsiWhiteSpace.class, "\n"), encodingComment);
}
final Editor editor = ((TextEditor) fileEditor).getEditor();
final Document document = editor.getDocument();
final int insertedLineNumber = document.getLineNumber(encodingComment.getTextOffset());
editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(insertedLineNumber + 1, 0));
}
}
use of com.intellij.openapi.editor.LogicalPosition in project ideavim by JetBrains.
the class MarkGroup method updateMarkFromInsert.
/**
* This updates all the marks for a file whenever text is inserted into the file. If the line that contains a mark
* that is after the start of the insertion point, shift the mark by the number of new lines added.
*
* @param editor The editor that was updated
* @param marks The editor's marks
* @param insStartOff The insertion point
* @param insLength The length of the insertion
*/
public static void updateMarkFromInsert(@Nullable Editor editor, @Nullable HashMap<Character, Mark> marks, int insStartOff, int insLength) {
if (marks != null && marks.size() > 0 && editor != null) {
int insEndOff = insStartOff + insLength;
LogicalPosition insStart = editor.offsetToLogicalPosition(insStartOff);
LogicalPosition insEnd = editor.offsetToLogicalPosition(insEndOff);
if (logger.isDebugEnabled())
logger.debug("mark insert. insStart = " + insStart + ", insEnd = " + insEnd);
int lines = insEnd.line - insStart.line;
if (lines == 0)
return;
for (Mark mark : marks.values()) {
if (logger.isDebugEnabled())
logger.debug("mark = " + mark);
// Shift the mark if the insertion began on a line prior to the marked line.
if (insStart.line < mark.getLogicalLine()) {
mark.setLogicalLine(mark.getLogicalLine() + lines);
if (logger.isDebugEnabled())
logger.debug("Shifting mark by " + lines + " lines");
}
}
}
}
use of com.intellij.openapi.editor.LogicalPosition in project ideavim by JetBrains.
the class MarkGroup method updateMarkFromDelete.
/**
* This updates all the marks for a file whenever text is deleted from the file. If the line that contains a mark
* is completely deleted then the mark is deleted too. If the deleted text is before the marked line, the mark is
* moved up by the number of deleted lines.
*
* @param editor The modified editor
* @param marks The editor's marks to update
* @param delStartOff The offset within the editor where the deletion occurred
* @param delLength The length of the deleted text
*/
public static void updateMarkFromDelete(@Nullable Editor editor, @Nullable HashMap<Character, Mark> marks, int delStartOff, int delLength) {
// Skip all this work if there are no marks
if (marks != null && marks.size() > 0 && editor != null) {
// Calculate the logical position of the start and end of the deleted text
int delEndOff = delStartOff + delLength - 1;
LogicalPosition delStart = editor.offsetToLogicalPosition(delStartOff);
LogicalPosition delEnd = editor.offsetToLogicalPosition(delEndOff + 1);
if (logger.isDebugEnabled())
logger.debug("mark delete. delStart = " + delStart + ", delEnd = " + delEnd);
// Now analyze each mark to determine if it needs to be updated or removed
for (Character ch : marks.keySet()) {
Mark mark = marks.get(ch);
if (logger.isDebugEnabled())
logger.debug("mark = " + mark);
// proper number of lines.
if (delEnd.line < mark.getLogicalLine()) {
int lines = delEnd.line - delStart.line;
if (logger.isDebugEnabled())
logger.debug("Shifting mark by " + lines + " lines");
mark.setLogicalLine(mark.getLogicalLine() - lines);
} else // If the deleted text begins before the mark and ends after the mark then it may be shifted or deleted
if (delStart.line <= mark.getLogicalLine() && delEnd.line >= mark.getLogicalLine()) {
int markLineStartOff = EditorHelper.getLineStartOffset(editor, mark.getLogicalLine());
int markLineEndOff = EditorHelper.getLineEndOffset(editor, mark.getLogicalLine(), true);
Command command = CommandState.getInstance(editor).getCommand();
// If text is being changed from the start of the mark line (a special case for mark deletion)
boolean changeFromMarkLineStart = command != null && command.getType() == Command.Type.CHANGE && delStartOff == markLineStartOff;
// If the marked line is completely within the deleted text, remove the mark (except the special case)
if (delStartOff <= markLineStartOff && delEndOff >= markLineEndOff && !changeFromMarkLineStart) {
VimPlugin.getMark().removeMark(ch, mark);
logger.debug("Removed mark");
} else // on a line prior to the marked line (which means the deletion must end on the marked line).
if (delStart.line < mark.getLogicalLine()) {
// shift mark
mark.setLogicalLine(delStart.line);
if (logger.isDebugEnabled())
logger.debug("Shifting mark to line " + delStart.line);
}
}
}
}
}
use of com.intellij.openapi.editor.LogicalPosition in project ideavim by JetBrains.
the class MarkGroup method addJump.
private void addJump(@NotNull Editor editor, int offset, boolean reset) {
final VirtualFile vf = EditorData.getVirtualFile(editor);
if (vf == null) {
return;
}
LogicalPosition lp = editor.offsetToLogicalPosition(offset);
Jump jump = new Jump(lp.line, lp.column, vf.getPath());
final String filename = jump.getFilename();
for (int i = 0; i < jumps.size(); i++) {
Jump j = jumps.get(i);
if (filename != null && filename.equals(j.getFilename()) && j.getLogicalLine() == jump.getLogicalLine()) {
jumps.remove(i);
break;
}
}
jumps.add(jump);
if (reset) {
jumpSpot = -1;
} else {
jumpSpot++;
}
if (jumps.size() > SAVE_JUMP_COUNT) {
jumps.remove(0);
}
}
Aggregations