Modern Apps git

xtool.git

Public
git clone https://git.maproj.org/xtool.git
main ▾

xtool.git / Sources / XToolSupport / LSPConfig.swift

LSPConfig.swift91 lines · 3.0 KiBRaw
1import XUtils
2import Foundation
3import PackLib
4
5enum LSPConfig {
6 static let bsp: (String, String) = (
7 ".bsp/xtool.json",
8 """
9 {
10 "name": "xtool",
11 "version": "1.0",
12 "bspVersion": "2.2.0",
13 "languages": ["c", "cpp", "objective-c", "objective-cpp", "swift"],
14 "argv": ["/usr/bin/env", "xtool", "dev", "build-server"]
15 }
16 """
17 )
18
19 static let legacyLSP: (String, String) = (
20 ".sourcekit-lsp/config.json",
21 """
22 {
23 "swiftPM": {
24 "swiftSDK": "arm64-apple-ios"
25 }
26 }
27 """
28 )
29
30 nonisolated(nonsending) static func tryEnsure(
31 using schema: PackSchema,
32 quiet: Bool = false,
33 currentDirectory directory: URL = URL(filePath: FileManager.default.currentDirectoryPath)
34 ) async {
35 do {
36 try await ensure(using: schema, quiet: quiet, currentDirectory: directory)
37 } catch {
38 d.out.print("warning: failed to validate LSP configuration: \(error)")
39 }
40 }
41
42 nonisolated(nonsending) static func ensure(
43 using schema: PackSchema,
44 quiet: Bool = false,
45 currentDirectory directory: URL = URL(filePath: FileManager.default.currentDirectoryPath)
46 ) async throws {
47 if schema.skipLSP == true {
48 return
49 }
50
51 if try await SwiftVersion.current.supportsSwiftBuild {
52 try await ensure(primary: bsp, secondary: legacyLSP, quiet: quiet, currentDirectory: directory)
53 } else {
54 try await ensure(primary: legacyLSP, secondary: bsp, quiet: quiet, currentDirectory: directory)
55 }
56 }
57
58 // creates the primary LSP-support file if it doesn't exist, and cleans up old files
59 private nonisolated(nonsending) static func ensure(
60 primary: (String, String),
61 secondary: (String, String),
62 quiet: Bool,
63 currentDirectory directory: URL
64 ) async throws {
65 let primaryURL = directory.appendingPathComponent(primary.0)
66 if FileManager.default.fileExists(atPath: primaryURL.path) {
67 return
68 }
69
70 try? FileManager.default.createDirectory(
71 at: primaryURL.deletingLastPathComponent(),
72 withIntermediateDirectories: true
73 )
74 try Data((primary.1 + "\n").utf8).write(to: primaryURL)
75
76 let secondaryURL = directory.appendingPathComponent(secondary.0)
77 if let secondaryData = try? await Data(reading: secondaryURL),
78 let secondaryString = String(data: secondaryData, encoding: .utf8),
79 secondaryString.trimmingCharacters(in: .whitespacesAndNewlines) == secondary.1 {
80 try? FileManager.default.removeItem(at: secondaryURL)
81 }
82
83 if !quiet {
84 d.out.print("""
85 [LSP] Created \(primary.0) for enhanced IDE support. You may
86 need to restart your editor for the change to take effect.
87 You can disable this with `skipLSP: true` in xtool.yml.
88 """)
89 }
90 }
91}