1+ import { CodeAction , CodeActionKind , languages , TextDocument , Uri , WorkspaceEdit } from "vscode" ;
2+ import { remoteAssistIsEnabled } from "./logic/available" ;
3+ import { getSqlDocument } from "./logic/parse" ;
4+
5+ class SqlCodeAction extends CodeAction {
6+ constructor ( title : string , kind : CodeActionKind , public file : { document : TextDocument , statementOffset : number , bindCount : number } ) {
7+ super ( title , kind ) ;
8+ }
9+ }
10+
11+ const invalidBindingLabels = [ `bind` , `cl` ] ;
12+
13+ export const actionProvider = languages . registerCodeActionsProvider ( { language : `sql` } , {
14+ provideCodeActions ( document , range , context , token ) {
15+ if ( range . isEmpty ) {
16+ const offset = document . offsetAt ( range . start ) ;
17+
18+ const enabled = remoteAssistIsEnabled ( ) ;
19+ if ( ! enabled ) return ;
20+
21+ const sqlDoc = getSqlDocument ( document ) ;
22+ if ( ! sqlDoc ) return ;
23+
24+ const currentStatement = sqlDoc . getStatementByOffset ( offset ) ;
25+ const label = currentStatement . getLabel ( ) ?. toLowerCase ( ) || `` ;
26+
27+ if ( currentStatement && ! invalidBindingLabels . includes ( label ) ) {
28+ const markers = currentStatement . getEmbeddedStatementAreas ( ) . filter ( a => a . type === `marker` ) ;
29+ const codeActions : SqlCodeAction [ ] = [ ] ;
30+
31+ if ( markers . length > 0 ) {
32+ const action = new SqlCodeAction ( `Generate bind statement` , CodeActionKind . QuickFix , {
33+ document,
34+ statementOffset : currentStatement . range . end ,
35+ bindCount : markers . length
36+ } ) ;
37+
38+ codeActions . push ( action ) ;
39+
40+ return codeActions ;
41+ }
42+ }
43+
44+ return [ ] ;
45+ }
46+ } ,
47+ resolveCodeAction ( codeAction : SqlCodeAction , token ) {
48+ if ( ! ( codeAction instanceof SqlCodeAction ) ) return codeAction ;
49+ codeAction . edit = new WorkspaceEdit ( ) ;
50+ const document = codeAction . file . document ;
51+
52+ const endOfStatementPos = document . positionAt ( codeAction . file . statementOffset ) ;
53+ const lineOfStatement = document . lineAt ( endOfStatementPos . line ) ;
54+
55+ let statement = `bind: ${ new Array ( codeAction . file . bindCount ) . fill ( `v` ) . join ( `, ` ) } ` ;
56+
57+ codeAction . edit . insert ( document . uri , lineOfStatement . range . end , `\n${ statement } ;` ) ;
58+ return codeAction ;
59+ }
60+ } ) ;
0 commit comments