Skip to content

Re-enable ExtensionCommandTests.cs #1657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 52 additions & 62 deletions src/PowerShellEditorServices/Services/Extension/ExtensionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ internal sealed class ExtensionService

#region Fields

private readonly Dictionary<string, EditorCommand> editorCommands =
new Dictionary<string, EditorCommand>();
private readonly Dictionary<string, EditorCommand> editorCommands = new();

private readonly ILanguageServerFacade _languageServer;

private IdempotentLatch _initializedLatch = new();
private readonly IdempotentLatch _initializedLatch = new();

#endregion

Expand All @@ -39,18 +38,18 @@ internal sealed class ExtensionService
/// Gets the IEditorOperations implementation used to invoke operations
/// in the host editor.
/// </summary>
public IEditorOperations EditorOperations { get; private set; }
public IEditorOperations EditorOperations { get; }

/// <summary>
/// Gets the EditorObject which exists in the PowerShell session as the
/// '$psEditor' variable.
/// </summary>
public EditorObject EditorObject { get; private set; }
public EditorObject EditorObject { get; }

/// <summary>
/// Gets the PowerShellContext in which extension code will be executed.
/// </summary>
internal IInternalPowerShellExecutionService ExecutionService { get; private set; }
internal IInternalPowerShellExecutionService ExecutionService { get; }

#endregion

Expand All @@ -62,7 +61,7 @@ internal sealed class ExtensionService
/// </summary>
/// <param name="languageServer">The PSES language server instance.</param>
/// <param name="serviceProvider">Services for dependency injection into the editor object.</param>
/// <param name="editorOptions">Options object to configure the editor.</param>
/// <param name="editorOperations">The interface for operating an editor.</param>
/// <param name="executionService">PowerShell execution service to run PowerShell execution requests.</param>
internal ExtensionService(
ILanguageServerFacade languageServer,
Expand All @@ -73,16 +72,15 @@ internal ExtensionService(
ExecutionService = executionService;
_languageServer = languageServer;

EditorObject =
new EditorObject(
serviceProvider,
this,
editorOperations);
EditorObject = new EditorObject(
serviceProvider,
this,
editorOperations);

// Attach to ExtensionService events
CommandAdded += ExtensionService_ExtensionAddedAsync;
CommandUpdated += ExtensionService_ExtensionUpdatedAsync;
CommandRemoved += ExtensionService_ExtensionRemovedAsync;
CommandAdded += ExtensionService_ExtensionAdded;
CommandUpdated += ExtensionService_ExtensionUpdated;
CommandRemoved += ExtensionService_ExtensionRemoved;
}

#endregion
Expand All @@ -93,7 +91,6 @@ internal ExtensionService(
/// Initializes this ExtensionService using the provided IEditorOperations
/// implementation for future interaction with the host editor.
/// </summary>
/// <param name="editorOperations">An IEditorOperations implementation.</param>
/// <returns>A Task that can be awaited for completion.</returns>
internal Task InitializeAsync()
{
Expand Down Expand Up @@ -121,28 +118,29 @@ internal Task InitializeAsync()
/// <param name="commandName">The unique name of the command to be invoked.</param>
/// <param name="editorContext">The context in which the command is being invoked.</param>
/// <returns>A Task that can be awaited for completion.</returns>
/// <exception cref="KeyNotFoundException">The command being invoked was not registered.</exception>
public async Task InvokeCommandAsync(string commandName, EditorContext editorContext)
{

if (this.editorCommands.TryGetValue(commandName, out EditorCommand editorCommand))
if (editorCommands.TryGetValue(commandName, out EditorCommand editorCommand))
{
PSCommand executeCommand = new PSCommand();
executeCommand.AddCommand("Invoke-Command");
executeCommand.AddParameter("ScriptBlock", editorCommand.ScriptBlock);
executeCommand.AddParameter("ArgumentList", new object[] { editorContext });
PSCommand executeCommand = new PSCommand()
.AddCommand("Invoke-Command")
.AddParameter("ScriptBlock", editorCommand.ScriptBlock)
.AddParameter("ArgumentList", new object[] { editorContext });

await ExecutionService.ExecutePSCommandAsync(
executeCommand,
CancellationToken.None,
new PowerShellExecutionOptions { WriteOutputToHost = !editorCommand.SuppressOutput, ThrowOnError = false, AddToHistory = !editorCommand.SuppressOutput })
.ConfigureAwait(false);
new PowerShellExecutionOptions
{
WriteOutputToHost = !editorCommand.SuppressOutput,
ThrowOnError = false,
AddToHistory = !editorCommand.SuppressOutput
}).ConfigureAwait(false);
}
else
{
throw new KeyNotFoundException(
string.Format(
"Editor command not found: '{0}'",
commandName));
throw new KeyNotFoundException($"Editor command not found: '{commandName}'");
}
}

Expand All @@ -156,20 +154,18 @@ public bool RegisterCommand(EditorCommand editorCommand)
{
Validate.IsNotNull(nameof(editorCommand), editorCommand);

bool commandExists =
this.editorCommands.ContainsKey(
editorCommand.Name);
bool commandExists = editorCommands.ContainsKey(editorCommand.Name);

// Add or replace the editor command
this.editorCommands[editorCommand.Name] = editorCommand;
editorCommands[editorCommand.Name] = editorCommand;

if (!commandExists)
{
this.OnCommandAdded(editorCommand);
OnCommandAdded(editorCommand);
}
else
{
this.OnCommandUpdated(editorCommand);
OnCommandUpdated(editorCommand);
}

return !commandExists;
Expand All @@ -179,19 +175,17 @@ public bool RegisterCommand(EditorCommand editorCommand)
/// Unregisters an existing EditorCommand based on its registered name.
/// </summary>
/// <param name="commandName">The name of the command to be unregistered.</param>
/// <exception cref="KeyNotFoundException">The command being unregistered was not registered.</exception>
public void UnregisterCommand(string commandName)
{
if (this.editorCommands.TryGetValue(commandName, out EditorCommand existingCommand))
if (editorCommands.TryGetValue(commandName, out EditorCommand existingCommand))
{
this.editorCommands.Remove(commandName);
this.OnCommandRemoved(existingCommand);
editorCommands.Remove(commandName);
OnCommandRemoved(existingCommand);
}
else
{
throw new KeyNotFoundException(
string.Format(
"Command '{0}' is not registered",
commandName));
throw new KeyNotFoundException($"Command '{commandName}' is not registered");
}
}

Expand All @@ -201,8 +195,8 @@ public void UnregisterCommand(string commandName)
/// <returns>An Array of all registered EditorCommands.</returns>
public EditorCommand[] GetCommands()
{
EditorCommand[] commands = new EditorCommand[this.editorCommands.Count];
this.editorCommands.Values.CopyTo(commands,0);
EditorCommand[] commands = new EditorCommand[editorCommands.Count];
editorCommands.Values.CopyTo(commands, 0);
return commands;
}

Expand All @@ -217,7 +211,7 @@ public EditorCommand[] GetCommands()

private void OnCommandAdded(EditorCommand command)
{
this.CommandAdded?.Invoke(this, command);
CommandAdded?.Invoke(this, command);
}

/// <summary>
Expand All @@ -227,7 +221,7 @@ private void OnCommandAdded(EditorCommand command)

private void OnCommandUpdated(EditorCommand command)
{
this.CommandUpdated?.Invoke(this, command);
CommandUpdated?.Invoke(this, command);
}

/// <summary>
Expand All @@ -237,35 +231,31 @@ private void OnCommandUpdated(EditorCommand command)

private void OnCommandRemoved(EditorCommand command)
{
this.CommandRemoved?.Invoke(this, command);
CommandRemoved?.Invoke(this, command);
}

private void ExtensionService_ExtensionAddedAsync(object sender, EditorCommand e)
private void ExtensionService_ExtensionAdded(object sender, EditorCommand e)
{
_languageServer?.SendNotification<ExtensionCommandAddedNotification>("powerShell/extensionCommandAdded",
_languageServer?.SendNotification<ExtensionCommandAddedNotification>(
"powerShell/extensionCommandAdded",
new ExtensionCommandAddedNotification
{
Name = e.Name,
DisplayName = e.DisplayName
});
{ Name = e.Name, DisplayName = e.DisplayName });
}

private void ExtensionService_ExtensionUpdatedAsync(object sender, EditorCommand e)
private void ExtensionService_ExtensionUpdated(object sender, EditorCommand e)
{
_languageServer?.SendNotification<ExtensionCommandUpdatedNotification>("powerShell/extensionCommandUpdated",
_languageServer?.SendNotification<ExtensionCommandUpdatedNotification>(
"powerShell/extensionCommandUpdated",
new ExtensionCommandUpdatedNotification
{
Name = e.Name,
});
{ Name = e.Name, });
}

private void ExtensionService_ExtensionRemovedAsync(object sender, EditorCommand e)
private void ExtensionService_ExtensionRemoved(object sender, EditorCommand e)
{
_languageServer?.SendNotification<ExtensionCommandRemovedNotification>("powerShell/extensionCommandRemoved",
_languageServer?.SendNotification<ExtensionCommandRemovedNotification>(
"powerShell/extensionCommandRemoved",
new ExtensionCommandRemovedNotification
{
Name = e.Name,
});
{ Name = e.Name, });
}

#endregion
Expand Down
Loading