PY-21458 Fixed: Live template hangs PyCharm in some case

Don't analyze implicit imports while searching iterable variable for live template
This commit is contained in:
Semyon Proshev
2016-11-25 19:06:51 +03:00
parent b7fa4c5269
commit 54a5e386f4
5 changed files with 75 additions and 10 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* 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.
@@ -25,17 +25,18 @@ import com.jetbrains.python.codeInsight.controlflow.ControlFlowCache;
import com.jetbrains.python.codeInsight.controlflow.ScopeOwner;
import com.jetbrains.python.codeInsight.dataflow.scope.Scope;
import com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil;
import com.jetbrains.python.psi.PyElement;
import com.jetbrains.python.psi.PyImportedNameDefiner;
import com.jetbrains.python.psi.PyImplicitImportNameDefiner;
import com.jetbrains.python.psi.PyTypedElement;
import com.jetbrains.python.psi.types.PyABCUtil;
import com.jetbrains.python.psi.types.PyType;
import com.jetbrains.python.psi.types.TypeEvalContext;
import one.util.streamex.StreamEx;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.StreamSupport;
/**
* @author vlan
@@ -106,13 +107,13 @@ public class PyIterableVariableMacro extends Macro {
for (ScopeOwner owner = ScopeUtil.getScopeOwner(anchor); owner != null; owner = ScopeUtil.getScopeOwner(owner)) {
final Scope scope = ControlFlowCache.getScope(owner);
results.addAll(scope.getNamedElements());
for (PyImportedNameDefiner importedNameDefiner : scope.getImportedNameDefiners()) {
for (PyElement importedElement : importedNameDefiner.iterateNames()) {
if (importedElement instanceof PsiNamedElement) {
results.add((PsiNamedElement)importedElement);
}
}
}
StreamEx
.of(scope.getImportedNameDefiners())
.filter(definer -> !PyImplicitImportNameDefiner.class.isInstance(definer))
.flatMap(definer -> StreamSupport.stream(definer.iterateNames().spliterator(), false))
.select(PsiNamedElement.class)
.forEach(results::add);
}
return results;
}
@@ -0,0 +1,3 @@
from m import *
comps<caret>
@@ -0,0 +1,3 @@
from m import *
{ for in }
@@ -0,0 +1,57 @@
/*
* 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.
*/
package com.jetbrains.python.codeInsight.liveTemplates;
import com.intellij.codeInsight.lookup.Lookup;
import com.intellij.codeInsight.lookup.LookupManager;
import com.intellij.codeInsight.lookup.impl.LookupImpl;
import com.intellij.codeInsight.template.impl.actions.ListTemplatesAction;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.jetbrains.python.fixtures.PyTestCase;
public class PyLiveTemplatesExpandingTest extends PyTestCase {
@Override
protected String getTestDataPath() {
return super.getTestDataPath() + "/codeInsight/liveTemplates/expanding/";
}
public void testIterableVariableFromImplicitImports() {
doMultiFileTest();
}
private void doMultiFileTest() {
myFixture.copyDirectoryToProject(getTestName(false), "");
myFixture.configureByFile("a.py");
final Editor editor = myFixture.getEditor();
final Project project = myFixture.getProject();
WriteCommandAction.runWriteCommandAction(
project,
() -> {
new ListTemplatesAction().actionPerformedImpl(project, editor);
final LookupImpl lookup = (LookupImpl)LookupManager.getActiveLookup(editor);
assertNotNull(lookup);
lookup.finishLookup(Lookup.NORMAL_SELECT_CHAR);
}
);
myFixture.checkResultByFile(getTestName(false) + "/a_after.py");
}
}