Repository navigation
Expand file tree
/
Copy pathCSharpEditorPlugin.cs
More file actions
690 lines (638 loc) · 31 KB
/
Copy pathCSharpEditorPlugin.cs
File metadata and controls
690 lines (638 loc) · 31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Controls;
using CommunityToolkit.Mvvm.Input;
using FryWorld.Sdk.AI;
using FryWorld.Sdk.Descriptors;
using FryWorld.Sdk.Models;
using FryWorld.Sdk.Plugins;
using FryWorld.Sdk.Services;
using FryWorld.Sdk.Shortcuts;
using Microsoft.Extensions.AI;
using PdfEditorApp.Plugins.CSharpEditor.Models;
using PdfEditorApp.Plugins.CSharpEditor.Services.AI.Providers;
using PdfEditorApp.Plugins.CSharpEditor.Services.AI.Tools;
using PdfEditorApp.Plugins.CSharpEditor.Services.Processes;
using PdfEditorApp.Plugins.CSharpEditor.Views;
using PdfEditorApp.Plugins.CSharpEditor.ViewModels;
using CSharpCodeStudioViewModel = PdfEditorApp.Plugins.CSharpEditor.ViewModels.CodeStudio.CSharpCodeStudioViewModel;
using CSharpNotebookStudioViewModel = PdfEditorApp.Plugins.CSharpEditor.ViewModels.Notebooks.CSharpNotebookStudioViewModel;
using CSharpStudioHostViewModel = PdfEditorApp.Plugins.CSharpEditor.ViewModels.Common.CSharpStudioHostViewModel;
namespace PdfEditorApp.Plugins.CSharpEditor;
/// <summary>
/// FryPDF plugin entry point for FrySharp.
/// Mounts as a full-viewport Workspace Studio Navigation page, Status Bar widget,
/// Command Palette action, and Dynamic Ribbon action (No floating shell overlay).
/// </summary>
public class CSharpEditorPlugin : IFryPlugin
{
public string Id => "com.fryworld.csharpstudio";
public string Name => "FrySharp";
public Version Version => new(2, 0, 6);
public bool AutoOpenOverlay => false;
public IReadOnlyList<Type> RequiredServices => Array.Empty<Type>();
public IReadOnlyDictionary<string, PluginSettingDefinition>? SettingsSchema =>
new Dictionary<string, PluginSettingDefinition>
{
["DefaultTemplate"] = new()
{
Label = "Default Starter Script",
Description = "Initial template loaded when creating a new script.",
Type = PluginSettingType.Select,
Options = new() { "Hello World Console", "PDF Document Automation", "LINQ Benchmark", "JSON Serializer" },
DefaultValue = "Hello World Console"
},
["EditorFontSize"] = new()
{
Label = "Editor Font Size (px)",
Description = "Code typography size in points/pixels.",
Type = PluginSettingType.Integer,
DefaultValue = 13
},
["ShowLineNumbers"] = new()
{
Label = "Show Line Numbers",
Description = "Display gutter line numbers along the left margin.",
Type = PluginSettingType.Boolean,
DefaultValue = true
},
["AutoCheckErrors"] = new()
{
Label = "Live Roslyn Diagnostics",
Description = "Continuously analyze C# code in the background for syntax and semantic errors.",
Type = PluginSettingType.Boolean,
DefaultValue = true
},
["ExecutionTimeoutSeconds"] = new()
{
Label = "Execution Timeout (seconds, 0 = no limit)",
Description = "Maximum allowed run time before script execution is automatically stopped. 0 disables the automatic limit — Stop is always available regardless.",
Type = PluginSettingType.Integer,
DefaultValue = 0
}
};
public Task ApplyAsync(IFryPluginContext ctx, CancellationToken ct = default)
{
// Scripts compile against the desktop API (Display, charts, Avalonia), so fix that before anything compiles.
PdfEditorApp.Plugins.CSharpEditor.Services.Roslyn.DesktopScriptHost.Configure();
CSharpStudioHostViewModel? activeHost = null;
if (ctx.TryGetService<FryWorld.Sdk.Theming.IThemeManager>(out var themeManager))
{
var tokenReg = themeManager.RegisterTokenContributor(new FryWorld.Sdk.Theming.Contributors.DeveloperStudioTokenContributor());
ctx.RegisterDisposable(tokenReg);
}
// Opens the studio page in whichever shell hosts the plugin: FryWorld, FryPDF or the standalone runner.
void NavigateToStudio()
{
if (ctx.TryGetService<IShellService>(out var shell)) shell.NavigateTo("com.fryworld.csharpstudio");
}
CSharpStudioHostViewModel GetHostViewModel()
{
if (activeHost == null)
{
ctx.TryGetService<ISettingsManager>(out var settingsStore);
activeHost = CSharpStudioHostViewModel.ActiveInstance ?? new CSharpStudioHostViewModel(ctx, settingsStore);
}
return activeHost;
}
var navReg = ctx.RegisterNavigationItem(new NavigationItemDescriptor
{
Id = "com.fryworld.csharpstudio",
Title = "FrySharp",
Category = "Overview",
IconKind = "CodeBraces",
AccentColor = "#75D59A",
Order = 38,
ViewFactory = () =>
{
var hostVm = GetHostViewModel();
var view = new CSharpStudioHostView { DataContext = hostVm };
if (ctx.TryGetService<FryWorld.Sdk.Theming.IThemeManager>(out var tm))
{
tm.TrackResourceRoot(view);
}
return view;
}
});
var cmdReg = ctx.RegisterCommand(new CommandPaletteDescriptor
{
Id = "cmd.studio.csharpeditor",
Title = "Open FrySharp",
Subtitle = "Launch in-app C# development studio with project manager, editor, and Roslyn runner",
Category = "Developer Tools",
IconKind = "CodeBraces",
ShortcutKey = OperatingSystem.IsMacOS() ? "Cmd+Alt+E" : "Ctrl+Alt+E",
Command = new RelayCommand(NavigateToStudio)
});
var customizationManager = new PdfEditorApp.Plugins.CSharpEditor.Services.Extensibility.CustomizationManager();
PdfEditorApp.Plugins.CSharpEditor.Services.Extensibility.StudioAppContext.Instance.CustomizationManager = customizationManager;
var extensionManager = new PdfEditorApp.Plugins.CSharpEditor.Services.Extensibility.Extensions.ExtensionManager();
PdfEditorApp.Plugins.CSharpEditor.Services.Extensibility.StudioAppContext.Instance.ExtensionManager = extensionManager;
// The look the user left the studio with (theme, density, colours), before their customization script runs, so
// the script still has the last word.
PdfEditorApp.Plugins.CSharpEditor.Services.Extensibility.Theming.AppearanceRestorer.RestoreFrom(
null, PdfEditorApp.Plugins.CSharpEditor.Services.Extensibility.StudioAppContext.Instance.ThemeEngine);
_ = Task.Run(async () =>
{
var ws = PdfEditorApp.Plugins.CSharpEditor.Services.Extensibility.StudioAppContext.Instance.WorkspaceService.RootPath;
await customizationManager.InitializeAsync(enableHotReload: true, ct);
await extensionManager.DiscoverAndLoadFromDefaultLocationsAsync(workspacePath: ws, enableHotReload: true, ct);
}, ct);
var reloadCustomizationsCmd = ctx.RegisterCommand(new CommandPaletteDescriptor
{
Id = "cmd.studio.reload_customizations",
Title = "Reload Customizations & Themes",
Subtitle = "Recompile and apply ~/.frysharp/init.csx and active themes in real time",
Category = "Developer Tools",
IconKind = "Refresh",
ShortcutKey = OperatingSystem.IsMacOS() ? "Cmd+Shift+R" : "Ctrl+Shift+R",
Command = new RelayCommand(() => { _ = customizationManager.ReloadAsync(); })
});
var openInitScriptCmd = ctx.RegisterCommand(new CommandPaletteDescriptor
{
Id = "cmd.studio.open_init_script",
Title = "Open Customization Script (init.csx)",
Subtitle = "Edit in-app startup script, colors, themes, commands, and hooks",
Category = "Developer Tools",
IconKind = "Tune",
Command = new RelayCommand(() =>
{
NavigateToStudio();
_ = customizationManager.Storage.EnsureInitScriptExistsAsync().ContinueWith(async t =>
{
if (activeHost?.CodeStudioViewModel != null)
{
var item = new ScriptDocumentItem
{
Id = "init.csx",
Title = "init.csx",
Code = await t,
LanguageId = "csharp",
SourceFilePath = customizationManager.Storage.GlobalInitScriptPath
};
await Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
{
activeHost.NavigateToCodeStudio(item);
});
}
});
})
});
var statusReg = ctx.RegisterStatusBarWidget(new StatusBarWidgetDescriptor
{
Id = "frypdf.status.csharpeditor",
AlignRight = true,
Order = 14,
Tooltip = "FrySharp & Automation Hub",
CustomControlFactory = () => new Button
{
Content = "{ } FrySharp",
Classes = { "m3-tonal-btn" },
Command = new RelayCommand(NavigateToStudio)
}
});
var ribbonActionReg = ctx.RegisterRibbonTab(new RibbonTabDescriptor
{
Id = "plugins",
Header = "Plugins",
Actions = new List<RibbonActionDescriptor>
{
new RibbonActionDescriptor
{
Id = "frypdf.ribbon.action.csharpeditor",
GroupId = "tools",
Label = "FrySharp",
Tooltip = "Open full-featured C# development studio and script automation workspace",
IconKind = "CodeBraces",
Order = 30,
Command = new RelayCommand(NavigateToStudio)
}
}
});
// 6. AI Subsystem: Real-time context provider for host Copilot & external MCP clients
var aiProviderReg = ctx.RegisterAiContextProvider(new CSharpAiContextProvider(() => GetHostViewModel()));
// 7. AI Subsystem: Callable tools for autonomous execution, inspection, and patching
var hostAiTools = new CSharpHostAiTools(() => GetHostViewModel(), NavigateToStudio);
var aiTools = new List<IDisposable>
{
ctx.RegisterAiTool(new AiToolDescriptor
{
Name = "csharp_get_active_code",
Description = "Gets the file name, programming language, and complete source code of the active editor document or notebook cell in FrySharp.",
Category = "C# Development",
Function = AIFunctionFactory.Create(hostAiTools.GetActiveCode, "csharp_get_active_code", "Gets the file name, programming language, and source code of the active FrySharp document.")
}),
ctx.RegisterAiTool(new AiToolDescriptor
{
Name = "csharp_get_diagnostics",
Description = "Analyzes C# code using Roslyn compiler diagnostics and returns syntax and semantic errors and warnings. Accepts optional code parameter; if omitted, the active document is analyzed.",
Category = "C# Development",
Function = AIFunctionFactory.Create(hostAiTools.GetDiagnostics, "csharp_get_diagnostics", "Analyzes C# code using Roslyn compiler diagnostics.")
}),
ctx.RegisterAiTool(new AiToolDescriptor
{
Name = "csharp_set_active_code",
Description = "Updates or replaces the code content in the active FrySharp editor document or notebook cell.",
Category = "C# Development",
RequiresConfirmation = false,
Function = AIFunctionFactory.Create(hostAiTools.SetActiveCode, "csharp_set_active_code", "Updates or replaces the code in the active editor.")
}),
ctx.RegisterAiTool(new AiToolDescriptor
{
Name = "csharp_run_script",
Description = "Executes the active script or notebook cell in FrySharp.",
Category = "C# Development",
RequiresConfirmation = false,
Function = AIFunctionFactory.Create(hostAiTools.RunScript, "csharp_run_script", "Executes the active script or notebook cell in FrySharp.")
}),
ctx.RegisterAiTool(new AiToolDescriptor
{
Name = "csharp_stop_script",
Description = "Halts or interrupts active execution in FrySharp.",
Category = "C# Development",
RequiresConfirmation = false,
Function = AIFunctionFactory.Create(hostAiTools.StopScript, "csharp_stop_script", "Halts or interrupts active script execution.")
}),
ctx.RegisterAiTool(new AiToolDescriptor
{
Name = "csharp_get_output",
Description = "Returns the latest execution console output, status, and compiler diagnostics from the active FrySharp editor or notebook.",
Category = "C# Development",
Function = AIFunctionFactory.Create(hostAiTools.GetExecutionOutput, "csharp_get_output", "Returns the latest execution console output, status, and compiler diagnostics.")
}),
ctx.RegisterAiTool(new AiToolDescriptor
{
Name = "csharp_execute_code",
Description = "Executes C# code directly in-memory using Roslyn and returns console output, compilation diagnostics, and execution telemetry.",
Category = "C# Development",
RequiresConfirmation = false,
Function = AIFunctionFactory.Create(hostAiTools.ExecuteCode, "csharp_execute_code", "Executes C# code directly in-memory using Roslyn.")
}),
ctx.RegisterAiTool(new AiToolDescriptor
{
Name = "csharp_new_script",
Description = "Creates a new C# automation script in FrySharp and opens it in Code Studio.",
Category = "C# Development",
RequiresConfirmation = false,
Function = AIFunctionFactory.Create(hostAiTools.NewScript, "csharp_new_script", "Creates a new C# automation script in FrySharp.")
}),
ctx.RegisterAiTool(new AiToolDescriptor
{
Name = "csharp_open_file",
Description = "Opens a script or notebook file from disk into FrySharp Code Studio or Notebook Studio.",
Category = "C# Development",
RequiresConfirmation = false,
Function = AIFunctionFactory.Create(hostAiTools.OpenFile, "csharp_open_file", "Opens a script or notebook file from disk into FrySharp.")
})
};
// 8. AI Subsystem: Turnkey Engineering Prompts
var aiPrompts = new List<IDisposable>
{
ctx.RegisterAiPrompt(new AiPromptDescriptor
{
Id = "csharp.generate_script",
Title = "Create Automation Script",
Description = "Generate a top-level C# automation script with Display(), Dump(), and LINQ.",
Category = "C# Development",
Template = "Create a robust, self-contained C# 13 script for FrySharp using top-level statements. Use Display() to show structured outputs or tables and include error handling."
}),
ctx.RegisterAiPrompt(new AiPromptDescriptor
{
Id = "csharp.fix_diagnostics",
Title = "Fix Compiler Diagnostics",
Description = "Diagnose active Roslyn compiler syntax and type errors and provide corrected code.",
Category = "C# Development",
Template = "Analyze the active C# compiler diagnostics in FrySharp. Identify why each error or warning occurs and provide a clean, idiomatic fix with minimal code changes."
}),
ctx.RegisterAiPrompt(new AiPromptDescriptor
{
Id = "csharp.linq_benchmark",
Title = "Optimize LINQ & Memory",
Description = "Refactor queries for zero-allocation and high performance using Span and Memory.",
Category = "C# Development",
Template = "Review the active C# code and optimize it for high performance and low memory allocation. Propose improvements using Memory<T>, ReadOnlySpan<T>, or optimized LINQ patterns."
}),
ctx.RegisterAiPrompt(new AiPromptDescriptor
{
Id = "csharp.explain_exception",
Title = "Diagnose Runtime Exception",
Description = "Analyze the stack trace and execution output to explain the root cause and remedy.",
Category = "C# Development",
Template = "Examine the latest execution failure or exception in the FrySharp script. Explain the root cause of the error and provide the exact patch needed to fix it."
})
};
// 9. Keyboard Shortcuts with explicit Scope and Category
var shortcuts = new List<IDisposable>
{
ctx.RegisterShortcut(new ShortcutDescriptor
{
Id = "csharp.studio.launch",
Title = "Launch FrySharp",
Category = "Navigation",
Description = "Open in-app C# development studio and script automation workspace.",
KeyGesture = OperatingSystem.IsMacOS() ? "Cmd+Alt+E" : "Ctrl+Alt+E",
Scope = ShortcutScope.Global,
Command = new RelayCommand(NavigateToStudio)
}),
ctx.RegisterShortcut(new ShortcutDescriptor
{
Id = "csharp.editor.run",
Title = "Run / Debug Script",
Category = "Code Studio",
Description = "Execute active C# script or debug at current breakpoint.",
KeyGesture = "F5",
Scope = Id,
Command = new RelayCommand(() =>
{
if (activeHost?.CurrentPage is not CSharpCodeStudioViewModel codeVm) return;
if (codeVm.IsPaused)
{
codeVm.ContinueDebug();
}
else if (!codeVm.IsExecuting && !codeVm.IsDebugging)
{
codeVm.DebugCodeCommand.Execute(null);
}
})
}),
ctx.RegisterShortcut(new ShortcutDescriptor
{
Id = "csharp.editor.runWithoutDebug",
Title = "Run Without Debugging",
Category = "Code Studio",
Description = "Execute active C# script or external language program directly without attaching the debugger.",
KeyGesture = "Ctrl+F5",
Scope = Id,
Command = new RelayCommand(() =>
{
if (activeHost?.CurrentPage is not CSharpCodeStudioViewModel codeVm) return;
if (!codeVm.IsExecuting && !codeVm.IsDebugging)
{
codeVm.RunCodeCommand.Execute(null);
}
})
}),
ctx.RegisterShortcut(new ShortcutDescriptor
{
Id = "csharp.editor.stop",
Title = "Stop Script",
Category = "Code Studio",
Description = "Halt currently running C# background script, or interrupt the active notebook cell.",
KeyGesture = "Shift+F5",
Scope = Id,
Command = new RelayCommand(() =>
{
switch (activeHost?.CurrentPage)
{
case CSharpCodeStudioViewModel { IsDebugging: true } codeVm:
codeVm.StopDebug();
break;
case CSharpCodeStudioViewModel codeVm:
codeVm.StopCommand.Execute(null);
break;
case CSharpNotebookStudioViewModel nbVm:
nbVm.InterruptExecutionCommand.Execute(null);
break;
}
})
}),
ctx.RegisterShortcut(new ShortcutDescriptor
{
Id = "csharp.editor.save",
Title = "Save Document",
Category = "File",
Description = "Save active C# script or notebook to local workspace storage.",
KeyGesture = OperatingSystem.IsMacOS() ? "Cmd+S" : "Ctrl+S",
Scope = Id,
Command = new RelayCommand(() =>
{
switch (activeHost?.CurrentPage)
{
case CSharpCodeStudioViewModel codeVm:
codeVm.SaveCommand.Execute(null);
break;
case CSharpNotebookStudioViewModel nbVm:
nbVm.SaveCommand.Execute(null);
break;
}
})
}),
ctx.RegisterShortcut(new ShortcutDescriptor
{
Id = "csharp.editor.new",
Title = "New Document",
Category = "File",
Description = "Create a new notebook when Notebook Studio is active, otherwise a new automation script.",
KeyGesture = OperatingSystem.IsMacOS() ? "Cmd+N" : "Ctrl+N",
Scope = Id,
Command = new RelayCommand(() =>
{
if (activeHost?.CurrentPage is CSharpNotebookStudioViewModel nbVm)
{
nbVm.NewNotebookTabCommand.Execute(null);
}
else
{
var newScriptTask = activeHost?.ManagerViewModel.CreateNewScriptAsync(null);
}
})
}),
ctx.RegisterShortcut(new ShortcutDescriptor
{
Id = "csharp.editor.show_references",
Title = "Toggle References Tab",
Category = "Code Studio",
Description = "Switch to References tab and manage NuGet package dependencies.",
KeyGesture = OperatingSystem.IsMacOS() ? "Cmd+Shift+P" : "Ctrl+Shift+P",
Scope = Id,
Command = new RelayCommand(() =>
{
if (activeHost?.CurrentPage is CSharpCodeStudioViewModel codeVm)
{
codeVm.SelectedLeftTabIndex = codeVm.SelectedLeftTabIndex == 1 ? 0 : 1;
}
})
}),
ctx.RegisterShortcut(new ShortcutDescriptor
{
Id = "csharp.notebook.run_cell",
Title = "Run Cell and Advance",
Category = "Notebook",
Description = "Execute current notebook code cell and advance cursor to next cell.",
KeyGesture = "Shift+Enter",
Scope = Id,
Command = new RelayCommand(() =>
{
if (activeHost?.CurrentPage is CSharpNotebookStudioViewModel nbVm)
{
nbVm.RunCellAndSelectNextCommand.Execute(null);
}
})
}),
ctx.RegisterShortcut(new ShortcutDescriptor
{
Id = "csharp.notebook.run_cell_stay",
Title = "Run Cell in Place",
Category = "Notebook",
Description = "Execute current notebook cell without moving cursor focus.",
KeyGesture = OperatingSystem.IsMacOS() ? "Cmd+Enter" : "Ctrl+Enter",
Scope = Id,
Command = new RelayCommand(() =>
{
if (activeHost?.CurrentPage is CSharpNotebookStudioViewModel { ActiveCell: not null } nbVm)
{
nbVm.RunSingleCellCommand.Execute(nbVm.ActiveCell);
}
})
}),
ctx.RegisterShortcut(new ShortcutDescriptor
{
Id = "csharp.notebook.run_all",
Title = "Run All Cells",
Category = "Notebook",
Description = "Restart notebook kernel and execute all cells in sequence.",
KeyGesture = OperatingSystem.IsMacOS() ? "Cmd+Shift+Enter" : "Ctrl+Shift+Enter",
Scope = Id,
Command = new RelayCommand(() =>
{
if (activeHost?.CurrentPage is CSharpNotebookStudioViewModel nbVm)
{
nbVm.RunAllCellsCommand.Execute(null);
}
})
}),
ctx.RegisterShortcut(new ShortcutDescriptor
{
Id = "csharp.notebook.insert_cell_below",
Title = "Insert Cell Below",
Category = "Notebook",
Description = "Insert a new C# code cell below current cell.",
KeyGesture = OperatingSystem.IsMacOS() ? "Cmd+Shift+B" : "Ctrl+Shift+B",
Scope = Id,
Command = new RelayCommand(() =>
{
if (activeHost?.CurrentPage is CSharpNotebookStudioViewModel nbVm)
{
nbVm.AddCodeCellCommand.Execute(nbVm.ActiveCell);
}
})
}),
ctx.RegisterShortcut(new ShortcutDescriptor
{
Id = "csharp.notebook.insert_cell_above",
Title = "Insert Cell Above",
Category = "Notebook",
Description = "Insert a new C# code cell above current cell.",
KeyGesture = OperatingSystem.IsMacOS() ? "Cmd+Shift+A" : "Ctrl+Shift+A",
Scope = Id,
Command = new RelayCommand(() =>
{
if (activeHost?.CurrentPage is CSharpNotebookStudioViewModel nbVm)
{
nbVm.AddCellAboveCommand.Execute(null);
}
})
}),
ctx.RegisterShortcut(new ShortcutDescriptor
{
Id = "csharp.notebook.delete_cell",
Title = "Delete Cell",
Category = "Notebook",
Description = "Remove the selected notebook cell from document.",
KeyGesture = OperatingSystem.IsMacOS() ? "Cmd+Shift+D" : "Ctrl+Shift+D",
Scope = Id,
Command = new RelayCommand(() =>
{
if (activeHost?.CurrentPage is CSharpNotebookStudioViewModel nbVm)
{
nbVm.DeleteActiveCellCommand.Execute(null);
}
})
}),
ctx.RegisterShortcut(new ShortcutDescriptor
{
Id = "csharp.editor.zoom_in",
Title = "Zoom In",
Category = "View",
Description = "Increase code canvas and terminal font size.",
KeyGesture = OperatingSystem.IsMacOS() ? "Cmd+=" : "Ctrl+=",
Scope = Id,
Command = new RelayCommand(() =>
{
if (activeHost?.CurrentPage is CSharpCodeStudioViewModel codeVm) codeVm.ZoomInCommand.Execute(null);
else if (activeHost?.CurrentPage is CSharpNotebookStudioViewModel nbVm) nbVm.ZoomInCommand.Execute(null);
})
}),
ctx.RegisterShortcut(new ShortcutDescriptor
{
Id = "csharp.editor.zoom_out",
Title = "Zoom Out",
Category = "View",
Description = "Decrease code canvas and terminal font size.",
KeyGesture = OperatingSystem.IsMacOS() ? "Cmd+-" : "Ctrl+-",
Scope = Id,
Command = new RelayCommand(() =>
{
if (activeHost?.CurrentPage is CSharpCodeStudioViewModel codeVm) codeVm.ZoomOutCommand.Execute(null);
else if (activeHost?.CurrentPage is CSharpNotebookStudioViewModel nbVm) nbVm.ZoomOutCommand.Execute(null);
})
}),
ctx.RegisterShortcut(new ShortcutDescriptor
{
Id = "csharp.editor.zoom_reset",
Title = "Reset Zoom",
Category = "View",
Description = "Reset typography to default 100% (13px).",
KeyGesture = OperatingSystem.IsMacOS() ? "Cmd+0" : "Ctrl+0",
Scope = Id,
Command = new RelayCommand(() =>
{
if (activeHost?.CurrentPage is CSharpCodeStudioViewModel codeVm) codeVm.ResetZoomCommand.Execute(null);
else if (activeHost?.CurrentPage is CSharpNotebookStudioViewModel nbVm) nbVm.ResetZoomCommand.Execute(null);
})
}),
ctx.RegisterShortcut(new ShortcutDescriptor
{
Id = "csharp.studio.reload_customizations",
Title = "Reload Customizations",
Category = "Developer Tools",
Description = "Recompile and apply in-app customization scripts and theme tokens.",
KeyGesture = OperatingSystem.IsMacOS() ? "Cmd+Shift+R" : "Ctrl+Shift+R",
Scope = Id,
Command = new RelayCommand(() => { _ = customizationManager.ReloadAsync(); })
})
};
// Programs the studio starts (Python runs and notebook kernels) end with it: when the app exits, and when the plugin
// is unloaded.
var killProcessesOnExit = ProcessRegistry.KillAllOnExit();
ctx.RegisterEffect(() =>
{
if (activeHost != null)
{
if (CSharpStudioHostViewModel.ActiveInstance == activeHost)
{
CSharpStudioHostViewModel.ActiveInstance = null;
}
activeHost.Dispose();
activeHost = null;
}
killProcessesOnExit.Dispose();
ProcessRegistry.KillAll();
navReg.Dispose();
cmdReg.Dispose();
reloadCustomizationsCmd.Dispose();
openInitScriptCmd.Dispose();
customizationManager.Dispose();
extensionManager.Dispose();
statusReg.Dispose();
ribbonActionReg.Dispose();
aiProviderReg.Dispose();
foreach (var t in aiTools) t.Dispose();
foreach (var p in aiPrompts) p.Dispose();
foreach (var s in shortcuts) s.Dispose();
});
return Task.CompletedTask;
}
}