Skip to content

Commit af9f5a9

Browse files
authored
Complete double quoted table's column (joe-re#123)
* s/substr/substring/ * enable to get last token with ""
1 parent 5bfba29 commit af9f5a9

File tree

7 files changed

+40
-49
lines changed

7 files changed

+40
-49
lines changed

packages/server/src/complete/Identifier.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class Identifier {
4747
let tableName = label
4848
const i = tableName.lastIndexOf('.')
4949
if (i > 0) {
50-
tableName = label.substr(i + 1)
50+
tableName = label.substring(i + 1)
5151
}
5252
tableAlias = makeTableAlias(tableName)
5353
kindName = 'table'

packages/server/src/complete/StringUtils.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,6 @@ export function getRidOfAfterPosString(sql: string, pos: Pos): string {
1717
.join('\n')
1818
}
1919

20-
// Gets the last token from the given string considering that tokens can contain dots.
21-
export function getLastToken(sql: string): string {
22-
const match = sql.match(/^(?:.|\s)*[^A-z0-9\\.:'](.*?)$/)
23-
if (match) {
24-
let prevToken = ''
25-
let currentToken = match[1]
26-
while (currentToken != prevToken) {
27-
prevToken = currentToken
28-
currentToken = prevToken.replace(/\[.*?\]/, '')
29-
}
30-
return currentToken
31-
}
32-
return sql
33-
}
34-
3520
export function makeTableName(table: Table): string {
3621
if (table.catalog) {
3722
return table.catalog + '.' + table.database + '.' + table.tableName

packages/server/src/complete/complete.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import {
1313
import log4js from 'log4js'
1414
import { CompletionItem } from 'vscode-languageserver-types'
1515
import { Schema, Table } from '../database_libs/AbstractClient'
16-
import { getRidOfAfterPosString, getLastToken } from './StringUtils'
16+
import { getRidOfAfterPosString } from './StringUtils'
17+
import { getLastToken } from './utils/getLastToken'
1718
import {
1819
isPosInLocation,
1920
createTablesFromFromNodes,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Gets the last token from the given string considering that tokens can contain dots.
2+
export function getLastToken(sql: string): string {
3+
const match = sql.match(/^(?:.|\s)*[^A-z0-9\\.:'"](.*?)$/)
4+
if (match) {
5+
let prevToken = ''
6+
let currentToken = match[1]
7+
while (currentToken !== prevToken) {
8+
prevToken = currentToken
9+
currentToken = prevToken.replace(/\[.*?\]/, '') // remove []
10+
}
11+
return currentToken.replace(/"/g, '') // remove ""
12+
}
13+
return sql
14+
}

packages/server/test/complete/StringUtils.test.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

packages/server/test/complete/complete_column.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ describe('ColumnName completion', () => {
123123
expect(result.candidates.length).toEqual(0)
124124
})
125125

126-
// TODO: Implement completion
127-
test.skip('complete ColumnName with back quoted table', () => {
126+
test('complete ColumnName with back quoted table', () => {
128127
const result = complete(
129128
'SELECT "TABLE1". FROM "TABLE1"',
130129
{ line: 0, column: 16 },
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { getLastToken } from '../../../src/complete/utils/getLastToken'
2+
3+
describe('getLastToken', () => {
4+
test('getLastToken', () => {
5+
expect(getLastToken('SELECT abc')).toEqual('abc')
6+
expect(getLastToken('SELECT abc.def')).toEqual('abc.def')
7+
expect(getLastToken('SELECT abc[0]')).toEqual('abc')
8+
expect(getLastToken('SELECT abc[0].')).toEqual('abc.')
9+
expect(getLastToken('SELECT abc[0].d')).toEqual('abc.d')
10+
expect(getLastToken('SELECT abc[0].def[0]')).toEqual('abc.def')
11+
expect(getLastToken('SELECT abc[0].def[0].')).toEqual('abc.def.')
12+
expect(getLastToken('SELECT abc[0].def[0].g')).toEqual('abc.def.g')
13+
14+
expect(getLastToken("SELECT abc['key']")).toEqual('abc')
15+
expect(getLastToken("SELECT abc['key.name'].")).toEqual('abc.')
16+
expect(getLastToken("SELECT abc['key'].d")).toEqual('abc.d')
17+
expect(getLastToken("SELECT abc['key'].def['key']")).toEqual('abc.def')
18+
expect(getLastToken("SELECT abc['key'].def['key'].")).toEqual('abc.def.')
19+
expect(getLastToken("SELECT abc['key'].def[0].g")).toEqual('abc.def.g')
20+
expect(getLastToken('SELECT "abc"')).toEqual('abc')
21+
})
22+
})

0 commit comments

Comments
 (0)