Repository navigation
Expand file tree
/
Copy pathCSharpEditorPlugin.cs
More file actions
551 lines (517 loc) · 23.5 KB
/
Copy pathCSharpEditorPlugin.cs
File metadata and controls
551 lines (517 loc) · 23.5 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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Controls;
using CommunityToolkit.Mvvm.Input;
using PdfEditorApp.Core.Plugins;
using PdfEditorApp.Core.Plugins.Descriptors;
using PdfEditorApp.Core.Plugins.Manifests;
using PdfEditorApp.Core.Plugins.Settings;
using PdfEditorApp.Plugins.CSharpEditor.Models;
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.frypdf.plugin.csharpeditor";
public string Name => "FrySharp";
public Version Version => new(1, 0, 0);
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 = "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 = "integer",
DefaultValue = 13
},
["ShowLineNumbers"] = new()
{
Label = "Show Line Numbers",
Description = "Display gutter line numbers along the left margin.",
Type = "boolean",
DefaultValue = true
},
["AutoCheckErrors"] = new()
{
Label = "Live Roslyn Diagnostics",
Description = "Continuously analyze C# code in the background for syntax and semantic errors.",
Type = "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 = "integer",
DefaultValue = 0
}
};
public Task ApplyAsync(IFryPluginContext ctx, CancellationToken ct = default)
{
CSharpStudioHostViewModel? activeHost = null;
void NavigateToStudio(IServiceProvider sp)
{
var homeType = Type.GetType("PdfEditorApp.ViewModels.HomeViewModel, PdfEditorApp");
if (homeType != null)
{
var home = sp.GetService(homeType);
homeType.GetMethod("SelectNavSection")?.Invoke(home, ["CSharpStudio"]);
}
}
var navReg = ctx.RegisterNavigationItem(new NavigationItemDescriptor
{
Id = "CSharpStudio",
Title = "FrySharp",
Group = "Overview",
IconKind = "CodeBraces",
BadgeText = "Studio",
BadgeColorHex = "#75D59A",
Order = 38,
DisplayMode = NavigationDisplayMode.FullViewport,
HideTopSearchBar = true,
ViewFactory = sp =>
{
ctx.TryGetService<IPluginSettingsStore>(out var settingsStore);
var hostVm = new CSharpStudioHostViewModel(sp, settingsStore);
activeHost = hostVm;
return new CSharpStudioHostView { DataContext = hostVm };
}
});
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",
Shortcut = "Ctrl+Alt+E",
Order = 85,
Action = 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",
Shortcut = "Ctrl+Shift+R",
Order = 86,
Action = sp => { _ = 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",
Order = 87,
Action = sp =>
{
NavigateToStudio(sp);
_ = 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
{
WidgetId = "frypdf.status.csharpeditor",
Alignment = StatusBarAlignment.Right,
Order = 14,
ToolTip = "FrySharp & Automation Hub",
Factory = _ => new Button
{
Content = "{ } FrySharp",
Classes = { "m3-tonal-btn" }
}
});
var ribbonActionReg = ctx.RegisterRibbonAction(new RibbonActionDescriptor
{
Id = "frypdf.ribbon.action.csharpeditor",
TabId = "plugins",
GroupId = "tools",
Label = "FrySharp",
Tooltip = "Open full-featured C# development studio and script automation workspace",
IconKind = "CodeBraces",
Order = 30,
Action = NavigateToStudio
});
var shortcuts = new List<IDisposable>
{
ctx.RegisterShortcut(new ShortcutDescriptor
{
Id = "csharp.studio.launch",
Title = "Launch FrySharp",
Description = "Open in-app C# development studio and script automation workspace.",
Category = "Editor",
DefaultGesture = "Ctrl+Alt+E",
MacGesture = "Cmd+Alt+E",
Scope = ShortcutScope.Global,
Action = NavigateToStudio
}),
ctx.RegisterShortcut(new ShortcutDescriptor
{
Id = "csharp.editor.run",
Title = "Run Script / Debug",
Description = "Execute active C# script or debug at current breakpoint.",
Category = "Editor",
DefaultGesture = "F5",
MacGesture = "F5",
Scope = ShortcutScope.Context,
ContextId = "CSharpStudio",
CanExecute = _ => activeHost?.CurrentPage is CSharpCodeStudioViewModel,
Action = _ =>
{
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.stop",
Title = "Stop Script Execution",
Description = "Halt currently running C# background script, or interrupt the active notebook cell.",
Category = "Editor",
DefaultGesture = "Shift+F5",
MacGesture = "Shift+F5",
Scope = ShortcutScope.Context,
ContextId = "CSharpStudio",
CanExecute = _ => activeHost?.CurrentPage is CSharpCodeStudioViewModel or CSharpNotebookStudioViewModel,
Action = _ =>
{
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 Script / Notebook",
Description = "Save active C# script or notebook to local workspace storage.",
Category = "Editor",
DefaultGesture = "Ctrl+S",
MacGesture = "Cmd+S",
Scope = ShortcutScope.Context,
ContextId = "CSharpStudio",
CanExecute = _ => activeHost?.CurrentPage is CSharpCodeStudioViewModel or CSharpNotebookStudioViewModel,
Action = _ =>
{
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 C# Script / Notebook",
Description = "Create a new notebook when Notebook Studio is active, otherwise a new automation script.",
Category = "Editor",
DefaultGesture = "Ctrl+N",
MacGesture = "Cmd+N",
Scope = ShortcutScope.Context,
ContextId = "CSharpStudio",
Action = sp =>
{
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 = "Open NuGet Package Manager",
Description = "Switch to References tab and manage NuGet package dependencies.",
Category = "Editor",
DefaultGesture = "Ctrl+Shift+P",
MacGesture = "Cmd+Shift+P",
Scope = ShortcutScope.Context,
ContextId = "CSharpStudio",
CanExecute = _ => activeHost?.CurrentPage is CSharpCodeStudioViewModel,
Action = _ =>
{
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 Select Next",
Description = "Execute current notebook code cell and advance cursor to next cell.",
Category = "Notebook",
DefaultGesture = "Shift+Enter",
MacGesture = "Shift+Enter",
Scope = ShortcutScope.Context,
ContextId = "CSharpStudio",
CanExecute = _ => activeHost?.CurrentPage is CSharpNotebookStudioViewModel,
Action = _ =>
{
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",
Description = "Execute current notebook cell without moving cursor focus.",
Category = "Notebook",
DefaultGesture = "Ctrl+Enter",
MacGesture = "Cmd+Enter",
Scope = ShortcutScope.Context,
ContextId = "CSharpStudio",
CanExecute = _ => activeHost?.CurrentPage is CSharpNotebookStudioViewModel { ActiveCell: not null },
Action = _ =>
{
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",
Description = "Restart notebook kernel and execute all cells in sequence.",
Category = "Notebook",
DefaultGesture = "Ctrl+Shift+Enter",
MacGesture = "Cmd+Shift+Enter",
Scope = ShortcutScope.Context,
ContextId = "CSharpStudio",
CanExecute = _ => activeHost?.CurrentPage is CSharpNotebookStudioViewModel,
Action = _ =>
{
if (activeHost?.CurrentPage is CSharpNotebookStudioViewModel nbVm)
{
nbVm.RunAllCellsCommand.Execute(null);
}
}
}),
ctx.RegisterShortcut(new ShortcutDescriptor
{
Id = "csharp.notebook.insert_cell_below",
Title = "Insert Cell Below",
Description = "Insert a new C# code cell below current cell.",
Category = "Notebook",
DefaultGesture = "Ctrl+Shift+B",
MacGesture = "Cmd+Shift+B",
Scope = ShortcutScope.Context,
ContextId = "CSharpStudio",
CanExecute = _ => activeHost?.CurrentPage is CSharpNotebookStudioViewModel,
Action = _ =>
{
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",
Description = "Insert a new C# code cell above current cell.",
Category = "Notebook",
DefaultGesture = "Ctrl+Shift+A",
MacGesture = "Cmd+Shift+A",
Scope = ShortcutScope.Context,
ContextId = "CSharpStudio",
CanExecute = _ => activeHost?.CurrentPage is CSharpNotebookStudioViewModel,
Action = _ =>
{
if (activeHost?.CurrentPage is CSharpNotebookStudioViewModel nbVm)
{
nbVm.AddCellAboveCommand.Execute(null);
}
}
}),
ctx.RegisterShortcut(new ShortcutDescriptor
{
Id = "csharp.notebook.delete_cell",
Title = "Delete Active Cell",
Description = "Remove the selected notebook cell from document.",
Category = "Notebook",
DefaultGesture = "Ctrl+Shift+D",
MacGesture = "Cmd+Shift+D",
Scope = ShortcutScope.Context,
ContextId = "CSharpStudio",
CanExecute = _ => activeHost?.CurrentPage is CSharpNotebookStudioViewModel,
Action = _ =>
{
if (activeHost?.CurrentPage is CSharpNotebookStudioViewModel nbVm)
{
nbVm.DeleteActiveCellCommand.Execute(null);
}
}
}),
ctx.RegisterShortcut(new ShortcutDescriptor
{
Id = "csharp.editor.zoom_in",
Title = "Zoom In",
Description = "Increase code canvas and terminal font size.",
Category = "Editor",
DefaultGesture = "Ctrl+=",
MacGesture = "Cmd+=",
Scope = ShortcutScope.Context,
ContextId = "CSharpStudio",
CanExecute = _ => activeHost?.CurrentPage is CSharpCodeStudioViewModel or CSharpNotebookStudioViewModel,
Action = _ =>
{
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",
Description = "Decrease code canvas and terminal font size.",
Category = "Editor",
DefaultGesture = "Ctrl+-",
MacGesture = "Cmd+-",
Scope = ShortcutScope.Context,
ContextId = "CSharpStudio",
CanExecute = _ => activeHost?.CurrentPage is CSharpCodeStudioViewModel or CSharpNotebookStudioViewModel,
Action = _ =>
{
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",
Description = "Reset typography to default 100% (13px).",
Category = "Editor",
DefaultGesture = "Ctrl+0",
MacGesture = "Cmd+0",
Scope = ShortcutScope.Context,
ContextId = "CSharpStudio",
CanExecute = _ => activeHost?.CurrentPage is CSharpCodeStudioViewModel or CSharpNotebookStudioViewModel,
Action = _ =>
{
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 & Themes",
Description = "Recompile and apply in-app customization scripts and theme tokens.",
Category = "Editor",
DefaultGesture = "Ctrl+Shift+R",
MacGesture = "Cmd+Shift+R",
Scope = ShortcutScope.Global,
Action = sp => { _ = 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(() =>
{
killProcessesOnExit.Dispose();
ProcessRegistry.KillAll();
navReg.Dispose();
cmdReg.Dispose();
reloadCustomizationsCmd.Dispose();
openInitScriptCmd.Dispose();
customizationManager.Dispose();
extensionManager.Dispose();
statusReg.Dispose();
ribbonActionReg.Dispose();
foreach (var s in shortcuts) s.Dispose();
});
return Task.CompletedTask;
}
}