public IWpfTextViewMargin CreateMargin(IWpfTextViewHost wpfTextViewHost, IWpfTextViewMargin parent)
        {
            try {
                if (wpfTextViewHost == null || !wpfTextViewHost.TextView.HasValidMarginRoles())
                {
                    return(null);
                }
                if (!TextDocumentExtensions.TryGetTextDocument(TextDocumentFactoryService,
                                                               wpfTextViewHost.TextView.TextBuffer, out var textDocument))
                {
                    return(null);
                }

                using (Log.CriticalOperation($"{nameof(DocumentMarkMarginProvider)} {nameof(CreateMargin)}", LogEventLevel.Debug)) {
                    TextViewMargin = new DocumentMarkMargin(
                        _viewTagAggregatorFactoryService,
                        _glyphFactoryProviders,
                        wpfTextViewHost, SessionService.Value, SettingsServiceFactory.Value.GetOrCreate
                            (nameof(DocumentMarkMarginProvider))
                        );

                    return(TextViewMargin);
                }
            }
            catch (Exception ex) {
                Log.Error(ex, nameof(CreateMargin));
                System.Diagnostics.Debug.WriteLine(ex);
#if DEBUG
                System.Diagnostics.Debugger.Break();
#endif
                return(null);
            }
        }
Ejemplo n.º 2
0
        public ITagger <T> CreateTagger <T>(ITextView textView, ITextBuffer buffer) where T : ITag
        {
            var wpfTextView = textView as IWpfTextView;

            if (wpfTextView == null || textView.TextBuffer != buffer)
            {
                return(null);
            }
            if (!wpfTextView.HasValidTaggerRoles())
            {
                return(null);
            }
            if (!TextDocumentExtensions.TryGetTextDocument(TextDocumentFactoryService, textView.TextBuffer, out var textDocument))
            {
                return(null);
            }

            var sessionService = (Package.GetGlobalService(typeof(SComponentModel)) as IComponentModel)?.GetService <ISessionService>();

            if (sessionService == null)
            {
                return(null);
            }

            return(textView.Properties.GetOrCreateSingletonProperty(typeof(DocumentMarkTagger),
                                                                    () => new DocumentMarkTagger(sessionService, textView, buffer)) as ITagger <T>);
        }
Ejemplo n.º 3
0
        public ActiveTextEditorSelection GetActiveTextEditorSelection()
        {
            try {
                var range = GetActiveEditorSelectedRange(out IVsTextView view);
                if (view == null)
                {
                    return(null);
                }

                var wpfTextView = _componentModel.GetService <IVsEditorAdaptersFactoryService>()?.GetWpfTextView(view);
                if (wpfTextView == null)
                {
                    return(null);
                }

                if (!TextDocumentExtensions.TryGetTextDocument(_componentModel.GetService <ITextDocumentFactoryService>(),
                                                               wpfTextView, out var textDocument))
                {
                    return(null);
                }

                return(new ActiveTextEditorSelection(textDocument.Uri, range));
            }
            catch (Exception ex) {
                Log.Error(ex, nameof(ActiveTextEditorSelection));
            }
            return(null);
        }
        public IWpfTextViewMargin CreateMargin(IWpfTextViewHost wpfTextViewHost, IWpfTextViewMargin parent)
        {
            try {
                if (wpfTextViewHost == null ||
                    !wpfTextViewHost.TextView.HasValidRoles())
                {
                    return(null);
                }
                if (!TextDocumentExtensions.TryGetTextDocument(TextDocumentFactoryService,
                                                               wpfTextViewHost.TextView.TextBuffer, out var textDocument))
                {
                    return(null);
                }

                var componentModel         = Package.GetGlobalService(typeof(SComponentModel)) as IComponentModel;
                var sessionService         = componentModel?.GetService <ISessionService>();
                var settingsServiceFactory = componentModel?.GetService <ISettingsServiceFactory>();

                TextViewMargin = new DocumentMarkMargin(
                    _viewTagAggregatorFactoryService,
                    _glyphFactoryProviders,
                    wpfTextViewHost, sessionService, settingsServiceFactory.Create()
                    );

                return(TextViewMargin);
            }
            catch (Exception ex) {
                System.Diagnostics.Debug.WriteLine(ex);
#if DEBUG
                System.Diagnostics.Debugger.Break();
#endif
                return(null);
            }
        }
Ejemplo n.º 5
0
        private async System.Threading.Tasks.Task OnTextViewLayoutChangedSubjectHandlerAsync(TextViewLayoutChangedSubject subject)
        {
            try {
                Debug.WriteLine($"{nameof(OnTextViewLayoutChangedSubjectHandlerAsync)} RequiresMarkerCheck={subject.RequiresMarkerCheck} TriggerTextViewLayoutChanged={subject.TriggerTextViewLayoutChanged}");

                var wpfTextView = subject.WpfTextView;
                if (TextDocumentExtensions.TryGetTextDocument(TextDocumentFactoryService, wpfTextView.TextBuffer, out var textDocument))
                {
                    // don't trigger for changes that don't result in lines being added or removed

                    if (wpfTextView.Properties.TryGetProperty(PropertyNames.DocumentMarkerManager, out DocumentMarkerManager documentMarkerManager) && documentMarkerManager != null)
                    {
                        // get markers if it's null (first time) or we did something that isn't scrolling/vertical changes
                        if (subject.RequiresMarkerCheck)
                        {
                            var updated = await documentMarkerManager.TrySetMarkersAsync();

                            if (updated)
                            {
                                subject.TriggerTextViewLayoutChanged = true;
                            }
                        }
                    }
                    if (subject.TriggerTextViewLayoutChanged)
                    {
                        //only send this if we have an actual change
                        try {
                            if (wpfTextView.InLayout || wpfTextView.IsClosed)
                            {
                                return;
                            }

                            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                            _ = OnVisibleRangesSubjectHandlerAsync(textDocument.FilePath.ToUri(), wpfTextView);

                            wpfTextView
                            .Properties
                            .GetProperty <List <ICodeStreamWpfTextViewMargin> >(PropertyNames.TextViewMarginProviders)
                            .OnTextViewLayoutChanged(subject.Sender, subject.EventArgs);
                        }
                        catch (InvalidOperationException ex) {
                            Log.Warning(ex, nameof(OnTextViewLayoutChangedSubjectHandlerAsync));
                        }
                        catch (Exception ex) {
                            Log.Error(ex, nameof(OnTextViewLayoutChangedSubjectHandlerAsync));
                        }
                    }
                }
            }
            catch (Exception ex) {
                Log.Warning(ex, nameof(OnTextViewLayoutChanged));
            }

            await System.Threading.Tasks.Task.CompletedTask;
        }
Ejemplo n.º 6
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));
            }
        }
        public ISuggestedActionsSource CreateSuggestedActionsSource(ITextView textView, ITextBuffer textBuffer)
        {
            if (textBuffer == null || textView == null)
            {
                return(null);
            }
            if (!TextDocumentExtensions.TryGetTextDocument(_textDocumentFactoryService, textBuffer, out var textDocument))
            {
                return(null);
            }

            return(new CodemarkSuggestedActionsSource((IComponentModel)_serviceProvider.GetService(typeof(SComponentModel)),
                                                      textView, textBuffer, textDocument));
        }
Ejemplo n.º 8
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 {
                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));
            }
        }
Ejemplo n.º 9
0
        private ActiveTextEditor GetActiveTextEditor(IVsTextView textView)
        {
            try {
                var wpfTextView = GetActiveWpfTextView(textView);
                if (wpfTextView == null)
                {
                    return(null);
                }
                if (!TextDocumentExtensions.TryGetTextDocument(_componentModel.GetService <ITextDocumentFactoryService>(), wpfTextView, out var textDocument))
                {
                    return(null);
                }

                return(ToActiveTextEditor(wpfTextView, textDocument));
            }
            catch (Exception ex) {
                Log.Error(ex, nameof(GetActiveTextEditor));
            }
            return(null);
        }
Ejemplo n.º 10
0
        private void OnDocumentMarkerChanged(IWpfTextView wpfTextView, DocumentMarkerChangedEvent e)
        {
            Debug.WriteLine($"{nameof(OnDocumentMarkerChanged)}");

            if (!TextDocumentExtensions.TryGetTextDocument(TextDocumentFactoryService, wpfTextView.TextBuffer, out var textDocument))
            {
                return;
            }

            var fileUri = textDocument.FilePath.ToUri();

            if (fileUri == null)
            {
                Log.Verbose($"{ nameof(fileUri)} is null");
                return;
            }
            try {
                if (e.Uri.EqualsIgnoreCase(fileUri))
                {
                    Log.Debug($"{nameof(DocumentMarkerChangedEvent)} for {fileUri}");

                    wpfTextView
                    .Properties
                    .GetProperty <DocumentMarkerManager>(PropertyNames.DocumentMarkerManager)
                    .TrySetMarkers(true);

                    wpfTextView
                    .Properties
                    .GetProperty <List <ICodeStreamWpfTextViewMargin> >(PropertyNames.TextViewMarginProviders)
                    .OnMarkerChanged();
                }
                else
                {
                    Log.Verbose($"{nameof(DocumentMarkerChangedEvent)} ignored for {fileUri}");
                }
            }
            catch (Exception ex) {
                Log.Warning(ex, $"{nameof(DocumentMarkerChangedEvent)} for {fileUri}");
            }
        }
Ejemplo n.º 11
0
        public ActiveTextEditor GetActiveTextEditor(ITextDocumentFactoryService textDocumentFactoryService, IWpfTextView wpfTextView)
        {
            try {
                if (textDocumentFactoryService == null || wpfTextView == null)
                {
                    return(null);
                }
                if (!TextDocumentExtensions.TryGetTextDocument(textDocumentFactoryService, wpfTextView.TextBuffer, out var textDocument))
                {
                    return(null);
                }

                return(new ActiveTextEditor(wpfTextView,
                                            textDocument.FileName,
                                            textDocument.Uri,
                                            wpfTextView.TextSnapshot.LineCount));
            }
            catch (Exception ex) {
                Log.Error(ex, nameof(GetActiveTextEditor));
            }
            return(null);
        }
Ejemplo n.º 12
0
        public IWpfTextViewMargin CreateMargin(IWpfTextViewHost wpfTextViewHost, IWpfTextViewMargin containerMargin)
        {
            try {
                var containerMarginAsVerticalScrollBar = containerMargin as IVerticalScrollBar;
                if (containerMarginAsVerticalScrollBar == null)
                {
                    return(null);
                }

                if (!wpfTextViewHost.TextView.HasValidMarginRoles())
                {
                    return(null);
                }
                if (!TextDocumentExtensions.TryGetTextDocument(TextDocumentFactoryService,
                                                               wpfTextViewHost.TextView.TextBuffer, out var textDocument))
                {
                    return(null);
                }

                using (Log.CriticalOperation($"{nameof(DocumentMarkScrollbarProvider)} {nameof(CreateMargin)}", LogEventLevel.Debug)) {
                    TextViewMargin = new DocumentMarkScrollbar(
                        wpfTextViewHost,
                        containerMarginAsVerticalScrollBar, SessionService.Value
                        );

                    return(TextViewMargin);
                }
            }
            catch (Exception ex) {
                Log.Error(ex, nameof(CreateMargin));
                System.Diagnostics.Debug.WriteLine(ex);
#if DEBUG
                System.Diagnostics.Debugger.Break();
#endif
            }

            return(null);
        }
        public IWpfTextViewMargin CreateMargin(IWpfTextViewHost wpfTextViewHost, IWpfTextViewMargin containerMargin)
        {
            try {
                var containerMarginAsVerticalScrollBar = containerMargin as IVerticalScrollBar;
                if (containerMarginAsVerticalScrollBar == null)
                {
                    return(null);
                }

                if (!wpfTextViewHost.TextView.HasValidRoles())
                {
                    return(null);
                }
                if (!TextDocumentExtensions.TryGetTextDocument(TextDocumentFactoryService,
                                                               wpfTextViewHost.TextView.TextBuffer, out var textDocument))
                {
                    return(null);
                }

                var sessionService = (Package.GetGlobalService(typeof(SComponentModel)) as IComponentModel)
                                     ?.GetService <ISessionService>();

                TextViewMargin = new DocumentMarkScrollbar(
                    wpfTextViewHost,
                    containerMarginAsVerticalScrollBar, sessionService
                    );

                return(TextViewMargin);
            }
            catch (Exception ex) {
                System.Diagnostics.Debug.WriteLine(ex);
#if DEBUG
                System.Diagnostics.Debugger.Break();
#endif
            }

            return(null);
        }
Ejemplo n.º 14
0
        private ActiveTextEditor GetActiveTextEditorCore(IWpfTextView wpfTextView)
        {
            try {
                if (wpfTextView == null)
                {
                    Log.Verbose($"{nameof(wpfTextView)} is null");
                    return(null);
                }

                if (!TextDocumentExtensions.TryGetTextDocument(_componentModel.GetService <ITextDocumentFactoryService>(), wpfTextView.TextBuffer, out IVirtualTextDocument virtualTextDocument))
                {
                    return(null);
                }

                return(new ActiveTextEditor(wpfTextView,
                                            virtualTextDocument.FileName,
                                            virtualTextDocument.Uri,
                                            wpfTextView.TextSnapshot.LineCount));
            }
            catch (Exception ex) {
                Log.Error(ex, nameof(GetActiveTextEditor));
            }
            return(null);
        }