Fix project completion in the beginning

This commit is contained in:
Nikolay Krasko
2012-04-16 20:50:01 +04:00
parent b8d2e62d2c
commit f8d50fd9bb
4 changed files with 30 additions and 16 deletions
@@ -29,6 +29,7 @@ import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetNamespaceHeader;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.plugin.project.WholeProjectAnalyzerFacade;
import org.jetbrains.jet.plugin.references.JetSimpleNameReference;
/**
* Performs completion in package directive. Should suggest only packages and avoid showing fake package produced by
@@ -59,20 +60,16 @@ public class JetPackagesContributor extends CompletionContributor {
final PsiReference ref = parameters.getPosition().getContainingFile().findReferenceAt(parameters.getOffset());
if (ref != null) {
if (ref instanceof JetSimpleNameReference) {
JetSimpleNameReference simpleNameReference = (JetSimpleNameReference)ref;
// For package there will be a wrong prefix matcher with the whole package directive as prefix
PsiElement nameIdentifier = namespaceHeader.getNameIdentifier();
if (nameIdentifier == null) {
String name = simpleNameReference.getExpression().getText();
if (name == null) {
return;
}
if (!(nameIdentifier.getTextOffset() <= parameters.getOffset())) {
return;
}
int prefixLength = parameters.getOffset() - nameIdentifier.getTextOffset();
result = result.withPrefixMatcher(new PlainPrefixMatcher(nameIdentifier.getText().substring(0, prefixLength)));
int prefixLength = parameters.getOffset() - simpleNameReference.getExpression().getTextOffset();
result = result.withPrefixMatcher(new PlainPrefixMatcher(name.substring(0, prefixLength)));
BindingContext bindingContext = WholeProjectAnalyzerFacade.analyzeProjectWithCacheOnAFile(
(JetFile)namespaceHeader.getContainingFile())
@@ -0,0 +1,5 @@
package kttesting<caret>.util
// Test that there won't be package with completion fake suffix
// NUMBER: 0
@@ -105,6 +105,10 @@ public class JetBasicCompletionTest extends JetCompletionTestBase {
doTest();
}
public void testInPackageBegin() {
doTest();
}
public void testInTypeAnnotation() {
doTest();
}
@@ -64,13 +64,22 @@ public abstract class JetCompletionTestBase extends LightCompletionTestCase {
ExpectedCompletionUtils.CompletionProposal[] unexpected = completionUtils.itemsShouldAbsent(fileText);
Integer itemsNumber = completionUtils.getExpectedNumber(fileText);
assertTrue("Should be some assertions about completion", expected.length != 0 || unexpected.length != 0 || itemsNumber != null);
assertTrue("Should be some assertions about completion",
expected.length != 0 || unexpected.length != 0 || itemsNumber != null);
if (myItems == null) {
myItems = new LookupElement[0];
}
ExpectedCompletionUtils.assertContainsRenderedItems(expected, myItems);
ExpectedCompletionUtils.assertNotContainsRenderedItems(unexpected, myItems);
if (itemsNumber != null) {
assertEquals("Invalid number of completion items", itemsNumber.intValue(), myItems.length);
assertEquals(
String.format(
"Invalid number of completion items: %s",
ExpectedCompletionUtils.listToString(ExpectedCompletionUtils.getItemsInformation(myItems))),
itemsNumber.intValue(), myItems.length);
}
}
finally {
@@ -78,9 +87,8 @@ public abstract class JetCompletionTestBase extends LightCompletionTestCase {
ConfigRuntimeUtil.unConfigureKotlinRuntime(getModule(), getProjectJDK());
}
}
} catch (Exception e) {
}
catch (Exception e) {
throw new AssertionError(e);
}
}
@@ -102,7 +110,7 @@ public abstract class JetCompletionTestBase extends LightCompletionTestCase {
protected void complete(final int time) {
new CodeCompletionHandlerBase(type, false, false, true).invokeCompletion(getProject(), getEditor(), time, false);
LookupImpl lookup = (LookupImpl) LookupManager.getActiveLookup(myEditor);
LookupImpl lookup = (LookupImpl)LookupManager.getActiveLookup(myEditor);
myItems = lookup == null ? null : lookup.getItems().toArray(LookupElement.EMPTY_ARRAY);
myPrefix = lookup == null ? null : lookup.itemPattern(lookup.getItems().get(0));
}