Skip to content

Commit 8d2124c

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 bec9efd commit 8d2124c

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
@@ -256,8 +256,18 @@ class Completer {
256256
if (!addedSome) {
257257
this.addCandidatesForUnscopedColumns(fromNodes, schemaAndSubqueries)
258258
}
259+
259260
this.addCandidatesForAliases(fromNodes)
260-
this.addCandidatesForTables(schemaAndSubqueries, true)
261+
262+
const fromNodesContainingCursor = fromNodes.filter((tableNode) =>
263+
isPosInLocation(tableNode.location, this.pos)
264+
)
265+
const isCursorInsideFromClause = fromNodesContainingCursor.length > 0
266+
if (isCursorInsideFromClause) {
267+
// only add table candidates if the cursor is inside a FROM clause or JOIN clause, etc.
268+
this.addCandidatesForTables(schemaAndSubqueries, true)
269+
}
270+
261271
if (logger.isDebugEnabled())
262272
logger.debug(
263273
`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
@@ -181,6 +181,17 @@ describe('on blank space', () => {
181181
expect(result.candidates).toEqual(expect.arrayContaining(expected))
182182
})
183183

184+
test('complete after SELECT FROM schema2.table2', () => {
185+
const result = complete(
186+
'SELECT FROM schema2.table2',
187+
{ line: 0, column: 8 },
188+
SIMPLE_NESTED_SCHEMA
189+
)
190+
expect(result.candidates).not.toContainEqual(
191+
expect.objectContaining({ label: 'TABLE1' })
192+
)
193+
})
194+
184195
test('complete function inside WHERE select star', () => {
185196
const result = complete(
186197
'SELECT * FROM tab1 WHERE arr',

0 commit comments

Comments
 (0)