@Override public boolean isMine(@NotNull String name, @NotNull GoNamedElement element) { if (myParent != null && element instanceof GoTypeSpec) { PsiReference reference = myParent.getReference(); return !(reference instanceof GoTypeReference) || ((GoTypeReference)reference).allowed((GoTypeSpec)element); } return false; } }
@Nullable private static PsiElement getType(@NotNull BallerinaNameReference ballerinaNameReference) { return CachedValuesManager.getCachedValue(ballerinaNameReference, () -> { PsiElement result = null; PsiElement identifier = ballerinaNameReference.getIdentifier(); PsiReference reference = identifier.getReference(); if (reference != null) { PsiElement resolvedElement = reference.resolve(); if (resolvedElement != null) { PsiElement parent = resolvedElement.getParent(); if (parent instanceof BallerinaTypeDefinition) { result = parent; } } } return CachedValueProvider.Result.create(result, ballerinaNameReference); }); }
@Override public boolean isAvailable(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { // We only perform this action on Ballerina modules since this might cause issues in other modules. Module module = ModuleUtil.findModuleForFile(file.getVirtualFile(), file.getProject()); boolean isBallerinaModule = BallerinaSdkService.getInstance(project).isBallerinaModule(module); if (!isBallerinaModule) { return false; } // If the file contain error elements, do not perform auto import. Since this might affect badly to the user // experience. Collection<PsiErrorElement> errorElements = PsiTreeUtil.findChildrenOfType(file, PsiErrorElement.class); if (!errorElements.isEmpty()) { return false; } PsiReference reference = startElement.getReference(); return file instanceof BallerinaFile && file.getManager().isInProject(file) && (reference == null || reference.resolve() == null); }
@Override protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet set) { GoReferenceExpressionBase expression = PsiTreeUtil.getParentOfType(parameters.getPosition(), GoReferenceExpressionBase.class); PsiFile originalFile = parameters.getOriginalFile(); if (expression != null) { fillVariantsByReference(expression.getReference(), originalFile, set.withPrefixMatcher(createPrefixMatcher(set.getPrefixMatcher()))); } PsiElement parent = parameters.getPosition().getParent(); if (parent != null) { fillVariantsByReference(parent.getReference(), originalFile, set.withPrefixMatcher(createPrefixMatcher(set.getPrefixMatcher()))); } }
public boolean processResolveVariants(@NotNull BallerinaScopeProcessor processor) { BallerinaFunctionDefinition ballerinaFunctionDefinition = PsiTreeUtil.getParentOfType(myElement, BallerinaFunctionDefinition.class); if (ballerinaFunctionDefinition == null) { return true; } BallerinaAttachedObject attachedObject = ballerinaFunctionDefinition.getAttachedObject(); if (attachedObject == null) { return true; } PsiElement identifier = attachedObject.getIdentifier(); PsiReference reference = identifier.getReference(); if (reference == null) { return true; } PsiElement resolvedElement = reference.resolve(); if (resolvedElement == null) { return true; } // Get suggestions from current file. return processor.execute(resolvedElement.getParent(), ResolveState.initial()); } }
@Nullable private static BallerinaFunctionDefinition getFunctionDefinition(@NotNull BallerinaFunctionInvocationReference invocationReference) { return CachedValuesManager.getCachedValue(invocationReference, () -> { BallerinaFunctionInvocation functionInvocation = invocationReference.getFunctionInvocation(); BallerinaFunctionNameReference functionNameReference = functionInvocation.getFunctionNameReference(); BallerinaAnyIdentifierName anyIdentifierName = functionNameReference.getAnyIdentifierName(); PsiElement identifier = anyIdentifierName.getIdentifier(); if (identifier == null) { return CachedValueProvider.Result.create(PsiTreeUtil.getParentOfType(null, BallerinaFunctionDefinition.class), invocationReference); } PsiReference reference = identifier.getReference(); if (reference == null) { return CachedValueProvider.Result.create(PsiTreeUtil.getParentOfType(null, BallerinaFunctionDefinition.class), invocationReference); } PsiElement resolvedElement = reference.resolve(); return CachedValueProvider.Result.create(PsiTreeUtil.getParentOfType(resolvedElement, BallerinaFunctionDefinition.class), invocationReference); }); }
private void assertResolvedNameArityRange(@NotNull PsiElement element, @NotNull String name, int arity) { PsiReference reference = element.getReference(); assertNotNull(reference); PsiElement resolved = reference.resolve(); assertNotNull(resolved); assertInstanceOf(resolved, Call.class); Call maybeDefCall = (Call) resolved; assertTrue(CallDefinitionClause.is(maybeDefCall)); assertEquals(new NameArityRange(name, new IntRange(arity, arity)), nameArityRange(maybeDefCall)); } }
private void assertUnresolvableReferenceNameArityRange(@NotNull String name, int arity) { PsiElement elementAtCaret = myFixture .getFile() .findElementAt(myFixture.getCaretOffset()); assertNotNull(elementAtCaret); PsiElement grandParent = elementAtCaret.getParent().getParent(); assertNotNull(grandParent); assertInstanceOf(grandParent, Call.class); Call grandParentCall = (Call) grandParent; assertEquals(name, grandParentCall.functionName()); assertEquals(arity, grandParentCall.resolvedFinalArity()); PsiReference reference = grandParent.getReference(); assertNotNull(reference); PsiElement resolved = reference.resolve(); assertNull(resolved); }
public void testDoubleAliasesModuleQualifier() { myFixture.configureByFiles("double_aliased_module_qualifier.ex", "referenced.ex"); PsiElement elementAtCaret = myFixture .getFile() .findElementAt(myFixture.getCaretOffset()); assertNotNull(elementAtCaret); PsiElement grandParent = elementAtCaret.getParent().getParent(); assertNotNull(grandParent); assertInstanceOf(grandParent, Call.class); PsiReference reference = grandParent.getReference(); assertNotNull(reference); PsiElement resolved = reference.resolve(); assertNull(resolved); }
public void testUnresolvedAliasQualifier() { myFixture.configureByFiles("unresolved_alias_qualifier.ex", "referenced.ex"); PsiElement elementAtCaret = myFixture .getFile() .findElementAt(myFixture.getCaretOffset()); assertNotNull(elementAtCaret); PsiElement grandParent = elementAtCaret.getParent().getParent(); assertNotNull(grandParent); assertInstanceOf(grandParent, Call.class); PsiReference reference = grandParent.getReference(); assertNotNull(reference); PsiElement resolved = reference.resolve(); assertNull(resolved); }
public void testMapAccessQualifier() { myFixture.configureByFiles("map_access_qualifier.ex", "referenced.ex"); PsiElement elementAtCaret = myFixture .getFile() .findElementAt(myFixture.getCaretOffset()); assertNotNull(elementAtCaret); PsiElement grandParent = elementAtCaret.getParent().getParent(); assertNotNull(grandParent); assertInstanceOf(grandParent, Call.class); PsiReference reference = grandParent.getReference(); assertNotNull(reference); PsiElement resolved = reference.resolve(); assertNull(resolved); }
public void testAmbiguousBackReference() { myFixture.configureByFiles("ambiguous_back.ex"); PsiElement ambiguous = myFixture .getFile() .findElementAt(myFixture.getCaretOffset()) .getPrevSibling(); assertInstanceOf(ambiguous.getFirstChild(), ElixirIdentifier.class); PsiReference reference = ambiguous.getReference(); assertNotNull("`referenced` has no reference", reference); PsiElement resolved = reference.resolve(); assertNotNull("`referenced` not resolved", resolved); assertEquals( "ambiguous reference does not resolve to previous function declaration", "def referenced do\n\n end", resolved.getText() ); }
public void testAmbiguousForwardReference() { myFixture.configureByFiles("ambiguous_forward.ex"); PsiElement ambiguous = myFixture .getFile() .findElementAt(myFixture.getCaretOffset()) .getPrevSibling(); assertInstanceOf(ambiguous.getFirstChild(), ElixirIdentifier.class); PsiReference reference = ambiguous.getReference(); assertNotNull("`referenced` has no reference", reference); PsiElement resolved = reference.resolve(); assertNotNull("`referenced` not resolved", resolved); assertEquals( "ambiguous reference does not resolve to forward function declaration", "def referenced do\n\n end", resolved.getText() ); }
public void testAmbiguousRecursiveReference() { myFixture.configureByFiles("ambiguous_recursive.ex"); PsiElement ambiguous = myFixture .getFile() .findElementAt(myFixture.getCaretOffset()) .getPrevSibling(); assertInstanceOf(ambiguous.getFirstChild(), ElixirIdentifier.class); PsiReference reference = ambiguous.getReference(); assertNotNull("`referenced` has no reference", reference); PsiElement resolved = reference.resolve(); assertNotNull("`referenced` not resolved", resolved); assertEquals( "ambiguous reference does not resolve to recursive function declaration", "def referenced do\n referenced\n\n a = 1\n end", resolved.getText() ); }
public void testParenthesesSingleCorrectArityReference() { myFixture.configureByFiles("parentheses_single_correct_arity.ex"); PsiElement parenthesesCall = myFixture .getFile() .findElementAt(myFixture.getCaretOffset()) .getParent() .getParent() .getParent(); assertInstanceOf(parenthesesCall.getFirstChild(), ElixirIdentifier.class); PsiReference reference = parenthesesCall.getReference(); assertNotNull("`referenced` has no reference", reference); PsiElement resolved = reference.resolve(); assertNotNull("`referenced` not resolved", resolved); assertEquals( "parentheses 1-arity reference does not resolve to single 1-arity function declaration", "def referenced(_) do\n end", resolved.getText() ); }
public void testParenthesesRecursiveReference() { myFixture.configureByFiles("parentheses_recursive.ex"); PsiElement parenthesesCall = myFixture .getFile() .findElementAt(myFixture.getCaretOffset()) .getParent() .getParent() .getParent(); assertInstanceOf(parenthesesCall.getFirstChild(), ElixirIdentifier.class); PsiReference reference = parenthesesCall.getReference(); assertNotNull("`referenced` has no reference", reference); PsiElement resolved = reference.resolve(); assertNotNull("`referenced` not resolved", resolved); assertEquals( "ambiguous reference does not resolve to recursive function declaration", "def referenced do\n referenced()\n\n a = 1\n end", resolved.getText() ); }
public void testReference() { myFixture.configureByFiles("reference.ex", "suffix.ex", "nested.ex"); PsiElement alias = myFixture .getFile() .findElementAt(myFixture.getCaretOffset()) .getParent() .getPrevSibling(); assertInstanceOf(alias, QualifiedAlias.class); PsiPolyVariantReference polyVariantReference = (PsiPolyVariantReference) alias.getReference(); assertNotNull(polyVariantReference); ResolveResult[] resolveResults = polyVariantReference.multiResolve(false); assertEquals(2, resolveResults.length); // alias assertEquals("alias Prefix.Suffix", resolveResults[0].getElement().getParent().getParent().getText()); // defmodule assertEquals("defmodule Prefix.Suffix.Nested do\nend", resolveResults[1].getElement().getText()); }
public void testReference() { myFixture.configureByFiles("reference.ex", "suffix.ex"); PsiElement alias = myFixture .getFile() .findElementAt(myFixture.getCaretOffset()) .getParent() .getPrevSibling() .getFirstChild(); assertInstanceOf(alias, ElixirAlias.class); PsiPolyVariantReference polyVariantReference = (PsiPolyVariantReference) alias.getReference(); assertNotNull(polyVariantReference); ResolveResult[] resolveResults = polyVariantReference.multiResolve(false); assertEquals(resolveResults.length, 2); // alias assertEquals("alias Prefix.Suffix", resolveResults[0].getElement().getParent().getParent().getText()); // defmodule assertEquals("defmodule Prefix.Suffix do\nend", resolveResults[1].getElement().getText()); }
public void testReference() { myFixture.configureByFiles("reference.ex", "multiple_alias_aye.ex", "multiple_alias_bee.ex", "nested.ex"); PsiElement alias = myFixture .getFile() .findElementAt(myFixture.getCaretOffset()) .getParent() .getPrevSibling(); assertInstanceOf(alias, QualifiedAlias.class); PsiPolyVariantReference polyVariantReference = (PsiPolyVariantReference) alias.getReference(); assertNotNull(polyVariantReference); ResolveResult[] resolveResults = polyVariantReference.multiResolve(false); assertEquals(2, resolveResults.length); // alias assertEquals( "alias Prefix.{MultipleAliasAye, MultipleAliasBee}", resolveResults[0].getElement().getParent().getParent().getParent().getParent().getParent().getText() ); // defmodule assertEquals("defmodule Prefix.MultipleAliasAye.Nested do\nend", resolveResults[1].getElement().getText()); }
public void testReference() { myFixture.configureByFiles("reference.ex", "suffix.ex", "nested.ex"); PsiElement alias = myFixture .getFile() .findElementAt(myFixture.getCaretOffset()) .getParent() .getPrevSibling(); assertInstanceOf(alias, QualifiedAlias.class); PsiPolyVariantReference polyVariantReference = (PsiPolyVariantReference) alias.getReference(); assertNotNull(polyVariantReference); ResolveResult[] resolveResults = polyVariantReference.multiResolve(false); assertEquals(2, resolveResults.length); // alias assertEquals( "alias Prefix.Suffix, as: As", resolveResults[0].getElement().getParent().getParent().getParent().getParent().getParent().getText() ); // defmodule assertEquals("defmodule Prefix.Suffix.Nested do\nend", resolveResults[1].getElement().getText()); }