Get selectText 、selectStartPosition、selectEndPosition、fileName from diff window right-click menu
Answered
Hello team
I 'm developing a plugin work with git and add a action to diff window right-click menu by code blew:
<action popup="true" id="action1",class="demo.action.Action1">
<add-to-group group-id="Diff.EditorPopupMenu" anchor="last"/>
</action>
And here is the Action1
code:
public class Action1 extends AnAction{
public Action1 (){
super("add");
}
@Override
public void actionPerformed(@NotNull AnActionEvent e){
//I want to get selectText、selectStartPosition、selectEndPosition、fileName here if user select text from the diff window
}
}
So how to get them?
Please sign in to leave a comment.
Hi,
Try:
to get the editor and its selection model and call its methods.
Thanks,I can get selectText、selectStartPosition、selectEndPosition from the SelectionModel but I cannot find the fileName
Where can I get it?
See https://plugins.jetbrains.com/docs/intellij/psi-files.html#how-do-i-get-a-psi-file or https://plugins.jetbrains.com/docs/intellij/virtual-file.html#how-do-i-get-a-virtual-file.
Thanks,I get all I need from code blew:
Be careful, I omitted all null checks.
I don't understand the
fileName
logic.currentFile.getName()
is enough. Avoid usingproject.getBasePath()
(see its Javadoc).you are right. In fact I make a mistake.
Code:
Paths.get(project.getBasePath()).relativize(Path.get(currentFile.getPath())).toString()
is the way which I use to get file relative path ,not the fileName.