Skip to content

Commit 0278e25

Browse files
committed
Only add table candidates if we are inside a FROM clause
This will avoid suggesting tables when cursor is between SELECT and FROM. See new unit test
1 parent 5e98a73 commit 0278e25

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

packages/server/src/complete/complete.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,18 @@ class Completer {
257257
if (!addedSome) {
258258
this.addCandidatesForUnscopedColumns(fromNodes, schemaAndSubqueries)
259259
}
260+
260261
this.addCandidatesForAliases(fromNodes)
261-
this.addCandidatesForTables(schemaAndSubqueries, true)
262+
263+
const fromNodesContainingCursor = fromNodes.filter((tableNode) =>
264+
isPosInLocation(tableNode.location, this.pos)
265+
)
266+
const isCursorInsideFromClause = fromNodesContainingCursor.length > 0
267+
if (isCursorInsideFromClause) {
268+
// only add table candidates if the cursor is inside a FROM clause or JOIN clause, etc.
269+
this.addCandidatesForTables(schemaAndSubqueries, true)
270+
}
271+
262272
if (logger.isDebugEnabled())
263273
logger.debug(
264274
`candidates for error returns: ${JSON.stringify(this.candidates)}`

packages/server/test/complete.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,17 @@ describe('on blank space', () => {
189189
expect(result.candidates).toEqual(expect.arrayContaining(expected))
190190
})
191191

192+
test('complete after SELECT FROM schema2.table2', () => {
193+
const result = complete(
194+
'SELECT FROM schema2.table2',
195+
{ line: 0, column: 8 },
196+
SIMPLE_NESTED_SCHEMA
197+
)
198+
expect(result.candidates).not.toContainEqual(
199+
expect.objectContaining({ label: 'TABLE1' })
200+
)
201+
})
202+
192203
test('complete function inside WHERE select star', () => {
193204
const result = complete(
194205
'SELECT * FROM tab1 WHERE arr',

0 commit comments

Comments
 (0)