|
1 | 1 | package com.cedricziel.idea.typo3.util; |
2 | 2 |
|
| 3 | +import com.cedricziel.idea.typo3.index.TablenameFileIndex; |
3 | 4 | import com.intellij.codeInsight.completion.CompletionResultSet; |
4 | 5 | import com.intellij.codeInsight.lookup.LookupElement; |
5 | | -import com.intellij.openapi.fileEditor.impl.LoadTextUtil; |
6 | 6 | import com.intellij.openapi.project.Project; |
7 | 7 | import com.intellij.psi.PsiElement; |
8 | 8 | import com.intellij.psi.PsiFile; |
| 9 | +import com.intellij.psi.PsiManager; |
9 | 10 | import com.intellij.psi.search.FilenameIndex; |
10 | 11 | import com.intellij.psi.search.GlobalSearchScope; |
| 12 | +import com.intellij.util.indexing.FileBasedIndex; |
11 | 13 | import org.jetbrains.annotations.NotNull; |
12 | 14 |
|
13 | | -import java.util.Arrays; |
14 | 15 | import java.util.HashSet; |
15 | | -import java.util.Objects; |
16 | 16 | import java.util.Set; |
17 | | -import java.util.regex.Matcher; |
18 | | -import java.util.regex.Pattern; |
19 | 17 |
|
20 | 18 | public class TableUtil { |
21 | 19 |
|
22 | 20 | public static final String EXT_TABLES_SQL_FILENAME = "ext_tables.sql"; |
23 | 21 |
|
24 | 22 | public static Set<String> getAvailableTableNames(@NotNull Project project) { |
25 | | - PsiFile[] extSqlFiles = getExtTablesSqlFilesInProject(project); |
26 | 23 |
|
27 | | - Set<String> tableNames = new HashSet<>(); |
28 | | - |
29 | | - for (PsiFile psiFile : extSqlFiles) { |
| 24 | + return new HashSet<>(FileBasedIndex.getInstance().getAllKeys(TablenameFileIndex.KEY, project)); |
| 25 | + } |
30 | 26 |
|
31 | | - if (psiFile != null) { |
| 27 | + public static PsiElement[] getTableDefinitionElements(@NotNull String tableName, @NotNull Project project) { |
32 | 28 |
|
33 | | - CharSequence charSequence = LoadTextUtil.loadText(psiFile.getVirtualFile()); |
| 29 | + PsiFile[] extTablesSqlFilesInProjectContainingTable = getExtTablesSqlFilesInProjectContainingTable(tableName, project); |
| 30 | + Set<PsiElement> elements = new HashSet<>(); |
34 | 31 |
|
35 | | - final Matcher matcher = Pattern |
36 | | - .compile("create\\s+table\\s+(if\\s+not\\s+exists\\s+)?([a-zA-Z_0-9]+)", Pattern.CASE_INSENSITIVE) |
37 | | - .matcher(charSequence); |
| 32 | + PsiManager psiManager = PsiManager.getInstance(project); |
38 | 33 |
|
39 | | - try { |
40 | | - while (matcher.find()) { |
41 | | - if (matcher.groupCount() < 2) { |
42 | | - return tableNames; |
43 | | - } |
| 34 | + for (PsiFile virtualFile : extTablesSqlFilesInProjectContainingTable) { |
| 35 | + FileBasedIndex.getInstance().processValues(TablenameFileIndex.KEY, tableName, virtualFile.getVirtualFile(), (file, value) -> { |
44 | 36 |
|
45 | | - tableNames.add(matcher.group(2)); |
| 37 | + PsiFile file1 = psiManager.findFile(file); |
| 38 | + if (file1 != null) { |
| 39 | + PsiElement elementAt = file1.findElementAt(value.getEndOffset() - 2); |
| 40 | + if (elementAt != null) { |
| 41 | + elements.add(elementAt); |
46 | 42 | } |
47 | | - } catch (IllegalStateException e) { |
48 | | - // no matches |
49 | 43 | } |
50 | | - } |
| 44 | + |
| 45 | + return true; |
| 46 | + }, GlobalSearchScope.allScope(project)); |
51 | 47 | } |
52 | 48 |
|
53 | | - return tableNames; |
| 49 | + return elements.toArray(new PsiElement[elements.size()]); |
54 | 50 | } |
55 | 51 |
|
56 | | - public static PsiElement[] getTableDefinitionElements(@NotNull String tableName, @NotNull Project project) { |
| 52 | + private static PsiFile[] getExtTablesSqlFilesInProjectContainingTable(@NotNull String tableName, @NotNull Project project) { |
| 53 | + Set<String> set = new HashSet<>(); |
| 54 | + set.add(tableName); |
57 | 55 |
|
58 | | - PsiFile[] extTablesSqlFilesInProjectContainingTable = getExtTablesSqlFilesInProjectContainingTable(tableName, project); |
| 56 | + Set<PsiFile> files = new HashSet<>(); |
| 57 | + PsiManager psiManager = PsiManager.getInstance(project); |
59 | 58 |
|
60 | | - return Arrays |
61 | | - .stream(extTablesSqlFilesInProjectContainingTable) |
62 | | - .map(file -> { |
63 | | - CharSequence charSequence = LoadTextUtil.loadText(file.getVirtualFile()); |
64 | | - |
65 | | - final Matcher matcher = Pattern |
66 | | - .compile("create\\s+table\\s+(if\\s+not\\s+exists\\s+)?([a-zA-Z_0-9]+)", Pattern.CASE_INSENSITIVE) |
67 | | - .matcher(charSequence); |
68 | | - try { |
69 | | - while (matcher.find()) { |
70 | | - if (matcher.groupCount() < 2) { |
71 | | - continue; |
72 | | - } |
73 | | - |
74 | | - final String foundTable = matcher.group(2); |
75 | | - if (!foundTable.equals(tableName)) { |
76 | | - continue; |
77 | | - } |
78 | | - |
79 | | - return file.findElementAt(matcher.end() - 2); |
80 | | - } |
81 | | - } catch (IllegalStateException e) { |
82 | | - // do nothing |
83 | | - } |
| 59 | + FileBasedIndex.getInstance().getFilesWithKey(TablenameFileIndex.KEY, set, virtualFile -> { |
84 | 60 |
|
85 | | - return null; |
86 | | - }) |
87 | | - .filter(Objects::nonNull) |
88 | | - .toArray(PsiElement[]::new); |
89 | | - } |
| 61 | + files.add(psiManager.findFile(virtualFile)); |
90 | 62 |
|
91 | | - private static PsiFile[] getExtTablesSqlFilesInProjectContainingTable(@NotNull String tableName, @NotNull Project project) { |
| 63 | + return true; |
| 64 | + }, GlobalSearchScope.allScope(project)); |
92 | 65 |
|
93 | | - return Arrays |
94 | | - .stream(getExtTablesSqlFilesInProject(project)) |
95 | | - .filter(Objects::nonNull) |
96 | | - .filter(file -> LoadTextUtil.loadText(file.getVirtualFile()).toString().contains(tableName)) |
97 | | - .toArray(PsiFile[]::new); |
| 66 | + return files.toArray(new PsiFile[files.size()]); |
98 | 67 | } |
99 | 68 |
|
100 | 69 | @NotNull |
|
0 commit comments