@Override public Result postProcessEnter(@NotNull PsiFile file, @NotNull Editor editor, @NotNull DataContext dataContext) { if (!file.getLanguage().is(BallerinaLanguage.INSTANCE)) { return Result.Continue; } // We need to save the file before checking. Otherwise issues can occur when we press enter in a string. Project project = file.getProject(); PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); // Checks whether the previous line starts with "#". LogicalPosition caretPos = editor.getCaretModel().getLogicalPosition(); int prevLine = caretPos.line - 1; String lineString = editor.getDocument().getText( new TextRange(editor.getDocument().getLineStartOffset(prevLine), editor.getDocument().getLineEndOffset(prevLine))); if (lineString.trim().startsWith("#")) { int newCol = lineString.indexOf("#"); String enteredText = editor.getDocument().getText( new TextRange(editor.getDocument().getLineStartOffset(caretPos.line), editor.getDocument().getLineEndOffset(caretPos.line))).trim(); editor.getDocument().deleteString(editor.getDocument().getLineStartOffset(caretPos.line), editor.getDocument().getLineEndOffset(caretPos.line)); editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(caretPos.line, 1)); enterNewLine(editor, enteredText, newCol); // Commit the document. PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); } return Result.Continue; }
break; case VISUAL_LINE: caretModel.moveToLogicalPosition(editor.xyToLogicalPosition(event.getPoint())); break;
/** * Scroll to the error specified by the given tree path, or do nothing * if no error is specified. * * @param treePath the tree path to scroll to. */ private void scrollToError(final TreePath treePath) { final DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) treePath.getLastPathComponent(); if (treeNode == null || !(treeNode.getUserObject() instanceof ResultTreeNode)) { return; } final ResultTreeNode nodeInfo = (ResultTreeNode) treeNode.getUserObject(); if (nodeInfo.getFile() == null || nodeInfo.getProblem() == null) { return; // no problem here :-) } final VirtualFile virtualFile = nodeInfo.getFile().getVirtualFile(); if (virtualFile == null || !virtualFile.exists()) { return; } final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project); final FileEditor[] editor = fileEditorManager.openFile( virtualFile, true); if (editor.length > 0 && editor[0] instanceof TextEditor) { final LogicalPosition problemPos = new LogicalPosition( Math.max(lineFor(nodeInfo) - 1, 0), Math.max(columnFor(nodeInfo), 0)); final Editor textEditor = ((TextEditor) editor[0]).getEditor(); textEditor.getCaretModel().moveToLogicalPosition(problemPos); textEditor.getScrollingModel().scrollToCaret(ScrollType.CENTER); } }
public void testEnterTyping() { initWithPerlTidy(); myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(65, 0)); final int iterations = 30; for (int i = 0; i < iterations; i++) { myFixture.type("\n"); } final int time = 1000; PlatformTestUtil.startPerformanceTest("PerlTidy enter typing", iterations * time, () -> { long start = System.currentTimeMillis(); for (int i = 0; i < iterations; i++) { myFixture.type("\n"); } long length = System.currentTimeMillis() - start; System.err.println("Typing enter done in " + length / iterations + " ms per iteration of " + time); }).assertTiming(); }
LogicalPosition newStatementPos = new LogicalPosition(pojo.getLineNumber() , pojo.getLineStartPos() + 1); LogicalPosition insertPos = new LogicalPosition(pojo.getLineNumber() + 1 , 0 ); caretModel.moveToLogicalPosition(newStatementPos); PsiElement currentFileElement = event.getData(LangDataKeys.PSI_ELEMENT); if (currentFileElement instanceof PsiClassImpl || currentFileElement instanceof ClsClassImpl) { caretModel.moveToLogicalPosition(insertPos); Integer offset = caretModel.getOffset(); String insert = insertSetter(project, pojo, document, currentFileElement, offset); caretModel.moveToLogicalPosition(oldLogicPos); SendToServerService.post(project,request); } catch (Throwable ignored) {