| 1 | using System.Text.Json.Serialization; |
| 2 | using CascadeIDE.Services; |
| 3 | |
| 4 | namespace CascadeIDE.Models; |
| 5 | |
| 6 | /// <summary>Языковые серверы. TOML: <c>[languages.csharp]</c>, <c>[languages.markdown]</c>.</summary> |
| 7 | public sealed class LanguagesSettings |
| 8 | { |
| 9 | [JsonPropertyName("csharp")] |
| 10 | public CSharpLanguageServerSettings CSharp { get; set; } = new(); |
| 11 | |
| 12 | [JsonPropertyName("markdown")] |
| 13 | public MarkdownLanguageServerSettings Markdown { get; set; } = new(); |
| 14 | } |
| 15 | |
| 16 | /// <summary>C# LSP c дискриминатором режима и вложенными профилями запуска.</summary> |
| 17 | public sealed class CSharpLanguageServerSettings |
| 18 | { |
| 19 | /// <summary>Дискриминатор режима (ParseOnly / OmniSharp / CSharpLs / Custom).</summary> |
| 20 | [JsonPropertyName("mode")] |
| 21 | public string Mode { get; set; } = "ParseOnly"; |
| 22 | |
| 23 | [JsonPropertyName("parse_only")] |
| 24 | public LanguageServerLaunchProfile ParseOnly { get; set; } = new(); |
| 25 | |
| 26 | [JsonPropertyName("omni_sharp")] |
| 27 | public LanguageServerLaunchProfile OmniSharp { get; set; } = new(); |
| 28 | |
| 29 | [JsonPropertyName("csharp_ls")] |
| 30 | public LanguageServerLaunchProfile CSharpLs { get; set; } = new(); |
| 31 | |
| 32 | [JsonPropertyName("custom")] |
| 33 | public LanguageServerLaunchProfile Custom { get; set; } = new(); |
| 34 | |
| 35 | public string GetNormalizedMode() |
| 36 | { |
| 37 | var mode = Mode; |
| 38 | if (string.IsNullOrWhiteSpace(mode)) |
| 39 | return "ParseOnly"; |
| 40 | return mode.Trim(); |
| 41 | } |
| 42 | |
| 43 | public (string Mode, string Executable, string Arguments) ResolveForRuntime() |
| 44 | { |
| 45 | var mode = GetNormalizedMode(); |
| 46 | var profile = GetProfileForMode(mode); |
| 47 | return (mode, profile.ResolveExecutable(), profile.ResolveArguments()); |
| 48 | } |
| 49 | |
| 50 | public void SetMode(string mode) |
| 51 | { |
| 52 | var normalized = string.IsNullOrWhiteSpace(mode) ? "ParseOnly" : mode.Trim(); |
| 53 | Mode = normalized; |
| 54 | } |
| 55 | |
| 56 | public void SetLaunchOverrides(string mode, string executable, string arguments) |
| 57 | { |
| 58 | var profile = GetProfileForMode(mode); |
| 59 | profile.Executable = executable ?? ""; |
| 60 | profile.Arguments = arguments ?? ""; |
| 61 | } |
| 62 | |
| 63 | private LanguageServerLaunchProfile GetProfileForMode(string mode) |
| 64 | { |
| 65 | return mode switch |
| 66 | { |
| 67 | "OmniSharp" => OmniSharp, |
| 68 | "CSharpLs" => CSharpLs, |
| 69 | "Custom" => Custom, |
| 70 | _ => ParseOnly, |
| 71 | }; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | public sealed class LanguageServerLaunchProfile |
| 76 | { |
| 77 | public string Executable { get; set; } = ""; |
| 78 | |
| 79 | /// <summary>TOML: <c>executable_env</c> — <c>PATH</c> (поиск в PATH) или имя переменной с путём (ADR 0149).</summary> |
| 80 | public string ExecutableEnv { get; set; } = ""; |
| 81 | |
| 82 | public string Arguments { get; set; } = ""; |
| 83 | |
| 84 | /// <summary>TOML: <c>arguments_env</c>.</summary> |
| 85 | public string ArgumentsEnv { get; set; } = ""; |
| 86 | |
| 87 | public string ResolveExecutable() => |
| 88 | SettingsEnvResolver.ResolveLaunchPath(Executable, ExecutableEnv); |
| 89 | |
| 90 | public string ResolveArguments() => |
| 91 | SettingsEnvResolver.Resolve(Arguments, ArgumentsEnv); |
| 92 | } |
| 93 | |
| 94 | /// <summary>Markdown LSP c дискриминатором режима и вложенными профилями запуска.</summary> |
| 95 | public sealed class MarkdownLanguageServerSettings |
| 96 | { |
| 97 | [JsonPropertyName("mode")] |
| 98 | public string Mode { get; set; } = "Off"; |
| 99 | |
| 100 | [JsonPropertyName("off")] |
| 101 | public LanguageServerLaunchProfile Off { get; set; } = new(); |
| 102 | |
| 103 | [JsonPropertyName("marksman")] |
| 104 | public LanguageServerLaunchProfile Marksman { get; set; } = new(); |
| 105 | |
| 106 | [JsonPropertyName("custom")] |
| 107 | public LanguageServerLaunchProfile Custom { get; set; } = new(); |
| 108 | |
| 109 | public string GetNormalizedMode() |
| 110 | { |
| 111 | var mode = Mode; |
| 112 | if (string.IsNullOrWhiteSpace(mode)) |
| 113 | return "Off"; |
| 114 | return mode.Trim(); |
| 115 | } |
| 116 | |
| 117 | public (string Mode, string Executable, string Arguments) ResolveForRuntime() |
| 118 | { |
| 119 | var mode = GetNormalizedMode(); |
| 120 | var profile = GetProfileForMode(mode); |
| 121 | return (mode, profile.ResolveExecutable(), profile.ResolveArguments()); |
| 122 | } |
| 123 | |
| 124 | public void SetMode(string mode) |
| 125 | { |
| 126 | var normalized = string.IsNullOrWhiteSpace(mode) ? "Off" : mode.Trim(); |
| 127 | Mode = normalized; |
| 128 | } |
| 129 | |
| 130 | public void SetLaunchOverrides(string mode, string executable, string arguments) |
| 131 | { |
| 132 | var profile = GetProfileForMode(mode); |
| 133 | profile.Executable = executable ?? ""; |
| 134 | profile.Arguments = arguments ?? ""; |
| 135 | } |
| 136 | |
| 137 | private LanguageServerLaunchProfile GetProfileForMode(string mode) |
| 138 | { |
| 139 | return mode switch |
| 140 | { |
| 141 | "Marksman" => Marksman, |
| 142 | "Custom" => Custom, |
| 143 | _ => Off, |
| 144 | }; |
| 145 | } |
| 146 | } |
| 147 | |