-
Notifications
You must be signed in to change notification settings - Fork 81
Add a new Generate SourceKit-LSP Configuration
command
#1726
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the VS Code Swift open source project | ||
// | ||
// Copyright (c) 2025 the VS Code Swift project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import { join } from "path"; | ||
import * as vscode from "vscode"; | ||
import { FolderContext } from "../FolderContext"; | ||
import { selectFolder } from "../ui/SelectFolderQuickPick"; | ||
import { WorkspaceContext } from "../WorkspaceContext"; | ||
import configuration from "../configuration"; | ||
|
||
export async function generateSourcekitConfiguration(ctx: WorkspaceContext): Promise<boolean> { | ||
if (ctx.folders.length === 0) { | ||
return false; | ||
} | ||
|
||
if (ctx.folders.length === 1) { | ||
const folder = ctx.folders[0]; | ||
const success = await createSourcekitConfiguration(ctx, folder); | ||
void vscode.window.showTextDocument(vscode.Uri.file(sourcekitConfigFilePath(folder))); | ||
return success; | ||
} | ||
|
||
const foldersToGenerate: FolderContext[] = await selectFolder( | ||
ctx, | ||
"Select a folder to generate a SourceKit-LSP configuration for" | ||
); | ||
if (!foldersToGenerate.length) { | ||
return false; | ||
} | ||
|
||
return ( | ||
await Promise.all( | ||
foldersToGenerate.map(folder => createSourcekitConfiguration(ctx, folder)) | ||
) | ||
).reduceRight((prev, curr) => prev || curr); | ||
} | ||
|
||
export const sourcekitFolderPath = (f: FolderContext) => join(f.folder.fsPath, ".sourcekit-lsp"); | ||
export const sourcekitConfigFilePath = (f: FolderContext) => | ||
join(sourcekitFolderPath(f), "config.json"); | ||
|
||
async function createSourcekitConfiguration( | ||
workspaceContext: WorkspaceContext, | ||
folderContext: FolderContext | ||
): Promise<boolean> { | ||
const sourcekitFolder = vscode.Uri.file(sourcekitFolderPath(folderContext)); | ||
const sourcekitConfigFile = vscode.Uri.file(sourcekitConfigFilePath(folderContext)); | ||
|
||
try { | ||
await vscode.workspace.fs.stat(sourcekitConfigFile); | ||
return true; | ||
} catch (error) { | ||
if ((error as NodeJS.ErrnoException).code !== "ENOENT") { | ||
workspaceContext.outputChannel.appendLine( | ||
`Failed to read file at ${sourcekitConfigFile.fsPath}: ${error}` | ||
); | ||
} | ||
// Ignore, don't care if the file doesn't exist yet | ||
} | ||
|
||
try { | ||
const stats = await vscode.workspace.fs.stat(sourcekitFolder); | ||
if (stats.type !== vscode.FileType.Directory) { | ||
void vscode.window.showErrorMessage( | ||
`File ${sourcekitFolder.fsPath} already exists but is not a directory` | ||
); | ||
return false; | ||
} | ||
} catch (error) { | ||
if ((error as NodeJS.ErrnoException).code !== "ENOENT") { | ||
workspaceContext.outputChannel.appendLine( | ||
`Failed to read folder at ${sourcekitFolder.fsPath}: ${error}` | ||
); | ||
} | ||
await vscode.workspace.fs.createDirectory(sourcekitFolder); | ||
} | ||
const version = folderContext.toolchain.swiftVersion; | ||
const versionString = `${version.major}.${version.minor}`; | ||
let branch = | ||
configuration.lsp.configurationBranch || | ||
(version.dev ? "main" : `release/${versionString}`); | ||
if (!(await checkURLExists(schemaURL(branch)))) { | ||
branch = "main"; | ||
} | ||
await vscode.workspace.fs.writeFile( | ||
sourcekitConfigFile, | ||
Buffer.from( | ||
JSON.stringify( | ||
{ | ||
$schema: schemaURL(branch), | ||
}, | ||
undefined, | ||
2 | ||
) | ||
) | ||
); | ||
return true; | ||
} | ||
|
||
const schemaURL = (branch: string) => | ||
`https://raw.githubusercontent.com/swiftlang/sourcekit-lsp/refs/heads/${branch}/config.schema.json`; | ||
|
||
async function checkURLExists(url: string): Promise<boolean> { | ||
try { | ||
const response = await fetch(url, { method: "HEAD" }); | ||
return response.ok; | ||
} catch { | ||
return false; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the VS Code Swift open source project | ||
// | ||
// Copyright (c) 2025 the VS Code Swift project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import { WorkspaceContext } from "../WorkspaceContext"; | ||
import * as vscode from "vscode"; | ||
import { FolderContext } from "../FolderContext"; | ||
|
||
type SelectFolderQuickPick = AllQuickPickItem | FolderQuickPickItem; | ||
|
||
interface AllQuickPickItem extends vscode.QuickPickItem { | ||
type: "all"; | ||
} | ||
|
||
interface FolderQuickPickItem extends vscode.QuickPickItem { | ||
type: "folder"; | ||
folder: FolderContext; | ||
} | ||
|
||
/** | ||
* Select a folder from the workspace context | ||
* @param ctx | ||
* @param labels Map "type" to the display label | ||
* @returns The selected folder or undefined if there was no selection | ||
*/ | ||
export async function selectFolder( | ||
ctx: WorkspaceContext, | ||
placeHolder: string, | ||
labels: Record<string, string> = {} | ||
): Promise<FolderContext[]> { | ||
const quickPickItems: SelectFolderQuickPick[] = ctx.folders.map(folder => ({ | ||
type: "folder", | ||
folder, | ||
label: folder.name, | ||
detail: folder.workspaceFolder.uri.fsPath, | ||
})); | ||
quickPickItems.push({ type: "all", label: labels["all"] || "Generate For All Folders" }); | ||
const selection = await vscode.window.showQuickPick(quickPickItems, { | ||
matchOnDetail: true, | ||
placeHolder, | ||
}); | ||
|
||
const folders: FolderContext[] = []; | ||
if (!selection) { | ||
return folders; | ||
} | ||
|
||
if (selection.type === "all") { | ||
folders.push(...ctx.folders); | ||
} else { | ||
folders.push(selection.folder); | ||
} | ||
return folders; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.