@Override
public void invoke(@NotNull final Project project, Editor editor, PsiFile file, @Nullable DataContext dataContext) {
if (!CommonRefactoringUtil.checkReadOnlyStatus(file)) return;
SelectionModel selectionModel = editor.getSelectionModel();
if (selectionModel.hasSelection()) {
Pair<PsiElement, PsiElement> pair = ErlangRefactoringUtil.selectionToElements(file, selectionModel);
if (pair.first == null || pair.second == null) {
showCannotPerformError(project, editor);
return;
}
List<ErlangExpression> selection = getSelectedExpressions(pair.first, pair.second);
if (selection.isEmpty()) {
showCannotPerformError(project, editor);
}
else {
perform(editor, selection);
}
}
else {
ErlangRefactoringUtil.smartIntroduce(editor, file,
new ErlangRefactoringUtil.Extractor() {
@Override
public boolean checkContext(@NotNull PsiFile file, @NotNull Editor editor, @Nullable PsiElement element) {
boolean ok = PsiTreeUtil.getParentOfType(element, ErlangClauseBody.class) != null;
if (!ok) {
showCannotPerformError(project, editor);
}
return ok;
}
@Override
public void process(@NotNull Editor editor, @NotNull ErlangExpression expression) {
perform(editor, ContainerUtil.newSmartList(expression));
}
});
}
selectionModel.removeSelection();
}