Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send DidFocus during initialization. #225

Merged
merged 2 commits into from
Feb 7, 2025
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
35 changes: 26 additions & 9 deletions src/Cody.VisualStudio/Services/DocumentsSyncService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace Cody.VisualStudio.Services
{
Expand All @@ -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;
Expand All @@ -30,7 +31,10 @@ public class DocumentsSyncService : IVsRunningDocTableEvents
private HashSet<uint> openNotificationSend = new HashSet<uint>();
private HashSet<uint> isSubscribed = new HashSet<uint>();

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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;

Expand All @@ -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
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);

Expand All @@ -319,7 +336,7 @@ private void OnSelectionChanged(object sender, EventArgs e)
private IEnumerable<DocumentChange> GetContentChanges(INormalizedTextChangeCollection textChanges, ITextSnapshot beforeSnapshot)
{
var results = new List<DocumentChange>();

foreach (var change in textChanges)
{
var start = ToDocumentPosition(beforeSnapshot, change.OldPosition);
Expand Down