From 569f8048d9b1a024374b9a3523163c35a6a34f1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Go=C5=82=C4=99biowski?= Date: Fri, 7 Feb 2025 11:30:05 +0100 Subject: [PATCH 1/2] Send Focus on initialization --- .../Services/DocumentsSyncService.cs | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/Cody.VisualStudio/Services/DocumentsSyncService.cs b/src/Cody.VisualStudio/Services/DocumentsSyncService.cs index 6614db7..4aa19b4 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,14 @@ 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) documentActions.OnFocus(path); } } finally @@ -120,19 +133,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 +171,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 +230,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 +316,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 +332,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); From 23e000bfcab15fe4eeb22b351254c4aa354a42eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Go=C5=82=C4=99biowski?= Date: Fri, 7 Feb 2025 11:32:26 +0100 Subject: [PATCH 2/2] Trace --- src/Cody.VisualStudio/Services/DocumentsSyncService.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Cody.VisualStudio/Services/DocumentsSyncService.cs b/src/Cody.VisualStudio/Services/DocumentsSyncService.cs index 4aa19b4..bea346c 100644 --- a/src/Cody.VisualStudio/Services/DocumentsSyncService.cs +++ b/src/Cody.VisualStudio/Services/DocumentsSyncService.cs @@ -69,7 +69,11 @@ public void Initialize() if (activeCookie != 0) { var path = rdt.GetDocumentInfo(activeCookie).Moniker; - if (path != null) documentActions.OnFocus(path); + if (path != null) + { + trace.TraceEvent("OnInitFocus"); + documentActions.OnFocus(path); + } } } finally