Skip to content

Commit 823236e

Browse files
authored
Merge pull request #201 from cedricziel/quoted-table-names
Expand table name regex to match quoted identifiers
2 parents c176101 + 207c294 commit 823236e

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

typo3-cms/src/main/java/com/cedricziel/idea/typo3/index/TablenameFileIndex.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import java.util.regex.Matcher;
1515
import java.util.regex.Pattern;
1616

17-
public class TablenameFileIndex extends FileBasedIndexExtension <String, TextRange> {
17+
public class TablenameFileIndex extends FileBasedIndexExtension<String, TextRange> {
1818

1919
public static ID<String, TextRange> KEY = ID.create("com.cedricziel.idea.typo3.index.tablename");
2020

@@ -33,16 +33,22 @@ public DataIndexer<String, TextRange, FileContent> getIndexer() {
3333
CharSequence charSequence = LoadTextUtil.loadText(inputData.getFile());
3434

3535
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);
36+
.compile("create\\s+table\\s+(if\\s+not\\s+exists\\s+)?([a-zA-Z_0-9]+)?(`[a-zA-Z_0-9]+`)?", Pattern.CASE_INSENSITIVE)
37+
.matcher(charSequence);
3838

3939
try {
4040
while (matcher.find()) {
4141
if (matcher.groupCount() < 2) {
4242
return map;
4343
}
4444

45-
map.put(matcher.group(2), new TextRange(matcher.start(), matcher.end()));
45+
if (matcher.group(2) != null) {
46+
String group = matcher.group(2);
47+
map.put(group, new TextRange(matcher.start(), matcher.end()));
48+
} else if (matcher.group(3) != null) {
49+
String group = matcher.group(3);
50+
map.put(group.substring(1, group.length() - 1), new TextRange(matcher.start(), matcher.end()));
51+
}
4652
}
4753
} catch (IllegalStateException e) {
4854
// no matches
@@ -66,7 +72,7 @@ public DataExternalizer<TextRange> getValueExternalizer() {
6672

6773
@Override
6874
public int getVersion() {
69-
return 0;
75+
return 1;
7076
}
7177

7278
@NotNull
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.cedricziel.idea.typo3.index;
2+
3+
import com.cedricziel.idea.typo3.AbstractTestCase;
4+
import com.cedricziel.idea.typo3.util.TableUtil;
5+
6+
public class TablenameIndexTest extends AbstractTestCase {
7+
public void testTablenameIsExtractedCorrectly() {
8+
myFixture.configureByText("ext_tables.sql", "CREATE TABLE foo {}");
9+
assertContainsLookupElementWithText(
10+
TableUtil.createAvailableTableNamesLookupElements(myFixture.getProject()),
11+
"foo"
12+
);
13+
14+
myFixture.configureByText("ext_tables.sql", "CREATE TABLE `bar` {}");
15+
assertContainsLookupElementWithText(
16+
TableUtil.createAvailableTableNamesLookupElements(myFixture.getProject()),
17+
"bar"
18+
);
19+
20+
myFixture.configureByText("ext_tables.sql", "CREATE TABLE `bingo {}");
21+
assertNotContainsLookupElementWithText(
22+
TableUtil.createAvailableTableNamesLookupElements(myFixture.getProject()),
23+
"bingo"
24+
);
25+
assertNotContainsLookupElementWithText(
26+
TableUtil.createAvailableTableNamesLookupElements(myFixture.getProject()),
27+
"`bingo"
28+
);
29+
30+
myFixture.configureByText("ext_tables.sql", "CREATE TABLE bongo` {}");
31+
assertNotContainsLookupElementWithText(
32+
TableUtil.createAvailableTableNamesLookupElements(myFixture.getProject()),
33+
"bongo`"
34+
);
35+
}
36+
}

0 commit comments

Comments
 (0)