-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathSwiftSnippets.ts
110 lines (102 loc) · 3.47 KB
/
SwiftSnippets.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//===----------------------------------------------------------------------===//
//
// This source file is part of the VS Code Swift open source project
//
// Copyright (c) 2024 Apple Inc. and 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 * as vscode from "vscode";
import * as path from "path";
import contextKeys from "./contextKeys";
import { createSwiftTask } from "./tasks/SwiftTaskProvider";
import { WorkspaceContext } from "./WorkspaceContext";
import { createSnippetConfiguration, debugLaunchConfig } from "./debugger/launch";
import { TaskOperation } from "./tasks/TaskQueue";
import configuration from "./configuration";
/**
* Set context key indicating whether current file is a Swift Snippet
* @param ctx Workspace context
*/
export function setSnippetContextKey(ctx: WorkspaceContext) {
if (
ctx.swiftVersion.isLessThan({ major: 5, minor: 7, patch: 0 }) ||
!ctx.currentFolder ||
!ctx.currentDocument
) {
contextKeys.fileIsSnippet = false;
return;
}
const filename = ctx.currentDocument.fsPath;
const snippetsFolder = path.join(ctx.currentFolder.folder.fsPath, "Snippets");
if (filename.startsWith(snippetsFolder)) {
contextKeys.fileIsSnippet = true;
} else {
contextKeys.fileIsSnippet = false;
}
return;
}
/**
* If current file is a Swift Snippet run it
* @param ctx Workspace Context
*/
export async function runSnippet(ctx: WorkspaceContext) {
await debugSnippetWithOptions(ctx, { noDebug: true });
}
/**
* If current file is a Swift Snippet run it in the debugger
* @param ctx Workspace Context
*/
export async function debugSnippet(ctx: WorkspaceContext) {
await debugSnippetWithOptions(ctx, {});
}
export async function debugSnippetWithOptions(
ctx: WorkspaceContext,
options: vscode.DebugSessionOptions
) {
const folderContext = ctx.currentFolder;
if (!ctx.currentDocument || !folderContext) {
return;
}
// create build task
const snippetName = path.basename(ctx.currentDocument.fsPath, ".swift");
const snippetBuildTask = createSwiftTask(
["build", "--product", snippetName],
`Build ${snippetName}`,
{
group: vscode.TaskGroup.Build,
cwd: folderContext.folder,
scope: folderContext.workspaceFolder,
presentationOptions: {
reveal: vscode.TaskRevealKind.Always,
},
showBuildStatus: configuration.showBuildStatus,
},
ctx.toolchain
);
try {
// queue build task and when it is complete run executable in the debugger
await folderContext.taskQueue
.queueOperation(new TaskOperation(snippetBuildTask))
.then(result => {
if (result === 0) {
const snippetDebugConfig = createSnippetConfiguration(
snippetName,
folderContext
);
return debugLaunchConfig(
folderContext.workspaceFolder,
snippetDebugConfig,
options
);
}
});
} catch {
// ignore error if task failed to run
}
}