in src/org/intellij/grammar/refactor/BnfIntroduceRuleHandler.java [65:119]
public void invoke(@NotNull Project project, Editor editor, 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];
BnfRule currentRule = PsiTreeUtil.getParentOfType(file.findElementAt(startOffset), BnfRule.class);
BnfExpression parentExpression = currentRule != null ? findParentExpression(bnfFile, startOffset, endOffset) : null;
if (parentExpression == null) {
CommonRefactoringUtil.showErrorHint(project, editor, RefactoringBundle.message("refactoring.introduce.context.error"), "Error", null);
return;
}
if (!selectionModel.hasSelection()) {
List<BnfExpression> expressions = new ArrayList<>();
while (parentExpression != null) {
expressions.add(parentExpression);
parentExpression = PsiTreeUtil.getParentOfType(parentExpression, BnfExpression.class);
}
if (expressions.size() == 1) {
invokeIntroduce(project, editor, file, currentRule, expressions);
}
else {
if (myPopupVariantsHandler != null) {
invokeIntroduce(project, editor, file, currentRule, Collections.singletonList(myPopupVariantsHandler.fun(expressions)));
}
else {
IntroduceTargetChooser.showChooser(
editor, expressions,
new Pass<>() {
@Override
public void pass(BnfExpression bnfExpression) {
invokeIntroduce(project, editor, file, currentRule,
Collections.singletonList(bnfExpression));
}
}, RENDER_FUNCTION, "Expressions"
);
}
}
}
else {
List<BnfExpression> selectedExpression = findSelectedExpressionsInRange(parentExpression, new TextRange(startOffset, endOffset));
if (selectedExpression.isEmpty()) {
CommonRefactoringUtil.showErrorHint(project, editor,
RefactoringBundle.message("refactoring.introduce.selection.error"), "Error", null);
return;
}
invokeIntroduce(project, editor, file, currentRule, selectedExpression);
}
}