Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sqllab] add autocomplete to AceEditor for table and column names #1475

Merged
merged 2 commits into from
Oct 29, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions caravel/assets/javascripts/SqlLab/components/AceEditorWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ import AceEditor from 'react-ace';
import 'brace/mode/sql';
import 'brace/theme/github';
import 'brace/ext/language_tools';
import ace from 'brace';

const langTools = ace.acequire('ace/ext/language_tools');

const propTypes = {
sql: React.PropTypes.string.isRequired,
onBlur: React.PropTypes.func,
sql: React.PropTypes.string.isRequired,
tables: React.PropTypes.array,
};

const defaultProps = {
onBlur: () => {},
tables: [],
};

class AceEditorWrapper extends React.PureComponent {
Expand All @@ -26,8 +31,31 @@ class AceEditorWrapper extends React.PureComponent {
onBlur() {
this.props.onBlur(this.state.sql);
}

setAutoCompleter() {
// Loading table and column names as auto-completable words
const completer = {
getCompletions: (aceEditor, session, pos, prefix, callback) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would pull this out into it's own method on the component, otherwise LGTM

let words = [];
const columns = {};
const tables = this.props.tables || [];
tables.forEach(t => {
words.push({ name: t.name, value: t.name, score: 55, meta: 'table' });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be nice to differentiate tables or views, optional

t.columns.forEach(col => {
columns[col.name] = null; // using an object as a unique set
});
});
words = words.concat(Object.keys(columns).map(col => (
{ name: col, value: col, score: 50, meta: 'column' }
)));
callback(null, words);
},
};
if (langTools) {
langTools.setCompleters([completer, langTools.keyWordCompleter]);
}
}
render() {
this.setAutoCompleter();
return (
<AceEditor
mode="sql"
Expand Down
1 change: 1 addition & 0 deletions caravel/assets/javascripts/SqlLab/components/SqlEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ class SqlEditor extends React.PureComponent {
</Col>
<Col md={9}>
<AceEditorWrapper
tables={this.props.tables}
sql={this.props.queryEditor.sql}
onBlur={this.setQueryEditorSql.bind(this)}
/>
Expand Down