To get selected text in a Intellij Platform Plugin project, we can use following code
1 2 3 4 5 6 |
public void actionPerformed(AnActionEvent e) { Editor editor = e.getData(PlatformDataKeys.EDITOR); String selectedText = editor.getSelectionModel().getSelectedText(); selectedText = selectedText.replace("、", ",").replace(",", ","); selectedText.split("[,\\s]"); } |
The first line is used to get current editor object
1 |
Editor editor = e.getData(PlatformDataKeys.EDITOR); |
Then call getSelectionModel and getSelectedText method to get selected text
1 |
String selectedText = editor.getSelectionModel().getSelectedText(); |
The last two lines is to to some operation for selected text, it's unrelated to our goal.