Example #1
0
        public async Task <bool> SerializeAsync(IPersistentStorageService persistentService, Project project, TextDocument?textDocument, string key, ImmutableArray <DiagnosticData> items, CancellationToken cancellationToken)
        {
            Contract.ThrowIfFalse(textDocument == null || textDocument.Project == project);

            using var stream = SerializableBytes.CreateWritableStream();

            using (var writer = new ObjectWriter(stream, leaveOpen: true, cancellationToken))
            {
                WriteDiagnosticData(writer, items, cancellationToken);
            }

            var storage = await persistentService.GetStorageAsync(project.Solution, cancellationToken).ConfigureAwait(false);

            await using var _ = storage.ConfigureAwait(false);

            stream.Position = 0;

            var writeTask = (textDocument != null) ?
                            textDocument is Document document?
                            storage.WriteStreamAsync(document, key, stream, cancellationToken) :
                            storage.WriteStreamAsync(GetSerializationKeyForNonSourceDocument(textDocument, key), stream, cancellationToken) :
                                storage.WriteStreamAsync(project, key, stream, cancellationToken);

            return(await writeTask.ConfigureAwait(false));
        }
Example #2
0
        public async ValueTask <ImmutableArray <DiagnosticData> > DeserializeAsync(
            IPersistentStorageService persistentService,
            Project project,
            TextDocument?textDocument,
            string key,
            CancellationToken cancellationToken
            )
        {
            Contract.ThrowIfFalse(textDocument == null || textDocument.Project == project);

            var storage = await persistentService
                          .GetStorageAsync(project.Solution, cancellationToken)
                          .ConfigureAwait(false);

            await using var _ = storage.ConfigureAwait(false);

            var readTask =
                (textDocument != null)
                    ? textDocument is Document document
                        ? storage.ReadStreamAsync(document, key, cancellationToken)
                        : storage.ReadStreamAsync(
                    GetSerializationKeyForNonSourceDocument(textDocument, key),
                    cancellationToken
                    )
                    : storage.ReadStreamAsync(project, key, cancellationToken);

            using var stream = await readTask.ConfigureAwait(false);

            using var reader = ObjectReader.TryGetReader(
                      stream,
                      cancellationToken: cancellationToken
                      );

            if (
                reader == null ||
                !TryReadDiagnosticData(
                    reader,
                    project,
                    textDocument,
                    cancellationToken,
                    out var data
                    )
                )
            {
                return(default);