@@ -22,7 +22,7 @@ public class TableUtil {
2222 public static final String EXT_TABLES_SQL_FILENAME = "ext_tables.sql" ;
2323
2424 public static Set <String > getAvailableTableNames (@ NotNull Project project ) {
25- PsiFile [] extSqlFiles = FilenameIndex . getFilesByName (project , EXT_TABLES_SQL_FILENAME , GlobalSearchScope . allScope ( project ) );
25+ PsiFile [] extSqlFiles = getExtTablesSqlFilesInProject (project );
2626
2727 Set <String > tableNames = new HashSet <>();
2828
@@ -53,17 +53,55 @@ public static Set<String> getAvailableTableNames(@NotNull Project project) {
5353 return tableNames ;
5454 }
5555
56- public static PsiElement [] getExtTablesSqlFilesForTable (@ NotNull String tableName , @ NotNull Project project ) {
57- PsiFile [] extSqlFiles = FilenameIndex .getFilesByName (project , EXT_TABLES_SQL_FILENAME , GlobalSearchScope .allScope (project ));
56+ public static PsiElement [] getTableDefinitionElements (@ NotNull String tableName , @ NotNull Project project ) {
57+
58+ PsiFile [] extTablesSqlFilesInProjectContainingTable = getExtTablesSqlFilesInProjectContainingTable (tableName , project );
5859
5960 return Arrays
60- .stream (extSqlFiles )
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+ }
84+
85+ return null ;
86+ })
6187 .filter (Objects ::nonNull )
62- .filter (file -> LoadTextUtil .loadText (file .getVirtualFile ()).toString ().contains (tableName ))
63- .map (file -> file .findElementAt (LoadTextUtil .loadText (file .getVirtualFile ()).toString ().indexOf (tableName )))
6488 .toArray (PsiElement []::new );
6589 }
6690
91+ private static PsiFile [] getExtTablesSqlFilesInProjectContainingTable (@ NotNull String tableName , @ NotNull Project project ) {
92+
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 );
98+ }
99+
100+ @ NotNull
101+ private static PsiFile [] getExtTablesSqlFilesInProject (@ NotNull Project project ) {
102+ return FilenameIndex .getFilesByName (project , EXT_TABLES_SQL_FILENAME , GlobalSearchScope .allScope (project ));
103+ }
104+
67105 public static void completeAvailableTableNames (@ NotNull Project project , @ NotNull CompletionResultSet completionResultSet ) {
68106 for (String name : TableUtil .getAvailableTableNames (project )) {
69107 completionResultSet .addElement (new LookupElement () {
0 commit comments