Skip to content

Commit

Permalink
Use temp threads handler
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmholt committed Apr 19, 2021
1 parent 8debfc1 commit 0fd091d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/PowerShellEditorServices/Server/PsesDebugServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,15 @@ public async Task StartAsync()
.WithInput(_inputStream)
.WithOutput(_outputStream)
.WithServices(serviceCollection => serviceCollection
.AddSingleton(_loggerFactory)
.AddLogging()
.AddOptions()
.AddPsesDebugServices(ServiceProvider, this, _useTempSession))
.WithHandler<LaunchAndAttachHandler>()
.WithHandler<DisconnectHandler>()
.WithHandler<BreakpointHandlers>()
.WithHandler<ConfigurationDoneHandler>()
.WithHandler<ThreadsHandler>()
.WithHandler<ThreadsTempHandler>()
.WithHandler<StackTraceHandler>()
.WithHandler<ScopesHandler>()
.WithHandler<VariablesHandler>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System.Management.Automation;
using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Handlers;
using Microsoft.PowerShell.EditorServices.Services.DebugAdapter;
using Microsoft.PowerShell.EditorServices.Services.PowerShellContext;
using Microsoft.PowerShell.EditorServices.Utility;
Expand Down Expand Up @@ -70,20 +71,20 @@ private void DebugService_DebuggerStopped(object sender, DebuggerStoppedEventArg
// "step", "breakpoint", "function breakpoint", "exception" and "pause".
// We don't support exception breakpoints and for "pause", we can't distinguish
// between stepping and the user pressing the pause/break button in the debug toolbar.
string debuggerStoppedReason = "step";
StoppedEventReason debuggerStoppedReason = StoppedEventReason.Step;
if (e.OriginalEvent.Breakpoints.Count > 0)
{
debuggerStoppedReason =
e.OriginalEvent.Breakpoints[0] is CommandBreakpoint
? "function breakpoint"
: "breakpoint";
? StoppedEventReason.FunctionBreakpoint
: StoppedEventReason.Breakpoint;
}

_debugAdapterServer.SendNotification(EventNames.Stopped,
new StoppedEvent
{
ThreadId = 1,
Reason = debuggerStoppedReason
Reason = debuggerStoppedReason,
ThreadId = ThreadsTempHandler.PipelineThread.Id,
});
}

Expand All @@ -108,8 +109,7 @@ private void PowerShellContext_RunspaceChanged(object sender, RunspaceChangedEve
_debugAdapterServer.SendNotification(EventNames.Continued,
new ContinuedEvent
{
ThreadId = 1,
AllThreadsContinued = true
ThreadId = ThreadsTempHandler.PipelineThread.Id,
});
}
}
Expand All @@ -119,8 +119,7 @@ private void PowerShellContext_DebuggerResumed(object sender, DebuggerResumeActi
_debugAdapterServer.SendNotification(EventNames.Continued,
new ContinuedEvent
{
AllThreadsContinued = true,
ThreadId = 1
ThreadId = ThreadsTempHandler.PipelineThread.Id,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,77 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using OmniSharp.Extensions.DebugAdapter.Protocol;
using OmniSharp.Extensions.DebugAdapter.Protocol.Client;
using OmniSharp.Extensions.DebugAdapter.Protocol.Models;
using OmniSharp.Extensions.DebugAdapter.Protocol.Requests;
using OmniSharp.Extensions.DebugAdapter.Protocol.Server;
using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.JsonRpc.Generation;
using Thread = OmniSharp.Extensions.DebugAdapter.Protocol.Models.Thread;

namespace System.Runtime.CompilerServices
{
[EditorBrowsable(EditorBrowsableState.Never)]
public class IsExternalInit{}
}

namespace OmniSharp.Extensions.DebugAdapter.Protocol
{

[Parallel]
[Method(RequestNames.Threads, Direction.ClientToServer)]
[
GenerateHandler,
GenerateHandlerMethods(typeof(IDebugAdapterServerRegistry)),
GenerateRequestMethods(typeof(IDebugAdapterClient)),
]
public record ThreadsTempArguments : IRequest<ThreadsTempResponse>
{
}

public record ThreadsTempResponse
{
public Container<Thread>? Threads { get; init; }
}

}

namespace Microsoft.PowerShell.EditorServices.Handlers
{
/*
internal class ThreadsHandler : IThreadsHandler
{
internal static Thread MainDebuggedThread { get; } = new Thread(() => { });
public Task<ThreadsResponse> Handle(ThreadsArguments request, CancellationToken cancellationToken)
{
return Task.FromResult(new ThreadsResponse
{
// TODO: This is an empty container of threads...do we need to make a thread?
Threads = new Container<System.Threading.Thread>()
// TODO: Omnisharp supports multithreaded debugging (where multiple threads can be debugged at once), but we don't.
// This means we always need to set AllThreadsStoppped and AllThreadsContinued in our events.
// But if we one day support multithreaded debugging, we'd need a way to associate debugged runspaces with .NET threads in a consistent way
Threads = new Container<Thread>(MainDebuggedThread),
});
}
}
*/

internal class ThreadsTempHandler : ThreadsTempHandlerBase
{
internal static Thread PipelineThread { get; } = new Thread { Id = 1, Name = "PowerShell Pipeline Thread" };

public override Task<ThreadsTempResponse> Handle(ThreadsTempArguments request, CancellationToken cancellationToken)
{
return Task.FromResult(
new ThreadsTempResponse
{
Threads = new Container<Thread>(PipelineThread)
});
}
}
}

0 comments on commit 0fd091d

Please sign in to comment.