Skip to content

Commit d6ad4cc

Browse files
authored
fix the case doesn't complete FROM keyword (#39)
1 parent 8087990 commit d6ad4cc

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

packages/server/complete.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ type Pos = { line: number, column: number }
1717

1818
const logger = log4js.getLogger()
1919

20+
const FROM_KEYWORD = { label: 'FROM', kind: CompletionItemKind.Text }
21+
2022
const CLAUSES: CompletionItem[] = [
2123
{ label: 'WHERE', kind: CompletionItemKind.Text },
2224
{ label: 'ORDER BY', kind: CompletionItemKind.Text },
@@ -191,6 +193,22 @@ function completeDeleteStatement (ast: DeleteStatement, pos: Pos, schema: Schema
191193
return []
192194
}
193195

196+
function completeSelectStatement(ast: SelectStatement, _pos: Pos, _schema: Schema): CompletionItem[] {
197+
let candidates: CompletionItem[] = []
198+
if (Array.isArray(ast.columns)) {
199+
const first = ast.columns[0]
200+
const rest = ast.columns.slice(1, ast.columns.length)
201+
const lastColumn = rest.reduce((p, c) => p.location.end.offset < c.location.end.offset ? c : p ,first)
202+
if (
203+
FROM_KEYWORD.label.startsWith(lastColumn.expr.column) ||
204+
(lastColumn.as && FROM_KEYWORD.label.startsWith(lastColumn.as))
205+
) {
206+
candidates.push(FROM_KEYWORD)
207+
}
208+
}
209+
return candidates
210+
}
211+
194212
export default function complete(sql: string, pos: Pos, schema: Schema = []) {
195213
logger.debug(`complete: ${sql}, ${JSON.stringify(pos)}`)
196214
let candidates: CompletionItem[] = []
@@ -208,6 +226,9 @@ export default function complete(sql: string, pos: Pos, schema: Schema = []) {
208226
if (ast.type === 'select' && !ast.distinct) {
209227
candidates.push({ label: 'DISTINCT', kind: CompletionItemKind.Text })
210228
}
229+
if (ast.type === 'select') {
230+
candidates = candidates.concat(completeSelectStatement(ast, pos, schema))
231+
}
211232
const columns = ast.columns
212233
if (Array.isArray(columns)) {
213234
const selectColumnRefs = (columns as any).map((v: any) => v.expr).filter((v: any) => !!v)

packages/server/test/complete.test.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@ describe('keyword completion', () => {
77
expect(result.candidates[0].label).toEqual('SELECT')
88
})
99

10-
test("complete 'FROM' keyword", () => {
11-
const result = complete('SELECT * F', { line: 0, column: 10 })
12-
expect(result.candidates.length).toEqual(1)
13-
expect(result.candidates[0].label).toEqual('FROM')
10+
describe('FROM keyword', () => {
11+
test("complete FROM word with the star column", () => {
12+
const result = complete('SELECT * F', { line: 0, column: 10 })
13+
expect(result.candidates.length).toEqual(1)
14+
expect(result.candidates[0].label).toEqual('FROM')
15+
})
16+
17+
test("complete FROM word with norm columns", () => {
18+
const sql = 'SELECT d, f F'
19+
const result = complete('SELECT d, f F', { line: 0, column: sql.length })
20+
expect(result.candidates.length).toEqual(1)
21+
expect(result.candidates[0].label).toEqual('FROM')
22+
})
1423
})
15-
24+
1625
test("complete 'WHERE' keyword", () => {
1726
const result = complete('SELECT * FROM FOO W', { line: 0, column: 19 })
1827
expect(result.candidates.length).toEqual(1)

0 commit comments

Comments
 (0)