diff --git a/src/Cody.VisualStudio/Services/DocumentsSyncService.cs b/src/Cody.VisualStudio/Services/DocumentsSyncService.cs index 6614db7..bea346c 100644 --- a/src/Cody.VisualStudio/Services/DocumentsSyncService.cs +++ b/src/Cody.VisualStudio/Services/DocumentsSyncService.cs @@ -11,6 +11,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; namespace Cody.VisualStudio.Services { @@ -19,7 +20,7 @@ public class DocumentsSyncService : IVsRunningDocTableEvents private static readonly TraceLogger trace = new TraceLogger(nameof(DocumentsSyncService)); private RunningDocumentTable rdt; - private uint rdtCookie = 0; + private uint rdtCookie = 0; private readonly IVsUIShell vsUIShell; private readonly IVsEditorAdaptersFactoryService editorAdaptersFactoryService; @@ -30,7 +31,10 @@ public class DocumentsSyncService : IVsRunningDocTableEvents private HashSet openNotificationSend = new HashSet(); private HashSet isSubscribed = new HashSet(); - public DocumentsSyncService(IVsUIShell vsUIShell, IDocumentSyncActions documentActions, IVsEditorAdaptersFactoryService editorAdaptersFactoryService, ILog log) + public DocumentsSyncService(IVsUIShell vsUIShell, + IDocumentSyncActions documentActions, + IVsEditorAdaptersFactoryService editorAdaptersFactoryService, + ILog log) { this.rdt = new RunningDocumentTable(); this.vsUIShell = vsUIShell; @@ -47,6 +51,7 @@ public void Initialize() try { + uint activeCookie = 0; foreach (var frame in GetOpenDocuments()) { if (frame.GetProperty((int)__VSFPROPID.VSFPROPID_DocCookie, out object cookie) != VSConstants.S_OK) continue; @@ -57,6 +62,18 @@ public void Initialize() documentActions.OnOpened(path, content, null, null); openNotificationSend.Add(docCookie); + + if (frame.IsOnScreen(out int onScreen) == VSConstants.S_OK && onScreen == 1) activeCookie = docCookie; + } + + if (activeCookie != 0) + { + var path = rdt.GetDocumentInfo(activeCookie).Moniker; + if (path != null) + { + trace.TraceEvent("OnInitFocus"); + documentActions.OnFocus(path); + } } } finally @@ -120,19 +137,19 @@ private DocumentRange GetVisibleRange(ITextView textView) if (ThreadHelper.CheckAccess()) { var lines = textView.TextViewLines; - if(lines != null && lines.IsValid) + if (lines != null && lines.IsValid) { try { firstVisiblePosition = ToDocumentPosition(lines.FirstVisibleLine.Start); lastVisiblePosition = ToDocumentPosition(lines.LastVisibleLine.End); } - catch(ObjectDisposedException) + catch (ObjectDisposedException) { return null; } } - else return null; + else return null; } else return null; @@ -158,7 +175,7 @@ private DocumentRange GetDocumentSelection(ITextView textView) } else return null; - if(start.Line > end.Line || (start.Line == end.Line && start.Column > end.Column)) swap = true; + if (start.Line > end.Line || (start.Line == end.Line && start.Column > end.Column)) swap = true; return new DocumentRange { @@ -217,7 +234,7 @@ int IVsRunningDocTableEvents.OnBeforeDocumentWindowShow(uint docCookie, int fFir if (!isSubscribed.Contains(docCookie)) { trace.TraceEvent("OnSubscribeDocument", path); - + var textView = GetVsTextView(pFrame); if (textView != null) { @@ -303,7 +320,7 @@ private void OnSelectionChanged(object sender, EventArgs e) var textView = ((ITextSelection)sender).TextView; var path = GetFilePath(textView); - if(path == null) return; + if (path == null) return; var selection = GetDocumentSelection(textView); var visibleRange = GetVisibleRange(textView); @@ -319,7 +336,7 @@ private void OnSelectionChanged(object sender, EventArgs e) private IEnumerable GetContentChanges(INormalizedTextChangeCollection textChanges, ITextSnapshot beforeSnapshot) { var results = new List(); - + foreach (var change in textChanges) { var start = ToDocumentPosition(beforeSnapshot, change.OldPosition);