private AsyncLazy <VersionStamp> CreateLazyLatestDocumentTopLevelChangeVersion(
            TextDocumentState newDocument,
            ImmutableDictionary <DocumentId, DocumentState> newDocumentStates,
            ImmutableDictionary <DocumentId, TextDocumentState> newAdditionalDocumentStates)
        {
            VersionStamp oldVersion;

            if (_lazyLatestDocumentTopLevelChangeVersion.TryGetValue(out oldVersion))
            {
                return(new AsyncLazy <VersionStamp>(c => ComputeTopLevelChangeTextVersionAsync(oldVersion, newDocument, c), cacheResult: true));
            }
            else
            {
                return(new AsyncLazy <VersionStamp>(c => ComputeLatestDocumentTopLevelChangeVersionAsync(newDocumentStates, newAdditionalDocumentStates, c), cacheResult: true));
            }
        }
            public void Dispose(bool joinThreads)
            {
                // Cancel the creation of the process if it is in progress.
                // If it is the cancellation will clean up all resources allocated during the creation.
                CancellationSource.Cancel();

                // If the value has been calculated already, dispose the service.
                InitializedRemoteService initializedService;

                if (InitializedService.TryGetValue(out initializedService) && initializedService.ServiceOpt != null)
                {
                    initializedService.ServiceOpt.Dispose(joinThreads);
                }
            }
        private void GetLatestDependentVersions(
            ImmutableDictionary <DocumentId, DocumentState> newDocumentStates,
            ImmutableDictionary <DocumentId, TextDocumentState> newAdditionalDocumentStates,
            TextDocumentState oldDocument, TextDocumentState newDocument,
            bool recalculateDependentVersions, bool textChanged,
            out AsyncLazy <VersionStamp> dependentDocumentVersion, out AsyncLazy <VersionStamp> dependentSemanticVersion)
        {
            var recalculateDocumentVersion = false;
            var recalculateSemanticVersion = false;

            if (recalculateDependentVersions)
            {
                VersionStamp oldVersion;
                if (oldDocument.TryGetTextVersion(out oldVersion))
                {
                    VersionStamp documentVersion;
                    if (!_lazyLatestDocumentVersion.TryGetValue(out documentVersion) || documentVersion == oldVersion)
                    {
                        recalculateDocumentVersion = true;
                    }

                    VersionStamp semanticVersion;
                    if (!_lazyLatestDocumentTopLevelChangeVersion.TryGetValue(out semanticVersion) || semanticVersion == oldVersion)
                    {
                        recalculateSemanticVersion = true;
                    }
                }
            }

            dependentDocumentVersion = recalculateDocumentVersion ?
                                       new AsyncLazy <VersionStamp>(c => ComputeLatestDocumentVersionAsync(newDocumentStates, newAdditionalDocumentStates, c), cacheResult: true) :
                                       textChanged ?
                                       new AsyncLazy <VersionStamp>(newDocument.GetTextVersionAsync, cacheResult: true) :
                                       _lazyLatestDocumentVersion;

            dependentSemanticVersion = recalculateSemanticVersion ?
                                       new AsyncLazy <VersionStamp>(c => ComputeLatestDocumentTopLevelChangeVersionAsync(newDocumentStates, newAdditionalDocumentStates, c), cacheResult: true) :
                                       textChanged?
                                       CreateLazyLatestDocumentTopLevelChangeVersion(newDocument, newDocumentStates, newAdditionalDocumentStates) :
                                           _lazyLatestDocumentTopLevelChangeVersion;
        }
 /// <summary>
 /// Get the current syntax tree for the document if the text is already loaded and the tree is already parsed.
 /// In almost all cases, you should call <see cref="GetSyntaxTreeAsync"/> to fetch the tree, which will parse the tree
 /// if it's not already parsed.
 /// </summary>
 public bool TryGetSyntaxTree(out SyntaxTreeBase syntaxTree)
 {
     return(_lazySyntaxTree.TryGetValue(out syntaxTree));
 }