Java Type Migration: enable intentions when the caret is after the last symbol of the variable identifier

GitOrigin-RevId: 2dc20b3b87c7cd2fa071485364e78462ba532f8c
This commit is contained in:
Bas Leijdekkers
2023-03-06 16:46:48 +01:00
committed by intellij-monorepo-bot
parent 3005f89a02
commit 18c1ad1b54
4 changed files with 23 additions and 39 deletions

View File

@@ -1,21 +1,7 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.refactoring.typeMigration.intentions;
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
import com.intellij.codeInsight.intention.BaseElementAtCaretIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
@@ -34,7 +20,7 @@ import java.util.concurrent.atomic.AtomicLong;
/**
* @author Dmitry Batkovich
*/
public class ConvertAtomicToLongAdderIntention extends PsiElementBaseIntentionAction {
public class ConvertAtomicToLongAdderIntention extends BaseElementAtCaretIntentionAction {
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {

View File

@@ -2,9 +2,9 @@
package com.intellij.refactoring.typeMigration.intentions;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInsight.intention.BaseElementAtCaretIntentionAction;
import com.intellij.codeInsight.intention.HighPriorityAction;
import com.intellij.codeInsight.intention.PriorityAction;
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo;
import com.intellij.ide.highlighter.JavaFileType;
import com.intellij.lang.java.JavaLanguage;
@@ -36,7 +36,7 @@ import java.util.concurrent.atomic.*;
/**
* @author anna
*/
public class ConvertFieldToAtomicIntention extends PsiElementBaseIntentionAction implements PriorityAction {
public class ConvertFieldToAtomicIntention extends BaseElementAtCaretIntentionAction implements PriorityAction {
private static final Logger LOG = Logger.getInstance(ConvertFieldToAtomicIntention.class);
private final Map<PsiType, String> myFromToMap = Map.of(
PsiTypes.intType(), AtomicInteger.class.getName(),

View File

@@ -2,8 +2,8 @@
package com.intellij.refactoring.typeMigration.intentions;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInsight.intention.BaseElementAtCaretIntentionAction;
import com.intellij.codeInsight.intention.LowPriorityAction;
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo;
import com.intellij.ide.highlighter.JavaFileType;
import com.intellij.lang.java.JavaLanguage;
@@ -27,7 +27,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class ConvertFieldToThreadLocalIntention extends PsiElementBaseIntentionAction implements LowPriorityAction {
public class ConvertFieldToThreadLocalIntention extends BaseElementAtCaretIntentionAction implements LowPriorityAction {
private static final Logger LOG = Logger.getInstance(ConvertFieldToThreadLocalIntention.class);
@NotNull

View File

@@ -1,19 +1,4 @@
/*
* Copyright 2000-2019 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.intention;
import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
@@ -23,6 +8,7 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public abstract class BaseElementAtCaretIntentionAction extends BaseIntentionAction {
private volatile boolean useElementToTheLeft;
@@ -64,8 +50,7 @@ public abstract class BaseElementAtCaretIntentionAction extends BaseIntentionAct
@Override
public final void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
int offset = editor.getCaretModel().getOffset();
PsiElement element = file.findElementAt(useElementToTheLeft ? offset - 1 : offset);
PsiElement element = getElement(editor, file);
if (element == null) {
return;
}
@@ -82,4 +67,17 @@ public abstract class BaseElementAtCaretIntentionAction extends BaseIntentionAct
* @throws IncorrectOperationException On errors.
*/
public abstract void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException;
/**
* Retrieves the element this intention was invoked on.
*
* @param editor the editor in which the intention was invoked, may be preview editor.
* @param file the file in which the intention was invoked.
* @return the element under the caret.
*/
@Nullable
protected PsiElement getElement(@NotNull Editor editor, @NotNull PsiFile file) {
int position = editor.getCaretModel().getOffset();
return file.findElementAt(useElementToTheLeft ? position - 1 : position);
}
}