Skip to content

Commit 6ebc63e

Browse files
tomaszgolebiowskiTomasz Gołębiowski
andauthored
textEditor/selection and textEditor/revealRange endpoints (#359)
Implementation of `textEditor/selection` and `textEditor/revealRange` endpoints. With their help agent will be able to select and scroll through code. Such actions are used by the agent, for example, during edits to show the user what has been changed at the end of larger file. ## Test plan N/A <!-- REQUIRED; info at https://docs-legacy.sourcegraph.com/dev/background-information/testing_principles --> Co-authored-by: Tomasz Gołębiowski <tgolebiowski@virtuslab.com>
1 parent a406276 commit 6ebc63e

File tree

3 files changed

+52
-34
lines changed

3 files changed

+52
-34
lines changed

src/Cody.Core/Agent/EditTaskNotificationHandlers.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
using Cody.Core.Infrastructure;
44
using Cody.Core.Logging;
55
using System;
6-
using System.Collections.Generic;
76
using System.Linq;
8-
using System.Text;
97
using System.Threading.Tasks;
108

119
namespace Cody.Core.Agent
@@ -29,9 +27,15 @@ public EditTaskNotificationHandlers(ILog logger, IEditCodeService editCodeServic
2927
[AgentCallback("textEditor/selection")]
3028
public void Selection(string uri, Range selection)
3129
{
32-
// no needed yet
33-
//var path = uri.ToWindowsPath();
34-
//documentService.SelectInDocument(path, selection);
30+
var path = uri.ToWindowsPath();
31+
documentService.SelectInDocument(path, selection);
32+
}
33+
34+
[AgentCallback("textEditor/revealRange")]
35+
public void RevealRange(string uri, Range range)
36+
{
37+
var path = uri.ToWindowsPath();
38+
documentService.RevealRangeInDocument(path, range);
3539
}
3640

3741
[AgentCallback("editTask/getUserInput", deserializeToSingleObject: true)]

src/Cody.Core/Infrastructure/IDocumentService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public interface IDocumentService
1313

1414
bool SelectInDocument(string path, Range selection);
1515

16+
bool RevealRangeInDocument(string path, Range range);
17+
1618
bool InsertTextInDocument(string path, Position position, string text);
1719

1820
bool ReplaceTextInDocument(string path, Range range, string text);

src/Cody.VisualStudio/Services/DocumentService.cs

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,60 +32,72 @@ public DocumentService(ILog log, IServiceProvider serviceProvider, IVsSolution v
3232
this.editorAdaptersFactoryService = editorAdaptersFactoryService;
3333
}
3434

35-
public bool ShowDocument(string path, Range selection)
35+
private bool ForDocument(string path, Func<IVsWindowFrame, IVsTextView, bool> action)
3636
{
3737
var result = ThreadHelper.JoinableTaskFactory.Run(async delegate
3838
{
3939
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
4040

4141
var tryOpenResult = VsShellUtilities.TryOpenDocument(serviceProvider, path, Guid.Empty, out _, out _, out IVsWindowFrame windowFrame);
42-
if (tryOpenResult == VSConstants.S_OK)
42+
if (tryOpenResult == VSConstants.S_OK && windowFrame != null)
4343
{
44-
windowFrame?.Show();
45-
46-
if (selection != null && windowFrame != null)
47-
{
48-
var textView = GetVsTextView(windowFrame);
49-
if (textView != null)
50-
{
51-
textView.CenterLines(selection.Start.Line, 0);
52-
textView.SetSelection(selection.Start.Line, selection.Start.Character, selection.End.Line, selection.End.Character);
53-
}
54-
}
44+
var textView = GetVsTextView(windowFrame);
45+
if (textView == null) log.Warn($"Cannot get VsTextView '{path}'");
5546

56-
return true;
47+
return action(windowFrame, textView);
5748
}
58-
else log.Error($"Cannot show document '{path}' (error code: {tryOpenResult})");
49+
else log.Error($"Cannot open document '{path}' (error code: {tryOpenResult})");
5950

6051
return false;
6152
});
6253

6354
return result;
6455
}
6556

57+
public bool ShowDocument(string path, Range selection)
58+
{
59+
return ForDocument(path, (windowFrame, textView) =>
60+
{
61+
windowFrame.Show();
62+
if (selection != null && textView != null)
63+
{
64+
textView.CenterLines(selection.Start.Line, 0);
65+
textView.SetSelection(selection.Start.Line, selection.Start.Character, selection.End.Line, selection.End.Character);
66+
}
67+
return true;
68+
});
69+
}
70+
6671
public bool SelectInDocument(string path, Range selection)
6772
{
68-
var result = ThreadHelper.JoinableTaskFactory.Run(async delegate
73+
return ForDocument(path, (windowFrame, textView) =>
6974
{
70-
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
75+
if (selection != null && textView != null)
76+
{
77+
textView.SetSelection(selection.Start.Line, selection.Start.Character, selection.End.Line, selection.End.Character);
78+
}
79+
return true;
80+
});
81+
}
7182

72-
var tryOpenResult = VsShellUtilities.TryOpenDocument(serviceProvider, path, Guid.Empty, out _, out _, out IVsWindowFrame windowFrame);
73-
if (tryOpenResult == VSConstants.S_OK)
83+
public bool RevealRangeInDocument(string path, Range range)
84+
{
85+
return ForDocument(path, (windowFrame, textView) =>
86+
{
87+
if (textView != null && range != null)
7488
{
75-
var textView = GetVsTextView(windowFrame);
76-
if (textView != null)
89+
var span = new TextSpan()
7790
{
78-
textView.SetSelection(selection.Start.Line, selection.Start.Character, selection.End.Line, selection.End.Character);
79-
}
80-
else log.Error($"Cannot get VsTextView '{path}'");
91+
iStartLine = range.Start.Line,
92+
iStartIndex = range.Start.Character,
93+
iEndLine = range.End.Line,
94+
iEndIndex = range.End.Character
95+
};
8196

97+
textView.EnsureSpanVisible(span);
8298
}
83-
else log.Error($"Cannot open document '{path}' (error code: {tryOpenResult})");
84-
85-
return false;
99+
return true;
86100
});
87-
88-
return result;
89101
}
90102

91103
public bool InsertTextInDocument(string path, Position position, string text)

0 commit comments

Comments
 (0)