11"use strict" ;
22
3- import * as path from "path" ;
43import * as fs from "fs" ;
54import * as tools from "./SwiftTools" ;
5+ import * as path from "path" ;
66import {
7- workspace ,
8- window ,
97 commands ,
10- languages ,
11- ExtensionContext ,
128 DiagnosticCollection ,
13- OutputChannel ,
9+ ExtensionContext ,
10+ languages ,
1411 TextDocument ,
12+ window ,
13+ workspace ,
1514} from "vscode" ;
16- import { LanguageClient , LanguageClientOptions , ServerOptions } from "vscode-languageclient" ;
17- import { absolutePath } from "./AbsolutePath" ;
18-
19- import * as config from "./config-helpers" ;
20- import output from "./output-channels" ;
21- import { LangaugeServerMode } from "./config-helpers" ;
22-
23- import { statusBarItem } from "./status-bar" ;
15+ import { absolutePath } from "./helpers/AbsolutePath" ;
16+ import * as config from "./vscode/config-helpers" ;
17+ import lsp from "./vscode/lsp-interop" ;
18+ import output from "./vscode/output-channels" ;
19+ import { statusBarItem } from "./vscode/status-bar" ;
2420
2521let swiftBinPath : string | null = null ;
2622let swiftBuildParams : string [ ] = [ "build" ] ;
@@ -39,47 +35,17 @@ export function activate(context: ExtensionContext) {
3935 output . build . log ( "Activating SDE" ) ;
4036
4137 initConfig ( ) ;
42-
43- // Options to control the language client
44- let clientOptions : LanguageClientOptions = {
45- // Register the server for plain text documents
46- documentSelector : [
47- { language : "swift" , scheme : "file" } ,
48- { pattern : "*.swift" , scheme : "file" } ,
49- ] ,
50- synchronize : {
51- configurationSection : [ "swift" , "editor" , "[swift]" ] ,
52- // Notify the server about file changes to '.clientrc files contain in the workspace
53- fileEvents : [
54- workspace . createFileSystemWatcher ( "**/*.swift" ) ,
55- workspace . createFileSystemWatcher ( ".build/*.yaml" ) ,
56- ] ,
57- } ,
58- initializationOptions : {
59- isLSPServerTracingOn : config . isLSPTracingOn ( ) , // used by sourcekites
60- skProtocolProcess,
61- skProtocolProcessAsShellCmd,
62- skCompilerOptions : workspace . getConfiguration ( ) . get ( "sde.sourcekit.compilerOptions" ) ,
63- toolchainPath :
64- workspace . getConfiguration ( "sourcekit-lsp" ) . get < string > ( "toolchainPath" ) || null ,
65- } ,
66- ...currentClientOptions ( context ) ,
67- } ;
68-
69- // Create the language client and start the client.
70- const lspOpts = currentServerOptions ( context ) ;
71- const langClient = new LanguageClient ( "Swift" , lspOpts , clientOptions ) ;
72- context . subscriptions . push ( langClient . start ( ) ) ;
38+ lsp . startLSPClient ( context ) ;
7339
7440 diagnosticCollection = languages . createDiagnosticCollection ( "swift" ) ;
7541 context . subscriptions . push ( diagnosticCollection ) ;
7642
7743 //commands
7844 context . subscriptions . push (
7945 commands . registerCommand ( "sde.commands.buildPackage" , buildSPMPackage ) ,
80- commands . registerCommand ( "sde.commands.restartLanguageServer" , ( ) => {
81- output . build . log ( "sde.commands.restartLanguageServer" ) ;
82- } ) ,
46+ commands . registerCommand ( "sde.commands.restartLanguageServer" , ( ) =>
47+ lsp . restartLSPClient ( context )
48+ ) ,
8349 commands . registerCommand ( "sde.commands.runPackage" , ( ) => {
8450 output . build . log ( "sde.commands.runPackage" ) ;
8551 } )
@@ -102,38 +68,14 @@ function initConfig() {
10268 checkToolsAvailability ( ) ;
10369
10470 //FIXME rootPath may be undefined for adhoc file editing mode???
105- swiftPackageManifestPath = workspace . rootPath
106- ? path . join ( workspace . rootPath , "Package.swift" )
71+ swiftPackageManifestPath = workspace . workspaceFolders [ 0 ]
72+ ? path . join ( workspace . workspaceFolders [ 0 ] . uri . fsPath , "Package.swift" )
10773 : null ;
10874}
10975
110- function currentServerOptions ( context : ExtensionContext ) : ServerOptions {
111- switch ( config . lsp ( ) ) {
112- case LangaugeServerMode . LanguageServer :
113- return config . lspServerOptions ( context ) ;
114- case LangaugeServerMode . SourceKit :
115- return config . sourcekitLspServerOptions ( context ) ;
116- case LangaugeServerMode . SourceKite :
117- return config . sourcekiteServerOptions ( context ) ;
118- }
119- }
120-
121- function currentClientOptions ( _context : ExtensionContext ) : Partial < LanguageClientOptions > {
122- switch ( config . lsp ( ) ) {
123- case LangaugeServerMode . SourceKit :
124- return {
125- documentSelector : [ "swift" , "cpp" , "c" , "objective-c" , "objective-cpp" ] ,
126- synchronize : undefined ,
127- } ;
128- default :
129- return { } ;
130- }
131- }
132-
13376function buildSPMPackage ( ) {
13477 if ( isSPMProject ( ) ) {
13578 statusBarItem . start ( ) ;
136- output . build . log ( "Starting package build" ) ;
13779 tools . buildPackage ( swiftBinPath , workspace . rootPath , swiftBuildParams ) ;
13880 }
13981}
0 commit comments