Skip to content

Commit

Permalink
Content load timing issue #120
Browse files Browse the repository at this point in the history
  • Loading branch information
Samir L. Boulema committed Sep 10, 2021
1 parent 2dbda4d commit 76ee60d
Show file tree
Hide file tree
Showing 41 changed files with 339 additions and 535 deletions.
136 changes: 3 additions & 133 deletions CodeNav.Shared/CodeNavMargin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using CodeNav.Helpers;
using Microsoft.VisualStudio.LanguageServices;
using Microsoft.VisualStudio.PlatformUI;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Outlining;
using HorizontalAlignment = System.Windows.HorizontalAlignment;
using CodeNav.Models;
using System.Linq;
using Task = System.Threading.Tasks.Task;
using Community.VisualStudio.Toolkit;
using Microsoft.VisualStudio.Shell;

namespace CodeNav
Expand All @@ -27,19 +21,11 @@ public class CodeNavMargin : DockPanel, IWpfTextViewMargin
public readonly IWpfTextView _textView;
private readonly ColumnDefinition _codeNavColumn;
private readonly Grid _codeNavGrid;
private readonly IOutliningManagerService _outliningManagerService;
private readonly IOutliningManager _outliningManager;
private readonly VisualStudioWorkspace _workspace;
public readonly MarginSideEnum MarginSide;

public CodeNavMargin(IWpfTextViewHost textViewHost, IOutliningManagerService outliningManagerService,
VisualStudioWorkspace workspace, MarginSideEnum side)
public CodeNavMargin(IWpfTextViewHost textViewHost, MarginSideEnum side)
{
// Wire up references for the event handlers in RegisterEvents
_textView = textViewHost.TextView;
_outliningManagerService = outliningManagerService;
_outliningManager = OutliningHelper.GetOutliningManager(outliningManagerService, _textView);
_workspace = workspace;
MarginSide = side;

// Add the view/content to the margin area
Expand All @@ -54,8 +40,6 @@ public CodeNavMargin(IWpfTextViewHost textViewHost, IOutliningManagerService out
}

Children.Add(_codeNavGrid);

RegisterEvents();
}

private Grid CreateGrid(IWpfTextViewHost textViewHost)
Expand Down Expand Up @@ -98,7 +82,7 @@ private Grid CreateGrid(IWpfTextViewHost textViewHost)

var columnIndex = (MarginSideEnum)General.Instance.MarginSide == MarginSideEnum.Left ? 0 : 2;

_control = new CodeViewUserControl(grid.ColumnDefinitions[columnIndex], _outliningManagerService, _workspace);
_control = new CodeViewUserControl(grid.ColumnDefinitions[columnIndex]);

grid.Children.Add(_control as UIElement);

Expand Down Expand Up @@ -129,7 +113,7 @@ private Grid CreateGridTop(IWpfTextViewHost textViewHost)

VSColorTheme.ThemeChanged += VSColorTheme_ThemeChanged;

_control = new CodeViewUserControlTop(grid.RowDefinitions[0], _outliningManagerService, _workspace);
_control = new CodeViewUserControlTop(grid.RowDefinitions[0]);

Grid.SetRow(_control as UIElement, 0);
Grid.SetRow(textViewHost.HostControl, 1);
Expand Down Expand Up @@ -176,106 +160,6 @@ private void DragCompleted(object sender, DragCompletedEventArgs e)
}
}

public void RegisterEvents()
{
// Subscribe to Cursor move event
if (_textView?.Caret != null &&
!General.Instance.DisableHighlight)
{
_textView.Caret.PositionChanged -= Caret_PositionChanged;
_textView.Caret.PositionChanged += Caret_PositionChanged;
}

// Subscribe to TextBuffer changes
if ((_textView.TextBuffer as ITextBuffer2) != null &&
General.Instance.ShowHistoryIndicators)
{
var textBuffer2 = _textView.TextBuffer as ITextBuffer2;

textBuffer2.ChangedOnBackground -= TextBuffer_ChangedOnBackground;
textBuffer2.ChangedOnBackground += TextBuffer_ChangedOnBackground;
}

// Subscribe to Document events
VS.Events.DocumentEvents.Saved += DocumentEvents_Saved;

// Subscribe to Outlining events
if (_outliningManager != null)
{
_outliningManager.RegionsExpanded -= OutliningManager_RegionsExpanded;
_outliningManager.RegionsExpanded += OutliningManager_RegionsExpanded;
_outliningManager.RegionsCollapsed -= OutliningManager_RegionsCollapsed;
_outliningManager.RegionsCollapsed += OutliningManager_RegionsCollapsed;
}

VS.Events.WindowEvents.ActiveFrameChanged -= WindowEvents_ActiveFrameChanged;
VS.Events.WindowEvents.ActiveFrameChanged += WindowEvents_ActiveFrameChanged;
}

private void WindowEvents_ActiveFrameChanged(ActiveFrameChangeEventArgs obj)
=> WindowChangedEvent(obj).FireAndForget();

private void DocumentEvents_Saved(string e)
=> UpdateDocument().FireAndForget();

private async Task WindowChangedEvent(ActiveFrameChangeEventArgs obj)
{
if (obj.OldFrame == obj.NewFrame)
{
return;
}

var documentView = await obj.NewFrame.GetDocumentViewAsync();

var filePath = documentView?.Document?.FilePath;

if (string.IsNullOrEmpty(filePath))
{
return;
}

UpdateDocument(filePath).FireAndForget();
}

private void TextBuffer_ChangedOnBackground(object sender, TextContentChangedEventArgs e)
{
var changedSpans = e.Changes.Select(c => c.OldSpan);

foreach (var span in changedSpans)
{
HistoryHelper.AddItemToHistory(_control.CodeDocumentViewModel, span);
}
}

private void OutliningManager_RegionsCollapsed(object sender, RegionsCollapsedEventArgs e) =>
_control.RegionsCollapsed(e);

private void OutliningManager_RegionsExpanded(object sender, RegionsExpandedEventArgs e) =>
_control.RegionsExpanded(e);

public void UnRegisterEvents()
{
_textView.Caret.PositionChanged -= Caret_PositionChanged;

if ((_textView.TextBuffer as ITextBuffer2) != null)
{
(_textView.TextBuffer as ITextBuffer2).ChangedOnBackground -= TextBuffer_ChangedOnBackground;
}
}

private void Caret_PositionChanged(object sender, CaretPositionChangedEventArgs e)
{
var oldLineNumber = e.OldPosition.BufferPosition.GetContainingLine().LineNumber;
var newLineNumber = e.NewPosition.BufferPosition.GetContainingLine().LineNumber;

if (oldLineNumber == newLineNumber)
{
return;
}

_control.HighlightCurrentItem(newLineNumber);
}

#endregion

#region IWpfTextViewMargin
Expand Down Expand Up @@ -361,8 +245,6 @@ public void Dispose()
return;
}

UnRegisterEvents();

GC.SuppressFinalize(this);
_isDisposed = true;
}
Expand All @@ -378,18 +260,6 @@ private void ThrowIfDisposed()
}
}

private async Task UpdateDocument(string filePath = "")
{
if (!await DocumentHelper.IsLargeDocument().ConfigureAwait(false))
{
_control.UpdateDocument(filePath);
}
else
{
_control.CodeDocumentViewModel.CodeDocument = PlaceholderHelper.CreateLineThresholdPassedItem();
}
}

#endregion
}
}
40 changes: 6 additions & 34 deletions CodeNav.Shared/CodeNavMarginFactory.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition;
using CodeNav.Helpers;
using Microsoft.VisualStudio.LanguageServices;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Utilities;
using CodeNav.Models;
Expand All @@ -20,15 +17,9 @@ namespace CodeNav
[TextViewRole(PredefinedTextViewRoles.Debuggable)] // This is to prevent the margin from loading in the diff view
internal sealed class CodeNavLeftFactory : IWpfTextViewMarginProvider
{
[Import(typeof(SVsServiceProvider))]
private IServiceProvider ServiceProvider { get; set; }

[Import(typeof(VisualStudioWorkspace))]
private VisualStudioWorkspace Workspace { get; set; }

public IWpfTextViewMargin CreateMargin(IWpfTextViewHost wpfTextViewHost, IWpfTextViewMargin marginContainer)
{
return CodeNavFactory.CreateMargin(wpfTextViewHost, Workspace, ServiceProvider, MarginSideEnum.Left);
return CodeNavFactory.CreateMargin(wpfTextViewHost, MarginSideEnum.Left);
}
}

Expand All @@ -43,17 +34,9 @@ public IWpfTextViewMargin CreateMargin(IWpfTextViewHost wpfTextViewHost, IWpfTex
[TextViewRole(PredefinedTextViewRoles.Debuggable)] // This is to prevent the margin from loading in the diff view
internal sealed class CodeNavRightFactory : IWpfTextViewMarginProvider
{
#pragma warning disable 0649
[Import(typeof(SVsServiceProvider))]
private readonly IServiceProvider ServiceProvider;
#pragma warning restore 0649

[Import(typeof(VisualStudioWorkspace))]
private VisualStudioWorkspace Workspace { get; set; }

public IWpfTextViewMargin CreateMargin(IWpfTextViewHost wpfTextViewHost, IWpfTextViewMargin marginContainer)
{
return CodeNavFactory.CreateMargin(wpfTextViewHost, Workspace, ServiceProvider, MarginSideEnum.Right);
return CodeNavFactory.CreateMargin(wpfTextViewHost, MarginSideEnum.Right);
}
}

Expand All @@ -68,17 +51,9 @@ public IWpfTextViewMargin CreateMargin(IWpfTextViewHost wpfTextViewHost, IWpfTex
[TextViewRole(PredefinedTextViewRoles.Debuggable)]
internal sealed class CodeNavTopFactory : IWpfTextViewMarginProvider
{
#pragma warning disable 0649
[Import(typeof(SVsServiceProvider))]
private readonly IServiceProvider ServiceProvider;
#pragma warning restore 0649

[Import(typeof(VisualStudioWorkspace))]
private VisualStudioWorkspace Workspace { get; set; }

public IWpfTextViewMargin CreateMargin(IWpfTextViewHost wpfTextViewHost, IWpfTextViewMargin marginContainer)
{
var margin = CodeNavFactory.CreateMargin(wpfTextViewHost, Workspace, ServiceProvider, MarginSideEnum.Top);
var margin = CodeNavFactory.CreateMargin(wpfTextViewHost, MarginSideEnum.Top);
new NavBarOverrider(margin as CodeNavMargin);

return margin;
Expand All @@ -87,17 +62,14 @@ public IWpfTextViewMargin CreateMargin(IWpfTextViewHost wpfTextViewHost, IWpfTex

internal static class CodeNavFactory
{
public static IWpfTextViewMargin CreateMargin(IWpfTextViewHost wpfTextViewHost,
VisualStudioWorkspace visualStudioWorkspace, IServiceProvider serviceProvider, MarginSideEnum side)
public static IWpfTextViewMargin CreateMargin(IWpfTextViewHost wpfTextViewHost, MarginSideEnum side)
{
if ((MarginSideEnum)General.Instance.MarginSide != side)
{
return null;
}

var outliningManagerService = OutliningHelper.GetOutliningManagerService(serviceProvider);

var codeNav = new CodeNavMargin(wpfTextViewHost, outliningManagerService, visualStudioWorkspace, side);
var codeNav = new CodeNavMargin(wpfTextViewHost, side);

return codeNav;
}
Expand Down
Loading

0 comments on commit 76ee60d

Please sign in to comment.