use of com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement in project Perl5-IDEA by Camelcade.
the class PerlSubReference method resolveInner.
@NotNull
@Override
protected ResolveResult[] resolveInner(boolean incompleteCode) {
PsiElement myElement = getElement();
assert myElement instanceof PerlSubNameElement;
PsiElement parent = myElement.getParent();
if (parent instanceof PerlSubDeclarationElement || parent instanceof PerlSubDefinitionElement) {
return ResolveResult.EMPTY_ARRAY;
}
PerlSubNameElement subNameElement = (PerlSubNameElement) myElement;
List<PsiElement> relatedItems = new ArrayList<>();
String packageName = subNameElement.getPackageName();
String subName = subNameElement.getName();
Project project = subNameElement.getProject();
PerlNamespaceElement expliclitPackageElement = null;
if (parent instanceof PerlNamespaceElementContainer) {
expliclitPackageElement = ((PerlNamespaceElementContainer) parent).getNamespaceElement();
}
if (!subName.isEmpty()) {
if (parent instanceof PerlMethod && ((PerlMethod) parent).isObjectMethod()) {
boolean isSuper = expliclitPackageElement != null && expliclitPackageElement.isSUPER();
relatedItems.addAll(PerlMro.resolveSub(project, isSuper ? PerlPackageUtil.getContextPackageName(subNameElement) : packageName, subName, isSuper));
} else // static resolution
{
if (PerlSharedSettings.getInstance(project).SIMPLE_MAIN_RESOLUTION && // fixme this is a dirty hack until proper names resolution implemented
PerlPackageUtil.isMain(packageName)) {
PsiFile file = subNameElement.getContainingFile();
GlobalSearchScope fileScope = GlobalSearchScope.fileScope(file);
collectRelatedItems(packageName + PerlPackageUtil.PACKAGE_SEPARATOR + subName, project, parent, relatedItems, fileScope);
// if (file instanceof PerlFile)
// ((PerlFile) file).getElementsResolveScope();
// System.err.println("Checking for " + subName);
}
if (relatedItems.isEmpty()) {
GlobalSearchScope globalSearchScope = GlobalSearchScope.allScope(project);
// check indexes for defined subs
collectRelatedItems(packageName + PerlPackageUtil.PACKAGE_SEPARATOR + subName, project, parent, relatedItems, globalSearchScope);
if (expliclitPackageElement == null) {
// check for imports to the current file
PerlNamespaceDefinitionElement namespaceContainer = PerlPackageUtil.getNamespaceContainerForElement(subNameElement);
if (namespaceContainer != null) {
for (PerlExportDescriptor exportDescriptor : namespaceContainer.getImportedSubsDescriptors()) {
if (exportDescriptor.getImportedName().equals(subName)) {
int currentSize = relatedItems.size();
collectRelatedItems(exportDescriptor.getTargetCanonicalName(), project, parent, relatedItems, globalSearchScope);
if (// imported, but not found, attempting autoload
relatedItems.size() == currentSize) {
collectRelatedItems(exportDescriptor.getRealPackage() + PerlSubUtil.SUB_AUTOLOAD_WITH_PREFIX, project, parent, relatedItems, globalSearchScope);
}
}
}
}
} else // check imports to target namespace
{
String targetPackageName = expliclitPackageElement.getCanonicalName();
if (targetPackageName != null) {
// fixme partially not DRY with previous block
for (PerlNamespaceDefinitionElement namespaceDefinition : PerlPackageUtil.getNamespaceDefinitions(project, targetPackageName)) {
for (PerlExportDescriptor exportDescriptor : namespaceDefinition.getImportedSubsDescriptors()) {
if (exportDescriptor.getImportedName().equals(subName)) {
collectRelatedItems(exportDescriptor.getTargetCanonicalName(), project, parent, relatedItems, globalSearchScope);
}
}
}
}
}
// check for builtins
if (relatedItems.isEmpty()) {
PerlSubDefinitionElement builtInSub = PerlBuiltInSubsService.getInstance(project).findSub(subName);
if (builtInSub != null) {
relatedItems.add(builtInSub);
}
}
// check for autoload
if (relatedItems.isEmpty() && // don't check for UNIVERSAL::AUTOLOAD
!PerlPackageUtil.isUNIVERSAL(packageName)) {
collectRelatedItems(packageName + PerlSubUtil.SUB_AUTOLOAD_WITH_PREFIX, project, parent, relatedItems, globalSearchScope);
}
}
}
}
List<ResolveResult> result = getResolveResults(relatedItems);
return result.toArray(new ResolveResult[result.size()]);
}
use of com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement in project Perl5-IDEA by Camelcade.
the class PerlMroC3 method getLinearISA.
/**
* Builds list of inheritance path for C3 mro (Dylan, Python, Perl6): http://perldoc.perl.org/mro.html#The-C3-MRO
*
* @param project project
* @param namespaceDefinitions list of package names to add
* @param recursionMap recursion protection map
* @param result list to populate
*/
@Override
public void getLinearISA(Project project, List<PerlNamespaceDefinitionElement> namespaceDefinitions, HashSet<String> recursionMap, ArrayList<String> result) {
// System.err.println("Resolving C3 for " + packageNames);
ProgressManager.checkCanceled();
Collection<PerlNamespaceDefinitionElement> nextIterationDefinitions = new ArrayList<>();
for (PerlNamespaceDefinitionElement namespaceDefinition : namespaceDefinitions) {
String packageName = namespaceDefinition.getPackageName();
if (!recursionMap.contains(packageName)) {
recursionMap.add(packageName);
result.add(packageName);
nextIterationDefinitions.add(namespaceDefinition);
}
}
for (PerlNamespaceDefinitionElement namespaceDefinition : nextIterationDefinitions) {
namespaceDefinition.getLinearISA(recursionMap, result);
}
}
use of com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement in project Perl5-IDEA by Camelcade.
the class PerlVariableReference method resolveInner.
@NotNull
@Override
protected ResolveResult[] resolveInner(boolean incompleteCode) {
PsiElement elementParent = myElement.getParent();
assert elementParent instanceof PerlVariable;
PerlVariable perlVariable = (PerlVariable) elementParent;
List<ResolveResult> result = new ArrayList<>();
PerlVariableDeclarationElement lexicalDeclaration = PerlResolveUtil.getLexicalDeclaration(perlVariable);
if (lexicalDeclaration == null || lexicalDeclaration.isGlobalDeclaration() && !(lexicalDeclaration instanceof PerlImplicitVariableDeclaration)) {
// not found explicit lexically visible declarations
// imports
PerlVariableType actualType = perlVariable.getActualType();
Project project = perlVariable.getProject();
PerlNamespaceDefinitionElement namespaceContainer = PerlPackageUtil.getNamespaceContainerForElement(perlVariable);
if (// not true if LPE in TemplateToolkit
namespaceContainer != null) {
String variableName = perlVariable.getName();
if (actualType == PerlVariableType.SCALAR) {
for (PerlExportDescriptor importEntry : namespaceContainer.getImportedScalarDescriptors()) {
if (importEntry.getImportedName().equals(variableName)) {
for (PerlVariableDeclarationElement targetVariable : PerlScalarUtil.getGlobalScalarDefinitions(project, importEntry.getTargetCanonicalName())) {
result.add(new PsiElementResolveResult(targetVariable));
}
}
}
} else if (actualType == PerlVariableType.ARRAY) {
for (PerlExportDescriptor importEntry : namespaceContainer.getImportedArrayDescriptors()) {
if (importEntry.getImportedName().equals(variableName)) {
for (PerlVariableDeclarationElement targetVariable : PerlArrayUtil.getGlobalArrayDefinitions(project, importEntry.getTargetCanonicalName())) {
result.add(new PsiElementResolveResult(targetVariable));
}
}
}
} else if (actualType == PerlVariableType.HASH) {
for (PerlExportDescriptor importEntry : namespaceContainer.getImportedHashDescriptors()) {
if (importEntry.getImportedName().equals(variableName)) {
for (PerlVariableDeclarationElement targetVariable : PerlHashUtil.getGlobalHashDefinitions(project, importEntry.getTargetCanonicalName())) {
result.add(new PsiElementResolveResult(targetVariable));
}
}
}
}
}
// our variable declaration
for (PerlGlobVariable glob : perlVariable.getRelatedGlobs()) {
result.add(new PsiElementResolveResult(glob));
}
// globs
for (PerlVariableDeclarationElement globalDeclaration : perlVariable.getGlobalDeclarations()) {
result.add(new PsiElementResolveResult(globalDeclaration));
}
} else {
result.add(new PsiElementResolveResult(lexicalDeclaration));
}
return result.toArray(new ResolveResult[result.size()]);
}
use of com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement in project Perl5-IDEA by Camelcade.
the class PerlMoveFileHandler method updateMovedFile.
@Override
public void updateMovedFile(PsiFile file) throws IncorrectOperationException {
String originalPackageName = file.getUserData(ORIGINAL_PACKAGE_NAME);
Project project = file.getProject();
VirtualFile virtualFile = file.getVirtualFile();
VirtualFile newInnermostRoot = PerlUtil.getFileClassRoot(project, virtualFile);
if (newInnermostRoot != null && originalPackageName != null) {
String newRelativePath = VfsUtil.getRelativePath(virtualFile, newInnermostRoot);
String newPackageName = PerlPackageUtil.getPackageNameByPath(newRelativePath);
final RenameRefactoring[] refactoring = { null };
for (PerlNamespaceDefinitionElement namespaceDefinition : PsiTreeUtil.findChildrenOfType(file, PerlNamespaceDefinitionElement.class)) {
if (originalPackageName.equals(namespaceDefinition.getPackageName())) {
if (refactoring[0] == null) {
refactoring[0] = RefactoringFactory.getInstance(file.getProject()).createRename(namespaceDefinition, newPackageName);
} else {
refactoring[0].addElement(namespaceDefinition, newPackageName);
}
}
}
if (refactoring[0] != null) {
ApplicationManager.getApplication().invokeLater(refactoring[0]::run);
}
}
}
use of com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement in project Perl5-IDEA by Camelcade.
the class PerlRenameFileProcessor method getPostRenameCallback.
@Nullable
@Override
public Runnable getPostRenameCallback(final PsiElement element, String newName, RefactoringElementListener elementListener) {
if (newName.endsWith(".pm")) {
final Project project = element.getProject();
final String currentPackageName = ((PerlFileImpl) element).getFilePackageName();
if (currentPackageName != null) {
String[] nameChunks = currentPackageName.split(PerlPackageUtil.PACKAGE_SEPARATOR);
nameChunks[nameChunks.length - 1] = newName.replaceFirst("\\.pm$", "");
final String newPackageName = StringUtils.join(nameChunks, PerlPackageUtil.PACKAGE_SEPARATOR);
final String newFileName = ((PerlFileImpl) element).getVirtualFile().getParent().getPath() + '/' + newName;
return () -> {
VirtualFile newFile = LocalFileSystem.getInstance().findFileByPath(newFileName);
if (newFile != null) {
PsiFile psiFile = PsiManager.getInstance(project).findFile(newFile);
if (psiFile != null) {
final RenameRefactoring[] refactoring = { null };
for (PerlNamespaceDefinitionElement namespaceDefinition : PsiTreeUtil.findChildrenOfType(psiFile, PerlNamespaceDefinitionElement.class)) {
if (currentPackageName.equals(namespaceDefinition.getPackageName())) {
if (refactoring[0] == null) {
refactoring[0] = RefactoringFactory.getInstance(psiFile.getProject()).createRename(namespaceDefinition, newPackageName);
} else {
refactoring[0].addElement(namespaceDefinition, newPackageName);
}
}
}
if (refactoring[0] != null) {
ApplicationManager.getApplication().invokeLater(refactoring[0]::run);
}
}
}
};
}
}
return super.getPostRenameCallback(element, newName, elementListener);
}
Aggregations