type migration: make root search available on scratch files

This commit is contained in:
Dmitry Batkovich
2017-09-27 10:57:38 +03:00
parent 673e33e864
commit be5e85aedd
14 changed files with 63 additions and 184 deletions
@@ -1,18 +1,6 @@
/*
* Copyright 2000-2015 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-2017 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.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.FileModificationService;
@@ -351,7 +339,7 @@ public class MethodReturnTypeFix extends LocalQuickFixAndIntentionActionOnPsiEle
new PsiType[]{returnType},
PsiUtil.getLanguageLevel(superClass));
final TypeMigrationRules rules = new TypeMigrationRules();
final TypeMigrationRules rules = new TypeMigrationRules(project);
final PsiSubstitutor compoundSubstitutor =
TypeConversionUtil.getSuperClassSubstitutor(superClass, derivedClass, PsiSubstitutor.EMPTY).putAll(psiSubstitutor);
rules.setBoundScope(new LocalSearchScope(derivedClass));
@@ -1,18 +1,6 @@
/*
* Copyright 2000-2015 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-2017 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.codeInsight.daemon.impl.quickfix;
@@ -66,7 +54,7 @@ public class VariableTypeFromCallFix implements IntentionAction {
@Override
public void invoke(@NotNull final Project project, final Editor editor, PsiFile file) throws IncorrectOperationException {
final TypeMigrationRules rules = new TypeMigrationRules();
final TypeMigrationRules rules = new TypeMigrationRules(project);
rules.setBoundScope(PsiSearchHelper.SERVICE.getInstance(project).getUseScope(myVar));
TypeMigrationProcessor.runHighlightingTypeMigration(project, editor, rules, myVar, myExpressionType);
@@ -1,18 +1,6 @@
/*
* 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-2017 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.codeInspection.miscGenerics;
import com.intellij.codeInspection.*;
@@ -118,7 +106,7 @@ public class RawTypeCanBeGenericInspection extends BaseJavaBatchLocalInspectionT
final PsiVariable variable = (PsiVariable)element;
final PsiType type = getSuggestedType(variable);
if (type != null) {
final TypeMigrationRules rules = new TypeMigrationRules();
final TypeMigrationRules rules = new TypeMigrationRules(project);
rules.setBoundScope(PsiSearchHelper.SERVICE.getInstance(project).getUseScope(variable));
TypeMigrationProcessor.runHighlightingTypeMigration(project, null, rules, variable, type, false, true);
}
@@ -1,18 +1,6 @@
/*
* Copyright 2000-2010 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-2017 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.refactoring.extractclass;
@@ -141,7 +129,7 @@ public class ExtractEnumProcessor {
}
}
final TypeMigrationRules rules = new TypeMigrationRules();
final TypeMigrationRules rules = new TypeMigrationRules(myProject);
rules.addConversionDescriptor(new EnumTypeConversionRule(myEnumConstants));
rules.setBoundScope(GlobalSearchScope.projectScope(myProject));
myTypeMigrationProcessor = new TypeMigrationProcessor(myProject,
@@ -1,18 +1,6 @@
/*
* Copyright 2000-2014 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-2017 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.refactoring.typeMigration;
import com.intellij.openapi.diagnostic.Logger;
@@ -55,7 +43,7 @@ public class TypeEvaluator {
TypeMigrationLabeler labeler,
Project project) {
myLabeler = labeler;
myRules = labeler == null ? new TypeMigrationRules() : labeler.getRules();
myRules = labeler == null ? new TypeMigrationRules(project) : labeler.getRules();
myTypeMap = new HashMap<>();
if (types != null) {
@@ -1,18 +1,6 @@
/*
* 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-2017 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.refactoring.typeMigration;
import com.intellij.codeInsight.generation.GenerateMembersUtil;
@@ -21,7 +9,6 @@ import com.intellij.lang.java.JavaLanguage;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Pair;
@@ -1130,12 +1117,12 @@ public class TypeMigrationLabeler {
return refs;
}
private boolean canBeRoot(@Nullable PsiElement element, @NotNull SearchScope migrationScope) {
private static boolean canBeRoot(@Nullable PsiElement element, @NotNull SearchScope migrationScope) {
if (element == null) return false;
return element.isValid() &&
element.isPhysical() &&
PsiSearchScopeUtil.isInScope(migrationScope, element) &&
ProjectRootManager.getInstance(element.getProject()).getFileIndex().isInSourceContent(element.getContainingFile().getVirtualFile());
element.isWritable() &&
PsiSearchScopeUtil.isInScope(migrationScope, element);
}
@TestOnly
@@ -1,23 +1,14 @@
/*
* 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-2017 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.refactoring.typeMigration;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.impl.LibraryScopeCache;
import com.intellij.openapi.util.Pair;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.refactoring.typeMigration.rules.DisjunctionTypeConversionRule;
@@ -25,6 +16,7 @@ import com.intellij.refactoring.typeMigration.rules.RootTypeConversionRule;
import com.intellij.refactoring.typeMigration.rules.TypeConversionRule;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
@@ -39,9 +31,11 @@ import java.util.Map;
public class TypeMigrationRules {
private final List<TypeConversionRule> myConversionRules;
private final Map<Class, Object> myConversionCustomSettings = new HashMap<>();
private final Project myProject;
private SearchScope mySearchScope;
public TypeMigrationRules() {
public TypeMigrationRules(@NotNull Project project) {
myProject = project;
final TypeConversionRule[] extensions = Extensions.getExtensions(TypeConversionRule.EP_NAME);
myConversionRules = new ArrayList<>(extensions.length + 2);
myConversionRules.add(new RootTypeConversionRule());
@@ -93,8 +87,8 @@ public class TypeMigrationRules {
return myConversionRules.stream().anyMatch(rule -> rule.shouldConvertNullInitializer(from, to, context));
}
public void setBoundScope(final SearchScope searchScope) {
mySearchScope = searchScope;
public void setBoundScope(@NotNull SearchScope searchScope) {
mySearchScope = searchScope.intersectWith(GlobalSearchScope.notScope(LibraryScopeCache.getInstance(myProject).getLibrariesOnlyScope()));
}
public SearchScope getSearchScope() {
@@ -1,18 +1,6 @@
/*
* 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-2017 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.refactoring.typeMigration.ui;
import com.intellij.find.FindSettings;
@@ -95,7 +83,7 @@ public abstract class TypeMigrationDialog extends RefactoringDialog {
}
FindSettings.getInstance().setDefaultScopeName(myScopeChooserCombo.getSelectedScopeName());
if (myRules == null) {
myRules = new TypeMigrationRules();
myRules = new TypeMigrationRules(getProject());
myRules.setBoundScope(myScopeChooserCombo.getSelectedScope());
}
invokeRefactoring(new TypeMigrationProcessor(myProject, myRoots, getMigrationTypeFunction(), myRules, true));
@@ -1,3 +1,6 @@
// Copyright 2000-2017 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.refactoring.typeMigration;
import com.intellij.codeInsight.FileModificationService;
@@ -69,7 +72,7 @@ public class TypeMigrationVariableTypeFixProvider implements ChangeVariableTypeQ
if (!FileModificationService.getInstance().prepareFileForWrite(variable.getContainingFile())) return;
try {
WriteAction.run(() -> variable.normalizeDeclaration());
final TypeMigrationRules rules = new TypeMigrationRules();
final TypeMigrationRules rules = new TypeMigrationRules(project);
rules.setBoundScope(GlobalSearchScope.projectScope(project));
TypeMigrationProcessor.runHighlightingTypeMigration(project, editor, rules, variable, targetType, optimizeImports, allowDependentRoots);
WriteAction.run(() -> JavaCodeStyleManager.getInstance(project).shortenClassReferences(variable));
@@ -1,18 +1,6 @@
/*
* Copyright 2000-2015 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-2017 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.refactoring.typeMigration.inspections;
import com.intellij.codeInsight.FileModificationService;
@@ -350,7 +338,7 @@ public class GuavaInspection extends BaseJavaLocalInspectionTool {
private void performTypeMigration(List<PsiElement> elements, List<PsiType> types) {
if (!FileModificationService.getInstance().preparePsiElementsForWrite(elements)) return;
final Project project = elements.get(0).getProject();
final TypeMigrationRules rules = new TypeMigrationRules();
final TypeMigrationRules rules = new TypeMigrationRules(project);
rules.setBoundScope(GlobalSearchScopesCore.projectProductionScope(project)
.union(GlobalSearchScopesCore.projectTestScope(project)));
rules.addConversionRuleSettings(new GuavaConversionSettings(ignoreJavaxNullable));
@@ -1,3 +1,6 @@
// Copyright 2000-2017 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.refactoring.typeMigration.intentions;
import com.intellij.codeInsight.daemon.JavaErrorMessages;
@@ -115,7 +118,7 @@ public class ChangeClassParametersIntention extends PsiElementBaseIntentionActio
classType.getPresentableText()));
return;
}
final TypeMigrationRules myRules = new TypeMigrationRules();
final TypeMigrationRules myRules = new TypeMigrationRules(project);
final PsiSubstitutor substitutor = result.getSubstitutor().put(typeParameter, targetParam);
final PsiType targetClassType = elementFactory.createType(baseClass, substitutor);
myRules.setBoundScope(new LocalSearchScope(aClass));
@@ -1,18 +1,6 @@
/*
* Copyright 2000-2016 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-2017 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.refactoring;
@@ -47,7 +35,7 @@ public class ChangeTypeSignatureTest extends LightCodeInsightTestCase {
PsiType migrationType = getJavaFacade().getElementFactory().createTypeFromText(migrationTypeText, null);
try {
final TypeMigrationRules rules = new TypeMigrationRules();
final TypeMigrationRules rules = new TypeMigrationRules(getProject());
rules.setBoundScope(GlobalSearchScope.projectScope(getProject()));
new TypeMigrationProcessor(getProject(),
new PsiElement[]{parameterList},
@@ -1,18 +1,6 @@
/*
* Copyright 2000-2015 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-2017 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.refactoring;
import com.intellij.openapi.command.WriteCommandAction;
@@ -169,7 +157,7 @@ public abstract class TypeMigrationTestBase extends MultiFileTestCase {
final PsiElement[] migrationElements = provider.victims(aClass);
final PsiType migrationType = provider.migrationType(migrationElements[0]);
final TypeMigrationRules rules = new TypeMigrationRules();
final TypeMigrationRules rules = new TypeMigrationRules(getProject());
rules.setBoundScope(new LocalSearchScope(aClass.getContainingFile()));
final TestTypeMigrationProcessor pr = new TestTypeMigrationProcessor(getProject(), migrationElements, migrationType, rules);
@@ -119,7 +119,7 @@ public class JavaFxFieldToPropertyIntention extends PsiElementBaseIntentionActio
final PsiType fromType = field.getType();
final PsiType toType = elementFactory.createTypeFromText(myProperty.myObservableType.myText, field);
try {
final TypeMigrationRules rules = new TypeMigrationRules();
final TypeMigrationRules rules = new TypeMigrationRules(myProject);
final Set<VirtualFile> virtualFiles = ContainerUtil.map2SetNotNull(myFiles, PsiFile::getVirtualFile);
rules.setBoundScope(GlobalSearchScope.filesScope(myProject, virtualFiles));
final TypeMigrationLabeler labeler = new TypeMigrationLabeler(rules, toType, myProject);