-
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.
Merge pull request #1038 from bohdan-harniuk/uct-1030-add-config-file…
…s-support UCT-1030: Added configuration files support
- Loading branch information
Showing
24 changed files
with
788 additions
and
28 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
6 changes: 6 additions & 0 deletions
6
resources/inspectionDescriptions/UsedDeprecatedConstantInConfig.html
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,6 @@ | ||
<html> | ||
<body> | ||
<p>[1234] Using Magento 2 @deprecated constant: consider using Magento Open Source|Adobe Commerce constant marked as @api instead.</p> | ||
<!-- tooltip end --> | ||
</body> | ||
</html> |
6 changes: 6 additions & 0 deletions
6
resources/inspectionDescriptions/UsedDeprecatedMethodInConfig.html
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,6 @@ | ||
<html> | ||
<body> | ||
<p>[1439] Using Magento 2 @deprecated method: this method will be removed in upcoming versions. Consider relying on methods declared in API interfaces instead.</p> | ||
<!-- tooltip end --> | ||
</body> | ||
</html> |
6 changes: 6 additions & 0 deletions
6
resources/inspectionDescriptions/UsedDeprecatedTypeInConfig.html
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,6 @@ | ||
<html> | ||
<body> | ||
<p>[1134] Using Magento 2 @deprecated type: consider using Magento Open Source|Adobe Commerce type marked as @api instead.</p> | ||
<!-- tooltip end --> | ||
</body> | ||
</html> |
6 changes: 6 additions & 0 deletions
6
resources/inspectionDescriptions/UsedNonExistentConstantInConfig.html
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,6 @@ | ||
<html> | ||
<body> | ||
<p>[1214] The used constant is no longer present in the codebase.</p> | ||
<!-- tooltip end --> | ||
</body> | ||
</html> |
6 changes: 6 additions & 0 deletions
6
resources/inspectionDescriptions/UsedNonExistentMethodInConfig.html
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,6 @@ | ||
<html> | ||
<body> | ||
<p>[1410] The used method is no longer present in the codebase.</p> | ||
<!-- tooltip end --> | ||
</body> | ||
</html> |
6 changes: 6 additions & 0 deletions
6
resources/inspectionDescriptions/UsedNonExistentTypeInConfig.html
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,6 @@ | ||
<html> | ||
<body> | ||
<p>[1110] The used type is no longer present in the codebase.</p> | ||
<!-- tooltip end --> | ||
</body> | ||
</html> |
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
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
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
151 changes: 151 additions & 0 deletions
151
src/com/magento/idea/magento2uct/inspections/xml/ModuleConfigFileInspection.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,151 @@ | ||
/* | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
package com.magento.idea.magento2uct.inspections.xml; | ||
|
||
import com.intellij.codeInspection.InspectionManager; | ||
import com.intellij.codeInspection.ProblemDescriptor; | ||
import com.intellij.codeInspection.ProblemsHolder; | ||
import com.intellij.codeInspection.XmlSuppressableInspectionTool; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.vfs.VirtualFile; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiElementVisitor; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.psi.tree.IElementType; | ||
import com.intellij.psi.util.PsiTreeUtil; | ||
import com.intellij.psi.xml.XmlToken; | ||
import com.intellij.psi.xml.XmlTokenType; | ||
import com.magento.idea.magento2uct.settings.UctSettingsService; | ||
import com.magento.idea.magento2uct.versioning.VersionStateManager; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
abstract class ModuleConfigFileInspection extends XmlSuppressableInspectionTool { | ||
|
||
private static final String LAYOUT_FILE_REGEX = "\\/(layout|ui_component)\\/.*\\.xml"; | ||
private static final Pattern LAYOUT_FILE_PATTERN = Pattern.compile(LAYOUT_FILE_REGEX); | ||
|
||
private final String[] supportedFiles = { | ||
"di.xml", | ||
"system.xml", | ||
"events.xml", | ||
"extension_attributes.xml", | ||
"webapi.xml", | ||
"communication.xml", | ||
"queue_consumer.xml", | ||
"crontab.xml", | ||
"indexer.xml", | ||
"mview.xml", | ||
"product_types.xml", | ||
"widget.xml", | ||
"queue.xml", | ||
"product_options.xml", | ||
"export.xml", | ||
"import.xml", | ||
"analytics.xml", | ||
"reports.xml", | ||
"pdf.xml", | ||
"cache.xml", | ||
"validation.xml" | ||
}; | ||
private ProblemsHolder problemsHolder; | ||
|
||
@Override | ||
public @NotNull PsiElementVisitor buildVisitor( | ||
final @NotNull ProblemsHolder holder, | ||
final boolean isOnTheFly | ||
) { | ||
problemsHolder = holder; | ||
|
||
return super.buildVisitor(holder, isOnTheFly); | ||
} | ||
|
||
@Override | ||
public @Nullable ProblemDescriptor[] checkFile( | ||
final @NotNull PsiFile file, | ||
final @NotNull InspectionManager manager, | ||
final boolean isOnTheFly | ||
) { | ||
final Project project = file.getProject(); | ||
final UctSettingsService settings = UctSettingsService.getInstance(project); | ||
final ProblemsHolder holder = getProblemsHolder(); | ||
|
||
if (!settings.isEnabled() || holder == null) { | ||
return getEmptyResult(); | ||
} | ||
|
||
if (Arrays.stream(supportedFiles).noneMatch(name -> name.equals(file.getName())) | ||
&& !isLayoutFile(file)) { | ||
return getEmptyResult(); | ||
} | ||
final List<IElementType> allowedTokenTypes = new ArrayList<>(); | ||
allowedTokenTypes.add(XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN); | ||
allowedTokenTypes.add(XmlTokenType.XML_DATA_CHARACTERS); | ||
|
||
final List<ProblemDescriptor> descriptors = new ArrayList<>(); | ||
|
||
for (final XmlToken token : PsiTreeUtil.findChildrenOfType(file, XmlToken.class)) { | ||
if (!allowedTokenTypes.contains(token.getTokenType())) { | ||
continue; | ||
} | ||
final String fqn = token.getText().trim(); | ||
|
||
if (!VersionStateManager.getInstance(project).isPresentInCodebase(fqn)) { | ||
continue; | ||
} | ||
// Inspection logic. | ||
doInspection(fqn, token, manager, holder, isOnTheFly, descriptors); | ||
} | ||
|
||
return descriptors.toArray(new ProblemDescriptor[0]); | ||
} | ||
|
||
/** | ||
* Implement this method to specify inspection logic. | ||
* | ||
* @param fqn String | ||
* @param target PsiElement | ||
* @param manager InspectionManager | ||
* @param holder ProblemsHolder | ||
* @param isOnTheFly boolean | ||
* @param descriptors List[ProblemDescriptor] | ||
*/ | ||
protected abstract void doInspection( | ||
final @NotNull String fqn, | ||
final @NotNull PsiElement target, | ||
final @NotNull InspectionManager manager, | ||
final @NotNull ProblemsHolder holder, | ||
final boolean isOnTheFly, | ||
final @NotNull List<ProblemDescriptor> descriptors | ||
); | ||
|
||
private @Nullable ProblemsHolder getProblemsHolder() { | ||
return problemsHolder; | ||
} | ||
|
||
/** | ||
* Retrieves an empty result. | ||
* | ||
* @return ProblemDescriptor[] | ||
*/ | ||
private ProblemDescriptor[] getEmptyResult() { | ||
return new ProblemDescriptor[0]; | ||
} | ||
|
||
private boolean isLayoutFile(final @NotNull PsiFile file) { | ||
final VirtualFile virtualFile = file.getVirtualFile(); | ||
|
||
if (virtualFile == null) { | ||
return false; | ||
} | ||
|
||
return LAYOUT_FILE_PATTERN.matcher(virtualFile.getPath()).find(); | ||
} | ||
} |
Oops, something went wrong.