Skip to content

600: Developed configurations autocompletion #1077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,11 @@
<projectService serviceImplementation="com.magento.idea.magento2plugin.project.Settings"/>
<projectService serviceImplementation="com.magento.idea.magento2uct.settings.UctSettingsService"/>

<completion.contributor language="XML" implementationClass="com.magento.idea.magento2plugin.completion.xml.XmlCompletionContributor" id="xml" />
<completion.contributor language="PHP" implementationClass="com.magento.idea.magento2plugin.completion.php.PhpCompletionContributor" id="php" />
<completion.contributor language="XML" implementationClass="com.magento.idea.magento2plugin.completion.xml.XmlCompletionContributor" id="xml"/>
<completion.contributor language="PHP" implementationClass="com.magento.idea.magento2plugin.completion.php.PhpCompletionContributor" id="php"/>

<completion.contributor language="XML" implementationClass="com.magento.idea.magento2plugin.completion.xml.SystemConfigurationContributor" id="magentoSystemConfiguration"/>
<xml.tagNameProvider implementation="com.magento.idea.magento2plugin.completion.provider.xml.ConfigXmlTagNameProvider"/>

<psi.referenceContributor language="XML" implementation="com.magento.idea.magento2plugin.reference.xml.XmlReferenceContributor"/>
<psi.referenceContributor language="PHP" implementation="com.magento.idea.magento2plugin.reference.php.PhpReferenceContributor"/>
Expand Down Expand Up @@ -209,6 +212,9 @@
<fileBasedIndex implementation="com.magento.idea.magento2plugin.stubs.indexes.xml.DeclarativeSchemaElementsIndex" />
<fileBasedIndex implementation="com.magento.idea.magento2plugin.stubs.indexes.xml.UIComponentIndex" />
<fileBasedIndex implementation="com.magento.idea.magento2plugin.stubs.indexes.xml.ProductTypeIndex" />
<fileBasedIndex implementation="com.magento.idea.magento2plugin.stubs.indexes.xml.SystemXmlSectionIndex" />
<fileBasedIndex implementation="com.magento.idea.magento2plugin.stubs.indexes.xml.SystemXmlGroupIndex" />
<fileBasedIndex implementation="com.magento.idea.magento2plugin.stubs.indexes.xml.SystemXmlFieldIndex" />

<codeInsight.lineMarkerProvider language="PHP" implementationClass="com.magento.idea.magento2plugin.linemarker.php.PluginLineMarkerProvider"/>
<codeInsight.lineMarkerProvider language="PHP" implementationClass="com.magento.idea.magento2plugin.linemarker.php.PluginTargetLineMarkerProvider"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.completion.provider.xml;

import static com.magento.idea.magento2plugin.magento.files.ModuleConfigXmlFile.FILE_NAME;
import static com.magento.idea.magento2plugin.magento.files.ModuleConfigXmlFile.ROOT_TAG_NAME;

import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.openapi.project.Project;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
import com.intellij.xml.XmlTagNameProvider;
import com.magento.idea.magento2plugin.project.Settings;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class ConfigXmlTagNameProvider implements XmlTagNameProvider {

@Override
public void addTagNameVariants(
final List<LookupElement> elements,
final @NotNull XmlTag tag,
final String prefix
) {
if (!Settings.isEnabled(tag.getProject()) || !validate(tag)) {
return;
}
final int tagDepth = getTagDepth(tag);

if (tagDepth == -1) {
return;
}
final ConfigTagScope tagScope = getTagScope(tag);

if (tagScope == null) {
return;
}
final ConfigTagTypeByScopedDepth configType = ConfigTagTypeByScopedDepth
.getTagTypeByScopeAndDepth(tagDepth, tagScope);

if (configType == null) {
return;
}
final String indexKey = configType.getConfigPath(tag);
final String phrase = indexKey.isEmpty() ? prefix : indexKey + "." + prefix;

elements.addAll(configType.makeCompletion(phrase, tag.getProject()));
}

private boolean validate(final @NotNull XmlTag tag) {
if (!(tag.getContainingFile() instanceof XmlFile)) {
return false;
}
final XmlFile file = (XmlFile) tag.getContainingFile();
final XmlTag rootTag = file.getRootTag();

if (!FILE_NAME.equals(file.getName())
|| rootTag == null
|| !ROOT_TAG_NAME.equals(rootTag.getName())) {
return false;
}
final int tagDepth = getTagDepth(tag);

return tagDepth >= 2 && tagDepth <= 5;
}

private int getTagDepth(final @NotNull XmlTag tag) {
if (!(tag.getContainingFile() instanceof XmlFile)) {
return -1;
}
final XmlFile file = (XmlFile) tag.getContainingFile();
final XmlTag rootTag = file.getRootTag();

if (rootTag == null) {
return -1;
}
int depth = 0;
XmlTag iterable = tag;

while (!iterable.equals(rootTag)) {
depth++;
iterable = iterable.getParentTag();

if (iterable == null) {
break;
}
}

return depth;
}

private @Nullable ConfigTagScope getTagScope(final @NotNull XmlTag tag) {
if (!(tag.getContainingFile() instanceof XmlFile)) {
return null;
}
final XmlFile file = (XmlFile) tag.getContainingFile();
final XmlTag rootTag = file.getRootTag();

if (rootTag == null) {
return null;
}
XmlTag scopeTag = null;
XmlTag iterable = tag;

while (!iterable.equals(rootTag)) {
scopeTag = iterable;
iterable = iterable.getParentTag();

if (iterable == null) {
break;
}
}

if (scopeTag == null) {
return null;
}

return ConfigTagScope.getScopeByTagName(scopeTag.getName());
}

private enum ConfigTagTypeByScopedDepth {

SECTION(2, 3, 3, 0),
GROUP(3, 4, 4, 1),
FIELD(4, 5, 5, 2);

private final int defaultScope;
private final int websitesScope;
private final int storesScope;
private final int fallbackDepth;

ConfigTagTypeByScopedDepth(
final int defaultScope,
final int websitesScope,
final int storesScope,
final int fallbackDepth
) {
this.defaultScope = defaultScope;
this.websitesScope = websitesScope;
this.storesScope = storesScope;
this.fallbackDepth = fallbackDepth;
}

public static ConfigTagTypeByScopedDepth getTagTypeByScopeAndDepth(
final int depth,
final ConfigTagScope scope
) {
for (final ConfigTagTypeByScopedDepth tagType : ConfigTagTypeByScopedDepth.values()) {
int depthForScope = -1;

if (ConfigTagScope.DEFAULT.equals(scope)) {
depthForScope = tagType.defaultScope;
} else if (ConfigTagScope.WEBSITES.equals(scope)) {
depthForScope = tagType.websitesScope;
} else if (ConfigTagScope.STORES.equals(scope)) {
depthForScope = tagType.storesScope;
}

if (depth == depthForScope) {
return tagType;
}
}

return null;
}

public @NotNull String getConfigPath(final @NotNull XmlTag tag) {
final List<String> parts = new ArrayList<>();
XmlTag iterable = tag.getParentTag();

for (int i = 0; i < fallbackDepth; i++) {
if (iterable != null) {
parts.add(iterable.getName());
iterable = iterable.getParentTag();
}
}

if (parts.isEmpty()) {
return "";
}
Collections.reverse(parts);

return String.join(".", parts);
}

public List<LookupElement> makeCompletion(
final @NotNull String phrase,
final @NotNull Project project
) {
if (this.equals(ConfigTagTypeByScopedDepth.SECTION)) {
return SectionNameCompletionProvider.makeCompletion(phrase, project);
} else if (this.equals(ConfigTagTypeByScopedDepth.GROUP)) {
return GroupNameCompletionProvider.makeCompletion(phrase, project);
} else if (this.equals(ConfigTagTypeByScopedDepth.FIELD)) {
return FieldNameCompletionProvider.makeCompletion(phrase, project);
}

return new ArrayList<>();
}
}

private enum ConfigTagScope {

DEFAULT("default"),
WEBSITES("websites"),
STORES("stores");

private final String scopeTagName;

ConfigTagScope(final String scopeTagName) {
this.scopeTagName = scopeTagName;
}

public static ConfigTagScope getScopeByTagName(final @NotNull String tagName) {
for (final ConfigTagScope scope : ConfigTagScope.values()) {
if (tagName.equals(scope.scopeTagName)) {
return scope;
}
}

return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.completion.provider.xml;

import com.intellij.codeInsight.completion.CompletionParameters;
import com.intellij.codeInsight.completion.CompletionProvider;
import com.intellij.codeInsight.completion.CompletionResultSet;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.XmlTag;
import com.intellij.util.ProcessingContext;
import com.intellij.util.indexing.FileBasedIndex;
import com.magento.idea.magento2plugin.MagentoIcons;
import com.magento.idea.magento2plugin.project.Settings;
import com.magento.idea.magento2plugin.stubs.indexes.xml.SystemXmlFieldIndex;
import com.magento.idea.magento2plugin.util.magento.xml.SystemConfigurationParserUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.jetbrains.annotations.NotNull;

public class FieldNameCompletionProvider 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 || !Settings.isEnabled(position.getProject())) {
return;
}
final XmlTag startingTag = PsiTreeUtil.getParentOfType(
position,
XmlTag.class
);

if (startingTag == null) {
return;
}
final String currentPath = SystemConfigurationParserUtil
.parseOuterConfigPath(
startingTag,
SystemConfigurationParserUtil.ParsingDepth.GROUP_ID
);

if (currentPath == null) {
return;
}
final String prefix = result.getPrefixMatcher().getPrefix().trim();
final String capture = currentPath + "." + prefix;

for (final LookupElement element : makeCompletion(capture, position.getProject())) {
result.addElement(element);
}
}

/**
* Make completion for fields.
*
* @param phrase String
* @param project Project
*
* @return List[LookupElement]
*/
public static List<LookupElement> makeCompletion(
final @NotNull String phrase,
final @NotNull Project project
) {
final List<LookupElement> result = new ArrayList<>();
final FileBasedIndex index = FileBasedIndex.getInstance();
final Collection<String> allKeys = index.getAllKeys(SystemXmlFieldIndex.KEY, project);
final AtomicInteger resultsCounter = new AtomicInteger(0);

allKeys.stream()
.filter(input -> input.startsWith(phrase) && !input.equals(phrase))
.takeWhile(input -> resultsCounter.get() < 5)
.forEach(
input -> {
if (index.getValues(
SystemXmlFieldIndex.KEY,
input,
GlobalSearchScope.allScope(project)).isEmpty()
) {
return;
}
final String[] pathParts = input.split("\\.");

if (pathParts.length != 3) { //NOPMD
return;
}
final String fieldId = pathParts[2];
result.add(
LookupElementBuilder.create(fieldId)
.withIcon(MagentoIcons.MODULE)
);
resultsCounter.incrementAndGet();
}
);

return result;
}
}
Loading