Skip to content

Fix temp debugging after it broke bringing in $psEditor #1150

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ function New-EditorFile {
}

end {
# If editorContext is null, then we're in a Temp session and
# this cmdlet won't work so return early.
$editorContext = $psEditor.GetEditorContext()
if (!$editorContext) {
return
}

if ($Path) {
foreach ($fileName in $Path)
{
Expand All @@ -142,7 +149,7 @@ function New-EditorFile {
}

$psEditor.Workspace.OpenFile($fileName, $preview)
$psEditor.GetEditorContext().CurrentFile.InsertText(($valueList | Out-String))
$editorContext.CurrentFile.InsertText(($valueList | Out-String))
} else {
$PSCmdlet.WriteError( (
New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList @(
Expand All @@ -154,7 +161,7 @@ function New-EditorFile {
}
} else {
$psEditor.Workspace.NewFile()
$psEditor.GetEditorContext().CurrentFile.InsertText(($valueList | Out-String))
$editorContext.CurrentFile.InsertText(($valueList | Out-String))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,17 @@ private void StartLogging()
_loggerUnsubscribers.Add(_logger.Subscribe(hostLogger));
}

string logPath = Path.Combine(GetLogDirPath(), "StartEditorServices.log");
string logDirPath = GetLogDirPath();
string logPath = Path.Combine(logDirPath, "StartEditorServices.log");

// Temp debugging sessions may try to reuse this same path,
// so we ensure they have a unique path
if (File.Exists(logPath))
{
int randomInt = new Random().Next();
logPath = Path.Combine(logDirPath, $"StartEditorServices-temp{randomInt.ToString("X")}.log");
}

var fileLogger = StreamLogger.CreateWithNewFile(logPath);
_disposableResources.Add(fileLogger);
IDisposable fileLoggerUnsubscriber = _logger.Subscribe(fileLogger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.IO;
using Microsoft.Extensions.DependencyInjection;
using OmniSharp.Extensions.LanguageServer.Server;
using Microsoft.PowerShell.EditorServices.Services;

#if DEBUG
using Serilog.Debugging;
Expand Down Expand Up @@ -123,8 +124,18 @@ public PsesDebugServer CreateDebugServerForTempSession(Stream inputStream, Strea
.SetMinimumLevel(LogLevel.Trace))
.AddSingleton<ILanguageServer>(provider => null)
.AddPsesLanguageServices(hostStartupInfo)
// For a Temp session, there is no LanguageServer so just set it to null
.AddSingleton(
typeof(OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer),
_ => null)
.BuildServiceProvider();

// This gets the ExtensionService which triggers the creation of the `$psEditor` variable.
// (because services are created only when they are first retrieved)
// Keep in mind, for Temp sessions, the `$psEditor` API is a no-op and the user is warned
// to run the command in the main PS Integrated Console.
serviceProvider.GetService<ExtensionService>();

return new PsesDebugServer(
_loggerFactory,
inputStream,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,27 @@ internal class EditorOperationsService : IEditorOperations
{
private const bool DefaultPreviewSetting = true;

private WorkspaceService _workspaceService;
private ILanguageServer _languageServer;
private readonly WorkspaceService _workspaceService;
private readonly PowerShellContextService _powerShellContextService;
private readonly ILanguageServer _languageServer;

public EditorOperationsService(
WorkspaceService workspaceService,
PowerShellContextService powerShellContextService,
ILanguageServer languageServer)
{
this._workspaceService = workspaceService;
this._languageServer = languageServer;
_workspaceService = workspaceService;
_powerShellContextService = powerShellContextService;
_languageServer = languageServer;
}

public async Task<EditorContext> GetEditorContextAsync()
{
if (!TestHasLanguageServer())
{
return null;
};

ClientEditorContext clientContext =
await _languageServer.SendRequest<GetEditorContextRequest, ClientEditorContext>(
"editor/getEditorContext",
Expand All @@ -39,6 +47,11 @@ await _languageServer.SendRequest<GetEditorContextRequest, ClientEditorContext>(

public async Task InsertTextAsync(string filePath, string text, BufferRange insertRange)
{
if (!TestHasLanguageServer())
{
return;
};

await _languageServer.SendRequest<InsertTextRequest>("editor/insertText", new InsertTextRequest
{
FilePath = filePath,
Expand All @@ -62,6 +75,10 @@ public async Task InsertTextAsync(string filePath, string text, BufferRange inse

public async Task SetSelectionAsync(BufferRange selectionRange)
{
if (!TestHasLanguageServer())
{
return;
};

await _languageServer.SendRequest<SetSelectionRequest>("editor/setSelection", new SetSelectionRequest
{
Expand Down Expand Up @@ -106,11 +123,21 @@ public EditorContext ConvertClientEditorContext(

public async Task NewFileAsync()
{
if (!TestHasLanguageServer())
{
return;
};

await _languageServer.SendRequest<string>("editor/newFile", null);
}

public async Task OpenFileAsync(string filePath)
{
if (!TestHasLanguageServer())
{
return;
};

await _languageServer.SendRequest<OpenFileDetails>("editor/openFile", new OpenFileDetails
{
FilePath = filePath,
Expand All @@ -120,6 +147,11 @@ public async Task OpenFileAsync(string filePath)

public async Task OpenFileAsync(string filePath, bool preview)
{
if (!TestHasLanguageServer())
{
return;
};

await _languageServer.SendRequest<OpenFileDetails>("editor/openFile", new OpenFileDetails
{
FilePath = filePath,
Expand All @@ -129,6 +161,11 @@ public async Task OpenFileAsync(string filePath, bool preview)

public async Task CloseFileAsync(string filePath)
{
if (!TestHasLanguageServer())
{
return;
};

await _languageServer.SendRequest<string>("editor/closeFile", filePath);
}

Expand All @@ -139,6 +176,11 @@ public async Task SaveFileAsync(string filePath)

public async Task SaveFileAsync(string currentPath, string newSavePath)
{
if (!TestHasLanguageServer())
{
return;
};

await _languageServer.SendRequest<SaveFileDetails>("editor/saveFile", new SaveFileDetails
{
FilePath = currentPath,
Expand All @@ -158,21 +200,41 @@ public string GetWorkspaceRelativePath(string filePath)

public async Task ShowInformationMessageAsync(string message)
{
if (!TestHasLanguageServer())
{
return;
};

await _languageServer.SendRequest<string>("editor/showInformationMessage", message);
}

public async Task ShowErrorMessageAsync(string message)
{
if (!TestHasLanguageServer())
{
return;
};

await _languageServer.SendRequest<string>("editor/showErrorMessage", message);
}

public async Task ShowWarningMessageAsync(string message)
{
if (!TestHasLanguageServer())
{
return;
};

await _languageServer.SendRequest<string>("editor/showWarningMessage", message);
}

public async Task SetStatusBarMessageAsync(string message, int? timeout)
{
if (!TestHasLanguageServer())
{
return;
}

await _languageServer.SendRequest<StatusBarMessageDetails>("editor/setStatusBarMessage", new StatusBarMessageDetails
{
Message = message,
Expand All @@ -182,7 +244,24 @@ public async Task SetStatusBarMessageAsync(string message, int? timeout)

public void ClearTerminal()
{
if (!TestHasLanguageServer())
{
return;
};

_languageServer.SendNotification("editor/clearTerminal");
}

private bool TestHasLanguageServer()
{
if (_languageServer != null)
{
return true;
}

_powerShellContextService.ExternalHost.UI.WriteWarningLine(
"Editor operations are not supported in temporary consoles. Re-run the command in the main PowerShell Intergrated Console.");
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ private void OnCommandRemoved(EditorCommand command)

private void ExtensionService_ExtensionAddedAsync(object sender, EditorCommand e)
{
_languageServer.SendNotification<ExtensionCommandAddedNotification>("powerShell/extensionCommandAdded",
_languageServer?.SendNotification<ExtensionCommandAddedNotification>("powerShell/extensionCommandAdded",
new ExtensionCommandAddedNotification
{
Name = e.Name,
Expand All @@ -232,7 +232,7 @@ private void ExtensionService_ExtensionAddedAsync(object sender, EditorCommand e

private void ExtensionService_ExtensionUpdatedAsync(object sender, EditorCommand e)
{
_languageServer.SendNotification<ExtensionCommandUpdatedNotification>("powerShell/extensionCommandUpdated",
_languageServer?.SendNotification<ExtensionCommandUpdatedNotification>("powerShell/extensionCommandUpdated",
new ExtensionCommandUpdatedNotification
{
Name = e.Name,
Expand All @@ -241,7 +241,7 @@ private void ExtensionService_ExtensionUpdatedAsync(object sender, EditorCommand

private void ExtensionService_ExtensionRemovedAsync(object sender, EditorCommand e)
{
_languageServer.SendNotification<ExtensionCommandRemovedNotification>("powerShell/extensionCommandRemoved",
_languageServer?.SendNotification<ExtensionCommandRemovedNotification>("powerShell/extensionCommandRemoved",
new ExtensionCommandRemovedNotification
{
Name = e.Name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static PowerShellContextService()

private EngineIntrinsics EngineIntrinsics { get; set; }

private PSHost ExternalHost { get; set; }
internal PSHost ExternalHost { get; set; }

/// <summary>
/// Gets a boolean that indicates whether the debugger is currently stopped,
Expand Down