11"use strict" ;
22
33import * as fs from "fs" ;
4- import * as tools from "./SwiftTools" ;
54import * as path from "path" ;
6- import {
7- commands ,
8- DiagnosticCollection ,
9- ExtensionContext ,
10- languages ,
11- TextDocument ,
12- window ,
13- workspace ,
14- } from "vscode" ;
5+ import { commands , DiagnosticCollection , ExtensionContext , window , workspace } from "vscode" ;
156import { absolutePath } from "./helpers/AbsolutePath" ;
7+ import * as tools from "./toolchain/SwiftTools" ;
168import * as config from "./vscode/config-helpers" ;
179import lsp from "./vscode/lsp-interop" ;
1810import output from "./vscode/output-channels" ;
1911import { statusBarItem } from "./vscode/status-bar" ;
2012
2113let swiftBinPath : string | null = null ;
2214let swiftBuildParams : string [ ] = [ "build" ] ;
23- let swiftPackageManifestPath : string | null = null ;
2415let skProtocolProcess : string | null = null ;
2516let skProtocolProcessAsShellCmd : string | null = null ;
2617export let diagnosticCollection : DiagnosticCollection ;
2718
19+ let mostRecentRunTarget = "" ;
20+
2821export function activate ( context : ExtensionContext ) {
2922 output . init ( context ) ;
3023
3124 if ( workspace . getConfiguration ( ) . get < boolean > ( "sde.enable" ) === false ) {
3225 output . build . log ( "SDE Disabled" , false ) ;
3326 return ;
3427 }
28+ tools . setRunning ( false ) ;
3529 output . build . log ( "Activating SDE" ) ;
3630
3731 initConfig ( ) ;
3832 lsp . startLSPClient ( context ) ;
3933
40- diagnosticCollection = languages . createDiagnosticCollection ( "swift" ) ;
41- context . subscriptions . push ( diagnosticCollection ) ;
42-
4334 //commands
35+ let toolchain = new tools . Toolchain (
36+ swiftBinPath ,
37+ workspace . workspaceFolders [ 0 ] . uri . fsPath ,
38+ swiftBuildParams
39+ ) ;
40+ context . subscriptions . push ( toolchain . diagnostics ) ;
41+ context . subscriptions . push ( toolchain . start ( ) ) ;
4442 context . subscriptions . push (
45- commands . registerCommand ( "sde.commands.buildPackage " , buildSPMPackage ) ,
43+ commands . registerCommand ( "sde.commands.build " , ( ) => toolchain . build ( ) ) ,
4644 commands . registerCommand ( "sde.commands.restartLanguageServer" , ( ) =>
4745 lsp . restartLSPClient ( context )
4846 ) ,
49- commands . registerCommand ( "sde.commands.runPackage" , ( ) => {
50- output . build . log ( "sde.commands.runPackage" ) ;
51- } )
47+ commands . registerCommand ( "sde.commands.run" , ( ) => toolchain . runStart ( ) ) ,
48+ commands . registerCommand ( "sde.commands.selectRun" , ( ) => {
49+ window
50+ . showInputBox ( { prompt : "Run which target?" , value : mostRecentRunTarget } )
51+ . then ( target => {
52+ if ( ! target ) {
53+ return ;
54+ }
55+ mostRecentRunTarget = target ;
56+ toolchain . runStart ( target ) ;
57+ } ) ;
58+ } ) ,
59+ commands . registerCommand ( "sde.commands.restart" , ( ) => {
60+ toolchain . runStop ( ) ;
61+ toolchain . runStart ( mostRecentRunTarget ) ;
62+ } ) ,
63+ commands . registerCommand ( "sde.commands.stop" , ( ) => toolchain . runStop ( ) ) ,
64+ commands . registerCommand ( "sde.commands.clean" , ( ) => toolchain . clean ( ) )
5265 ) ;
5366
54- workspace . onDidSaveTextDocument ( onSave , null , context . subscriptions ) ;
67+ workspace . onDidSaveTextDocument (
68+ document => {
69+ if ( tools . shouldBuildOnSave ( ) && document . languageId === "swift" ) {
70+ toolchain . build ( ) ;
71+ }
72+ } ,
73+ null ,
74+ context . subscriptions
75+ ) ;
5576
5677 // respond to settings changes
5778 workspace . onDidChangeConfiguration ( event => {
@@ -61,33 +82,11 @@ export function activate(context: ExtensionContext) {
6182 } ) ;
6283
6384 // build on startup
64- buildSPMPackage ( ) ;
85+ toolchain . build ( ) ;
6586}
6687
6788function initConfig ( ) {
6889 checkToolsAvailability ( ) ;
69-
70- //FIXME rootPath may be undefined for adhoc file editing mode???
71- swiftPackageManifestPath = workspace . workspaceFolders [ 0 ]
72- ? path . join ( workspace . workspaceFolders [ 0 ] . uri . fsPath , "Package.swift" )
73- : null ;
74- }
75-
76- function buildSPMPackage ( ) {
77- if ( isSPMProject ( ) ) {
78- statusBarItem . start ( ) ;
79- tools . buildPackage ( swiftBinPath , workspace . rootPath , swiftBuildParams ) ;
80- }
81- }
82-
83- function onSave ( document : TextDocument ) {
84- if ( config . buildOnSave ( ) && document . languageId === "swift" ) {
85- buildSPMPackage ( ) ;
86- }
87- }
88-
89- function isSPMProject ( ) : boolean {
90- return swiftPackageManifestPath ? fs . existsSync ( swiftPackageManifestPath ) : false ;
9190}
9291
9392function checkToolsAvailability ( ) {
@@ -105,7 +104,7 @@ function checkToolsAvailability() {
105104
106105 if ( ! swiftBinPath || ! fs . existsSync ( swiftBinPath ) ) {
107106 window . showErrorMessage (
108- 'missing dependent swift tool, please configure correct "swift.path.swift_driver_bin"'
107+ 'missing dependent ` swift` tool, please configure correct "swift.path.swift_driver_bin"'
109108 ) ;
110109 }
111110 if ( ! sourcekitePathEnableShCmd ) {
0 commit comments