-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from digital-phoenix/main
fixing Resharper support
- Loading branch information
Showing
18 changed files
with
1,706 additions
and
364 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.