Skip to content

Commit

Permalink
fix: changes to get columns function and better calling of stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
wmontgomery committed Oct 31, 2022
1 parent 6e0aafe commit f6f54a4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 42 deletions.
28 changes: 5 additions & 23 deletions apps/studio/src/components/TabQueryEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -780,29 +780,11 @@ import { FavoriteQuery } from '@/common/appdb/models/favorite_query'
fakeRemoteChange() {
this.query.text = "select * from foo"
},
getColumnsForAutocomplete() {
const cm = this.editor.getValue()
if (cm.toLowerCase().search(/[from(\s)?|join(\s)?]/g) === -1) return
const triggerWords = ['join', 'from']
const allTables = this.hintOptions
const cmValue = cm.replace(/[;[\]'"]/g, '').replace(/\r?\n|\r/g, ' ').split(' ').filter(word => word !== '')
const tablesToFind = cmValue
.reduce((acc, word, index, arr) => {
if (index === arr.length) return acc
if (triggerWords.includes(word.toLowerCase())) {
// will need to clean up the word if it's wrapped up in stuff
if (allTables.tables[arr[index + 1]]?.length === 0){
acc.push(arr[index + 1])
}
}
return acc
}, [])
.forEach(async(table) => {
const tableToFind = this.tables.find(t => t.name === table)
await this.$store.dispatch('updateTableColumns', tableToFind)
setTimeout(() => this.editor?.setOption('hintOptions', this.hintOptions), 1)
})
async getColumnsForAutocomplete(tableName) {
const tableToFind = this.tables.find(t => t.name === tableName)
await this.$store.dispatch('updateTableColumns', tableToFind)
this.editor?.setOption('hintOptions', this.hintOptions)
return this.hintOptions.tables[tableName]
}
},
mounted() {
Expand Down
7 changes: 1 addition & 6 deletions apps/studio/src/vendor/show-hint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,8 @@
return this.cm.state.completionActive == this;
},

pick: async function(data, i) {
pick: function(data, i) {
var completion = data.list[i], self = this;
// when using the space (column finisher thing which relies on a promise to get values from), the ch position in "data.from" was returning as an unresolved promise
// so in order to get the position, we need to make sure the data coming back is a resolved value, so time to have some fun.
if (Object.prototype.toString.call(data.from?.ch) === '[object Promise]') {
data.from.ch = await data.from.ch
}
this.cm.operation(function() {
if (completion.hint)
completion.hint(self.cm, data, completion);
Expand Down
28 changes: 15 additions & 13 deletions apps/studio/src/vendor/sql-hint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@
return value
}

async function parseTables(input) {
if (globalEditorOptions?.getColumns) {
await globalEditorOptions.getColumns()
}
function parseTables(input) {
var result = {}
if (isArray(input)) {
for (var i = input.length - 1; i >= 0; i--) {
Expand Down Expand Up @@ -167,13 +164,11 @@
if (table !== oldTable) alias = true;
}

if (globalEditorOptions?.getColumns) {
await globalEditorOptions.getColumns()
}
var columns = getTable(table);
var columns = globalEditorOptions.getColumns ? await globalEditorOptions.getColumns(table): getTable(table)

if (columns && columns.columns)
columns = columns.columns;

if (columns) {
addMatches(result, string, columns, function(w) {
var tableInsert = table;
Expand Down Expand Up @@ -249,7 +244,7 @@

CodeMirror.registerHelper("hint", "sql", async function(editor, options) {
globalEditorOptions = {...editor.options}
tables = await parseTables(options?.tables)
tables = parseTables(options?.tables)
var defaultTableName = options?.defaultTable;
var disableKeywords = options?.disableKeywords;
defaultTable = defaultTableName && getTable(defaultTableName);
Expand Down Expand Up @@ -305,9 +300,16 @@
addMatches(result, search, keywords, function(w) {
return objectOrClass(w.toUpperCase(), "CodeMirror-hint-keyword");
});
}

return {list: result, from: Pos(cur.line, start), to: Pos(cur.line, end)};
}

const dataFrom = Pos(cur.line, start)
// Because there are some promises around for getting the columns, the ch position in the "from" object was returning as an unresolved promise
// so in order to get the position, we need to make sure the data coming back is a resolved value, so time to have some fun.
if (Object.prototype.toString.call(dataFrom.ch) === '[object Promise]') {
dataFrom.ch = await dataFrom.ch
}

return {list: result, from: dataFrom, to: Pos(cur.line, end)};
});
CodeMirror.defineOption("getColumns", null);
});

0 comments on commit f6f54a4

Please sign in to comment.