@@ -3,14 +3,19 @@ import AceEditor from 'react-ace';
3
3
import 'brace/mode/sql' ;
4
4
import 'brace/theme/github' ;
5
5
import 'brace/ext/language_tools' ;
6
+ import ace from 'brace' ;
7
+
8
+ const langTools = ace . acequire ( 'ace/ext/language_tools' ) ;
6
9
7
10
const propTypes = {
8
- sql : React . PropTypes . string . isRequired ,
9
11
onBlur : React . PropTypes . func ,
12
+ sql : React . PropTypes . string . isRequired ,
13
+ tables : React . PropTypes . array ,
10
14
} ;
11
15
12
16
const defaultProps = {
13
17
onBlur : ( ) => { } ,
18
+ tables : [ ] ,
14
19
} ;
15
20
16
21
class AceEditorWrapper extends React . PureComponent {
@@ -26,8 +31,32 @@ class AceEditorWrapper extends React.PureComponent {
26
31
onBlur ( ) {
27
32
this . props . onBlur ( this . state . sql ) ;
28
33
}
29
-
34
+ getCompletions ( aceEditor , session , pos , prefix , callback ) {
35
+ let words = [ ] ;
36
+ const columns = { } ;
37
+ const tables = this . props . tables || [ ] ;
38
+ tables . forEach ( t => {
39
+ words . push ( { name : t . name , value : t . name , score : 55 , meta : 'table' } ) ;
40
+ t . columns . forEach ( col => {
41
+ columns [ col . name ] = null ; // using an object as a unique set
42
+ } ) ;
43
+ } ) ;
44
+ words = words . concat ( Object . keys ( columns ) . map ( col => (
45
+ { name : col , value : col , score : 50 , meta : 'column' }
46
+ ) ) ) ;
47
+ callback ( null , words ) ;
48
+ }
49
+ setAutoCompleter ( ) {
50
+ // Loading table and column names as auto-completable words
51
+ const completer = {
52
+ getCompletions : this . getCompletions . bind ( this ) ,
53
+ } ;
54
+ if ( langTools ) {
55
+ langTools . setCompleters ( [ completer , langTools . keyWordCompleter ] ) ;
56
+ }
57
+ }
30
58
render ( ) {
59
+ this . setAutoCompleter ( ) ;
31
60
return (
32
61
< AceEditor
33
62
mode = "sql"
0 commit comments