001/*
002 * Copyright 2010-2013 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
017package org.jetbrains.jet.lang.psi;
018
019import com.intellij.openapi.project.Project;
020import com.intellij.psi.*;
021import com.intellij.psi.impl.PsiManagerEx;
022import com.intellij.psi.search.GlobalSearchScope;
023import com.intellij.psi.tree.IElementType;
024import com.intellij.testFramework.LightVirtualFile;
025import org.jetbrains.annotations.NotNull;
026import org.jetbrains.jet.plugin.JetFileType;
027
028public class JetCodeFragmentImpl extends JetFile implements PsiCodeFragment {
029    protected PsiElement context;
030    private GlobalSearchScope resolveScope;
031
032    public JetCodeFragmentImpl(Project project, String name, CharSequence text, IElementType elementType, PsiElement context) {
033        super(((PsiManagerEx) PsiManager.getInstance(project)).getFileManager().createFileViewProvider(
034                new LightVirtualFile(name, JetFileType.INSTANCE, text), true));
035        ((SingleRootFileViewProvider)getViewProvider()).forceCachedPsi(this);
036        init(TokenType.CODE_FRAGMENT, elementType);
037        this.context = context;
038    }
039
040    @Override
041    public void forceResolveScope(GlobalSearchScope scope) {
042        resolveScope = scope;
043    }
044
045    @Override
046    public GlobalSearchScope getForcedResolveScope() {
047        return resolveScope;
048    }
049
050    @Override
051    public boolean isPhysical() {
052        return true;
053    }
054
055    @Override
056    public boolean isValid() {
057        return true;
058    }
059
060    @Override
061    public PsiElement getContext() {
062        return context;
063    }
064
065    @Override
066    @NotNull
067    public GlobalSearchScope getResolveScope() {
068        if (resolveScope != null) return resolveScope;
069        return super.getResolveScope();
070    }
071}