Skip to content

Commit

Permalink
Merge pull request #75 from digital-phoenix/main
Browse files Browse the repository at this point in the history
fixing Resharper support
  • Loading branch information
fortenforge authored May 16, 2024
2 parents dc3acb1 + dbe7e85 commit 42002b3
Show file tree
Hide file tree
Showing 18 changed files with 1,706 additions and 364 deletions.
98 changes: 98 additions & 0 deletions CodeiumVS/CodeLensConnection/CodeLensConnectionHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#nullable enable

using System.Collections.Generic;
using CodeiumVS.Packets;

namespace CodeiumVS
{
using System;
using System.Diagnostics;
using System.IO.Pipes;
using System.Linq;
using System.Threading.Tasks;

using StreamJsonRpc;

using CodeLensConnections = System.Collections.Concurrent.ConcurrentDictionary<System.Guid, CodeLensConnectionHandler>;
using CodeLensDetails = System.Collections.Concurrent.ConcurrentDictionary<System.Guid, FunctionInfo>;

public class CodeLensConnectionHandler : IRemoteVisualStudio, IDisposable
{
private static readonly CodeLensConnections connections = new CodeLensConnections();
private static readonly CodeLensDetails detailsData = new CodeLensDetails();

private JsonRpc? rpc;
private Guid? dataPointId;

public static async Task AcceptCodeLensConnections()
{
try
{
while (true)
{
var stream = new NamedPipeServerStream(
PipeName.Get(Process.GetCurrentProcess().Id),
PipeDirection.InOut,
NamedPipeServerStream.MaxAllowedServerInstances,
PipeTransmissionMode.Byte,
PipeOptions.Asynchronous);
await stream.WaitForConnectionAsync().Caf();
_ = HandleConnection(stream);
}
}
catch (Exception ex)
{
throw;
}

static async Task HandleConnection(NamedPipeServerStream stream)
{
try
{
var handler = new CodeLensConnectionHandler();
var rpc = JsonRpc.Attach(stream, handler);
handler.rpc = rpc;
await rpc.Completion;
handler.Dispose();
stream.Dispose();
}
catch (Exception ex)
{
CodeiumVSPackage.Instance.LogAsync("Handle Connection Error");
}
}
}

public void Dispose()
{
if (dataPointId.HasValue)
{
_ = connections.TryRemove(dataPointId.Value, out var _);
_ = detailsData.TryRemove(dataPointId.Value, out var _);
}
}

// Called from each CodeLensDataPoint via JSON RPC.
public void RegisterCodeLensDataPoint(Guid id)
{
dataPointId = id;
connections[id] = this;
}

public static void StoreDetailsData(Guid id, FunctionInfo closestFunction) => detailsData[id] = closestFunction;

public static FunctionInfo GetDetailsData(Guid id) => detailsData[id];

public static async Task RefreshCodeLensDataPoint(Guid id)
{
if (!connections.TryGetValue(id, out var conn))
throw new InvalidOperationException($"CodeLens data point {id} was not registered.");

Debug.Assert(conn.rpc != null);
await conn.rpc!.InvokeAsync(nameof(IRemoteCodeLens.Refresh)).Caf();
}

public static async Task RefreshAllCodeLensDataPoints()
=> await Task.WhenAll(connections.Keys.Select(RefreshCodeLensDataPoint)).Caf();
}
}
102 changes: 102 additions & 0 deletions CodeiumVS/CodeLensConnection/CodeLensListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#nullable enable

using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Language.CodeLens;
using Microsoft.VisualStudio.Utilities;
using CodeiumVS.Packets;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using System.Windows.Shapes;
using Microsoft.VisualStudio.Debugger.Interop;

namespace CodeiumVS
{

[Export(typeof(ICodeLensCallbackListener))]
[ContentType("C/C++")]
[ContentType("CSharp")]
[ContentType("Basic")]
[ContentType("vbscript")]
[ContentType("TypeScript")]
[ContentType("JavaScript")]
public class CodeLensListener : ICodeLensCallbackListener, ICodeLensListener
{

public int GetVisualStudioPid() => Process.GetCurrentProcess().Id;

public bool IsCodeiumCodeLensActive() => CodeiumVSPackage.Instance != null && CodeiumVSPackage.Instance.SettingsPage.EnableCodeLens;
FunctionInfo GetClosestFunction(IList<Packets.FunctionInfo>? functions, int line)
{

FunctionInfo minFunction = null;
int minDistance = int.MaxValue;
foreach (var f in functions)
{
var distance = Math.Abs(f.DefinitionLine - line);
if (distance < minDistance)
{
minDistance = distance;
minFunction = f;
}
}

return minFunction;
}

public async Task<FunctionInfo> LoadInstructions(
Guid dataPointId,
Guid projGuid,
string filePath,
int textStart,
int textLen,
CancellationToken ct)
{
try
{

ITextDocument _document;
TextViewListener.Instance.documentDictionary.TryGetValue(filePath.ToLower(), out _document);
var key = typeof(CodeiumCompletionHandler);
var props = _document.TextBuffer.Properties;
CodeiumCompletionHandler handler;
if (props.ContainsProperty(key))
{
handler = props.GetProperty<CodeiumCompletionHandler>(key);
}
else
{
handler = null;
return null;
}

IList<FunctionInfo>? functions =
await CodeiumVSPackage.Instance.LanguageServer.GetFunctionsAsync(
_document.FilePath,
_document.TextBuffer.CurrentSnapshot.GetText(),
handler.GetLanguage(),
0,
ct);

var line = new Span(textStart, textLen);
var snapshotLine = _document.TextBuffer.CurrentSnapshot.GetLineFromPosition(line.Start);
var lineN = snapshotLine.LineNumber;
FunctionInfo closestFunction = GetClosestFunction(functions, lineN);
CodeLensConnectionHandler.StoreDetailsData(dataPointId, closestFunction);

return closestFunction;
}
catch (Exception ex)
{
CodeiumVSPackage.Instance.LogAsync(ex.ToString());

return null;
}
}

}
}
8 changes: 7 additions & 1 deletion CodeiumVS/CodeiumVS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@
<LangVersion>preview</LangVersion>
</PropertyGroup>
<ItemGroup>
<Compile Include="CodeLensConnection\CodeLensConnectionHandler.cs" />
<Compile Include="CodeLensConnection\CodeLensListener.cs" />
<Compile Include="CodeLensOOP\CodeiumCodeLensProvider.cs" />
<Compile Include="CodeLensOOP\CodeLensLogger.cs" />
<Compile Include="CodeLensOOP\SharedCodeLens.cs" />
<Compile Include="CodeLensOOP\VisualStudioConnectionHandler.cs" />
<Compile Include="InlineDiff\InlineDiffAdornment.cs" />
<Compile Include="InlineDiff\InlineDiffControl.xaml.cs">
<DependentUpon>InlineDiffControl.xaml</DependentUpon>
Expand Down Expand Up @@ -208,4 +214,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
12 changes: 12 additions & 0 deletions CodeiumVS/CodeiumVSPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ await VS.MessageBox.ShowErrorAsync(
"Codeium might not work correctly. Please check the output window for more details.");
}

try
{
await base.InitializeAsync(cancellationToken, progress);
await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
_ = CodeLensConnectionHandler.AcceptCodeLensConnections();
}
catch (Exception ex)
{
await LogAsync("Codeium Error" + ex);
throw;
}

await LanguageServer.InitializeAsync();
await LogAsync("Codeium Extension for Visual Studio");
}
Expand Down
Loading

0 comments on commit 42002b3

Please sign in to comment.