Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions src/utils/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ export const findStatementAtPosition = (
statements: { text: string; start: number; end: number }[],
position: number
) => {
if (statements.length === 0) return null

// Check if cursor is exactly after a semicolon
if (position > 0) {
for (let i = 0; i < statements.length; i += 1) {
Expand All @@ -99,6 +101,13 @@ export const findStatementAtPosition = (
return { statement, index: statements.indexOf(statement) }
}

// Cursor in trailing whitespace after a statement (e.g. on empty lines below `;`)
for (let i = statements.length - 1; i >= 0; i -= 1) {
if (position > statements[i].start) {
return { statement: statements[i], index: i }
}
}

return null
}

Expand Down
31 changes: 23 additions & 8 deletions src/views/dashboard/query/editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,6 @@ a-modal(
return false
})

const handleReadySql = (payload: any) => {
sqlView.value = payload.view
}
const handleReadyPromql = (payload: any) => {
promqlView.value = payload.view
}

const remeasureEditors = () => {
nextTick(() => {
requestAnimationFrame(() => {
Expand Down Expand Up @@ -345,15 +338,37 @@ a-modal(
}
}

const handleReadySql = (payload: any) => {
sqlView.value = payload.view
codeUpdate('sql')
}
const handleReadyPromql = (payload: any) => {
promqlView.value = payload.view
codeUpdate('promql')
}

const executeRunAll = async () => {
const res = await runQuery(codes.value[queryType.value].trim(), queryType.value, false, promForm, 'run-all')
if ((res as { cancelled?: boolean })?.cancelled) return
if (res?.results?.length) session.appendResults(res.results)
if (res?.log) session.appendLog(res.log)
}

const resolveStatementToRun = () => {
if (queryType.value === 'promql') {
return currentStatement.value || codes.value.promql
}
const view = sqlView.value
if (view) {
const statements = parseSqlStatements(view.state.doc.toString())
const result = findStatementAtPosition(statements, view.state.selection.main.from)
if (result) return result.statement.text
}
return currentStatement.value
}

const executePartQuery = async () => {
const res = await runQuery(currentStatement.value, queryType.value, false, promForm, 'run-part')
const res = await runQuery(resolveStatementToRun(), queryType.value, false, promForm, 'run-part')
if ((res as { cancelled?: boolean })?.cancelled) return
if (res?.results?.length) session.appendResults(res.results)
if (res?.log) session.appendLog(res.log)
Expand Down
Loading