Skip to content

Commit

Permalink
Document sync & completions fix (#192)
Browse files Browse the repository at this point in the history
## Test plan
- Create a new solution
- Start adding code
- Check if 'grey code' with suggestions appears
- Accept the suggestions by pressing `Tab`
- Reject the suggestion by pressing `Esc`
<!-- 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 Jan 6, 2025
1 parent bbe2c1a commit 8c4074e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/Cody.Core.Tests/Cody.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<ItemGroup>
<Compile Include="CustomConfigurationTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StringExtensionsTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Cody.Core\Cody.Core.csproj">
Expand Down
38 changes: 38 additions & 0 deletions src/Cody.Core.Tests/StringExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Cody.Core.Common;
using NUnit.Framework;
using NUnit.Framework.Legacy;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cody.Core.Tests
{
[TestFixture]
public class StringExtensionsTests
{

[TestCase(@"c:\path\to\file", "file:///c%3A/path/to/file")]
[TestCase(@"c:/path/to/file", "file:///c%3A/path/to/file")]
[TestCase(@"c:/file.txt", "file:///c%3A/file.txt")]
public void Can_Convert_Path_To_Valid_Uri(string path, string expectedPath)
{
var result = StringExtensions.ToUri(path);

Assert.That(result, Is.EqualTo(expectedPath));
}

[TestCase("line1\rline2\rline3\r", "line1\nline2\nline3\n")]
[TestCase("line1\nline2\nline3\n", "line1\nline2\nline3\n")]
[TestCase("line1\r\nline2\r\nline3\r\n", "line1\nline2\nline3\n")]
[TestCase("line1\r\nline2\rline3\n", "line1\nline2\nline3\n")]
public void Can_Replace_Line_Brakers(string text, string expectedText)
{
var result = StringExtensions.ConvertLineBreaks(text, "\n");

Assert.That(result, Is.EqualTo(expectedText));
}
}
}
2 changes: 0 additions & 2 deletions src/Cody.Core/DocumentSync/DocumentSyncCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ public void OnChanged(string fullPath, DocumentRange visibleRange, DocumentRange
};

agentService.DidChange(docState);
//var result = agentService.GetWorkspaceDocuments(new GetDocumentsParams { Uris = new[] { fullPath.ToUri() } }).Result;
//trace.TraceEvent("AfterDidChange", result.Documents.First().Content);
}

public void OnClosed(string fullPath)
Expand Down
2 changes: 1 addition & 1 deletion src/Cody.VisualStudio/Completions/CodyProposalSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.TextManager.Interop;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -163,6 +162,7 @@ private void UpdateCaretAndCompletion(ref VirtualSnapshotPoint caret, ref Comple
if (trackedSnapshot == null || trackedSnapshot.Version.VersionNumber < caret.Position.Snapshot.Version.VersionNumber)
{
trace.TraceEvent("TrackinSnapshotFailed");
return;
}

caret = caret.TranslateTo(trackedSnapshot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public class CodyProposalSourceProvider : ProposalSourceProviderBase, IDisposabl
private static TraceLogger trace = new TraceLogger(nameof(CodyProposalSourceProvider));

private readonly ITextDocumentFactoryService textDocumentFactoryService;
private readonly IVsEditorAdaptersFactoryService editorAdaptersFactoryService;
private readonly SuggestionServiceBase suggestionServiceBase;

public const string ProposalIdPrefix = "cody";
Expand All @@ -35,11 +34,9 @@ public class CodyProposalSourceProvider : ProposalSourceProviderBase, IDisposabl
[ImportingConstructor]
public CodyProposalSourceProvider(
ITextDocumentFactoryService textDocumentFactoryService,
IVsEditorAdaptersFactoryService editorAdaptersFactoryService,
SuggestionServiceBase suggestionServiceBase)
{
this.textDocumentFactoryService = textDocumentFactoryService;
this.editorAdaptersFactoryService = editorAdaptersFactoryService;
this.suggestionServiceBase = suggestionServiceBase;

//suggestionServiceBase.ProposalRejected += OnProposalRejected;
Expand Down Expand Up @@ -93,12 +90,6 @@ private void OnSuggestionAccepted(object sender, SuggestionAcceptedEventArgs e)

public async override Task<ProposalSourceBase> GetProposalSourceAsync(ITextView view, CancellationToken cancel)
{
//var list = Input.ToArray();
//foreach (var zm in Input)
//{
// var pom = zm.GetType().Assembly.Location;
//}

trace.TraceEvent("Enter");
IWpfTextView wpfTextView = view as IWpfTextView;
if (wpfTextView != null && view.Roles.Contains("DOCUMENT") && view.Roles.Contains("EDITABLE"))
Expand Down
5 changes: 3 additions & 2 deletions src/Cody.VisualStudio/Services/DocumentsSyncService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ private DocumentRange GetVisibleRange(IVsTextView textView, ITextSnapshot snapsh
{
DocumentPosition firstVisiblePosition = null, lastVisiblePosition = null;

if (textView != null)
if (textView != null && ThreadHelper.CheckAccess())
{
var wpfTextView = editorAdaptersFactoryService.GetWpfTextView(textView);
if (wpfTextView == null) return null;
snapshot = snapshot ?? wpfTextView.TextSnapshot;
var lines = wpfTextView.TextViewLines;

Expand All @@ -158,7 +159,7 @@ private DocumentRange GetDocumentSelection(IVsTextView textView, ITextSnapshot s
{
bool swap = false;
DocumentPosition start = null, end = null;
if (textView != null)
if (textView != null && ThreadHelper.CheckAccess())
{
var wpfTextView = editorAdaptersFactoryService.GetWpfTextView(textView);
if (wpfTextView != null)
Expand Down

0 comments on commit 8c4074e

Please sign in to comment.