001    /*
002     * Copyright 2010-2014 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.jet.lang.descriptors.impl;
018    
019    import com.google.common.collect.Lists;
020    import com.intellij.util.containers.ContainerUtil;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.annotations.Nullable;
023    import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
024    import org.jetbrains.jet.lang.descriptors.PackageViewDescriptor;
025    import org.jetbrains.jet.lang.resolve.name.FqName;
026    import org.jetbrains.jet.lang.resolve.name.Name;
027    import org.jetbrains.jet.lang.resolve.scopes.JetScopeImpl;
028    import org.jetbrains.jet.utils.Printer;
029    
030    import java.util.Collection;
031    import java.util.List;
032    
033    public class SubpackagesScope extends JetScopeImpl {
034        private final PackageViewDescriptor packageView;
035    
036        public SubpackagesScope(PackageViewDescriptor packageView) {
037            this.packageView = packageView;
038    
039        }
040    
041        @NotNull
042        @Override
043        public DeclarationDescriptor getContainingDeclaration() {
044            return packageView;
045        }
046    
047        @Nullable
048        @Override
049        public PackageViewDescriptor getPackage(@NotNull Name name) {
050            return name.isSpecial() ? null : packageView.getModule().getPackage(packageView.getFqName().child(name));
051        }
052    
053        @NotNull
054        @Override
055        public Collection<DeclarationDescriptor> getAllDescriptors() {
056            List<DeclarationDescriptor> result = Lists.newArrayList();
057            for (FqName subFqName : packageView.getModule().getPackageFragmentProvider().getSubPackagesOf(packageView.getFqName())) {
058                ContainerUtil.addIfNotNull(result, getPackage(subFqName.shortName()));
059            }
060            return result;
061        }
062    
063        @Override
064        public void printScopeStructure(@NotNull Printer p) {
065            p.println(getClass().getSimpleName(), " {");
066            p.pushIndent();
067    
068            p.println("thisDescriptor = ", packageView);
069    
070            p.popIndent();
071            p.println("}");
072        }
073    }