public static SourceGeneratedDocumentState Create(
            SourceGeneratedDocumentIdentity documentIdentity,
            SourceText generatedSourceText,
            ParseOptions parseOptions,
            HostLanguageServices languageServices,
            SolutionServices solutionServices)
        {
            var textAndVersion = TextAndVersion.Create(generatedSourceText, VersionStamp.Create());
            var textSource     = new ConstantValueSource <TextAndVersion>(textAndVersion);
            var treeSource     = CreateLazyFullyParsedTree(
                textSource,
                documentIdentity.DocumentId.ProjectId,
                documentIdentity.FilePath,
                parseOptions,
                languageServices);

            return(new SourceGeneratedDocumentState(
                       documentIdentity,
                       languageServices,
                       solutionServices,
                       documentServiceProvider: null,
                       new DocumentInfo.DocumentAttributes(
                           documentIdentity.DocumentId,
                           name: documentIdentity.HintName,
                           folders: SpecializedCollections.EmptyReadOnlyList <string>(),
                           parseOptions.Kind,
                           filePath: documentIdentity.FilePath,
                           isGenerated: true,
                           designTimeOnly: false),
                       parseOptions,
                       textSource,
                       treeSource));
        }
Exemple #2
0
        public static SourceGeneratedDocumentState Create(
            string hintName,
            SourceText generatedSourceText,
            SyntaxTree generatedSyntaxTree,
            DocumentId documentId,
            ISourceGenerator sourceGenerator,
            HostLanguageServices languageServices,
            SolutionServices solutionServices,
            CancellationToken cancellationToken)
        {
            var options  = generatedSyntaxTree.Options;
            var filePath = generatedSyntaxTree.FilePath;

            var textAndVersion = TextAndVersion.Create(generatedSourceText, VersionStamp.Create());
            ValueSource <TextAndVersion> textSource = new ConstantValueSource <TextAndVersion>(textAndVersion);

            var root = generatedSyntaxTree.GetRoot(cancellationToken);

            Contract.ThrowIfNull(languageServices.SyntaxTreeFactory, "We should not have a generated syntax tree for a language that doesn't support trees.");

            if (languageServices.SyntaxTreeFactory.CanCreateRecoverableTree(root))
            {
                // We will only create recoverable text if we can create a recoverable tree; if we created a
                // recoverable text but not a new tree, it would mean tree.GetText() could still potentially return
                // the non-recoverable text, but asking the document directly for it's text would give a recoverable
                // text with a different object identity.
                textSource = CreateRecoverableText(textAndVersion, solutionServices);

                generatedSyntaxTree = languageServices.SyntaxTreeFactory.CreateRecoverableTree(
                    documentId.ProjectId,
                    filePath: generatedSyntaxTree.FilePath,
                    options,
                    textSource,
                    generatedSourceText.Encoding,
                    root);
            }

            var treeAndVersion = TreeAndVersion.Create(generatedSyntaxTree, textAndVersion.Version);

            return(new SourceGeneratedDocumentState(
                       languageServices,
                       solutionServices,
                       documentServiceProvider: null,
                       new DocumentInfo.DocumentAttributes(
                           documentId,
                           name: hintName,
                           folders: SpecializedCollections.EmptyReadOnlyList <string>(),
                           options.Kind,
                           filePath: filePath,
                           isGenerated: true,
                           designTimeOnly: false),
                       options,
                       sourceText: null, // don't strongly hold the text
                       textSource,
                       treeAndVersion,
                       sourceGenerator,
                       hintName));
        }
Exemple #3
0
            public static CachedRecoverableSyntaxRoot <TRoot> Create(
                AbstractSyntaxTreeFactoryService service,
                string filePath,
                ParseOptions options,
                ValueSource <TextAndVersion> text,
                TRoot root,
                bool reparse)
            {
                var rootSource = new ConstantValueSource <TRoot>(root);
                int length     = root.FullSpan.Length;

                if (reparse)
                {
                    return(new ReparsedSyntaxRoot <TRoot>(service, filePath, options, text, rootSource, length));
                }
                else
                {
                    return(new SerializedSyntaxRoot <TRoot>(service, filePath, options, text, rootSource, length));
                }
            }