-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathVsCodeSettings.cs
More file actions
111 lines (99 loc) · 4.37 KB
/
Copy pathVsCodeSettings.cs
File metadata and controls
111 lines (99 loc) · 4.37 KB
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
111
using System.Text.Json;
namespace MSBuild.CompileCommands.Extractor
{
/// <summary>
/// Helpers that wire the extractor's output into a Visual Studio Code workspace
/// for use with the Microsoft C/C++ extension (cpptools). These helpers are
/// independent of the extractor output format; they only run when the user
/// passes --c-cpp-properties (which is mutually exclusive with --format rich).
/// </summary>
public static class VsCodeSettings
{
/// <summary>
/// Generate a .vscode/c_cpp_properties.json that references the compile_commands.json,
/// and a .vscode/settings.json that turns on cpptools squiggles and verbose logging.
/// </summary>
public static void GenerateCCppProperties(string compileCommandsPath, string platform, string baseDir)
{
var vsCodeDir = Path.Combine(baseDir, ".vscode");
Directory.CreateDirectory(vsCodeDir);
var propsPath = Path.Combine(vsCodeDir, "c_cpp_properties.json");
if (File.Exists(propsPath))
{
Console.WriteLine($"Warning: {propsPath} already exists, overwriting");
}
// Make compileCommands path relative using ${workspaceFolder} if possible
string compileCommandsRef;
var fullCcPath = Path.GetFullPath(compileCommandsPath);
var fullBaseDir = Path.GetFullPath(baseDir).TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
if (fullCcPath.StartsWith(fullBaseDir, StringComparison.OrdinalIgnoreCase))
compileCommandsRef = "${workspaceFolder}/" + fullCcPath[fullBaseDir.Length..].Replace('\\', '/');
else
compileCommandsRef = fullCcPath.Replace('\\', '/');
var intelliSenseMode = platform.ToLowerInvariant() switch
{
"x64" => "msvc-x64",
"win32" or "x86" => "msvc-x86",
"arm64" or "arm64ec" => "msvc-arm64",
"arm" => "msvc-arm",
_ => "msvc-x64"
};
var configName = platform.ToLowerInvariant() switch
{
"x64" => "MSVC x64",
"win32" or "x86" => "MSVC x86",
"arm64" => "MSVC ARM64",
"arm" => "MSVC ARM",
_ => "MSVC"
};
var config = new
{
configurations = new[]
{
new
{
name = configName,
compileCommands = compileCommandsRef,
intelliSenseMode = intelliSenseMode
}
},
version = 4
};
var json = JsonSerializer.Serialize(config, new JsonSerializerOptions
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
File.WriteAllText(propsPath, json);
Console.WriteLine($"Wrote {propsPath}");
// Also generate .vscode/settings.json with C++ diagnostics enabled
var settingsPath = Path.Combine(vsCodeDir, "settings.json");
var settings = new Dictionary<string, object>
{
["C_Cpp.errorSquiggles"] = "enabled",
["C_Cpp.loggingLevel"] = "Debug",
["C_Cpp.default.enableConfigurationSquiggles"] = true
};
if (File.Exists(settingsPath))
{
// Merge into existing settings, don't overwrite unrelated keys
try
{
var existing = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(File.ReadAllText(settingsPath));
if (existing != null)
{
foreach (var kv in existing)
{
if (!settings.ContainsKey(kv.Key))
settings[kv.Key] = kv.Value;
}
}
}
catch { /* overwrite if unparseable */ }
}
var settingsJson = JsonSerializer.Serialize(settings, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(settingsPath, settingsJson);
Console.WriteLine($"Wrote {settingsPath}");
}
}
}