package org.intellij.ibatis.provider;

import com.intellij.codeInsight.lookup.LookupValueFactory;
import com.intellij.javaee.dataSource.DataSource;
import com.intellij.javaee.dataSource.DataSourceManager;
import com.intellij.javaee.dataSource.DatabaseTableData;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiManager;
import com.intellij.psi.PsiReference;
import com.intellij.psi.javadoc.PsiDocTag;
import com.intellij.util.IncorrectOperationException;
import org.intellij.ibatis.facet.IbatisFacet;
import org.intellij.ibatis.util.IbatisConstants;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
 * reference provider for table name
 */
public class DatabaseTableReferenceProvider extends BaseReferenceProvider {
    @NotNull public PsiReference[] getReferencesByElement(PsiElement psiElement) {
        final PsiDocTag docTag = (PsiDocTag) psiElement;
        final Project project = docTag.getProject();
        return new PsiReference[]{new PsiReference() {
            public PsiElement getElement() {
                return docTag;
            }

            public TextRange getRangeInElement() {
                int offset = PsiManager.getInstance(project).getElementManipulatorsRegistry().getOffsetInElement(docTag);
                return new TextRange(offset, docTag.getTextLength());
            }

            @Nullable public PsiElement resolve() {
                return null;
            }

            public String getCanonicalText() {
                return docTag.getText().trim();
            }

            public PsiElement handleElementRename(String s) throws IncorrectOperationException {
                return null;
            }

            public PsiElement bindToElement(@NotNull PsiElement psiElement) throws IncorrectOperationException {
                return null;
            }

            public boolean isReferenceTo(PsiElement psiElement) {
                return false;
            }

            public Object[] getVariants() {
                DataSource dataSource = getDataSourceForIbatis(docTag);
                List<Object> variants = new ArrayList<Object>();
                if (dataSource != null) {
                    List<DatabaseTableData> tables = dataSource.getTables();
                    for (DatabaseTableData table : tables) {
                        variants.add(LookupValueFactory.createLookupValue(table.getName(), IbatisConstants.DATABASE_TABLE));
                    }
                }
                return variants.toArray();
            }

            public boolean isSoft() {
                return true;
            }
        }};
    }

    public static DataSource getDataSourceForIbatis(PsiElement psiElement) {
        Module module = ModuleUtil.findModuleForPsiElement(psiElement);
        IbatisFacet ibatisFacet = IbatisFacet.getInstance(module);
        if (ibatisFacet == null) return null;
        String selectedDataSourceName = ibatisFacet.getConfiguration().dataSourceName;
        DataSourceManager dataSourceManager = DataSourceManager.getInstance(psiElement.getProject());
        return dataSourceManager.getDataSourceByName(selectedDataSourceName);
    }
}

@@ -2965,24 +2965,159 @@
 ;%0D%0A    %7D%0D%0A%0D%0A
+    /**%0D%0A     * get the datasource for ibatis%0D%0A     *%0D%0A     * @param psiElement psiElement%0D%0A     * @return DataSource Object%0D%0A     */%0D%0A
     public s
@@ -3251,24 +3251,295 @@
 iElement);%0D%0A
+        return getDataSourceForIbatis(module);%0D%0A    %7D%0D%0A%0D%0A    /**%0D%0A     * get the datasource for ibatis in module%0D%0A     *%0D%0A     * @param module Module object%0D%0A     * @return DataSource object%0D%0A     */%0D%0A    public static DataSource getDataSourceForIbatis(Module module) %7B%0D%0A
         Ibat
@@ -3805,26 +3805,22 @@
 nstance(
-psiElement
+module
 .getProj

comment updated