IDEA-252714 [java] use anchor if request is coming from tree child of target class

GitOrigin-RevId: 8248fb4f61867773b41c5274b52f2e73b3867972
This commit is contained in:
Daniil Ovchinnikov
2020-11-12 14:55:22 +00:00
committed by intellij-monorepo-bot
parent 5ee07a5589
commit 932ea20745
4 changed files with 19 additions and 3 deletions
@@ -1,4 +1,4 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
@file:JvmName("CreateFieldFromUsage")
package com.intellij.lang.java.request
@@ -15,6 +15,7 @@ import com.intellij.lang.jvm.actions.EP_NAME
import com.intellij.lang.jvm.actions.groupActionsByType
import com.intellij.psi.*
import com.intellij.psi.impl.PsiImplUtil
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.util.PsiUtil.resolveClassInClassTypeOnly
import com.intellij.psi.util.PsiUtilCore
import com.intellij.psi.util.parentOfType
@@ -115,7 +116,7 @@ private class CreateFieldRequests(val myRef: PsiReferenceExpression) {
val request = CreateFieldFromJavaUsageRequest(
modifiers = modifiers,
reference = myRef,
useAnchor = target.toJavaClassOrNull() == ownerClass,
useAnchor = ownerClass != null && PsiTreeUtil.isAncestor(target.toJavaClassOrNull(), ownerClass, false),
isConstant = false
)
addRequest(target, request)
@@ -0,0 +1,7 @@
// "Create field 'i' in 'A'" "true"
class A {
private int i<caret>;
Object o = new Object() {
public void f(A a) {
a.i = 0;
@@ -0,0 +1,6 @@
// "Create field 'i' in 'A'" "true"
class A {
Object o = new Object() {
public void f(A a) {
a.<caret>i = 0;
@@ -1,4 +1,4 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.codeInsight.daemon.quickFix;
import com.intellij.application.options.CodeStyle;
@@ -73,6 +73,8 @@ public class CreateFieldFromUsageTest extends LightQuickFixTestCase {
public void testInnerGeneric() { doSingleTest(); }
public void testInnerGenericArray() { doSingleTest(); }
public void testFromUnfinishedAnonymousClass() { doSingleTest(); }
protected void doSingleTest() {
doSingleTest(getTestName(false) + ".java");