Skip to content

Commit

Permalink
Send DidFocus during initialization. (#225)
Browse files Browse the repository at this point in the history
When opening a solution via a .sln file, chat incorrectly displays the
name of the currently active document.

## Test plan
- start Visual Studio by clicking on the file with the solution (.sln). 
- once solution is open, active document must match the name tag in the
chat window

<!-- REQUIRED; info at
https://docs-legacy.sourcegraph.com/dev/background-information/testing_principles
-->

---------

Co-authored-by: Tomasz Gołębiowski <[email protected]>
  • Loading branch information
tomaszgolebiowski and Tomasz Gołębiowski authored Feb 7, 2025
1 parent d6eb83b commit 311addf
Showing 1 changed file with 26 additions and 9 deletions.
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

0 comments on commit 311addf

Please sign in to comment.