Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 65 additions & 41 deletions src/Cody.VisualStudio/Services/DocumentsSyncService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Cody.Core.DocumentSync;
using Cody.Core.Logging;
using Cody.Core.Settings;
using Cody.Core.Trace;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Editor;
Expand All @@ -12,7 +13,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Cody.Core.Settings;
using System.Threading;

namespace Cody.VisualStudio.Services
{
Expand Down Expand Up @@ -248,67 +249,90 @@ int IVsRunningDocTableEvents.OnAfterSave(uint docCookie)

int IVsRunningDocTableEvents.OnBeforeDocumentWindowShow(uint docCookie, int fFirstShow, IVsWindowFrame pFrame)
{
trace.TraceEvent("OnBeforeDocumentWindowShow");
if (lastShowDocCookie != docCookie)
try
{
var path = rdt.GetDocumentInfo(docCookie).Moniker;
if (path == null) return VSConstants.S_OK;

if (!isSubscribed.Contains(docCookie))
trace.TraceEvent("OnBeforeDocumentWindowShow");
if (lastShowDocCookie != docCookie)
{
trace.TraceEvent("OnSubscribeDocument", path);
var path = rdt.GetDocumentInfo(docCookie).Moniker;
if (path == null) return VSConstants.S_OK;

var content = rdt.GetRunningDocumentContents(docCookie);
if (content == null) return VSConstants.S_OK;

var textView = GetVsTextView(pFrame);
if (textView != null)
if (!isSubscribed.Contains(docCookie))
{
var wpfTextView = editorAdaptersFactoryService.GetWpfTextView(textView);
if (wpfTextView != null)
{
Subscribe(wpfTextView);
trace.TraceEvent("OnSubscribeDocument", path);

if (!openNotificationSend.Contains(docCookie))
{

var docRange = GetDocumentSelection(wpfTextView);
var visibleRange = GetVisibleRange(wpfTextView);
var content = rdt.GetRunningDocumentContents(docCookie);
if (content == null) return VSConstants.S_OK;

documentActions.OnOpened(path, content, visibleRange, docRange);
openNotificationSend.Add(docCookie);
var textView = GetVsTextView(pFrame);
if (textView != null)
{
var wpfTextView = editorAdaptersFactoryService.GetWpfTextView(textView);
if (wpfTextView != null)
{
Subscribe(wpfTextView, path, docCookie, content);
}

isSubscribed.Add(docCookie);
}
}
}

trace.TraceEvent("OnDocumentFocus", path);
documentActions.OnFocus(path);
trace.TraceEvent("OnDocumentFocus", path);
documentActions.OnFocus(path);

lastShowDocCookie = docCookie;
lastShowDocCookie = docCookie;
}
}
catch (Exception ex)
{
log.Error("OnBeforeDocumentWindowShow failed", ex);
}

return VSConstants.S_OK;
}

private void Subscribe(IWpfTextView textView)
private void Subscribe(IWpfTextView textView, string path, uint docCookie, string content)
{
textView.TextBuffer.Properties.AddProperty(typeof(IWpfTextView), textView);
textView.Selection.SelectionChanged += OnSelectionChanged;
textView.TextBuffer.ChangedLowPriority += OnTextBufferChanged;
textView.Closed += UnsubscribeOnClosed;
try
{

textView.TextBuffer.Properties.AddProperty(typeof(IWpfTextView), textView);
textView.Selection.SelectionChanged += OnSelectionChanged;
textView.TextBuffer.ChangedLowPriority += OnTextBufferChanged;
textView.Closed += UnsubscribeOnClosed;

if (!openNotificationSend.Contains(docCookie))
{

var docRange = GetDocumentSelection(textView);
var visibleRange = GetVisibleRange(textView);

documentActions.OnOpened(path, content, visibleRange, docRange);
openNotificationSend.Add(docCookie);
}

isSubscribed.Add(docCookie);

}
catch (Exception ex)
{
log.Error("Subscribe failed", ex);
}
}

private void UnsubscribeOnClosed(object sender, EventArgs e)
{
var textView = (IWpfTextView)sender;
try
{
var textView = (IWpfTextView)sender;

textView.TextBuffer.Properties.RemoveProperty(typeof(IWpfTextView));
textView.Selection.SelectionChanged -= OnSelectionChanged;
textView.TextBuffer.ChangedLowPriority -= OnTextBufferChanged;
textView.Closed -= UnsubscribeOnClosed;
textView.TextBuffer.Properties.RemoveProperty(typeof(IWpfTextView));
textView.Selection.SelectionChanged -= OnSelectionChanged;
textView.TextBuffer.ChangedLowPriority -= OnTextBufferChanged;
textView.Closed -= UnsubscribeOnClosed;
}
catch (Exception ex)
{
log.Error("Unsubscribe failed", ex);
}
}

int IVsRunningDocTableEvents.OnAfterDocumentWindowHide(uint docCookie, IVsWindowFrame pFrame) => VSConstants.S_OK;
Expand Down Expand Up @@ -422,7 +446,7 @@ public int OnAfterAttributeChangeEx(uint docCookie, uint grfAttribs, IVsHierarch
{
trace.TraceEvent("OnRename");
documentActions.OnRename(pszMkDocumentOld, pszMkDocumentNew);
}
}
return VSConstants.S_OK;
}
}
Expand Down
Loading