Example #1
0
        /// <summary>
        /// SubjectBuffersConnected happens first
        /// </summary>
        /// <param name="wpfTextView"></param>
        /// <param name="reason"></param>
        /// <param name="subjectBuffers"></param>
        public void SubjectBuffersConnected(IWpfTextView wpfTextView, ConnectionReason reason, Collection <ITextBuffer> subjectBuffers)
        {
            try {
                var logPrefix = $"{nameof(SubjectBuffersConnected)}";
                using (var metrics = Log.WithMetrics($"{logPrefix} ")) {
                    if (wpfTextView == null || !wpfTextView.HasValidDocumentRoles())
                    {
                        return;
                    }

                    IVirtualTextDocument virtualTextDocument = null;
                    if (!TextDocumentExtensions.TryGetTextDocument(TextDocumentFactoryService, wpfTextView, out virtualTextDocument))
                    {
                        Log.Warning($"{logPrefix} Could not create virtualTextDocument");
                        return;
                    }

                    Log.Verbose($"{logPrefix} pre-Lock");
                    lock (WeakTableLock) {
                        Log.Verbose($"{logPrefix} in-Lock");
                        foreach (var buffer in subjectBuffers)
                        {
                            if (!TextBufferTable.TryGetValue(buffer, out HashSet <IWpfTextView> textViews))
                            {
                                textViews = new HashSet <IWpfTextView>();
                                TextBufferTable.Add(buffer, textViews);
                            }

                            textViews.Add(wpfTextView);
                        }
                        using (metrics.Measure($"{logPrefix} Building properties")) {
                            if (virtualTextDocument.SupportsMarkers)
                            {
                                wpfTextView.Properties.GetOrCreateSingletonProperty(PropertyNames.DocumentMarkerManager,
                                                                                    () => new DocumentMarkerManager(CodeStreamAgentServiceFactory.Create(), wpfTextView, virtualTextDocument));
                            }
                            wpfTextView.Properties.GetOrCreateSingletonProperty(PropertyNames.TextViewDocument, () => virtualTextDocument);
                            wpfTextView.Properties.GetOrCreateSingletonProperty(PropertyNames.TextViewState, () => new TextViewState());
#if DEBUG
                            if (TextViewCache == null)
                            {
                                System.Diagnostics.Debugger.Break();
                            }
#endif
                        }
                        TextViewCache.Add(virtualTextDocument, wpfTextView);
                    }
                    Log.Verbose($"{logPrefix} Uri={virtualTextDocument.Uri}");
                }
            }
            catch (Exception ex) {
                Log.Error(ex, nameof(SubjectBuffersConnected));
            }
        }
        /// <summary>
        /// SubjectBuffersConnected happens first
        /// </summary>
        /// <param name="wpfTextView"></param>
        /// <param name="reason"></param>
        /// <param name="subjectBuffers"></param>
        public void SubjectBuffersConnected(IWpfTextView wpfTextView, ConnectionReason reason, Collection <ITextBuffer> subjectBuffers)
        {
            try {
                if (wpfTextView == null || !wpfTextView.HasValidRoles())
                {
                    return;
                }
                if (!TextDocumentExtensions.TryGetTextDocument(TextDocumentFactoryService, wpfTextView.TextBuffer,
                                                               out var textDocument))
                {
                    return;
                }

                lock (WeakTableLock) {
                    foreach (var buffer in subjectBuffers)
                    {
                        if (!TextBufferTable.TryGetValue(buffer, out HashSet <IWpfTextView> textViews))
                        {
                            textViews = new HashSet <IWpfTextView>();
                            TextBufferTable.Add(buffer, textViews);
                        }

                        textViews.Add(wpfTextView);
                    }

                    wpfTextView.Properties.GetOrCreateSingletonProperty(PropertyNames.DocumentMarkerManager,
                                                                        () => new DocumentMarkerManager(CodeStreamAgentServiceFactory.Create(), wpfTextView, textDocument));
                    wpfTextView.Properties.AddProperty(PropertyNames.TextViewFilePath, textDocument.FilePath);
                    wpfTextView.Properties.AddProperty(PropertyNames.TextViewState, new TextViewState());
#if DEBUG
                    if (TextViewCache == null)
                    {
                        Debugger.Break();
                    }
#endif
                    TextViewCache.Add(textDocument.FilePath, wpfTextView);
                }
                Log.Verbose($"{nameof(SubjectBuffersConnected)} FilePath={textDocument.FilePath}");
            }
            catch (Exception ex) {
                Log.Error(ex, nameof(SubjectBuffersConnected));
            }
        }