Skip to content

Commit

Permalink
Send Focus on initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Gołębiowski committed Feb 7, 2025
1 parent d6eb83b commit 569f804
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 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,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
Expand Down Expand Up @@ -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;

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

Expand All @@ -319,7 +332,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

0 comments on commit 569f804

Please sign in to comment.