Skip to content

Commit 6e52a9d

Browse files
Fix temp debugging after it broke bringing in $psEditor (#1150)
* Fix temp debugging after it broke bringing in $psEditor * codacy
1 parent 5f1253e commit 6e52a9d

File tree

6 files changed

+118
-11
lines changed

6 files changed

+118
-11
lines changed

module/PowerShellEditorServices/Commands/Public/CmdletInterface.ps1

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ function New-EditorFile {
124124
}
125125

126126
end {
127+
# If editorContext is null, then we're in a Temp session and
128+
# this cmdlet won't work so return early.
129+
$editorContext = $psEditor.GetEditorContext()
130+
if (!$editorContext) {
131+
return
132+
}
133+
127134
if ($Path) {
128135
foreach ($fileName in $Path)
129136
{
@@ -142,7 +149,7 @@ function New-EditorFile {
142149
}
143150

144151
$psEditor.Workspace.OpenFile($fileName, $preview)
145-
$psEditor.GetEditorContext().CurrentFile.InsertText(($valueList | Out-String))
152+
$editorContext.CurrentFile.InsertText(($valueList | Out-String))
146153
} else {
147154
$PSCmdlet.WriteError( (
148155
New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList @(
@@ -154,7 +161,7 @@ function New-EditorFile {
154161
}
155162
} else {
156163
$psEditor.Workspace.NewFile()
157-
$psEditor.GetEditorContext().CurrentFile.InsertText(($valueList | Out-String))
164+
$editorContext.CurrentFile.InsertText(($valueList | Out-String))
158165
}
159166
}
160167
}

src/PowerShellEditorServices.Hosting/Commands/StartEditorServicesCommand.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,17 @@ private void StartLogging()
256256
_loggerUnsubscribers.Add(_logger.Subscribe(hostLogger));
257257
}
258258

259-
string logPath = Path.Combine(GetLogDirPath(), "StartEditorServices.log");
259+
string logDirPath = GetLogDirPath();
260+
string logPath = Path.Combine(logDirPath, "StartEditorServices.log");
261+
262+
// Temp debugging sessions may try to reuse this same path,
263+
// so we ensure they have a unique path
264+
if (File.Exists(logPath))
265+
{
266+
int randomInt = new Random().Next();
267+
logPath = Path.Combine(logDirPath, $"StartEditorServices-temp{randomInt.ToString("X")}.log");
268+
}
269+
260270
var fileLogger = StreamLogger.CreateWithNewFile(logPath);
261271
_disposableResources.Add(fileLogger);
262272
IDisposable fileLoggerUnsubscriber = _logger.Subscribe(fileLogger);

src/PowerShellEditorServices/Hosting/EditorServicesServerFactory.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.IO;
1414
using Microsoft.Extensions.DependencyInjection;
1515
using OmniSharp.Extensions.LanguageServer.Server;
16+
using Microsoft.PowerShell.EditorServices.Services;
1617

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

133+
// This gets the ExtensionService which triggers the creation of the `$psEditor` variable.
134+
// (because services are created only when they are first retrieved)
135+
// Keep in mind, for Temp sessions, the `$psEditor` API is a no-op and the user is warned
136+
// to run the command in the main PS Integrated Console.
137+
serviceProvider.GetService<ExtensionService>();
138+
128139
return new PsesDebugServer(
129140
_loggerFactory,
130141
inputStream,

src/PowerShellEditorServices/Services/PowerShellContext/EditorOperationsService.cs

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,27 @@ internal class EditorOperationsService : IEditorOperations
1616
{
1717
private const bool DefaultPreviewSetting = true;
1818

19-
private WorkspaceService _workspaceService;
20-
private ILanguageServer _languageServer;
19+
private readonly WorkspaceService _workspaceService;
20+
private readonly PowerShellContextService _powerShellContextService;
21+
private readonly ILanguageServer _languageServer;
2122

2223
public EditorOperationsService(
2324
WorkspaceService workspaceService,
25+
PowerShellContextService powerShellContextService,
2426
ILanguageServer languageServer)
2527
{
26-
this._workspaceService = workspaceService;
27-
this._languageServer = languageServer;
28+
_workspaceService = workspaceService;
29+
_powerShellContextService = powerShellContextService;
30+
_languageServer = languageServer;
2831
}
2932

3033
public async Task<EditorContext> GetEditorContextAsync()
3134
{
35+
if (!TestHasLanguageServer())
36+
{
37+
return null;
38+
};
39+
3240
ClientEditorContext clientContext =
3341
await _languageServer.SendRequest<GetEditorContextRequest, ClientEditorContext>(
3442
"editor/getEditorContext",
@@ -39,6 +47,11 @@ await _languageServer.SendRequest<GetEditorContextRequest, ClientEditorContext>(
3947

4048
public async Task InsertTextAsync(string filePath, string text, BufferRange insertRange)
4149
{
50+
if (!TestHasLanguageServer())
51+
{
52+
return;
53+
};
54+
4255
await _languageServer.SendRequest<InsertTextRequest>("editor/insertText", new InsertTextRequest
4356
{
4457
FilePath = filePath,
@@ -62,6 +75,10 @@ public async Task InsertTextAsync(string filePath, string text, BufferRange inse
6275

6376
public async Task SetSelectionAsync(BufferRange selectionRange)
6477
{
78+
if (!TestHasLanguageServer())
79+
{
80+
return;
81+
};
6582

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

107124
public async Task NewFileAsync()
108125
{
126+
if (!TestHasLanguageServer())
127+
{
128+
return;
129+
};
130+
109131
await _languageServer.SendRequest<string>("editor/newFile", null);
110132
}
111133

112134
public async Task OpenFileAsync(string filePath)
113135
{
136+
if (!TestHasLanguageServer())
137+
{
138+
return;
139+
};
140+
114141
await _languageServer.SendRequest<OpenFileDetails>("editor/openFile", new OpenFileDetails
115142
{
116143
FilePath = filePath,
@@ -120,6 +147,11 @@ public async Task OpenFileAsync(string filePath)
120147

121148
public async Task OpenFileAsync(string filePath, bool preview)
122149
{
150+
if (!TestHasLanguageServer())
151+
{
152+
return;
153+
};
154+
123155
await _languageServer.SendRequest<OpenFileDetails>("editor/openFile", new OpenFileDetails
124156
{
125157
FilePath = filePath,
@@ -129,6 +161,11 @@ public async Task OpenFileAsync(string filePath, bool preview)
129161

130162
public async Task CloseFileAsync(string filePath)
131163
{
164+
if (!TestHasLanguageServer())
165+
{
166+
return;
167+
};
168+
132169
await _languageServer.SendRequest<string>("editor/closeFile", filePath);
133170
}
134171

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

140177
public async Task SaveFileAsync(string currentPath, string newSavePath)
141178
{
179+
if (!TestHasLanguageServer())
180+
{
181+
return;
182+
};
183+
142184
await _languageServer.SendRequest<SaveFileDetails>("editor/saveFile", new SaveFileDetails
143185
{
144186
FilePath = currentPath,
@@ -158,21 +200,41 @@ public string GetWorkspaceRelativePath(string filePath)
158200

159201
public async Task ShowInformationMessageAsync(string message)
160202
{
203+
if (!TestHasLanguageServer())
204+
{
205+
return;
206+
};
207+
161208
await _languageServer.SendRequest<string>("editor/showInformationMessage", message);
162209
}
163210

164211
public async Task ShowErrorMessageAsync(string message)
165212
{
213+
if (!TestHasLanguageServer())
214+
{
215+
return;
216+
};
217+
166218
await _languageServer.SendRequest<string>("editor/showErrorMessage", message);
167219
}
168220

169221
public async Task ShowWarningMessageAsync(string message)
170222
{
223+
if (!TestHasLanguageServer())
224+
{
225+
return;
226+
};
227+
171228
await _languageServer.SendRequest<string>("editor/showWarningMessage", message);
172229
}
173230

174231
public async Task SetStatusBarMessageAsync(string message, int? timeout)
175232
{
233+
if (!TestHasLanguageServer())
234+
{
235+
return;
236+
}
237+
176238
await _languageServer.SendRequest<StatusBarMessageDetails>("editor/setStatusBarMessage", new StatusBarMessageDetails
177239
{
178240
Message = message,
@@ -182,7 +244,24 @@ public async Task SetStatusBarMessageAsync(string message, int? timeout)
182244

183245
public void ClearTerminal()
184246
{
247+
if (!TestHasLanguageServer())
248+
{
249+
return;
250+
};
251+
185252
_languageServer.SendNotification("editor/clearTerminal");
186253
}
254+
255+
private bool TestHasLanguageServer()
256+
{
257+
if (_languageServer != null)
258+
{
259+
return true;
260+
}
261+
262+
_powerShellContextService.ExternalHost.UI.WriteWarningLine(
263+
"Editor operations are not supported in temporary consoles. Re-run the command in the main PowerShell Intergrated Console.");
264+
return false;
265+
}
187266
}
188267
}

src/PowerShellEditorServices/Services/PowerShellContext/ExtensionService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ private void OnCommandRemoved(EditorCommand command)
222222

223223
private void ExtensionService_ExtensionAddedAsync(object sender, EditorCommand e)
224224
{
225-
_languageServer.SendNotification<ExtensionCommandAddedNotification>("powerShell/extensionCommandAdded",
225+
_languageServer?.SendNotification<ExtensionCommandAddedNotification>("powerShell/extensionCommandAdded",
226226
new ExtensionCommandAddedNotification
227227
{
228228
Name = e.Name,
@@ -232,7 +232,7 @@ private void ExtensionService_ExtensionAddedAsync(object sender, EditorCommand e
232232

233233
private void ExtensionService_ExtensionUpdatedAsync(object sender, EditorCommand e)
234234
{
235-
_languageServer.SendNotification<ExtensionCommandUpdatedNotification>("powerShell/extensionCommandUpdated",
235+
_languageServer?.SendNotification<ExtensionCommandUpdatedNotification>("powerShell/extensionCommandUpdated",
236236
new ExtensionCommandUpdatedNotification
237237
{
238238
Name = e.Name,
@@ -241,7 +241,7 @@ private void ExtensionService_ExtensionUpdatedAsync(object sender, EditorCommand
241241

242242
private void ExtensionService_ExtensionRemovedAsync(object sender, EditorCommand e)
243243
{
244-
_languageServer.SendNotification<ExtensionCommandRemovedNotification>("powerShell/extensionCommandRemoved",
244+
_languageServer?.SendNotification<ExtensionCommandRemovedNotification>("powerShell/extensionCommandRemoved",
245245
new ExtensionCommandRemovedNotification
246246
{
247247
Name = e.Name,

src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static PowerShellContextService()
8484

8585
private EngineIntrinsics EngineIntrinsics { get; set; }
8686

87-
private PSHost ExternalHost { get; set; }
87+
internal PSHost ExternalHost { get; set; }
8888

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

0 commit comments

Comments
 (0)