protected override Task <MSBuildParseResult> StartParseAsync( ITextSnapshot2 snapshot, MSBuildParseResult previousParse, ITextSnapshot2 previousSnapshot, CancellationToken token) { if (runtimeInformation == null) { EnsureInitialized(); } return(Task.Run(() => { var oldDoc = previousParse?.MSBuildDocument; var filepath = TryGetFilePath(); MSBuildRootDocument doc = MSBuildRootDocument.Empty; try { doc = MSBuildRootDocument.Parse(snapshot.GetTextSource(filepath), oldDoc, schemaProvider, runtimeInformation, taskMetadataBuilder, token); } catch (Exception ex) { LoggingService.LogError("Unhandled error in MSBuild parser", ex); doc = MSBuildRootDocument.Empty; } return new MSBuildParseResult(doc, doc.XDocument, doc.Errors); })); }
static MSBuildRootDocument ParseDoc(string contents, string filename = "myfile.csproj") { var runtimeInfo = new MSBuildEnvironmentRuntimeInformation(); var textSource = new StringTextSource(contents); var schemaProvider = new MSBuildSchemaProvider(); var taskBuilder = new NoopTaskMetadataBuilder(); return(MSBuildRootDocument.Parse(textSource, filename, null, schemaProvider, runtimeInfo, taskBuilder, default)); }
protected void VerifyDiagnostics(string source, ICollection <MSBuildAnalyzer> analyzers, bool includeCoreDiagnostics, params MSBuildDiagnostic[] expectedDiagnostics) { var token = CancellationToken.None; var(env, _) = MSBuildTestEnvironment.EnsureInitialized(); var host = env.GetEditorHost(); var doc = MSBuildRootDocument.Parse( new StringTextSource(source), "FakeProject.csproj", null, host.GetService <MSBuildSchemaProvider> (), host.GetService <IRuntimeInformation> (), host.GetService <ITaskMetadataBuilder> (), token); var analyzerDriver = new MSBuildAnalyzerDriver(); if (analyzers != null && analyzers.Count > 0) { analyzerDriver.AddAnalyzers(analyzers); } else if (!includeCoreDiagnostics) { throw new ArgumentException("Analyzers can only be null or empty if core diagnostics are included", nameof(analyzers)); } var actualDiagnostics = analyzerDriver.Analyze(doc, includeCoreDiagnostics, token); foreach (var expectedDiag in expectedDiagnostics) { bool found = false; for (int i = 0; i < actualDiagnostics.Count; i++) { var actualDiag = actualDiagnostics[i]; // todo: compare properties if (actualDiag.Descriptor == expectedDiag.Descriptor && actualDiag.Span.Equals(expectedDiag.Span)) { found = true; actualDiagnostics.RemoveAt(i); break; } } if (!found) { Assert.Fail($"Did not find expected diagnostic {expectedDiag.Descriptor.Id}@{expectedDiag.Span.Start}-{expectedDiag.Span.End}"); } } if (actualDiagnostics.Count > 0) { var diag = actualDiagnostics[0]; Assert.Fail($"Found unexpected diagnostic {diag.Descriptor.Id}@{diag.Span.Start}-{diag.Span.End}"); } }
internal static ParsedDocument ParseInternal(ParseOptions options, CancellationToken token) { var oldDoc = (options.OldParsedDocument as MSBuildParsedDocument)?.Document; var runtimeInformation = oldDoc ?.RuntimeInformation ?? new MSBuildRuntimeInformation(Runtime.SystemAssemblyService.CurrentRuntime, MSBuildToolsVersion.Unknown); var schemaProvider = new MonoDevelopMSBuildSchemaProvider(); var doc = new MSBuildParsedDocument(options.FileName); doc.Flags |= ParsedDocumentFlags.NonSerializable; doc.Document = MSBuildRootDocument.Parse(options.FileName, options.Content, oldDoc, schemaProvider, runtimeInformation, token); doc.XDocument = doc.Document.XDocument; return(doc); }
protected override Task <MSBuildParseResult> StartOperationAsync( XmlParseResult input, MSBuildParseResult previousOutput, XmlParseResult previousInput, CancellationToken token) { return(Task.Run(() => { var oldDoc = previousOutput?.MSBuildDocument; MSBuildRootDocument doc; try { doc = MSBuildRootDocument.Parse( input.TextSnapshot.GetTextSource(), filepath, oldDoc, SchemaProvider, RuntimeInformation, TaskMetadataBuilder, token); var analyzerDiagnostics = analyzerDriver.Analyze(doc, token); doc.Diagnostics.Clear(); doc.Diagnostics.AddRange(analyzerDiagnostics); } catch (Exception ex) when(!(ex is OperationCanceledException && token.IsCancellationRequested)) { LoggingService.LogError("Unhandled error in MSBuild parser", ex); doc = MSBuildRootDocument.Empty; } // for some reason the VS debugger thinks cancellation exceptions aren't handled? catch (OperationCanceledException) when(token.IsCancellationRequested) { return null; } return new MSBuildParseResult(doc, doc.Diagnostics, input.TextSnapshot); }, token)); }