====== Architettura delle estensioni ======
Descrizione dettagliata dell'architettura interna di un'estensione WvdS per VSCode.
===== Ciclo di vita =====
┌─────────────────────────────────────────────────────────────┐
│ EXTENSION LIFECYCLE │
├─────────────────────────────────────────────────────────────┤
│ │
│ 1. Avvio VS Code │
│ ↓ │
│ 2. Activation Event attivato │
│ (onCommand, onLanguage, onStartupFinished, ...) │
│ ↓ │
│ 3. extension_main.js viene caricato │
│ ↓ │
│ 4. Activate() viene chiamato │
│ → Inizializzare i servizi │
│ → Registrare i comandi │
│ → Configurare gli event handler │
│ ↓ │
│ 5. L'estensione e attiva │
│ → Interazione utente │
│ → Esecuzione comandi │
│ ↓ │
│ 6. Shutdown VS Code / Estensione disattivata │
│ ↓ │
│ 7. Deactivate() viene chiamato │
│ → Cleanup │
│ → Rilascio risorse │
│ │
└─────────────────────────────────────────────────────────────┘
===== Entry Point =====
Ogni estensione ha esattamente un entry point: ''extension_main.pas''
unit extension_main;
{$mode objfpc}{$H+}
interface
uses
JS, VSCode.API, VSCode.ExtensionExports;
procedure Activate(AContext: TExtensionContext);
procedure Deactivate;
implementation
procedure Activate(AContext: TExtensionContext);
begin
// Inizializzazione qui
end;
procedure Deactivate;
begin
// Cleanup qui
end;
initialization
ExportActivateDeactivate(@Activate, @Deactivate);
end.
===== Activation Events =====
^ Evento ^ Descrizione ^ Esempio ^
| onStartupFinished | Dopo l'avvio di VS Code | Estensione Core |
| onCommand:* | Alla chiamata di un comando | wvds.build.run |
| onLanguage:* | All'apertura di un file | pascal, pxaml |
| onView:* | All'apertura di una vista | wvds.toolbox |
| workspaceContains:** | Quando esiste un file | *.lpr, *.pas |
| onCustomEditor:* | Con Custom Editor | wvds.designer |
===== Registrazione comandi =====
procedure RegisterCommands(AContext: TExtensionContext);
begin
// Comando semplice
RegisterCommand('wvds.feature.action', @HandleAction);
// Comando con argomenti
RegisterCommand('wvds.feature.open', @HandleOpen);
// Comando asincrono
RegisterAsyncCommand('wvds.feature.process', @HandleProcessAsync);
end;
procedure HandleAction(Args: TJSValueDynArray);
begin
ShowInfoMessage('Azione eseguita');
end;
function HandleProcessAsync(Args: TJSValueDynArray): TJSPromise;
begin
Result := TJSPromise.New(
procedure(Resolve, Reject: TJSPromiseResolver)
begin
// Lavoro asincrono...
Resolve(nil);
end
);
end;
===== Pattern Service =====
// Feature.Service.pas
unit Feature.Service;
interface
type
TWvdSFeatureService = class
private
FInitialized: Boolean;
public
procedure Initialize;
procedure Shutdown;
function DoWork(const AInput: string): string;
end;
var
FeatureService: TWvdSFeatureService;
implementation
procedure TWvdSFeatureService.Initialize;
begin
if FInitialized then Exit;
// Inizializzare il servizio
FInitialized := True;
end;
procedure TWvdSFeatureService.Shutdown;
begin
if not FInitialized then Exit;
// Pulizia
FInitialized := False;
end;
initialization
FeatureService := TWvdSFeatureService.Create;
finalization
FeatureService.Free;
end.
===== Disposables =====
VS Code usa i Disposables per la gestione delle risorse:
procedure Activate(AContext: TExtensionContext);
var
Disposable: TDisposable;
begin
// Registrare comando e ottenere Disposable
Disposable := RegisterCommand('wvds.test', @Handler);
// Aggiungere al context per cleanup automatico
AContext.Subscriptions.Push(Disposable);
end;
===== Integrazione WebView =====
procedure ShowWebViewPanel;
var
Panel: TWebviewPanel;
begin
Panel := CreateWebviewPanel(
'wvds.feature.panel', // viewType
'Feature Panel', // title
ViewColumn.One, // showOptions
WebviewOptions // options
);
// Impostare HTML
Panel.Webview.Html := GetPanelHtml;
// Configurare messaging
Panel.Webview.OnDidReceiveMessage(@HandleWebviewMessage);
// Inviare messaggi
Panel.Webview.PostMessage(TJSObject.New);
end;
===== Gestione eventi =====
procedure SetupEventHandlers;
begin
// Cambio configurazione
OnDidChangeConfiguration(@HandleConfigChange);
// Eventi documento
OnDidOpenTextDocument(@HandleDocumentOpen);
OnDidSaveTextDocument(@HandleDocumentSave);
OnDidCloseTextDocument(@HandleDocumentClose);
// Eventi workspace
OnDidChangeWorkspaceFolders(@HandleWorkspaceChange);
end;
===== Vedi anche =====
* [[.:architektur|Panoramica architettura]]
* [[.:extension-entwicklung|Sviluppo estensioni]]
* [[.:vscode-wrapper|API Wrapper VSCode]]