|
1 | 1 | using Google.Protobuf; |
2 | 2 | using Microsoft.Extensions.DependencyInjection; |
3 | 3 | using Newtonsoft.Json.Linq; |
| 4 | +using OmniSharp.Extensions.LanguageServer.Protocol; |
4 | 5 | using OmniSharp.Extensions.LanguageServer.Protocol.Models; |
5 | 6 | using OmniSharp.Extensions.LanguageServer.Protocol.Server; |
6 | 7 | using OmniSharp.Extensions.LanguageServer.Protocol.Window; |
@@ -166,6 +167,14 @@ public static LanguageServerOptions ConfigureOptions(LanguageServerOptions optio |
166 | 167 | } |
167 | 168 | ); |
168 | 169 |
|
| 170 | + // Register 'get document state' command |
| 171 | + options.OnExecuteCommand<DocumentStateOutput>( |
| 172 | + (commandParams) => GetDocumentStateAsync(workspace, commandParams), (_, _) => new ExecuteCommandRegistrationOptions |
| 173 | + { |
| 174 | + Commands = new[] { Commands.GetDocumentState } |
| 175 | + } |
| 176 | + ); |
| 177 | + |
169 | 178 | return options; |
170 | 179 | } |
171 | 180 |
|
@@ -213,6 +222,55 @@ private static Task<Container<ProjectInfo>> ListProjectsAsync(Workspace workspac |
213 | 222 | return Task.FromResult(Container.From(info)); |
214 | 223 | } |
215 | 224 |
|
| 225 | + private static Task<DocumentStateOutput> GetDocumentStateAsync(Workspace workspace, ExecuteCommandParams<DocumentStateOutput> commandParams) |
| 226 | + { |
| 227 | + if (commandParams.Arguments?.Count < 1) |
| 228 | + { |
| 229 | + throw new ArgumentException("Expected at least one argument passed to " + Commands.GetDocumentState); |
| 230 | + } |
| 231 | + if (commandParams.Arguments![0].Type != JTokenType.String) |
| 232 | + { |
| 233 | + throw new ArgumentException("Expected parameter 0 of " + Commands.GetDocumentState + " to be a string"); |
| 234 | + } |
| 235 | + |
| 236 | + |
| 237 | + DocumentUri uri; |
| 238 | + try |
| 239 | + { |
| 240 | + uri = DocumentUri.Parse((string)commandParams.Arguments[0], strict: true); |
| 241 | + } |
| 242 | + catch |
| 243 | + { |
| 244 | + return Task.FromResult(DocumentStateOutput.InvalidUri); |
| 245 | + } |
| 246 | + |
| 247 | + var projects = workspace.GetProjectsForUri(uri); |
| 248 | + |
| 249 | + if (!projects.Any()) |
| 250 | + { |
| 251 | + return Task.FromResult(new DocumentStateOutput(uri) |
| 252 | + { |
| 253 | + State = DocumentStateOutput.DocumentState.NotFound |
| 254 | + }); |
| 255 | + } |
| 256 | + |
| 257 | + var nodes = projects |
| 258 | + .SelectMany(project => project.Nodes.Where(n => n.File != null && uri == n.File.Uri)) |
| 259 | + .NonNull() |
| 260 | + .DistinctBy(n => n.UniqueTitle) |
| 261 | + ; |
| 262 | + |
| 263 | + // The document contains errors if any of its projects have an error |
| 264 | + // diagnostic attributable to this file |
| 265 | + var containsErrors = projects.SelectMany(p => p.Diagnostics.Where(d => d.FileName == uri.ToString())).Any(d => d.Severity == Yarn.Compiler.Diagnostic.DiagnosticSeverity.Error); |
| 266 | + |
| 267 | + return Task.FromResult(new DocumentStateOutput(uri) |
| 268 | + { |
| 269 | + Nodes = nodes.ToList(), |
| 270 | + State = containsErrors ? DocumentStateOutput.DocumentState.ContainsErrors : DocumentStateOutput.DocumentState.Valid |
| 271 | + }); |
| 272 | + } |
| 273 | + |
216 | 274 | private static Task<TextDocumentEdit> AddNodeToDocumentAsync(Workspace workspace, ExecuteCommandParams<TextDocumentEdit> commandParams) |
217 | 275 | { |
218 | 276 | if (commandParams.Arguments == null) |
|
0 commit comments