[lang] Make getElement API protected in PsiElementBaseIntentionAction

Can be used to retrieve element in intention preview.

GitOrigin-RevId: c1fe0e4b83d941259ad3504639af05b58488fe41
This commit is contained in:
Bart van Helvert
2023-01-20 16:24:32 +00:00
committed by intellij-monorepo-bot
parent e63a35f6f3
commit e5fea5533b
3 changed files with 10 additions and 4 deletions
@@ -10,7 +10,6 @@ import com.intellij.java.JavaBundle;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ApplicationNamesInfo;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
@@ -67,7 +66,7 @@ public class ExtractSetFromComparisonChainAction extends PsiElementBaseIntention
@Override
public @NotNull IntentionPreviewInfo generatePreview(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
PsiElement element = getElement(editor, file);
if (element == null) return IntentionPreviewInfo.EMPTY;
extract(project, editor, element);
return IntentionPreviewInfo.DIFF;
@@ -48,7 +48,7 @@ public class ConvertToSingleReturnAction extends PsiElementBaseIntentionAction {
@Override
public @NotNull IntentionPreviewInfo generatePreview(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
PsiElement element = getElement(editor, file);
PsiCodeBlock block = findBlock(element);
if (block == null) return IntentionPreviewInfo.EMPTY;
PsiCodeBlock replacement = generateBody(project, block, new EmptyProgressIndicator());
@@ -77,8 +77,15 @@ public abstract class PsiElementBaseIntentionAction extends BaseIntentionAction
*/
public abstract boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element);
/**
* Retrieves the element this intention was invoked on.
*
* @param editor the editor in which the intention was invoked, can be preview editor.
* @param file the file in which the intention was invoked.
* @return the element under caret.
*/
@Nullable
private static PsiElement getElement(@NotNull Editor editor, @NotNull PsiFile file) {
protected static PsiElement getElement(@NotNull Editor editor, @NotNull PsiFile file) {
CaretModel caretModel = editor.getCaretModel();
int position = caretModel.getOffset();
return file.findElementAt(position);