Skip to content

Commit

Permalink
improve signature, restore snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
doublefint committed Jan 22, 2018
1 parent 1b1dfcd commit 073aa08
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 43 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ All notable changes to the "vscode-pgsql" extension will be documented in this f

## [Unreleased]

## 0.1.0
### Changed
- Signature loaded from Postgres 9.5
### Fixed
- Signature completion for multiline scenario
- Restore snippets

## 0.0.9
### Fixed
- Signature completion
Expand Down
1 change: 1 addition & 0 deletions extension/signature-update.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
psql -U postgres -f signature.sql -t -o signature.json postgres
97 changes: 61 additions & 36 deletions extension/signature.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,72 @@
const sigdatas = require('./signature-data').default

const parse = sd => {
const regex = /\(([^)]+)\)/
const matches = regex.exec( sd )
if ( !matches ) return []
if ( matches.length < 2 ) return []
const sps = matches[1]
return sps.split(",").map( sp => ({ label: sp }))
}
const data = require('./signature.json')

/*const parse = args => {
if ( !args ) return []
return args.split(",").map( sp => ({ label: sp }))
}*/

const signatures = sigdatas.map( sd => {
if ( !sd ) return
if ( sd.length < 2 ) return
const signatures = data.map( sd => {
return {
label: sd[0],
documentation: sd[1],
parameters: parse( sd[0] )
name: sd.nm,
label: sd.nm + "( " + sd.args + " )",
documentation: sd.nt,
parameters: sd.args.split(",").map( sp => ({ label: sp }))
}
})

exports.default = {
provideSignatureHelp: ( doc, pos ) => {

const text = doc.lineAt( pos.line ).text.substring( 0, pos.character )
const parts = text.split("(") //text before and after (
const callLookup = ( code, commas )=>{
const call = { commas }
let char='', i = code.length-1;
for ( ;i>=0; i-- ){
char = code[i]
if ( char === ',' ) call.commas++
if ( char === '(' ) { // first open parenthesis
let rest = code.substring( 0, i ) + "("
var matches = rest.match( /\S+(?=\()/g )
if ( matches.length ){
//last sequence before (
call.name = matches[ matches.length - 1 ]
}
break
}
}
return call
}

// walk back (and up)
// find word before first open parenthesis
// also calculate commas before first open parenthesis
// return { word, commas: count }
// NOTE: only for simple cases
const walkbackToCall = ( doc, pos ) => {

let line = pos.line
let code = doc.lineAt( line ).text
code = code.substring( 0, pos.character )

let commas = 0, maxLines = 10, call = { }

while ( !call.name ) {
call = callLookup( code, commas )
commas = call.commas

if ( !parts.length ) return Promise.resolve( null )
if (call.name) break
if (( line-- < 0 ) || ( maxLines-- < 0 )) break

const ftext = parts[0]
const fwords = ftext.split(" ")
if ( !fwords.length ) return Promise.resolve( null )

const fname = fwords[fwords.length-1]; // last word before '('
if ( !fname ) return Promise.resolve( null )

let activeParameter = 0
if ( parts.length === 2 ){
activeParameter = parts[1].split(",").length - 1
}
code = doc.lineAt( line ).text
}
return call

}

exports.default = {
provideSignatureHelp: ( doc, pos ) => { // function called at "(" and ","

const mask = fname.toUpperCase()
const filtered = signatures.filter( s => ~s.label.toUpperCase().indexOf( mask ) && ( s.parameters.length > activeParameter ) )
return Promise.resolve( { activeParameter, activeSignature: 0, signatures: filtered } )
const call = walkbackToCall( doc, pos ) // returns { name: 'token before'}
if ( !call.name ) return Promise.resolve( null )
const mask = call.name.toLowerCase()
const filtered = signatures.filter( s => ~s.name.indexOf( mask ) && ( s.parameters.length > call.commas ) )
return Promise.resolve( { activeParameter: call.commas, activeSignature: 0, signatures: filtered } )

}
}
2 changes: 2 additions & 0 deletions extension/signature.json

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions extension/signature.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
With signatures as (
Select --n.nspname || '.' ||
p.proname as nm, --name
pg_catalog.pg_get_function_arguments(p.oid) as "args",
pg_catalog.pg_get_function_result(p.oid) as "ret", --returns
d.description as nt --note
From pg_catalog.pg_proc p
Left Join pg_catalog.pg_namespace n ON n.oid = p.pronamespace
Left Join pg_catalog.pg_description d On p.oid = d.objoid
Where pg_catalog.pg_function_is_visible(p.oid)
And p.prorettype <> 'pg_catalog.trigger'::pg_catalog.regtype
--AND n.nspname <> 'pg_catalog'
--AND n.nspname <> 'information_schema'
Order By 1
) Select array_to_json( array_agg( s.* ) ) From signatures s;
10 changes: 5 additions & 5 deletions extension/snippets.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
"prefix": "pgsql",
"body": [
"Select * ",
"From ${tableName}",
"From ${1:tableName}",
"Where 1=1 And id = ? ",
";"
],
"description": "Insert 'Select * From ... Where ... ' statement"
"description": "Insert 'Select From Where' statement"
},

"Create Function": {
"prefix": "pgsql",
"body": [
"Create Or Replace Function ${funcName}( data json ) Returns json As $$",
"Create Or Replace Function ${1:funcName}( data json ) Returns json As $$",
"\tDeclare",
"\t\t-- declare variables",
"\t\t-- for example: id int := Cast( data->>'id' as int ); ",
Expand All @@ -29,14 +29,14 @@
"Create Table": {
"prefix": "pgsql",
"body": [
"Create Table ${tableName} (",
"Create Table ${1:tableName} (",
"\tid serial primary key,",
"\tcd text not null,",
"\tnm text,",
"\t--ref int references another.table,",
"\tcreated timestamptz default now()",
");",
"Create Index on ${tableName} (cd);"
"Create Index on ${1:tableName} (cd);"
],
"description": "Insert 'Create Table ...' statement"
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "pgsql",
"description": "run sql in your postgres instance",
"license": "MIT",
"version": "0.0.9",
"version": "0.1.0",
"publisher": "doublefint",
"engines": {
"vscode": "^1.19.0"
Expand Down Expand Up @@ -68,7 +68,8 @@
"description": "connection string to your postgres db"
}
}
}
},
"snippets": [ { "language": "pgsql", "path": "./extension/snippets.json" } ]
},
"scripts": {
"postinstall": "node ./node_modules/vscode/bin/install",
Expand Down

0 comments on commit 073aa08

Please sign in to comment.