-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added index, completion and references for table names, column names …
…in db_schema.xml file
- Loading branch information
1 parent
2e1a9f5
commit df995c8
Showing
23 changed files
with
964 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/com/magento/idea/magento2plugin/completion/provider/ColumnNameCompletionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
package com.magento.idea.magento2plugin.completion.provider; | ||
|
||
import com.intellij.codeInsight.completion.CompletionParameters; | ||
import com.intellij.codeInsight.completion.CompletionProvider; | ||
import com.intellij.codeInsight.completion.CompletionResultSet; | ||
import com.intellij.codeInsight.lookup.LookupElementBuilder; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.impl.source.xml.XmlAttributeImpl; | ||
import com.intellij.psi.impl.source.xml.XmlTagImpl; | ||
import com.intellij.psi.xml.XmlTag; | ||
import com.intellij.util.ProcessingContext; | ||
import com.intellij.util.indexing.FileBasedIndex; | ||
import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml; | ||
import com.magento.idea.magento2plugin.stubs.indexes.xml.TableAndColumnNameIndex; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* Provides column names for completion. | ||
*/ | ||
public class ColumnNameCompletionProvider extends CompletionProvider<CompletionParameters> { | ||
@Override | ||
protected void addCompletions( | ||
final @NotNull CompletionParameters parameters, | ||
final @NotNull ProcessingContext context, | ||
final @NotNull CompletionResultSet result | ||
) { | ||
final PsiElement position = parameters.getPosition().getOriginalElement(); | ||
final String currentAttrName = getCurrentAttributeName(position); | ||
if (position == null || currentAttrName == null) { | ||
return; | ||
} | ||
String targetTableAttrName; | ||
|
||
switch (currentAttrName) { | ||
case ModuleDbSchemaXml.XML_ATTR_CONSTRAINT_COLUMN_NAME: | ||
targetTableAttrName = ModuleDbSchemaXml.XML_ATTR_CONSTRAINT_TABLE_NAME; | ||
break; | ||
case ModuleDbSchemaXml.XML_ATTR_CONSTRAINT_REFERENCE_COLUMN_NAME: | ||
targetTableAttrName = ModuleDbSchemaXml.XML_ATTR_CONSTRAINT_REFERENCE_TABLE_NAME; | ||
break; | ||
default: | ||
return; | ||
} | ||
final String targetTableName = getTargetTableFromPositionAndAttrName( | ||
position, | ||
targetTableAttrName | ||
); | ||
|
||
if (targetTableName == null) { | ||
return; | ||
} | ||
final Collection<String> tableAndColumnNames = FileBasedIndex.getInstance().getAllKeys( | ||
TableAndColumnNameIndex.KEY, position.getProject() | ||
); | ||
final List<String> filteredColumnNames = tableAndColumnNames.stream() | ||
.filter(name -> name.contains(targetTableName + ".")).collect(Collectors.toList()) | ||
.stream().map(name -> name.substring(name.indexOf(".") + 1)) | ||
.collect(Collectors.toList()); | ||
|
||
for (final String columnName: filteredColumnNames) { | ||
result.addElement(LookupElementBuilder.create(columnName)); | ||
} | ||
} | ||
|
||
/** | ||
* Get attribute name from position. | ||
* | ||
* @param position PsiElement | ||
* | ||
* @return String | ||
*/ | ||
private String getCurrentAttributeName(final PsiElement position) { | ||
if (position instanceof XmlAttributeImpl) { | ||
return ((XmlAttributeImpl) position).getName(); | ||
} else { | ||
return getCurrentAttributeName(position.getParent()); | ||
} | ||
} | ||
|
||
/** | ||
* Get reference table name from current position. | ||
* | ||
* @param position PsiElement | ||
* @param targetTableAttrName String | ||
* | ||
* @return String | ||
*/ | ||
private String getTargetTableFromPositionAndAttrName( | ||
final PsiElement position, | ||
final String targetTableAttrName | ||
) { | ||
if (targetTableAttrName == null) { | ||
return null; | ||
} | ||
|
||
if (position instanceof XmlTagImpl | ||
&& ((XmlTag) position).getName().equals(ModuleDbSchemaXml.XML_TAG_CONSTRAINT)) { | ||
return ((XmlTag) position) | ||
.getAttributeValue(targetTableAttrName); | ||
} else { | ||
return getTargetTableFromPositionAndAttrName( | ||
position.getParent(), | ||
targetTableAttrName | ||
); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/com/magento/idea/magento2plugin/completion/provider/TableNameCompletionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
package com.magento.idea.magento2plugin.completion.provider; | ||
|
||
import com.intellij.codeInsight.completion.CompletionParameters; | ||
import com.intellij.codeInsight.completion.CompletionProvider; | ||
import com.intellij.codeInsight.completion.CompletionResultSet; | ||
import com.intellij.codeInsight.lookup.LookupElementBuilder; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.util.ProcessingContext; | ||
import com.intellij.util.indexing.FileBasedIndex; | ||
import com.magento.idea.magento2plugin.stubs.indexes.xml.TableAndColumnNameIndex; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* Provides table names for completion. | ||
*/ | ||
public class TableNameCompletionProvider extends CompletionProvider<CompletionParameters> { | ||
@Override | ||
protected void addCompletions( | ||
final @NotNull CompletionParameters parameters, | ||
final @NotNull ProcessingContext context, | ||
final @NotNull CompletionResultSet result | ||
) { | ||
final PsiElement position = parameters.getPosition().getOriginalElement(); | ||
if (position == null) { | ||
return; | ||
} | ||
|
||
final Collection<String> tableNames = FileBasedIndex.getInstance().getAllKeys( | ||
TableAndColumnNameIndex.KEY, position.getProject() | ||
); | ||
final List<String> filteredTableNames = tableNames.stream() | ||
.filter(name -> !name.contains(".")).collect(Collectors.toList()); | ||
|
||
for (final String tableName: filteredTableNames) { | ||
result.addElement(LookupElementBuilder.create(tableName)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/com/magento/idea/magento2plugin/magento/files/ModuleDbSchemaXml.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
package com.magento.idea.magento2plugin.magento.files; | ||
|
||
import com.intellij.lang.Language; | ||
import com.intellij.lang.xml.XMLLanguage; | ||
|
||
public class ModuleDbSchemaXml implements ModuleFileInterface { | ||
private static final ModuleDbSchemaXml INSTANCE = new ModuleDbSchemaXml(); | ||
public static final String FILE_NAME = "db_schema.xml"; | ||
public static final String TEMPLATE = "Magento Module Declarative Schema XML"; | ||
|
||
//attributes | ||
public static final String XML_ATTR_TABLE_NAME = "name"; | ||
public static final String XML_ATTR_COLUMN_NAME = "name"; | ||
public static final String XML_ATTR_CONSTRAINT_TABLE_NAME = "table"; | ||
public static final String XML_ATTR_CONSTRAINT_REFERENCE_TABLE_NAME = "referenceTable"; | ||
public static final String XML_ATTR_CONSTRAINT_COLUMN_NAME = "column"; | ||
public static final String XML_ATTR_CONSTRAINT_REFERENCE_COLUMN_NAME = "referenceColumn"; | ||
//tags | ||
public static final String XML_TAG_SCHEMA = "schema"; | ||
public static final String XML_TAG_TABLE = "table"; | ||
public static final String XML_TAG_COLUMN = "column"; | ||
public static final String XML_TAG_CONSTRAINT = "constraint"; | ||
|
||
public static ModuleDbSchemaXml getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public String getFileName() { | ||
return FILE_NAME; | ||
} | ||
|
||
@Override | ||
public String getTemplate() { | ||
return TEMPLATE; | ||
} | ||
|
||
@Override | ||
public Language getLanguage() { | ||
return XMLLanguage.INSTANCE; | ||
} | ||
} |
Oops, something went wrong.