|
| 1 | +import { commands, Uri, window, workspace } from "vscode"; |
| 2 | +import { absolutePath } from "../helpers/AbsolutePath"; |
| 3 | +import * as config from "./config-helpers"; |
| 4 | +import { LangaugeServerMode } from "./config-helpers"; |
| 5 | +import * as fs from "fs"; |
| 6 | + |
| 7 | +export async function currentLspPreconditions(): Promise<void> { |
| 8 | + await generalPreconditions(); |
| 9 | + switch (config.lsp()) { |
| 10 | + case LangaugeServerMode.LanguageServer: |
| 11 | + return currentLanguageServerPreconditions(); |
| 12 | + case LangaugeServerMode.SourceKit: |
| 13 | + return currentSourcekitLspPreconditions(); |
| 14 | + case LangaugeServerMode.SourceKite: |
| 15 | + return currentSourcekitePreconditions(); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +enum PreconditionActions { |
| 20 | + Retry = "Retry", |
| 21 | + OpenSettings = "Settings", |
| 22 | + InstructionsSourcekitLsp = "Help for sourcekit-lsp", |
| 23 | + InstructionsSourcekite = "Help for sourcekite", |
| 24 | + InstructionsLangserver = "Help for LangserverSwift", |
| 25 | +} |
| 26 | + |
| 27 | +async function generalPreconditions(): Promise<void> { |
| 28 | + const shellPath = absolutePath(workspace.getConfiguration().get("swift.path.shell")); |
| 29 | + if (!shellPath || !fs.existsSync(shellPath)) { |
| 30 | + await handlePreconditionAction( |
| 31 | + await window.showErrorMessage( |
| 32 | + `Wrong shell path ${shellPath} for setting swift.path.shell.`, |
| 33 | + PreconditionActions.Retry, |
| 34 | + PreconditionActions.OpenSettings |
| 35 | + ) |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + const swiftPath = absolutePath(workspace.getConfiguration().get("swift.path.swift_driver_bin")); |
| 40 | + if (!swiftPath || !fs.existsSync(swiftPath)) { |
| 41 | + await handlePreconditionAction( |
| 42 | + await window.showErrorMessage( |
| 43 | + `Swift not found at path ${swiftPath} for setting swift.path.swift_driver_bin`, |
| 44 | + PreconditionActions.Retry, |
| 45 | + PreconditionActions.OpenSettings |
| 46 | + ) |
| 47 | + ); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +async function currentLanguageServerPreconditions(): Promise<void> { |
| 52 | + const lspPath = config.languageServerPath(); |
| 53 | + if (!fs.existsSync(lspPath)) { |
| 54 | + await handlePreconditionAction( |
| 55 | + await window.showErrorMessage( |
| 56 | + `Langserver not found at \`${lspPath}\`. |
| 57 | + Install it and provide the path to \`swift.languageServerPath\`.`, |
| 58 | + PreconditionActions.Retry, |
| 59 | + PreconditionActions.OpenSettings, |
| 60 | + PreconditionActions.InstructionsLangserver |
| 61 | + ) |
| 62 | + ); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +async function currentSourcekitLspPreconditions(): Promise<void> { |
| 67 | + const sourcekitLspPath = config.sourceKitLSPLocation(config.toolchainPath()); |
| 68 | + |
| 69 | + if (!fs.existsSync(sourcekitLspPath)) { |
| 70 | + await handlePreconditionAction( |
| 71 | + await window.showErrorMessage( |
| 72 | + `sourcekit-lsp not found at \`${sourcekitLspPath}\`. |
| 73 | +Install it and provide the path to \`sourcekit-lsp.serverPath\`.`, |
| 74 | + PreconditionActions.Retry, |
| 75 | + PreconditionActions.OpenSettings, |
| 76 | + PreconditionActions.InstructionsSourcekitLsp |
| 77 | + ) |
| 78 | + ); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +async function currentSourcekitePreconditions(): Promise<void> { |
| 83 | + const isDockerMode = workspace |
| 84 | + .getConfiguration() |
| 85 | + .get<boolean>("swift.path.sourcekiteDockerMode", false); |
| 86 | + const sourcekitePath = absolutePath(workspace.getConfiguration().get("swift.path.sourcekite")); |
| 87 | + |
| 88 | + if (!isDockerMode && !fs.existsSync(sourcekitePath)) { |
| 89 | + await handlePreconditionAction( |
| 90 | + await window.showErrorMessage( |
| 91 | + `\`sourcekite\` not found at \`${sourcekitePath}\`. |
| 92 | + Install it and provide the path to \`swift.path.sourcekite\`.`, |
| 93 | + PreconditionActions.Retry, |
| 94 | + PreconditionActions.OpenSettings, |
| 95 | + PreconditionActions.InstructionsSourcekite |
| 96 | + ) |
| 97 | + ); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +async function handlePreconditionAction( |
| 102 | + action: string | PreconditionActions | undefined |
| 103 | +): Promise<void> { |
| 104 | + switch (action) { |
| 105 | + case PreconditionActions.Retry: |
| 106 | + await currentLspPreconditions(); |
| 107 | + break; |
| 108 | + case PreconditionActions.OpenSettings: |
| 109 | + if (action === "Settings") { |
| 110 | + await commands.executeCommand("workbench.action.openSettings"); |
| 111 | + } |
| 112 | + break; |
| 113 | + case PreconditionActions.InstructionsSourcekite: |
| 114 | + await commands.executeCommand( |
| 115 | + "vscode.open", |
| 116 | + Uri.parse( |
| 117 | + "https://github.com/vknabel/vscode-swift-development-environment/tree/2.11.1#using-sourcekit-lsp" |
| 118 | + ) |
| 119 | + ); |
| 120 | + break; |
| 121 | + case PreconditionActions.InstructionsSourcekitLsp: |
| 122 | + await commands.executeCommand( |
| 123 | + "vscode.open", |
| 124 | + Uri.parse( |
| 125 | + "https://github.com/vknabel/vscode-swift-development-environment/tree/2.11.1#using-sourcekite" |
| 126 | + ) |
| 127 | + ); |
| 128 | + break; |
| 129 | + case PreconditionActions.InstructionsLangserver: |
| 130 | + await commands.executeCommand( |
| 131 | + "vscode.open", |
| 132 | + Uri.parse( |
| 133 | + "https://github.com/vknabel/vscode-swift-development-environment/tree/2.11.1#using-langserver-swift" |
| 134 | + ) |
| 135 | + ); |
| 136 | + break; |
| 137 | + } |
| 138 | +} |
0 commit comments