Search in sources :

Example 6 with DasTable

use of com.intellij.database.model.DasTable in project yii2support by nvlad.

the class DatabaseUtils method buildLookup.

@NotNull
public static LookupElementBuilder buildLookup(Object field, boolean showSchema, Project project) {
    String lookupString = "-";
    if (field instanceof DasObject) {
        lookupString = ((DasObject) field).getName();
        lookupString = RemoveTablePrefix(lookupString, project);
    }
    if (field instanceof Field) {
        lookupString = ((Field) field).getName();
    }
    LookupElementBuilder builder = LookupElementBuilder.create(field, lookupString);
    if (field instanceof Field) {
        builder = builder.withTypeText(((Field) field).getType().toString()).withIcon(((Field) field).getIcon());
    }
    if (field instanceof PhpDocProperty) {
        builder = builder.withTypeText(((PhpDocProperty) field).getType().toString()).withIcon(((PhpDocProperty) field).getIcon());
    }
    if (field instanceof DasColumn) {
        DasColumn column = (DasColumn) field;
        builder = builder.withTypeText(column.getDataType().typeName, true);
        if (column.getDbParent() != null && showSchema && column.getDbParent().getDbParent() != null) {
            builder = builder.withTailText(" (" + column.getDbParent().getDbParent().getName() + "." + RemoveTablePrefix(column.getDbParent().getName(), project) + ")", true);
        }
        if (column instanceof DasColumn)
            builder = builder.withIcon(TypePresentationService.getService().getIcon(field));
        if (column instanceof DbColumnImpl)
            builder = builder.withIcon(((DbColumnImpl) column).getIcon());
    }
    if (field instanceof DasTable) {
        DasTable table = (DasTable) field;
        DasObject tableSchema = table.getDbParent();
        if (tableSchema != null) {
            if (tableSchema instanceof DbNamespaceImpl) {
                Object dataSource = tableSchema.getDbParent();
                // DbDataSourceImpl dataSource = (DbDataSourceImpl) ((DbNamespaceImpl) tableSchema).getDbParent();
                if (dataSource != null && dataSource instanceof DbDataSourceImpl) {
                    builder = builder.withTypeText(((DbDataSourceImpl) dataSource).getName(), true);
                }
                if (dataSource != null && dataSource instanceof DbDataSourceImpl) {
                    builder = builder.withTypeText(((DbDataSourceImpl) dataSource).getName(), true);
                }
            }
        }
        if (showSchema && tableSchema != null) {
            builder = builder.withTailText(" (" + table.getDbParent().getName() + ")", true);
        }
        if (table instanceof DasTable)
            builder = builder.withIcon(TypePresentationService.getService().getIcon(table));
        if (table instanceof DbElement)
            builder = builder.withIcon(((DbElement) table).getIcon());
        builder = builder.withInsertHandler((insertionContext, lookupElement) -> {
            if (Yii2SupportSettings.getInstance(project).insertWithTablePrefix) {
                Document document = insertionContext.getDocument();
                int insertPosition = insertionContext.getSelectionEndOffset();
                document.insertString(insertPosition - lookupElement.getLookupString().length(), "{{%");
                document.insertString(insertPosition + 3, "}}");
                insertionContext.getEditor().getCaretModel().getCurrentCaret().moveToOffset(insertPosition + 5);
            }
        });
    }
    return builder;
}
Also used : PhpDocComment(com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment) DasObject(com.intellij.database.model.DasObject) JBIterable(com.intellij.util.containers.JBIterable) StringLiteralExpressionImpl(com.jetbrains.php.lang.psi.elements.impl.StringLiteralExpressionImpl) com.jetbrains.php.lang.psi.elements(com.jetbrains.php.lang.psi.elements) Document(com.intellij.openapi.editor.Document) ClassConstImpl(com.jetbrains.php.lang.psi.elements.impl.ClassConstImpl) Yii2SupportSettings(com.nvlad.yii2support.utils.Yii2SupportSettings) DasTable(com.intellij.database.model.DasTable) com.intellij.database.psi(com.intellij.database.psi) TypePresentationService(com.intellij.ide.TypePresentationService) ArrayList(java.util.ArrayList) PhpDocPropertyTag(com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocPropertyTag) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) Matcher(java.util.regex.Matcher) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) TableInfo(com.nvlad.yii2support.database.TableInfo) DasColumn(com.intellij.database.model.DasColumn) PhpDocProperty(com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocProperty) LinkedHashSet(java.util.LinkedHashSet) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) Collection(java.util.Collection) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Pattern(java.util.regex.Pattern) NotNull(org.jetbrains.annotations.NotNull) DasColumn(com.intellij.database.model.DasColumn) DasObject(com.intellij.database.model.DasObject) Document(com.intellij.openapi.editor.Document) PhpDocProperty(com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocProperty) DasTable(com.intellij.database.model.DasTable) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) DasObject(com.intellij.database.model.DasObject) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

DasObject (com.intellij.database.model.DasObject)6 DasTable (com.intellij.database.model.DasTable)6 ArrayList (java.util.ArrayList)5 DasColumn (com.intellij.database.model.DasColumn)4 TableInfo (com.nvlad.yii2support.database.TableInfo)4 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)3 PhpDocProperty (com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocProperty)2 Nullable (org.jetbrains.annotations.Nullable)2 com.intellij.database.psi (com.intellij.database.psi)1 TypePresentationService (com.intellij.ide.TypePresentationService)1 Document (com.intellij.openapi.editor.Document)1 Project (com.intellij.openapi.project.Project)1 PsiElement (com.intellij.psi.PsiElement)1 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)1 JBIterable (com.intellij.util.containers.JBIterable)1 PhpDocComment (com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment)1 PhpDocPropertyTag (com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocPropertyTag)1 com.jetbrains.php.lang.psi.elements (com.jetbrains.php.lang.psi.elements)1 ClassConstImpl (com.jetbrains.php.lang.psi.elements.impl.ClassConstImpl)1 StringLiteralExpressionImpl (com.jetbrains.php.lang.psi.elements.impl.StringLiteralExpressionImpl)1