import { TextEditor , window , workspace , SnippetString , env , commands , Uri , } from 'vscode' ; import { DismissedError } from '@sqltools/util/exception' ; import { isEmpty } from '@sqltools/util/validation' ; export async function getOrCreateEditor ( forceCreate = false ) { if ( forceCreate || ! window . activeTextEditor || ! window . activeTextEditor . viewColumn ) { const doc = await workspace . openTextDocument ( { language : 'sql' } ) ; await window . showTextDocument ( doc , 1 , false ) ; } const editor = window . activeTextEditor ; return editor ; } export async function getSelectedText ( action = 'proceed' , fullText = false ) { const editor = await getOrCreateEditor ( ) ; const query = editor . document . getText ( fullText ? undefined : editor . selection ) ; if ( isEmpty ( query ) ) { window . showInformationMessage ( `Can't ${ action }. You have selected nothing.` ) ; throw new DismissedError ( ) ; } return query ; } export async function insertText ( text , forceCreate = false , insertAsText = false ) { const editor = await getOrCreateEditor ( forceCreate ) ; if ( typeof text === 'string' && insertAsText ) { return void Promise . all ( editor . selections . map ( cursor => editor . edit ( e => e . insert ( cursor . active , text ) ) ) ) ; } await Promise . all ( editor . selections . map ( cursor => editor . insertSnippet ( text instanceof SnippetString ? text : new SnippetString ( `\${1:${ text }}$0` ) , cursor . active ) ) ) ; } export async function readInput ( prompt , placeholder ? , value ? ) { const data = await window . showInputBox ( { prompt , placeHolder : placeholder || prompt , value } ) ; if ( isEmpty ( data ) ) throw new DismissedError ( ) ; return data ; } export function openExternal ( url ) { let uri = Uri . parse ( url ) ; if ( uri . query && /http/ . test ( uri . scheme ) ) { uri = uri . with ( { query : uri . query . replace ( /\n/g , encodeURI ( '\n' ) ) } ) ; } if ( env && typeof ( env as any ) . openExternal === 'function' ) { return ( env as any ) . openExternal ( uri ) ; } if ( env && typeof ( env as any ) . open === 'function' ) { return ( env as any ) . open ( uri ) ; } return commands . executeCommand ( 'vscode.open' , uri ) ; }