@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file, @Nullable DataContext dataContext) {
if (!(file instanceof BnfFileImpl)) return;
BnfFile bnfFile = (BnfFileImpl)file;
SelectionModel selectionModel = editor.getSelectionModel();
int[] starts = selectionModel.getBlockSelectionStarts();
int[] ends = selectionModel.getBlockSelectionEnds();
if (starts.length == 0) return;
int startOffset = starts[0];
int endOffset = ends[ends.length-1];
final BnfRule currentRule = PsiTreeUtil.getParentOfType(file.findElementAt(startOffset), BnfRule.class);
BnfExpression parentExpression = currentRule != null ? findParentExpression(bnfFile, startOffset, endOffset) : null;
if (parentExpression == null) {
CommonRefactoringUtil.showErrorHint(editor.getProject(), editor, RefactoringBundle.message("refactoring.introduce.context.error"), "Error", null);
return;
}
if (!selectionModel.hasSelection()) {
List<BnfExpression> expressions = ContainerUtil.newArrayList();
while (parentExpression != null) {
expressions.add(parentExpression);
parentExpression = PsiTreeUtil.getParentOfType(parentExpression, BnfExpression.class);
}