Skip to content

Commit 294c97f

Browse files
committed
fix(web-console): prevent next query to be run whne the first one is clicked
1 parent 53788d2 commit 294c97f

5 files changed

Lines changed: 83 additions & 23 deletions

File tree

packages/browser-tests/cypress/commands.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ Cypress.Commands.add("runLineWithResponse", (response) => {
132132
cy.wait("@exec");
133133
});
134134

135+
Cypress.Commands.add("clickLine", (n) => {
136+
cy.get(".monaco-editor .view-line")
137+
.eq(n - 1)
138+
.click();
139+
});
140+
135141
Cypress.Commands.add("clickRun", () => {
136142
cy.intercept("/exec*").as("exec");
137143
return cy.get("button").contains("Run").click().wait("@exec");

packages/browser-tests/cypress/integration/console/editor.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ const baseUrl = `http://localhost:9999${contextPath}`;
66
const getTabDragHandleByTitle = (title) =>
77
`.chrome-tab[data-tab-title="${title}"] .chrome-tab-drag-handle`;
88

9+
describe.only("run query", () => {
10+
beforeEach(() => {
11+
cy.loadConsoleWithAuth();
12+
cy.getEditorContent().should("be.visible");
13+
cy.clearEditor();
14+
});
15+
16+
it("should correctly run query in the first line", () => {
17+
cy.typeQuery("select 1;\n\nselect 2;");
18+
cy.clickLine(1);
19+
cy.clickRun();
20+
cy.getGridRow(0).should("contain", "1");
21+
});
22+
});
23+
924
describe("appendQuery", () => {
1025
const consoleConfiguration = {
1126
savedQueries: [

packages/browser-tests/questdb

Submodule questdb updated 45 files

packages/web-console/src/scenes/Editor/Monaco/index.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ const MonacoEditor = () => {
185185
editor: editor.IStandaloneCodeEditor,
186186
) => {
187187
const queryAtCursor = getQueryFromCursor(editor)
188+
189+
// Clear decorations if queryAtCursor is undefined
190+
if (!queryAtCursor) {
191+
decorationsRef.current?.clear()
192+
return
193+
}
194+
188195
const model = editor.getModel()
189196
if (queryAtCursor && model !== null) {
190197
const activeBufferId = activeBufferRef.current.id as number

packages/web-console/src/scenes/Editor/Monaco/utils.ts

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ export const getQueryFromCursor = (
7070
let startRow = 0
7171
let startCol = 0
7272
let startPos = -1
73-
let sql = null
73+
let nextSql = null
7474
let inQuote = false
7575

7676
if (!position) return
7777

7878
for (let i = 0; i < text.length; i++) {
79-
if (sql !== null) {
79+
if (nextSql !== null) {
8080
break
8181
}
8282

@@ -97,6 +97,8 @@ export const getQueryFromCursor = (
9797
row: startRow,
9898
col: startCol,
9999
position: startPos,
100+
endRow: row,
101+
endCol: column,
100102
limit: i,
101103
})
102104
startRow = row
@@ -105,7 +107,14 @@ export const getQueryFromCursor = (
105107
column++
106108
} else {
107109
// empty queries, aka ;; , make sql.length === 0
108-
sql = text.substring(startPos === -1 ? 0 : startPos, i)
110+
nextSql = {
111+
row: startRow,
112+
col: startCol,
113+
position: startPos,
114+
endRow: row,
115+
endCol: column,
116+
limit: i,
117+
}
109118
}
110119
break
111120
}
@@ -130,7 +139,6 @@ export const getQueryFromCursor = (
130139
startRow = row
131140
startCol = column
132141
startPos = i + 1
133-
column++
134142
}
135143
break
136144
}
@@ -148,28 +156,52 @@ export const getQueryFromCursor = (
148156
}
149157
}
150158

151-
if (sql === null) {
152-
sql = startPos === -1 ? text : text.substring(startPos)
153-
}
154-
155-
if (sql.length === 0) {
156-
const prev = sqlTextStack.pop()
159+
// lastStackItem is the last query that is completed before the current cursor position.
160+
// nextSql is the next query that is not completed before the current cursor position, or started after the current cursor position.
161+
const normalizedCurrentRow = position.lineNumber - 1
162+
const lastStackItem = sqlTextStack.length > 0 ? sqlTextStack[sqlTextStack.length - 1] : null
157163

158-
if (prev) {
164+
if (!lastStackItem && !nextSql) {
165+
return
166+
}
167+
const lastStackItemRowRange = lastStackItem ? {
168+
start: lastStackItem.row,
169+
end: lastStackItem.endRow,
170+
} : null
171+
const nextSqlRowRange = nextSql ? {
172+
start: nextSql.row,
173+
end: nextSql.endRow,
174+
} : null
175+
const isInLastStackItemRowRange = lastStackItemRowRange && normalizedCurrentRow >= lastStackItemRowRange.start && normalizedCurrentRow <= lastStackItemRowRange.end
176+
const isInNextSqlRowRange = nextSqlRowRange && normalizedCurrentRow >= nextSqlRowRange.start && normalizedCurrentRow <= nextSqlRowRange.end
177+
178+
if (isInLastStackItemRowRange && !isInNextSqlRowRange) {
179+
return {
180+
query: text.substring(lastStackItem!.position, lastStackItem!.limit),
181+
row: lastStackItem!.row,
182+
column: lastStackItem!.endCol,
183+
}
184+
} else if (isInNextSqlRowRange && !isInLastStackItemRowRange) {
185+
return {
186+
query: text.substring(nextSql!.position, nextSql!.limit),
187+
row: nextSql!.row,
188+
column: nextSql!.col,
189+
}
190+
} else if (isInLastStackItemRowRange && isInNextSqlRowRange) {
191+
const lastStackItemEndCol = lastStackItem!.endCol
192+
const normalizedCurrentCol = position.column - 1
193+
if (normalizedCurrentCol > lastStackItemEndCol + 1) {
159194
return {
160-
column: prev.col,
161-
query: text.substring(prev.position, prev.limit),
162-
row: prev.row,
195+
query: text.substring(nextSql!.position, nextSql!.limit),
196+
row: nextSql!.row,
197+
column: nextSql!.col,
163198
}
164199
}
165-
166-
return
167-
}
168-
169-
return {
170-
column: startCol,
171-
query: sql,
172-
row: startRow,
200+
return {
201+
query: text.substring(lastStackItem!.position, lastStackItem!.limit),
202+
row: lastStackItem!.row,
203+
column: lastStackItem!.endCol,
204+
}
173205
}
174206
}
175207

0 commit comments

Comments
 (0)