Skip to content

Commit

Permalink
Merge pull request #16 from kazssym/update-global-object
Browse files Browse the repository at this point in the history
New feature to integrate script engine execution
  • Loading branch information
kazssym authored Jan 3, 2025
2 parents 0aafadc + c95d09c commit 2cae950
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
19 changes: 17 additions & 2 deletions VSExtension1/Command1.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Diagnostics;
using Microsoft;
using Microsoft.ClearScript;
using Microsoft.VisualStudio.Extensibility;
using Microsoft.VisualStudio.Extensibility.Commands;
using Microsoft.VisualStudio.Extensibility.Shell;
using System.Diagnostics;


namespace VSExtension1
{
Expand All @@ -14,15 +16,19 @@ internal class Command1 : Command
{
private readonly TraceSource logger;

private readonly ScriptEngine _scriptEngine;

/// <summary>
/// Initializes a new instance of the <see cref="Command1"/> class.
/// </summary>
/// <param name="traceSource">Trace source instance to utilize.</param>
public Command1(TraceSource traceSource)
public Command1(VisualStudioExtensibility extensibility, TraceSource traceSource, ScriptEngine scriptEngine) :
base(extensibility)
{
// This optional TraceSource can be used for logging in the command. You can use dependency injection to access
// other services here as well.
this.logger = Requires.NotNull(traceSource, nameof(traceSource));
this._scriptEngine = Requires.NotNull(scriptEngine);
}

/// <inheritdoc />
Expand All @@ -45,6 +51,15 @@ public override Task InitializeAsync(CancellationToken cancellationToken)
public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
{
await this.Extensibility.Shell().ShowPromptAsync("Hello from an extension!", PromptOptions.OK, cancellationToken);
try
{
this._scriptEngine.Evaluate("command1()");
}
catch (ScriptEngineException e)
{
await this.Extensibility.Shell().ShowPromptAsync(e.Message, PromptOptions.OK, cancellationToken);
throw;
}
}
}
}
4 changes: 4 additions & 0 deletions VSExtension1/scripts/__init__.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
// This file will be used to initialize the ScriptEngine.

extension.Output.WriteLine("Hello from __init__.js")

globalThis.command1 = function () {
extension.Output.WriteLine("command1 called")
}

0 comments on commit 2cae950

Please sign in to comment.