"use strict" ; import * as fs from "fs" ; import * as os from "os" ; import * as utils from "./utils" ; import * as syntaxer from "./syntaxer" ; import * as vscode from "vscode" ; import * as path from "path" ; import { Uri , commands , DiagnosticSeverity , TextEditorSelectionChangeKind , Location , window , TextEditor , workspace , Terminal , TextEditorSelectionChangeEvent , Selection , TextDocument , TextDocumentChangeEvent , ExtensionContext } from "vscode" ; import { ErrorInfo , Utils , unlock , is_busy , with_lock , actual_output , settings , vsc_config , user_dir , create_dir , select_line , ActiveEditorTracker } from "./utils" ; import { Syntaxer } from "./syntaxer" ; export let syntax_readme = path . join ( user_dir ( ) , 'dotnet' , "cs-script.syntax.txt" ) ; let ext_context ; let cscs_exe = path . join ( user_dir ( ) , 'dotnet' , "cscs.dll" ) ; let readme = path . join ( user_dir ( ) , 'dotnet' , "cs-script.help.txt" ) ; let outputChannel = window . createOutputChannel ( "CS-Script" ) ; let script_proj_name = "script.csproj" ; let startup_file_key = "cs-script.open_file_at_startup" ; export let csproj_dir = path . join ( os . tmpdir ( ) , "CSSCRIPT" , "VSCode" , "cs-script - OmniSharp" ) ; function extra_args ( ) { return vsc_config . get ( "cs-script.extra_args_for_debug" , "-co:/debug:pdbonly" ) ; } let core_engine = path . join ( user_dir ( ) , "dotnet" , "cscs.dll" ) ; function is_dotnet_debug ( ) { return vsc_config . get ( "cs-script.engine_debug.dotnet" , true ) ; } export function load_project ( ) { with_lock ( ( ) => { unlock ( ) ; let editor = window . activeTextEditor ; let workspaceIsAlreadyLoaded = Utils . IsSamePath ( workspace . rootPath , csproj_dir ) ; if ( workspaceIsAlreadyLoaded ) { if ( editor != null && ! Utils . IsScript ( editor . document . fileName ) ) { window . showErrorMessage ( "The active document is not a C# code file. Please open a C# document and try again." ) ; } else { outputChannel . show ( true ) ; outputChannel . clear ( ) ; outputChannel . appendLine ( "Loading script..." ) ; try { if ( editor == null ) { let scriptFile = parse_proj_dir ( csproj_dir ) ; commands . executeCommand ( "vscode.open" , Uri . file ( scriptFile ! ) ) ; } else { if ( editor . document . isDirty ) { editor . document . save ( ) ; return ; } generate_proj_file ( csproj_dir , editor . document . fileName ) ; } } finally { setTimeout ( ( ) => outputChannel . clear ( ) , 700 ) ; } } } else { if ( editor == null ) { window . showErrorMessage ( "No active document found. Please open a C# document and try again." ) ; } else if ( ! Utils . IsScript ( editor . document . fileName ) ) { window . showErrorMessage ( "The active document is not a C# code file. Please open a C# document and try again." ) ; } else { outputChannel . clear ( ) ; outputChannel . appendLine ( "Loading OmniSharp project..." ) ; if ( editor . document . isDirty ) { editor . document . save ( ) ; return ; } generate_proj_file ( csproj_dir , editor . document . fileName ) ; ext_context . globalState . update ( startup_file_key , editor . document . fileName ) ; if ( settings . show_load_proj_info ) { let info_msg = "In order to activate intellisense an OmniSharp project will be initialized and the current script file will be loaded in its context.\n\n" ; let ok_dont_show_again = "OK, Don't show this message again" ; let ok = "OK" ; window . showInformationMessage ( info_msg , { modal : true } , ok , ok_dont_show_again ) . then ( response => { if ( response == ok ) { setTimeout ( ( ) => commands . executeCommand ( "vscode.openFolder" , Uri . file ( csproj_dir ) ) , 100 ) ; } else if ( response == ok_dont_show_again ) { settings . show_load_proj_info = false ; utils . Settings . Save ( settings ) ; setTimeout ( ( ) => commands . executeCommand ( "vscode.openFolder" , Uri . file ( csproj_dir ) ) , 100 ) ; } } ) ; } else { setTimeout ( ( ) => commands . executeCommand ( "vscode.openFolder" , Uri . file ( csproj_dir ) ) , 100 ) ; } } } } ) ; } export function parse_proj_dir ( proj_dir ) { let proj_file = path . join ( proj_dir , script_proj_name ) ; let prefix = '<Compile Include="' ; let suffix = '"/>' ; for ( let line of fs . readFileSync ( proj_file , "utf8" ) . split ( os . EOL ) ) { line = line . trim ( ) ; if ( line . startsWith ( prefix ) ) { let file = line . slice ( prefix . length , - suffix . length ) ; return file ; } } return null ; } function generate_proj_file ( proj_dir , scriptFile ) { try { let command = `dotnet "${ core_engine }" -proj:csproj "${ scriptFile }"` ; let output = Utils . RunSynch ( command ) ; let lines = output . trim ( ) . lines ( ) . filter ( actual_output ) ; let src_proj_file = lines [ 0 ] . replace ( "project:" , "" ) ; let proj_name = "script" + path . extname ( src_proj_file ) ; let src_proj_dir = path . dirname ( src_proj_file ) ; let src_vscode_dir = path . join ( user_dir ( ) , 'dotnet' , '.vscode' ) ; create_dir ( proj_dir ) ; utils . copy_file_to_sync2 ( path . basename ( src_proj_file ) , proj_name , src_proj_dir , proj_dir ) ; utils . copy_dir_to_sync ( src_vscode_dir , proj_dir ) ; commands . executeCommand ( "cs-script.refresh_tree" ) ; } catch ( error ) { window . showErrorMessage ( `Cannot generate project from the script. Check the //css_ref and //css_in directives.` ) ; throw error ; } } export function print_project ( ) { if ( Utils . IsSamePath ( workspace . rootPath , csproj_dir ) ) { let proj_file = path . join ( csproj_dir , "script.csproj" ) ; let file = Utils . getScriptName ( proj_file ) ; print_project_for ( file ) ; } else { print_project_for_document ( ) ; } } export function print_project_for_document ( ) { let editor = window . activeTextEditor ; let file = editor ?. document . fileName ; editor ?. document . save ( ) ; outputChannel . show ( true ) ; outputChannel . clear ( ) ; outputChannel . appendLine ( "Analyzing..." ) ; if ( ! print_project_for ( file ) ) outputChannel . clear ( ) ; } export function print_project_for ( file ) { if ( Utils . IsScript ( file ) ) { with_lock ( ( ) => { let command = `dotnet "${ core_engine }" -l -proj:dbg "${ file }"` ; Utils . Run ( command , ( code , output ) => { let lines = output . lines ( ) . filter ( actual_output ) ; outputChannel . clear ( ) ; lines . forEach ( ( line , i ) => outputChannel . appendLine ( line ) ) ; unlock ( ) ; } ) ; } ) ; return true ; } else { window . showErrorMessage ( `"${ file }" is not a valid C# script file.` ) ; return false ; } } export function get_project_tree_items ( ) { let lines = [ ] ; let editor = window . activeTextEditor ; let file = "" ; if ( Utils . IsSamePath ( workspace . rootPath , csproj_dir ) ) { let proj_file = path . join ( csproj_dir , "script.csproj" ) ; file = Utils . getScriptName ( proj_file ) ; } else if ( editor && Utils . IsScript ( editor . document . fileName ) ) { file = editor . document . fileName ; editor . document . save ( ) ; } if ( file != "" ) { if ( ! is_busy ( ) ) with_lock ( ( ) => { let command = `dotnet "${ core_engine }"  -l -proj "${ file }"` ; let output = Utils . RunSynch ( command ) ; lines = output . lines ( ) . filter ( actual_output ) ; unlock ( ) ; } ) ; else { setTimeout ( ( ) => commands . executeCommand ( "cs-script.refresh_tree" ) , 500 ) ; } } return lines ; } export function check ( ) { with_lock ( ( ) => { let editor = window . activeTextEditor ! ; let file = editor ?. document . fileName ; editor . document . save ( ) ; outputChannel . show ( true ) ; outputChannel . clear ( ) ; outputChannel . appendLine ( "Checking..." ) ; let extra_args = vsc_config . get ( "cs-script.extra_args" ) ; let command = `dotnet "${ core_engine }" ${ extra_args } -check "${ file }"` ; let cleared = false ; Utils . Run2 ( command , data => { if ( ! cleared ) { cleared = true ; outputChannel . clear ( ) ; } outputChannel . append ( data ) ; } , ( code , output ) => { let lines = output . split ( /\r?\n/g ) . filter ( actual_output ) ; let errors = [ ] ; lines . forEach ( ( line , i ) => { let error = ErrorInfo . parse ( line ) ; if ( error && ( error . severity == DiagnosticSeverity . Error || error . severity == DiagnosticSeverity . Warning ) ) { errors . push ( error ) ; } } ) ; Utils . SentToDiagnostics ( errors ) ; unlock ( ) ; } ) ; } ) ; } export async function find_references ( ) { let editor = window . activeTextEditor ; let document = editor ! . document ; let position = editor ! . selection . active ; with_lock ( async ( ) => { outputChannel . show ( true ) ; outputChannel . clear ( ) ; outputChannel . appendLine ( "Resolving references..." ) ; if ( document . languageId == "csharp" || document . languageId == "vb" ) { try { let data = await Syntaxer . getReferencesAsync ( document . getText ( ) , document . fileName , document . offsetAt ( position ) ) ; outputChannel . clear ( ) ; if ( ! data . startsWith ( "<null>" ) && ! data . startsWith ( "<error>" ) ) { let lines = data . lines ( ) ; outputChannel . clear ( ) ; outputChannel . appendLine ( `${ lines . length } references:` ) ; lines . forEach ( line => outputChannel . appendLine ( line ) ) ; } } catch ( error ) { outputChannel . clear ( ) ; } } else { try { let locations = await commands . executeCommand ( "vscode.executeReferenceProvider" , Uri . file ( document . fileName ) , position ) ; let lines = locations as Location [ ] ; outputChannel . clear ( ) ; outputChannel . appendLine ( `Found ${ lines . length } references:` ) ; let lastLocationFile = "" ; let lastLocationLines ; lines . forEach ( location => { let hint = "..." ; if ( lastLocationFile != location . uri . fsPath ) { lastLocationLines = [ ] ; if ( fs . existsSync ( location . uri . fsPath ) ) { try { lastLocationLines = fs . readFileSync ( location . uri . fsPath , "utf8" ) . lines ( ) ; } catch { } } } if ( lastLocationLines . length > location . range . start . line ) try { hint = lastLocationLines [ location . range . start . line ] . substring ( location . range . start . character ) ; } catch { } outputChannel . appendLine ( `${ location . uri . fsPath }(${ location . range . start . line + 1 },${ location . range . start . character + 1 }): ${ hint }` ) ; } ) ; } catch { outputChannel . clear ( ) ; } } unlock ( ) ; } ) ; } export function css_config ( ) { utils . ensure_default_core_config ( core_engine , config_file => { commands . executeCommand ( "vscode.open" , Uri . file ( config_file ) ) ; } ) ; } export function about ( ) { with_lock ( ( ) => { outputChannel . show ( true ) ; outputChannel . clear ( ) ; outputChannel . appendLine ( "Analyzing..." ) ; let command = `dotnet "${ core_engine }" -ver` ; Utils . Run ( command , ( code , output ) => { outputChannel . clear ( ) ; outputChannel . appendLine ( "CS-Script.VSCode - v" + utils . ext_version ) ; outputChannel . appendLine ( "-------------------------------------------------------" ) ; outputChannel . appendLine ( output . trim ( ) ) ; outputChannel . appendLine ( "-------------------------------------------------------" ) ; outputChannel . appendLine ( "Syntaxer" ) ; outputChannel . appendLine ( "   " + syntaxer . server ( ) ) ; outputChannel . appendLine ( "Extension" ) ; outputChannel . appendLine ( "   " + __dirname ) ; unlock ( ) ; } ) ; } ) ; } let terminal ; export function run_in_terminal ( ) { with_lock ( ( ) => { let editor = window . activeTextEditor ! ; let file = editor . document . fileName ; editor . document . save ( ) ; if ( terminal == null ) terminal = window . createTerminal ( "Ext Terminal cs-script" ) ; let dir = path . dirname ( file ) ; terminal . show ( false ) ; terminal . sendText ( `cd "${ dir }"` ) ; if ( os . platform ( ) == "win32" ) terminal . sendText ( "cls" ) ; let extra_args = vsc_config . get ( "cs-script.extra_args" ) ; terminal . sendText ( `dotnet "${ core_engine }" ${ extra_args } "${ file }"` ) ; unlock ( ) ; } ) ; } export function engine_help ( ) { with_lock ( ( ) => { let command = `dotnet "${ core_engine }" -help` ; Utils . Run ( command , ( code , output ) => { fs . writeFileSync ( readme , output , { encoding : "utf8" } ) ; commands . executeCommand ( "vscode.open" , Uri . file ( readme ) ) ; unlock ( ) ; } ) ; } ) ; } export function generate_syntax_help ( force = false ) { let command = `dotnet "${ core_engine }" -syntax` ; let output = Utils . RunSynch ( command ) ; fs . writeFileSync ( syntax_readme , output , { encoding : "utf8" } ) ; return output ; } export function new_script ( ) { with_lock ( ( ) => { let new_file_path = path . join ( user_dir ( ) , "new_script.cs" ) ; let backup_file = null ; if ( fs . existsSync ( new_file_path ) ) backup_file = new_file_path + ".bak" ; if ( backup_file && fs . existsSync ( backup_file ) ) { fs . unlinkSync ( backup_file ) ; fs . renameSync ( new_file_path , backup_file ) ; } let backup_comment = "" ; if ( backup_file ) backup_comment = "// The previous content of this file has been saved into \n" + "// " + backup_file + " \n" ; let content = utils . prepare_new_script ( ) . replace ( "$backup_comment$" , backup_comment ) ; fs . writeFileSync ( new_file_path , content ) ; if ( fs . existsSync ( new_file_path ) ) commands . executeCommand ( "vscode.open" , Uri . file ( new_file_path ) ) ; unlock ( ) ; } ) ; } export function new_script_vb ( ) { with_lock ( ( ) => { vscode . window . showErrorMessage ( "Executing VB.NET scripts is only supported for C# script hosted on Mono but this version CS-Script extension is configured to be run on .NET 5/.NET Core. VB support will be available in teh future releases" ) ; return ; let new_file_path = path . join ( user_dir ( ) , "new_script.vb" ) ; let backup_file = null ; if ( fs . existsSync ( new_file_path ) ) backup_file = new_file_path + ".bak" ; if ( backup_file && fs . existsSync ( backup_file ) ) { fs . unlinkSync ( backup_file ) ; fs . renameSync ( new_file_path , backup_file ) ; } let backup_comment = "" ; if ( backup_file ) backup_comment = "' // The previous content of this file has been saved into \n" + "' // " + backup_file + " \n" ; let content = utils . prepare_new_script_vb ( ) . replace ( "$backup_comment$" , backup_comment ) ; fs . writeFileSync ( new_file_path , content ) ; if ( fs . existsSync ( new_file_path ) ) commands . executeCommand ( "vscode.open" , Uri . file ( new_file_path ) ) ; unlock ( ) ; } ) ; } export function build_exe ( ) { with_lock ( ( ) => { let editor = window . activeTextEditor ! ; let file = editor . document . fileName ; outputChannel . show ( true ) ; outputChannel . clear ( ) ; outputChannel . appendLine ( 'Building executable from the script "' + file + '"' ) ; outputChannel . appendLine ( "---------------------" ) ; let command = `dotnet "${ cscs_exe }" -e "${ file }"` ; Utils . Run ( command , ( code , output ) => { outputChannel . appendLine ( output ) ; outputChannel . appendLine ( "\n[Done]" ) ; unlock ( ) ; } ) ; } ) ; } var debugging = false ; export async function debug ( ) { let workspace_loaded = workspace . workspaceFolders != undefined ; let suppress_for_workspaces = vsc_config . get ( "cs-script.suppress_script_debug_for_workspaces" ) ; let fallback_to_launch_json = vsc_config . get ( "cs-script.fallback_to_launch_json" ) ; if ( workspace_loaded && ! suppress_for_workspaces ) { if ( fallback_to_launch_json ) { if ( workspace . workspaceFolders != undefined ) { const launchFile = workspace . getConfiguration ( "launch" ) ; const configs = launchFile . get < any [ ] > ( "configurations" ) ; vscode . debug . onDidTerminateDebugSession ( session => { debugging = false ; } ) ; if ( ! debugging ) { debugging = true ; await vscode . debug . startDebugging ( workspace . workspaceFolders [ 0 ] , configs ! [ 0 ] ) ; } } } return ; } let editor = window . activeTextEditor ! ; editor . document . save ( ) ; if ( ! fs . existsSync ( editor . document . fileName ) ) { window . showInformationMessage ( 'Cannot find file "' + editor . document . fileName + '"' ) ; return ; } with_lock ( ( ) => { let config = "" ; if ( os . platform ( ) == "win32" ) { let run_as_dotnet = vsc_config . get ( "cs-script.dotnet_run_host_on_win" , false ) ; if ( run_as_dotnet ) { config = `-config:css_config.xml` ; } } let debug_as_external_console = vsc_config . get ( "cs-script.debug_as_external_console" , false ) ; let launchConfigMono = { name : "Launch" , type : "mono" , request : "launch" , program : cscs_exe , externalConsole : debug_as_external_console . toString ( ) , showOutput : "always" , args : [ "-d" , "-inmem:0" , extra_args ( ) , config , "-ac:2" , editor . document . fileName ] , env : { } } ; let launchConfigCore = { name : ".NET Core Launch (console)" , type : "coreclr" , request : "launch" , program : "dotnet" , args : [ core_engine , "-d" , extra_args ( ) . replace ( "-co:/debug:pdbonly" , "" ) , vsc_config . get ( "cs-script.extra_args" ) , "" , "-l" , config , "-ac:2" , editor . document . fileName ] , cwd : path . dirname ( editor . document . fileName ) , console : "internalConsole" , stopAtEntry : false } ; var launchConfig = launchConfigMono ; if ( is_dotnet_debug ( ) ) launchConfig = launchConfigCore ; vscode . debug . startDebugging ( undefined , < vscode . DebugConfiguration > launchConfig ) . then ( ( ) => { commands . executeCommand ( "workbench.debug.action.focusRepl" ) ; } , err => { window . showInformationMessage ( "Error: " + err . message ) ; } ) ; unlock ( ) ; } ) ; } async function getOpenEditors ( ) { const editorTracker = new ActiveEditorTracker ( ) ; let active = window . activeTextEditor ; let editor = active ; const openEditors = [ ] ; function same ( lhs , rhs ) { return lhs . _id == rhs . _id && lhs . viewColumn == rhs . viewColumn ; } do { if ( editor !== undefined ) { if ( active === undefined ) { active = editor ; } openEditors . push ( editor ) ; } editor = await editorTracker . awaitNext ( 500 ) ; if ( editor !== undefined && openEditors . any ( x => same ( x , editor ) ) ) break ; } while ( ( active === undefined && editor === undefined ) || ! same ( active , editor ) ) ; editorTracker . dispose ( ) ; return openEditors ; } export async function save_script_project ( dependencies_only ) { let editor = window . activeTextEditor ! ; let file = editor . document . fileName ; if ( ! dependencies_only ) { editor . document . save ( ) ; } let command = `dotnet "${ core_engine }" -proj:dbg "${ file }"` ; let response = Utils . RunSynch ( command ) ; let dependencies = response . lines ( ) . where < string > ( l => l . startsWith ( "file:" ) ) . select ( l => l . substring ( 5 ) ) ; let dirty_docs = unsaved_documents . where < string > ( x => x != file ) . select ( x => x . toLocaleLowerCase ( ) ) ; let relevant_files = dependencies . select ( x => x . toLocaleLowerCase ( ) ) ; let there_are_unsaved = relevant_files . any ( x => dirty_docs . contains ( x ) ) ; if ( there_are_unsaved ) { let openTextEditors = await getOpenEditors ( ) ; openTextEditors . forEach ( ed => { let isActiveDoc = ed == editor ; let tabFileName = ed . document . fileName . toLocaleLowerCase ( ) ; if ( ! isActiveDoc && dependencies . any < string > ( x => x . toLocaleLowerCase ( ) == tabFileName ) ) { ed . document . save ( ) ; } } ) ; } } export function run ( ) { with_lock ( async ( ) => { let editor = window . activeTextEditor ! ; let exec = require ( "child_process" ) . exec ; let showExecutionMessage = true ; outputChannel . clear ( ) ; let file = editor . document . fileName ; await save_script_project ( false ) ; let extra_args = vsc_config . get ( "cs-script.extra_args" ) ; let command = `dotnet "${ core_engine }" ${ extra_args } "${ file }"` ; if ( showExecutionMessage ) { outputChannel . appendLine ( "[Running] " + command ) ; } outputChannel . show ( true ) ; let startTime = new Date ( ) ; let p = exec ( command ) ; p . stdout . on ( "data" , ( data ) => { if ( ! data . startsWith ( "failed to get 100ns ticks" ) ) outputChannel . append ( data ) ; } ) ; p . stderr . on ( "data" , ( data ) => { outputChannel . append ( data ) ; } ) ; p . on ( "close" , ( code ) => { let endTime = new Date ( ) ; let elapsedTime = ( endTime . getTime ( ) - startTime . getTime ( ) ) / 1000 ; outputChannel . appendLine ( "" ) ; if ( showExecutionMessage ) { outputChannel . appendLine ( "[Done] exited with code=" + code + " in " + elapsedTime + " seconds" ) ; outputChannel . appendLine ( "" ) ; } unlock ( ) ; } ) ; } ) ; } let current_doc = "" ; function onActiveEditorChange ( editor ) { if ( editor != null ) { current_doc = editor . document . fileName ; } } function onActiveEditorSelectionChange ( event ) { if ( event . kind == TextEditorSelectionChangeKind . Mouse && event . textEditor . document . fileName . startsWith ( "extension-output-" ) && event . textEditor . selection . isEmpty ) { let enabled = vsc_config . get ( "cs-script.single_click_navigate_from_output" , true ) ; let select_whole_line = vsc_config . get ( "select_line_on_navigate_from_output" , true ) ; let single_line_in_selection = ( event . textEditor . selection . start . line == event . textEditor . selection . end . line ) ; if ( enabled && single_line_in_selection ) { let line = event . textEditor . document . lineAt ( event . textEditor . selection . start . line ) . text ; let info = ErrorInfo . parse ( line ) ; if ( info != null && fs . existsSync ( info . file ) ) { let already_active = current_doc == info . file ; if ( ! already_active ) setTimeout ( ( ) => commands . executeCommand ( "vscode.open" , Uri . file ( info . file ) ) . then ( value => { let editor = window . activeTextEditor ! ; const position = editor . selection . active ; if ( select_whole_line ) { select_line ( info . range . start . line ) ; } else { let start = position . with ( info . range . start . line , info . range . start . character ) ; let newSelection = new Selection ( start , start ) ; editor . selection = newSelection ; } } ) , 100 ) ; } } } } var unsaved_documents = [ ] ; function add_to_unsaved ( file ) { if ( fs . existsSync ( file ) ) { var index = unsaved_documents . indexOf ( file , 0 ) ; if ( index == - 1 ) { unsaved_documents . push ( file ) ; } } } function remove_from_unsaved ( file ) { if ( fs . existsSync ( file ) ) { var index = unsaved_documents . indexOf ( file , 0 ) ; if ( index > - 1 ) unsaved_documents . splice ( index , 1 ) ; } } function onDidSaveTextDocument ( document ) { if ( Utils . IsSamePath ( workspace . rootPath , csproj_dir ) ) { let proj_file = path . join ( csproj_dir , "script.csproj" ) ; let scripts = Utils . getScriptFiles ( proj_file ) ; if ( scripts . any < string > ( x => x . pathNormalize ( ) == document . fileName . pathNormalize ( ) ) ) generate_proj_file ( csproj_dir , scripts . first ( ) ) ; } remove_from_unsaved ( document . fileName ) ; } function onDidCloseTextDocument ( document ) { remove_from_unsaved ( document . fileName ) ; } function onDidChangeTextDocument ( documentEvent ) { add_to_unsaved ( documentEvent . document . fileName ) ; } export function ActivateDiagnostics ( context ) { try { ext_context = context ; window . onDidChangeActiveTextEditor ( onActiveEditorChange ) ; window . onDidChangeTextEditorSelection ( onActiveEditorSelectionChange ) ; workspace . onDidSaveTextDocument ( onDidSaveTextDocument ) ; workspace . onDidChangeTextDocument ( onDidChangeTextDocument ) ; workspace . onDidCloseTextDocument ( onDidCloseTextDocument ) ; let file = ext_context . globalState . get ( "cs-script.open_file_at_startup" , "" ) ; if ( file != null && file != "" ) { ext_context . globalState . update ( startup_file_key , "" ) ; commands . executeCommand ( "vscode.open" , Uri . file ( file ) ) ; } return utils . ActivateDiagnostics ( context ) ; } catch ( error ) { window . showErrorMessage ( "CS-Script: " + String ( error ) ) ; } }