Skip to content

Commit 0b7ac60

Browse files
committed
Add legacy classes for IDE index
1 parent 4c002c5 commit 0b7ac60

File tree

4 files changed

+250
-0
lines changed

4 files changed

+250
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package com.cedricziel.idea.typo3.index.php;
2+
3+
import com.cedricziel.idea.typo3.index.externalizer.ObjectStreamDataExternalizer;
4+
import com.intellij.openapi.project.Project;
5+
import com.intellij.patterns.PlatformPatterns;
6+
import com.intellij.psi.PsiElement;
7+
import com.intellij.psi.PsiRecursiveElementVisitor;
8+
import com.intellij.psi.search.GlobalSearchScope;
9+
import com.intellij.util.indexing.*;
10+
import com.intellij.util.io.DataExternalizer;
11+
import com.intellij.util.io.EnumeratorStringDescriptor;
12+
import com.intellij.util.io.KeyDescriptor;
13+
import com.jetbrains.php.lang.psi.elements.ClassReference;
14+
import com.jetbrains.php.lang.psi.elements.PhpClass;
15+
import gnu.trove.THashMap;
16+
import org.jetbrains.annotations.NotNull;
17+
import org.jetbrains.annotations.Nullable;
18+
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
public class LegacyClassesForIDEIndex extends FileBasedIndexExtension<String, String> {
23+
24+
public static ID<String, String> KEY = ID.create("com.cedricziel.idea.typo3.index.php.legacy_classes");
25+
26+
private final KeyDescriptor<String> myKeyDescriptor = new EnumeratorStringDescriptor();
27+
28+
public static boolean isLegacyClass(@NotNull Project project, @NotNull String fqn) {
29+
30+
return FileBasedIndex.getInstance().getAllKeys(LegacyClassesForIDEIndex.KEY, project).contains(fqn);
31+
}
32+
33+
@Nullable
34+
public static String findReplacementClass(@NotNull Project project, @NotNull String fqn) {
35+
List<String> values = FileBasedIndex.getInstance().getValues(KEY, fqn, GlobalSearchScope.allScope(project));
36+
if (values.size() > 0) {
37+
return values.iterator().next();
38+
}
39+
40+
return null;
41+
}
42+
43+
@NotNull
44+
@Override
45+
public ID<String, String> getName() {
46+
return KEY;
47+
}
48+
49+
@NotNull
50+
@Override
51+
public DataIndexer<String, String, FileContent> getIndexer() {
52+
return inputData -> {
53+
Map<String, String> map = new THashMap<>();
54+
55+
LegacyClassesRecursiveVisitor visitor = new LegacyClassesRecursiveVisitor();
56+
visitor.visitElement(inputData.getPsiFile());
57+
58+
visitor.getMap().forEach(map::put);
59+
60+
return map;
61+
};
62+
}
63+
64+
@NotNull
65+
@Override
66+
public KeyDescriptor<String> getKeyDescriptor() {
67+
return myKeyDescriptor;
68+
}
69+
70+
@NotNull
71+
@Override
72+
public DataExternalizer<String> getValueExternalizer() {
73+
return new ObjectStreamDataExternalizer<>();
74+
}
75+
76+
@Override
77+
public int getVersion() {
78+
return 0;
79+
}
80+
81+
@NotNull
82+
@Override
83+
public FileBasedIndex.InputFilter getInputFilter() {
84+
return file -> file.getName().equals("LegacyClassesForIde.php");
85+
}
86+
87+
@Override
88+
public boolean dependsOnFileContent() {
89+
return true;
90+
}
91+
92+
private static class LegacyClassesRecursiveVisitor extends PsiRecursiveElementVisitor {
93+
private final Map<String, String> map;
94+
95+
LegacyClassesRecursiveVisitor() {
96+
map = new THashMap<>();
97+
}
98+
99+
public Map<String, String> getMap() {
100+
return map;
101+
}
102+
103+
@Override
104+
public void visitElement(PsiElement element) {
105+
106+
if (!PlatformPatterns.psiElement(PhpClass.class).accepts(element)) {
107+
super.visitElement(element);
108+
return;
109+
}
110+
111+
PhpClass phpClass = (PhpClass) element;
112+
113+
String fqn = phpClass.getFQN();
114+
115+
String superFqn = null;
116+
if (!phpClass.isInterface()) {
117+
superFqn = phpClass.getSuperFQN();
118+
} else {
119+
List<ClassReference> referenceElements = phpClass.getExtendsList().getReferenceElements();
120+
for (ClassReference cr : referenceElements) {
121+
superFqn = cr.getFQN();
122+
}
123+
}
124+
125+
if (superFqn != null) {
126+
map.put(fqn, superFqn);
127+
}
128+
129+
super.visitElement(element);
130+
}
131+
}
132+
}

src/main/resources/META-INF/plugin.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ It is a great inspiration for possible solutions and parts of the code.</p>
302302
<fileBasedIndex implementation="com.cedricziel.idea.typo3.index.ResourcePathIndex"/>
303303
<fileBasedIndex implementation="com.cedricziel.idea.typo3.index.RouteIndex"/>
304304
<fileBasedIndex implementation="com.cedricziel.idea.typo3.index.TablenameFileIndex"/>
305+
<fileBasedIndex implementation="com.cedricziel.idea.typo3.index.php.LegacyClassesForIDEIndex"/>
306+
305307

306308
<!-- completion -->
307309
<completion.contributor language="PHP"
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.cedricziel.idea.typo3.index.php;
2+
3+
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
4+
5+
public class LegacyClassesForIDEIndexTest extends LightCodeInsightFixtureTestCase {
6+
7+
@Override
8+
protected String getTestDataPath() {
9+
return "testData/com/cedricziel/idea/typo3/index";
10+
}
11+
12+
13+
@Override
14+
public void setUp() throws Exception {
15+
super.setUp();
16+
17+
myFixture.configureByFile("LegacyClassesForIde.php");
18+
}
19+
20+
public void testLegacyClassesForIDEFilesAreIndexed() {
21+
assertTrue(LegacyClassesForIDEIndex.isLegacyClass(myFixture.getProject(), "\\TYPO3\\CMS\\Fluid\\Core\\Exception"));
22+
assertTrue(LegacyClassesForIDEIndex.isLegacyClass(myFixture.getProject(), "\\TYPO3\\CMS\\Fluid\\Core\\ViewHelper\\ViewHelperInterface"));
23+
}
24+
25+
public void testReplacementClassNamesCanBeRetrieved() {
26+
assertEquals(LegacyClassesForIDEIndex.findReplacementClass(myFixture.getProject(), "\\TYPO3\\CMS\\Fluid\\Core\\ViewHelper\\ViewHelperInterface"), "\\TYPO3Fluid\\Fluid\\Core\\ViewHelper\\ViewHelperInterface");
27+
assertNull(LegacyClassesForIDEIndex.findReplacementClass(myFixture.getProject(), "\\TYPO3\\CMS\\NoRealClass"));
28+
}
29+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
namespace {
3+
die('Access denied');
4+
}
5+
namespace TYPO3\CMS\Fluid\Core\Parser {
6+
interface InterceptorInterface extends \TYPO3Fluid\Fluid\Core\Parser\InterceptorInterface
7+
{
8+
}
9+
}
10+
namespace TYPO3\CMS\Fluid\Core\Parser\SyntaxTree {
11+
interface NodeInterface extends \TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\NodeInterface
12+
{
13+
}
14+
}
15+
namespace TYPO3\CMS\Fluid\Core\Rendering {
16+
interface RenderingContextInterface extends \TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface
17+
{
18+
}
19+
}
20+
namespace TYPO3\CMS\Fluid\Core\ViewHelper {
21+
interface ViewHelperInterface extends \TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInterface
22+
{
23+
}
24+
}
25+
namespace TYPO3\CMS\Fluid\Core\ViewHelper\Facets {
26+
interface ChildNodeAccessInterface extends \TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInterface
27+
{
28+
}
29+
interface CompilableInterface extends \TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInterface
30+
{
31+
}
32+
interface PostParseInterface extends \TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInterface
33+
{
34+
}
35+
}
36+
namespace TYPO3\CMS\Fluid\Core {
37+
class Exception extends \TYPO3Fluid\Fluid\Core\Exception
38+
{
39+
}
40+
}
41+
namespace TYPO3\CMS\Fluid\Core\ViewHelper {
42+
class Exception extends \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
43+
{
44+
}
45+
}
46+
namespace TYPO3\CMS\Fluid\Core\ViewHelper\Exception {
47+
class InvalidVariableException extends \TYPO3Fluid\Fluid\Core\Exception
48+
{
49+
}
50+
}
51+
namespace TYPO3\CMS\Fluid\View {
52+
class Exception extends \TYPO3Fluid\Fluid\View\Exception
53+
{
54+
}
55+
}
56+
namespace TYPO3\CMS\Fluid\View\Exception {
57+
class InvalidSectionException extends \TYPO3Fluid\Fluid\View\Exception\InvalidSectionException
58+
{
59+
}
60+
class InvalidTemplateResourceException extends \TYPO3Fluid\Fluid\View\Exception\InvalidTemplateResourceException
61+
{
62+
}
63+
}
64+
namespace TYPO3\CMS\Fluid\Core\Compiler {
65+
class TemplateCompiler extends \TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler
66+
{
67+
}
68+
}
69+
namespace TYPO3\CMS\Fluid\Core\Parser\SyntaxTree {
70+
class RootNode extends \TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\RootNode
71+
{
72+
}
73+
class ViewHelperNode extends \TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ViewHelperNode
74+
{
75+
}
76+
}
77+
namespace TYPO3\CMS\Fluid\Core\ViewHelper {
78+
class TemplateVariableContainer extends \TYPO3Fluid\Fluid\Core\Variables\StandardVariableProvider
79+
{
80+
}
81+
class ViewHelperVariableContainer extends \TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer
82+
{
83+
}
84+
class TagBuilder extends \TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder
85+
{
86+
}
87+
}

0 commit comments

Comments
 (0)