根据注释续写代码 用字符变量value的值 替换选中的代码 并实现编辑框代码逐个字符自动生成的特效 Override public void actionPerformedNotNull AnActionEvent e
Editor editor = e.getData(CommonDataKeys.EDITOR); if (editor == null) { return; }
Document document = editor.getDocument(); SelectionModel selectionModel = editor.getSelectionModel();
String selectedText = selectionModel.getSelectedText(); if (selectedText == null) { return; }
String value = "new value"; // 替换选中的代码的新值
WriteCommandAction.runWriteCommandAction(editor.getProject(), () -> { int startOffset = selectionModel.getSelectionStart(); int endOffset = selectionModel.getSelectionEnd(); document.replaceString(startOffset, endOffset, value);
editor.getCaretModel().moveToOffset(startOffset + value.length());
// 实现编辑框代码逐个字符自动生成的特效
for (int i = startOffset; i < startOffset + value.length(); i++) {
editor.getCaretModel().moveToOffset(i + 1);
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
});
原文地址: https://www.cveoy.top/t/topic/br5R 著作权归作者所有。请勿转载和采集!