Action perform on specific file type

已回答

I created an action on CutCopyPasteGroup but I want to perform that action only on json files. Is it possible?

0

Yes:

final FileType type = file.getFileType();
final boolean isJson = type instanceof LanguageFileType && ((LanguageFileType)type).getLanguage().isKindOf(JsonLanguage.INSTANCE);
0

I can detect file's type in action, i want to create an action that only triggered on right clicks on json files. User will not able to see the plugin if performs right click on the e.g. a java file.

something like this:

<action id="id" class="action.myClass" text="Plugin Text"
description="Plugin Description" fileType="JSON">
<add-to-group group-id="CutCopyPasteGroup" anchor="last"/>
</action>
0

The question you are really asking, then is "How can I include/hide an action on the context menu depending upon the editor's file type?"  Not whether you can only perform the action on a particular file type.

So, this should work:

import com.intellij.json.JsonLanguage;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.psi.PsiFile;

class MyAction extends AnAction {}
@Override
public void update(AnActionEvent e) {
if (e.isFromContextMenu()) {
PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
this.getTemplatePresentation().setEnabledAndVisible(psiFile.getLanguage().isKindOf(JsonLanguage.INSTANCE));
}
}
}

 

1

Actually i'm asking a real filtering. Your last code worked fine in UI but when right clicking on file, action triggered and then hiding. I'm asking is it possible to never trigger action when clicked different file types.

Btw your code fine for me, i implemented it. My real question about performance.

Thank you so much!

0

请先登录再写评论。