use of com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement in project Perl5-IDEA by Camelcade.
the class MasonNamespaceDefinitionImpl method getParentNamespaceDefinitions.
@Override
public List<PerlNamespaceDefinitionElement> getParentNamespaceDefinitions() {
List<String> parentsPaths;
PerlNamespaceDefinitionStub stub = getStub();
if (stub != null) {
parentsPaths = stub.getParentNamespacesNames();
} else {
parentsPaths = getParentNamespacesNamesFromPsi();
}
VirtualFile containingFile = MasonCoreUtil.getContainingVirtualFile(getContainingFile());
List<PerlNamespaceDefinitionElement> parentsNamespaces;
if (!parentsPaths.isEmpty() && containingFile != null) {
parentsNamespaces = Mason2Util.collectComponentNamespacesByPaths(getProject(), parentsPaths, containingFile.getParent());
} else {
String autobaseParent = getParentNamespaceFromAutobase();
if (autobaseParent != null) {
parentsNamespaces = Mason2Util.getMasonNamespacesByAbsolutePath(getProject(), autobaseParent);
} else {
parentsNamespaces = new ArrayList<>();
}
}
if (parentsNamespaces.isEmpty()) {
parentsNamespaces.addAll(PerlPackageUtil.getNamespaceDefinitions(getProject(), MASON_DEFAULT_COMPONENT_PARENT));
}
return parentsNamespaces;
}
use of com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement in project Perl5-IDEA by Camelcade.
the class Mason2Util method collectComponentNamespacesByPaths.
@NotNull
public static List<PerlNamespaceDefinitionElement> collectComponentNamespacesByPaths(@NotNull Project project, @NotNull List<String> componentPaths, @NotNull VirtualFile anchorDir) {
List<PerlNamespaceDefinitionElement> result = new ArrayList<>();
MasonSettings masonSettings = MasonSettings.getInstance(project);
for (String componentPath : componentPaths) {
VirtualFile componentFile = null;
if (componentPath.startsWith(// abs path relative to mason roots, see the Mason::Interp::_determine_parent_compc
"" + VfsUtil.VFS_SEPARATOR_CHAR)) {
for (VirtualFile componentRoot : masonSettings.getComponentsRoots()) {
componentFile = componentRoot.findFileByRelativePath(componentPath.substring(1));
if (componentFile != null) {
break;
}
}
} else // relative path
{
componentFile = anchorDir.findFileByRelativePath(componentPath);
}
if (componentFile != null) {
String absolutePath = VfsUtil.getRelativePath(componentFile, project.getBaseDir());
if (absolutePath != null) {
result.addAll(Mason2Util.getMasonNamespacesByAbsolutePath(project, absolutePath));
}
}
}
return result;
}
use of com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement in project Perl5-IDEA by Camelcade.
the class PerlPackageUtil method getContextPackageName.
/**
* Searching of namespace element is in. If no explicit namespaces defined, main is returned
*
* @param element psi element to find definition for
* @return canonical package name
*/
@Contract("null -> null")
public static String getContextPackageName(@Nullable PsiElement element) {
if (element == null) {
return null;
}
PerlNamespaceDefinitionElement namespaceDefinition = getContainingNamespace(element);
if (// checking that definition is valid and got namespace
namespaceDefinition != null && namespaceDefinition.getPackageName() != null) {
String name = namespaceDefinition.getPackageName();
assert name != null;
return name;
}
// default value
PsiFile file = element.getContainingFile();
if (file instanceof PerlFileImpl) {
PsiElement contextParent = file.getContext();
PsiElement realParent = file.getParent();
if (contextParent != null && !contextParent.equals(realParent)) {
return getContextPackageName(contextParent);
}
return ((PerlFileImpl) file).getPackageName();
} else {
return MAIN_PACKAGE;
}
}
use of com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement in project Perl5-IDEA by Camelcade.
the class PerlPackageUtil method collectNestedPackageDefinitionsFromFile.
/**
* Adds to queue netsted namespaces, which names should be adjusted to the new package name/path
*
* @param queue - RenameRefactoringQueue
* @param file - file has been moved
* @param oldPath - previous filepath
*/
public static void collectNestedPackageDefinitionsFromFile(@NotNull RenameRefactoringQueue queue, VirtualFile file, String oldPath) {
Project project = queue.getProject();
VirtualFile newInnermostRoot = PerlUtil.getFileClassRoot(project, file);
if (newInnermostRoot != null) {
String newRelativePath = VfsUtil.getRelativePath(file, newInnermostRoot);
String newPackageName = getPackageNameByPath(newRelativePath);
VirtualFile oldInnermostRoot = PerlUtil.getFileClassRoot(project, oldPath);
if (oldInnermostRoot != null) {
String oldRelativePath = oldPath.substring(oldInnermostRoot.getPath().length());
String oldPackageName = getPackageNameByPath(oldRelativePath);
if (!oldPackageName.equals(newPackageName)) {
PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
if (psiFile != null) {
for (PerlNamespaceDefinitionElement namespaceDefinition : PsiTreeUtil.findChildrenOfType(psiFile, PerlNamespaceDefinitionElement.class)) {
if (oldPackageName.equals(namespaceDefinition.getPackageName())) {
queue.addElement(namespaceDefinition, newPackageName);
}
}
}
}
}
}
}
use of com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement in project Perl5-IDEA by Camelcade.
the class PerlPackageUtil method processParentClassesSubs.
public static void processParentClassesSubs(PerlNamespaceDefinitionElement childClass, Set<String> processedSubsNames, Set<PerlNamespaceDefinitionElement> recursionMap, Processor<PerlSubElement> processor) {
if (childClass == null || recursionMap.contains(childClass)) {
return;
}
recursionMap.add(childClass);
for (PerlNamespaceDefinitionElement parentNamespace : childClass.getParentNamespaceDefinitions()) {
for (PsiElement subDefinitionBase : collectNamespaceSubs(parentNamespace)) {
ProgressManager.checkCanceled();
String subName = ((PerlSubElement) subDefinitionBase).getSubName();
if (subDefinitionBase.isValid() && ((PerlSubElement) subDefinitionBase).isMethod() && !processedSubsNames.contains(subName)) {
processedSubsNames.add(subName);
processor.process(((PerlSubElement) subDefinitionBase));
}
}
processParentClassesSubs(parentNamespace, processedSubsNames, recursionMap, processor);
}
}
Aggregations