/*
 * Copyright (c) 2017. tangzx(love.tangzx@qq.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tang.intellij.lua.editor;

import com.intellij.ide.actions.QualifiedNameProvider;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.util.ObjectUtils;
import com.tang.intellij.lua.psi.LuaFile;
import com.tang.intellij.lua.psi.LuaFileUtil;
import org.jetbrains.annotations.Nullable;

/**
 *
 * Created by TangZX on 2016/12/30.
 */
public class LuaQualifiedNameProvider implements QualifiedNameProvider {
    @Nullable
    @Override
    public PsiElement adjustElementToCopy(PsiElement psiElement) {
        return null;
    }

    @Nullable
    @Override
    public String getQualifiedName(PsiElement psiElement) {
        if (psiElement instanceof LuaFile) {
            LuaFile file = (LuaFile) psiElement;
            VirtualFile virtualFile = file.getVirtualFile();
            Project project = file.getProject();

            VirtualFile logicalRootFile = LuaFileUtil.getSourceRoot(project, virtualFile);
            if (logicalRootFile != null && !virtualFile.equals(logicalRootFile)) {
                String value = ObjectUtils.assertNotNull(VfsUtilCore.getRelativePath(virtualFile, logicalRootFile, '.'));
                int lastDot = value.lastIndexOf('.');
                if (lastDot != -1)
                    value = value.substring(0, lastDot);
                return value;
            }
        }
        return null;
    }

    @Override
    public PsiElement qualifiedNameToElement(String s, Project project) {
        return null;
    }

    @Override
    public void insertQualifiedName(String s, PsiElement psiElement, Editor editor, Project project) {

    }
}

@@ -791,53 +791,8 @@
 ct;%0A
-import com.intellij.openapi.vfs.VfsUtilCore;%0A
 impo
@@ -872,46 +872,8 @@
 nt;%0A
-import com.intellij.util.ObjectUtils;%0A
 impo
@@ -1535,17 +1535,16 @@
 ject();%0A
-%0A
         
@@ -1551,481 +1551,63 @@
     
-VirtualFile logicalRootFile = LuaFileUtil.getSourceRoot(project, virtualFile);%0A            if (logicalRootFile != null && !virt
+return L
 ua
-l
 File
-.equals(logicalRootFile)) %7B%0A                String value = Object
 Util
-s
 .as
-sertNotNull(VfsUtilCore.getRelativePath(virtualFile, logicalRootFile, '.'));%0A                int lastDot = value.lastIndexOf('.');%0A                if (lastDot != -1)%0A                    value = value.substring(0, lastDot);%0A                return value;%0A            %7D
+RequirePath(project, virtualFile);
 %0A   

clean code