我有这段代码
private static void highlightElement(int lineNum,Project project,VirtualFile virtualFile) {
final FileEditorManager editorManager =
FileEditorManager.getInstance(project);
final Editor editor = editorManager.getSelectedTextEditor();
final TextAttributes textattributes = new TextAttributes(null, new Color(239, 43, 18), null, EffectType.LINE_UNDERSCORE, Font.PLAIN);
editor.getMarkupModel().addLineHighlighter(lineNum, HighlighterLayer.CARET_ROW, textattributes);
editor.getMarkupModel().
final WindowManager windowManager = WindowManager.getInstance();
final StatusBar statusBar = windowManager.getStatusBar(project);
statusBar.setInfo("Press Esc to remove highlighting");
}
代码按预期工作但我想要的是附加一个事件监听器,所以当我按下esc函数editor.getMarkupModel()。removeAllHighlighters();将被召唤。提前谢谢你!
答案 0 :(得分:0)
不要通过MarkupModel.getLineHighlighter()
添加荧光笔,而是使用HighlightManager.addRangeHighlight()
。然后当用户按下Esc时它们将被自动删除;你不需要做任何额外的事情。