Files
Joshen Lim 558bee9ebf Improve SQL Editor auto completion for column names (#48734)
## Context

Currently with the SQL Editor, the auto-completion via intellisense only
works nicely with the `.` operator - e.g after keying in a schema and
trying to find a table as such:
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/86ec8455-47b9-43d3-925a-c13d0fd6ac44"
/>

But lacks support for finding the columns of a table after a `where`
clause - so the changes here addresses that by mainly adjust the
`PgSQLCompletionProvider`

Also addresses a number of type fixes (replaces all the `any` types)

<img width="500" alt="image"
src="https://github.com/user-attachments/assets/e23672a6-2b48-4a98-b300-69f822d12b38"
/>

<img width="415" height="319" alt="image"
src="https://github.com/user-attachments/assets/e6ee64d2-90dd-47d6-9fd2-e8b07821ebb6"
/>



<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Improved PostgreSQL SQL editor suggestions with table, column, alias,
schema, and join-aware completions.
* Added context-aware support for statements, quoted identifiers,
subqueries, and qualified columns.
  * Enhanced PostgreSQL function signature assistance.
* Added safer behavior when database metadata is incomplete or
unavailable.

* **Bug Fixes**
  * Prioritized relevant columns and removed duplicate suggestions.

* **Tests**
* Added comprehensive coverage for SQL parsing, metadata handling, and
completion behavior.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-09-15 12:34:20 +08:00

48 lines
1.5 KiB
TypeScript

import type { Monaco } from '@monaco-editor/react'
import type { editor, Position } from 'monaco-editor'
import type { RefObject } from 'react'
import BackwardIterator from './BackwardIterator'
import type { PgInfo } from './Providers.types'
export function getPgsqlSignatureHelpProvider(
_monaco: Monaco,
pgInfoRef: RefObject<PgInfo | null>
) {
return {
signatureHelpTriggerCharacters: ['(', ','],
provideSignatureHelp: function (model: editor.ITextModel, position: Position) {
const pgInfo = pgInfoRef.current
if (!pgInfo) return null
// position.column should minus 2 as it returns 2 for first char
// position.lineNumber should minus 1
const iterator = new BackwardIterator(model, position.column - 2, position.lineNumber - 1)
let paramCount = iterator.readArguments()
if (paramCount < 0) return null
let ident = iterator.readIdent()
if (!ident || ident.match(/^\".*?\"$/)) return null
let fn = pgInfo.functions.find(
(f) => f.name.toLocaleLowerCase() === ident.toLocaleLowerCase()
)
if (!fn) return null
if (!fn.args || fn.args.length < paramCount) return null
const activeSignature = 0
const activeParameter = Math.min(paramCount, fn.args.length - 1)
const signatures = []
signatures.push({
label: `${fn.name}(${fn.argument_types})`,
parameters: fn.args.map((v) => {
return { label: v.name }
}),
})
return { value: { signatures, activeSignature, activeParameter }, dispose: () => {} }
},
}
}