public GoImportPackageQuickFix(@NotNull PsiReference reference) { super(reference.getElement()); myPackageName = reference.getCanonicalText(); }
@NotNull @Override public Access getReferenceAccess(@Nullable PsiElement referencedElement, @NotNull PsiReference reference) { return getExpressionAccess(reference.getElement()); }
@Override public void prepareRenaming(PsiElement element, String newName, @NotNull Map<PsiElement, String> allRenames, @NotNull SearchScope scope) { if (element instanceof GoTypeSpec) { Query<PsiReference> search = ReferencesSearch.search(element, scope); for (PsiReference ref : search) { PsiElement refElement = ref.getElement(); PsiElement type = refElement == null ? null : refElement.getParent(); if (!(type instanceof GoType)) continue; PsiElement typeParent = type.getParent(); GoPointerType pointer = ObjectUtils.tryCast(typeParent, GoPointerType.class); PsiElement anon = pointer != null ? pointer.getParent() : typeParent; if (anon instanceof GoAnonymousFieldDefinition) { allRenames.put(anon, newName); } } } else if (element instanceof GoAnonymousFieldDefinition) { GoTypeReferenceExpression reference = ((GoAnonymousFieldDefinition)element).getTypeReferenceExpression(); PsiElement resolve = reference != null ? reference.resolve() : null; if (resolve instanceof GoTypeSpec) { allRenames.put(resolve, newName); } } } }
/** * Finds a reference at the specified offset from the start of the text range of this node. * * @param offset the relative offset for which the reference is requested. * @return the reference at the offset, or null if none is found. */ @Nullable @Override public PsiReference findReferenceAt(int offset) { PsiElement mirror = getMirror(); PsiReference referenceAt = null; if (mirror != null) { PsiReference mirrorRef = mirror.findReferenceAt(offset); if (mirrorRef != null) { PsiElement mirrorElement = mirrorRef.getElement(); PsiElement element = mirrorToElement(mirrorElement); if (element != null) { referenceAt = element.getReference(); } } } return referenceAt; }
GoReferenceExpression refExpression = ObjectUtils.tryCast(reference.getElement(), GoReferenceExpression.class); GoStructLiteralCompletion.Variants variants = GoStructLiteralCompletion.allowedVariants(refExpression); PsiElement element = reference.getElement(); PsiElement spec = PsiTreeUtil.getParentOfType(element, GoFieldDeclaration.class, GoTypeSpec.class); boolean insideParameter = PsiTreeUtil.getParentOfType(element, GoParameterDeclaration.class) != null;
private static void autoImport(@NotNull InsertionContext context, @NotNull GoNamedElement element) { PsiFile file = context.getFile(); boolean vendoringEnabled = GoVendoringUtil.isVendoringEnabled(ModuleUtilCore.findModuleForPsiElement(file)); String importPath = element.getContainingFile().getImportPath(vendoringEnabled); if (StringUtil.isEmpty(importPath)) return; PsiDocumentManager.getInstance(context.getProject()).commitDocument(context.getEditor().getDocument()); PsiReference reference = file.findReferenceAt(context.getStartOffset()); if (reference != null) { PsiElement referenceElement = reference.getElement(); GoImportPackageQuickFix fix = new GoImportPackageQuickFix(referenceElement, importPath); fix.invoke(context.getProject(), file, context.getEditor(), referenceElement, referenceElement); } } }
boolean foundReference = !ReferencesSearch.search(o, o.getUseScope()).forEach(reference1 -> { ProgressManager.checkCanceled(); PsiElement element = reference1.getElement(); if (element == null) return true; PsiElement parent = element.getParent();
@NotNull protected UsageInfo[] findUsages() { if (myInlineThisOnly) return new UsageInfo[]{new UsageInfo(myReference.getElement())}; List<UsageInfo> result = ContainerUtil.newArrayList(); for (PsiReference reference : ReferencesSearch.search(myRule, myRule.getUseScope(), false)) { PsiElement element = reference.getElement(); if (GrammarUtil.isInAttributesReference(element)) continue; result.add(new UsageInfo(element)); } return result.toArray(new UsageInfo[result.size()]); }
private static boolean isUsedAsArgument(@NotNull PsiReference ref) { PsiElement element = ref.getElement(); if (!(element instanceof BnfExpression)) { return false; } PsiElement parent = element.getParent(); if (!(parent instanceof BnfExternalExpression) || ((BnfExternalExpression)parent).getRefElement() != element) { return false; } return isArgument((BnfExpression)parent); }
private void processDocReference(String currentBaseName, String newName, PsiReference reference, Map<PsiElement, String> allRenames) { PsiElement sourceElement = reference.getElement(); if (sourceElement.getLanguage().isKindOf(PodLanguage.INSTANCE)) { PsiNameIdentifierOwner identifierOwner = PsiTreeUtil.getParentOfType(sourceElement, PsiNameIdentifierOwner.class); if (identifierOwner != null) { PsiElement nameIdentifier = identifierOwner.getNameIdentifier(); if (nameIdentifier != null && nameIdentifier.getTextRange().contains(sourceElement.getTextRange())) { String currentName = identifierOwner.getName(); if (currentName != null) { String newSectionName = currentName.replace(currentBaseName, newName); allRenames.put(identifierOwner, newSectionName); } } } } }
private void applyReference(PsiReference reference) { _editor = FileEditorManager.getInstance(_project).getSelectedTextEditor(); if (_editor == null) { debug("no editor => no need to highlight"); return; } if (isHighlightOn() && isElementInEditor(_editor, reference.getElement())) { TextRange textRange = reference.getElement().getTextRange().cutOut(reference.getRangeInElement()); debug("Adding reference highlighting for " + textRange); final int docTextLength = _editor.getDocument().getTextLength(); _referenceHighlighter = _editor.getMarkupModel().addRangeHighlighter(textRange.getStartOffset(), Math.min(textRange.getEndOffset(), docTextLength), PsiViewerConstants.PSIVIEWER_REFERENCE_HIGHLIGHT_LAYER, _referenceTextAttributes, HighlighterTargetArea.EXACT_RANGE); } }
@Override public void prepareMovedFile(PsiFile file, PsiDirectory moveDestination, Map<PsiElement, PsiElement> oldToNewMap) { file.putUserData(ORIGINAL_PACKAGE_NAME, ((PerlFileImpl)file).getFilePackageName()); String newFilePath = moveDestination.getVirtualFile().getPath() + '/' + file.getName(); VirtualFile newClassRoot = PerlUtil.getFileClassRoot(moveDestination.getProject(), newFilePath); if (newClassRoot != null) { String newRelativePath = newFilePath.substring(newClassRoot.getPath().length()); String newPackageName = PerlPackageUtil.getPackageNameByPath(newRelativePath); if (newPackageName != null) { for (PsiReference reference : ReferencesSearch.search(file, file.getUseScope()).findAll()) { PerlPsiUtil.renameElement(reference.getElement(), newPackageName); } } } }
@Override public Result execute() { final PsiFile psiFile = ProjectUtil.getPsiFile(project, file); final PsiReference ref = psiFile.findReferenceAt(offset); if (ref == null) { return SimpleResult.error("No element under cursor"); } final PsiElement element = ref.getElement(); final PsiElement lookup = ref.resolve(); final DocumentationProvider provider = DocumentationManager.getProviderFromElement(element); final String doc = provider.generateDoc(lookup, element); if (doc == null) return SimpleResult.error("Could not find documentation"); if (doc.isEmpty()) return SimpleResult.error("No documentation for " + element); return SimpleResult.success(stripHtml(doc)); }
private List<PsiReference> collectFileReferences() { final List<PsiReference> references = new ArrayList<>(); PsiFile file = getFile(); file.accept(new PsiElementVisitor() { @Override public void visitElement(PsiElement element) { Collections.addAll(references, element.getReferences()); element.acceptChildren(this); } }); references.sort((o1, o2) -> o1.getElement().getTextRange().getStartOffset() + o1.getRangeInElement().getStartOffset() - o2.getElement().getTextRange().getStartOffset() + o2.getRangeInElement().getStartOffset()); return references; }
/** * Checks that the file tree walking visits every element only once. * * @param processor * @throws Exception */ protected void checkTreeWalking(BashAbstractProcessor processor) throws Exception { PsiFile file = configure().getElement().getContainingFile(); file.processDeclarations(processor, ResolveState.initial(), null, file); assertSingleVisitedElements(file); }
private Set<PsiFile> findIncludedFiles(boolean justBashFiles) throws Exception { PsiReference ref = configure(); addFile("included.txt"); Assert.assertNotNull("Reference was not found", ref); PsiFile file = ref.getElement().getContainingFile(); Assert.assertTrue(file instanceof BashFile); return FileInclusionManager.findIncludedFiles(file, true, justBashFiles); } }
@Test public void testValidScope() throws Exception { PsiReference variableReference = configure(); PsiFile included = addFile("included.bash"); PsiElement varDef = variableReference.resolve(); Assert.assertNotNull("The reference did not resolve", varDef); Assert.assertTrue("var def must resolve to the definition in included.bash", included.equals(varDef.getContainingFile())); Assert.assertTrue("The definition and usage scope must be valid", BashVarUtils.isInDefinedScope(variableReference.getElement(), (BashVarDef) varDef)); }
public void testVarResolveWalking() throws Exception { PsiReference reference = configure(); PsiElement element = reference.getElement(); assert element instanceof BashVar; DummyBashProcessor processor = new DummyBashProcessor(countKey); BashPsiUtils.varResolveTreeWalkUp(processor, (BashVar) element, element.getContainingFile(), ResolveState.initial()); assertSingleVisitedElements(element.getContainingFile()); }
@Test public void testBasicFunctionResolveToFirstDef() throws Exception { checkFunctionReference(); PsiReference element = configure(); PsiElement def = element.resolve(); Assert.assertTrue(def.getTextOffset() < element.getElement().getTextOffset()); }
@NotNull private PsiElement doAssertIsWellDefined(boolean dumMode) throws Exception { DumbServiceImpl instance = DumbServiceImpl.getInstance(myProject); boolean oldDumb = instance.isDumb(); instance.setDumb(dumMode); try { PsiReference start = configure(); PsiElement varDef = start.resolve(); //Assert.assertTrue("The reference is NOT a var reference", start.getElement() instanceof BashVarUse); Assert.assertNotNull("Could not find a definition for the reference: " + start.getElement().getText(), varDef); Assert.assertTrue("The definition is NOT a variable definition: " + varDef, varDef instanceof BashVarDef); Assert.assertTrue("The reference is NOT a reference to the definition", start.isReferenceTo(varDef)); return varDef; } finally { instance.setDumb(oldDumb); } }