|
1 | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
4 | | -import * as path from 'path'; |
5 | | -import * as fs from 'fs-extra'; |
6 | | -import { parse } from 'jsonc-parser'; |
7 | | -import { DebugConfiguration, Uri, WorkspaceFolder } from 'vscode'; |
8 | | -import { getConfiguration, getWorkspaceFolder } from '../../../common/vscodeapi'; |
9 | | -import { traceError, traceLog, traceWarn } from '../../../common/log/logging'; |
| 4 | +import * as path from "path"; |
| 5 | +import * as fs from "fs-extra"; |
| 6 | +import { parse } from "jsonc-parser"; |
| 7 | +import { DebugConfiguration, Uri, WorkspaceFolder } from "vscode"; |
10 | 8 |
|
11 | | -export async function getConfigurationsForWorkspace(workspace: WorkspaceFolder): Promise<DebugConfiguration[]> { |
12 | | - traceLog('Getting configurations for workspace'); |
13 | | - const filename = path.join(workspace.uri.fsPath, '.vscode', 'launch.json'); |
14 | | - if (!(await fs.pathExists(filename))) { |
15 | | - return getConfigurationsFromSettings(workspace); |
16 | | - } |
17 | | - const text = await fs.readFile(filename, 'utf-8'); |
18 | | - const parsed = parse(text, [], { allowTrailingComma: true, disallowComments: false }); |
19 | | - // no launch.json or no configurations found in launch.json, look in settings.json |
20 | | - if (!parsed || !parsed.configurations) { |
21 | | - traceLog('No configurations found in launch.json, looking in settings.json.'); |
22 | | - return getConfigurationsFromSettings(workspace); |
23 | | - } |
24 | | - // configurations found in launch.json, verify them then return |
25 | | - if (!Array.isArray(parsed.configurations) || parsed.configurations.length === 0) { |
26 | | - traceError('Invalid configurations in launch.json'); |
27 | | - throw Error('Invalid configurations in launch.json'); |
28 | | - } |
29 | | - if (!parsed.version) { |
30 | | - traceWarn('Missing field in launch.json: version'); |
31 | | - } |
32 | | - traceLog('Using configuration in launch.json'); |
33 | | - return parsed.configurations; |
| 9 | +import { traceError, traceLog, traceWarn } from "../../../common/log/logging"; |
| 10 | +import { |
| 11 | + getConfiguration, |
| 12 | + getWorkspaceFolder, |
| 13 | +} from "../../../common/vscodeapi"; |
| 14 | + |
| 15 | +export async function getConfigurationsForWorkspace( |
| 16 | + workspace: WorkspaceFolder, |
| 17 | +): Promise<DebugConfiguration[]> { |
| 18 | + traceLog("Getting configurations for workspace"); |
| 19 | + const filename = path.join(workspace.uri.fsPath, ".vscode", "launch.json"); |
| 20 | + if (!(await fs.pathExists(filename))) { |
| 21 | + return getConfigurationsFromSettings(workspace); |
| 22 | + } |
| 23 | + const text = await fs.readFile(filename, "utf-8"); |
| 24 | + const parsed = parse(text, [], { |
| 25 | + allowTrailingComma: true, |
| 26 | + disallowComments: false, |
| 27 | + }); |
| 28 | + // no launch.json or no configurations found in launch.json, look in settings.json |
| 29 | + if (!parsed || !parsed.configurations) { |
| 30 | + traceLog( |
| 31 | + "No configurations found in launch.json, looking in settings.json.", |
| 32 | + ); |
| 33 | + return getConfigurationsFromSettings(workspace); |
| 34 | + } |
| 35 | + // configurations found in launch.json, verify them then return |
| 36 | + if ( |
| 37 | + !Array.isArray(parsed.configurations) || |
| 38 | + parsed.configurations.length === 0 |
| 39 | + ) { |
| 40 | + traceError("Invalid configurations in launch.json"); |
| 41 | + throw Error("Invalid configurations in launch.json"); |
| 42 | + } |
| 43 | + if (!parsed.version) { |
| 44 | + traceWarn("Missing field in launch.json: version"); |
| 45 | + } |
| 46 | + traceLog("Using configuration in launch.json"); |
| 47 | + return parsed.configurations; |
34 | 48 | } |
35 | 49 |
|
36 | | -export async function getConfigurationsByUri(uri?: Uri): Promise<DebugConfiguration[]> { |
37 | | - if (uri) { |
38 | | - const workspace = getWorkspaceFolder(uri); |
39 | | - if (workspace) { |
40 | | - return getConfigurationsForWorkspace(workspace); |
41 | | - } |
42 | | - } |
43 | | - return []; |
| 50 | +export async function getConfigurationsByUri( |
| 51 | + uri?: Uri, |
| 52 | +): Promise<DebugConfiguration[]> { |
| 53 | + if (uri) { |
| 54 | + const workspace = getWorkspaceFolder(uri); |
| 55 | + if (workspace) { |
| 56 | + return getConfigurationsForWorkspace(workspace); |
| 57 | + } |
| 58 | + } |
| 59 | + return []; |
44 | 60 | } |
45 | 61 |
|
46 | | -export function getConfigurationsFromSettings(workspace: WorkspaceFolder): DebugConfiguration[] { |
47 | | - // look in settings.json |
48 | | - const codeWorkspaceConfig = getConfiguration('launch', workspace); |
49 | | - // if this includes user configs, how do I make sure it selects the workspace ones first |
50 | | - if ( |
51 | | - !codeWorkspaceConfig.configurations || |
52 | | - !Array.isArray(codeWorkspaceConfig.configurations) || |
53 | | - codeWorkspaceConfig.configurations.length === 0 |
54 | | - ) { |
55 | | - traceLog('No configurations found in settings.json or launch.json.'); |
56 | | - return []; |
57 | | - } |
58 | | - traceLog('Using configuration in workspace settings.json.'); |
59 | | - return codeWorkspaceConfig.configurations; |
| 62 | +export function getConfigurationsFromSettings( |
| 63 | + workspace: WorkspaceFolder, |
| 64 | +): DebugConfiguration[] { |
| 65 | + // look in settings.json |
| 66 | + const codeWorkspaceConfig = getConfiguration("launch", workspace); |
| 67 | + // if this includes user configs, how do I make sure it selects the workspace ones first |
| 68 | + if ( |
| 69 | + !codeWorkspaceConfig.configurations || |
| 70 | + !Array.isArray(codeWorkspaceConfig.configurations) || |
| 71 | + codeWorkspaceConfig.configurations.length === 0 |
| 72 | + ) { |
| 73 | + traceLog("No configurations found in settings.json or launch.json."); |
| 74 | + return []; |
| 75 | + } |
| 76 | + traceLog("Using configuration in workspace settings.json."); |
| 77 | + return codeWorkspaceConfig.configurations; |
60 | 78 | } |
0 commit comments