Skip to content
Open
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
3 changes: 2 additions & 1 deletion src/PluginInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ export default class PluginInterface<T extends Plugin = Plugin> {
beforeFileValidate: 'beforeValidateFile',
onFileValidate: 'validateFile',
afterFileValidate: 'afterValidateFile',
onSerializeProgram: 'serializeProgram'
onSerializeProgram: 'serializeProgram',
onGetSourceFixAllCodeActions: 'provideSourceFixAllCodeActions'
};

for (const [oldEvent, newEvent] of Object.entries(upgradeWithWarn)) {
Expand Down
22 changes: 17 additions & 5 deletions src/Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Scope } from './Scope';
import type { NamespaceContainer, NamespaceFileContribution } from './Scope';
import { SymbolTable } from './SymbolTable';
import { DiagnosticMessages } from './DiagnosticMessages';
import type { FileObj, SemanticToken, FileLink, ProvideHoverEvent, ProvideCompletionsEvent, Hover, ProvideDefinitionEvent, ProvideReferencesEvent, ProvideDocumentSymbolsEvent, ProvideWorkspaceSymbolsEvent, BeforeAddFileEvent, BeforeRemoveFileEvent, PrepareFileEvent, PrepareProgramEvent, ProvideFileEvent, SerializedFile, TranspileObj, SerializeFileEvent, ScopeValidationOptions, ExtraSymbolData, ProvideSelectionRangesEvent, OnGetSourceFixAllCodeActionsEvent } from './interfaces';
import type { FileObj, SemanticToken, FileLink, ProvideHoverEvent, ProvideCompletionsEvent, Hover, ProvideDefinitionEvent, ProvideReferencesEvent, ProvideDocumentSymbolsEvent, ProvideWorkspaceSymbolsEvent, BeforeAddFileEvent, BeforeRemoveFileEvent, PrepareFileEvent, PrepareProgramEvent, ProvideFileEvent, SerializedFile, TranspileObj, SerializeFileEvent, ScopeValidationOptions, ExtraSymbolData, ProvideSelectionRangesEvent, ProvideSourceFixAllCodeActionsEvent } from './interfaces';
import type { SourceFixAllCodeAction } from './CodeActionUtil';
import { codeActionUtil } from './CodeActionUtil';
import { standardizePath as s, util } from './util';
Expand Down Expand Up @@ -767,6 +767,8 @@ export class Program {
this.files[file.srcPath.toLowerCase()] = file;
this.destMap.set(file.destPath.toLowerCase(), file);

this.plugins.emit('addFile', fileAddEvent);

this.plugins.emit('afterAddFile', fileAddEvent);

return file;
Expand Down Expand Up @@ -1036,6 +1038,7 @@ export class Program {

const event: BeforeRemoveFileEvent = { file: file, program: this };
this.plugins.emit('beforeRemoveFile', event);
this.plugins.emit('removeFile', event);

//if there is a scope named the same as this file's path, remove it (i.e. xml scopes)
let scope = this.scopes[file.destPath];
Expand Down Expand Up @@ -1906,8 +1909,9 @@ export class Program {

/**
* Compute "source fix all" code actions for the given file.
* Fires the `onGetSourceFixAllCodeActions` plugin event with all diagnostics for the file (no range filter),
* then converts each contributed SourceFixAllCodeAction into an LSP CodeAction.
* Fires the `provideSourceFixAllCodeActions` plugin event (along with its before/after variants)
* with all diagnostics for the file (no range filter), then converts each contributed
* SourceFixAllCodeAction into an LSP CodeAction.
*/
public getSourceFixAllCodeActions(srcPath: string): CodeAction[] {
const actions: SourceFixAllCodeAction[] = [];
Expand All @@ -1918,13 +1922,16 @@ export class Program {
.getDiagnostics()
.filter(x => x.location?.uri === fileUri);
const scopes = this.getScopesForFile(file);
this.plugins.emit('onGetSourceFixAllCodeActions', {
const event: ProvideSourceFixAllCodeActionsEvent = {
program: this,
file: file,
diagnostics: diagnostics,
scopes: scopes,
actions: actions
} as OnGetSourceFixAllCodeActionsEvent);
};
this.plugins.emit('beforeProvideSourceFixAllCodeActions', event);
this.plugins.emit('provideSourceFixAllCodeActions', event);
this.plugins.emit('afterProvideSourceFixAllCodeActions', event);
}
return actions.map(action => codeActionUtil.createCodeAction({
...action,
Expand Down Expand Up @@ -2195,6 +2202,9 @@ export class Program {
files: files,
outDir: outDir
});

await this.plugins.emitAsync('writeProgram', programEvent);

//empty the out directory
await fsExtra.emptyDir(outDir);

Expand Down Expand Up @@ -2238,6 +2248,8 @@ export class Program {
files: options?.files ?? Object.values(this.files)
});

await this.plugins.emitAsync('buildProgram', event);

//prepare the program (and files) for building
event.files = await this.prepare(event.files);

Expand Down
4 changes: 2 additions & 2 deletions src/bscPlugin/BscPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isBrsFile, isXmlFile } from '../astUtils/reflection';
import type { Plugin, ValidateFileEvent, ProvideCodeActionsEvent, ProvideHoverEvent, ProvideSemanticTokensEvent, ValidateScopeEvent, ProvideCompletionsEvent, ProvideDefinitionEvent, ProvideReferencesEvent, ProvideDocumentSymbolsEvent, ProvideWorkspaceSymbolsEvent, AfterProvideFileEvent, AfterValidateFileEvent, AfterValidateProgramEvent, AfterSerializeFileEvent, BeforeBuildProgramEvent, OnPrepareFileEvent, WriteFileEvent, OnGetSourceFixAllCodeActionsEvent, ProvideSelectionRangesEvent } from '../interfaces';
import type { Plugin, ValidateFileEvent, ProvideCodeActionsEvent, ProvideHoverEvent, ProvideSemanticTokensEvent, ValidateScopeEvent, ProvideCompletionsEvent, ProvideDefinitionEvent, ProvideReferencesEvent, ProvideDocumentSymbolsEvent, ProvideWorkspaceSymbolsEvent, AfterProvideFileEvent, AfterValidateFileEvent, AfterValidateProgramEvent, AfterSerializeFileEvent, BeforeBuildProgramEvent, OnPrepareFileEvent, WriteFileEvent, ProvideSourceFixAllCodeActionsEvent, ProvideSelectionRangesEvent } from '../interfaces';
import { CodeActionsProcessor } from './codeActions/CodeActionsProcessor';
import { FixAllCodeActionsProcessor } from './codeActions/FixAllCodeActionsProcessor';
import { CompletionsProcessor } from './completions/CompletionsProcessor';
Expand Down Expand Up @@ -35,7 +35,7 @@ export class BscPlugin implements Plugin {
new CodeActionsProcessor(event).process();
}

public onGetSourceFixAllCodeActions(event: OnGetSourceFixAllCodeActionsEvent) {
public provideSourceFixAllCodeActions(event: ProvideSourceFixAllCodeActionsEvent) {
new FixAllCodeActionsProcessor(event).process();
}

Expand Down
4 changes: 2 additions & 2 deletions src/bscPlugin/codeActions/FixAllCodeActionsProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import type { BsDiagnostic } from '../../interfaces';
import { DiagnosticCodeMap } from '../../DiagnosticMessages';
import type { DiagnosticMessageType } from '../../DiagnosticMessages';
import type { XmlFile } from '../../files/XmlFile';
import type { OnGetSourceFixAllCodeActionsEvent } from '../../interfaces';
import type { ProvideSourceFixAllCodeActionsEvent } from '../../interfaces';
import { isXmlFile } from '../../astUtils/reflection';
import { getMissingExtendsInsertPosition, getRemoveReturnValueChange } from './codeActionHelpers';

export class FixAllCodeActionsProcessor {
public constructor(
public event: OnGetSourceFixAllCodeActionsEvent
public event: ProvideSourceFixAllCodeActionsEvent
) { }

public process() {
Expand Down
40 changes: 38 additions & 2 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,21 @@ export interface Plugin {
removeProgram?(event: RemoveProgramEvent): any;
afterRemoveProgram?(event: AfterRemoveProgramEvent): any;

/**
* Called before the `provideSourceFixAllCodeActions` hook.
*/
beforeProvideSourceFixAllCodeActions?(event: BeforeProvideSourceFixAllCodeActionsEvent): any;
/**
* Emitted when VS Code requests "source fix all" source actions for a file.
* Plugins push one or more `SourceFixAllCodeAction` objects onto `event.actions`,
* each representing a distinct named group that will appear in the Source Actions menu.
* Plugins are responsible for assembling and merging all changes within each action.
*/
onGetSourceFixAllCodeActions?(event: OnGetSourceFixAllCodeActionsEvent): any;
provideSourceFixAllCodeActions?(event: ProvideSourceFixAllCodeActionsEvent): any;
/**
* Called after `provideSourceFixAllCodeActions`. Use this to intercept or sanitize the actions before they are converted to LSP CodeActions.
*/
afterProvideSourceFixAllCodeActions?(event: AfterProvideSourceFixAllCodeActionsEvent): any;

/**
* Emitted before the program starts collecting completions
Expand Down Expand Up @@ -437,6 +445,11 @@ export interface Plugin {
* Includes physical files as well as any virtual files produced by `provideFile` events
*/
beforeAddFile?(event: BeforeAddFileEvent): any;
/**
* Called while a file is being added to the program. This is the central event fired between `beforeAddFile` and `afterAddFile`.
* Includes physical files as well as any virtual files produced by `provideFile` events
*/
addFile?(event: AddFileEvent): any;
/**
* Called after a file has been added to the program.
* Includes physical files as well as any virtual files produced by `provideFile` events
Expand All @@ -447,6 +460,11 @@ export interface Plugin {
* Called before a file is removed from the program. This includes physical and virtual files
*/
beforeRemoveFile?(event: BeforeRemoveFileEvent): any;
/**
* Called while a file is being removed from the program. This is the central event fired between `beforeRemoveFile` and `afterRemoveFile`.
* This includes physical and virtual files
*/
removeFile?(event: RemoveFileEvent): any;
/**
* Called after a file has been removed from the program. This includes physical and virtual files
*/
Expand All @@ -471,6 +489,10 @@ export interface Plugin {
* Called right before the program builds (i.e. generates the code and puts it in the outDir
*/
beforeBuildProgram?(event: BeforeBuildProgramEvent): any;
/**
* Called while the program builds. This is the central event fired between `beforeBuildProgram` and `afterBuildProgram`.
*/
buildProgram?(event: BuildProgramEvent): any;
/**
* Called right after the program builds (i.e. generates the code and puts it in the outDir
*/
Expand Down Expand Up @@ -523,6 +545,10 @@ export interface Plugin {
* Called before any files are written
*/
beforeWriteProgram?(event: BeforeWriteProgramEvent): any;
/**
* Called while files are being written. This is the central event fired between `beforeWriteProgram` and `afterWriteProgram`.
*/
writeProgram?(event: WriteProgramEvent): any;
/**
* Called after all files are written
*/
Expand Down Expand Up @@ -592,7 +618,7 @@ export interface AfterValidateProgramEvent extends BeforeValidateProgramEvent {
wasCancelled: boolean;
}

export interface OnGetSourceFixAllCodeActionsEvent {
export interface ProvideSourceFixAllCodeActionsEvent {
program: Program;
file: BscFile;
/** All diagnostics for this file (not range-filtered) */
Expand All @@ -604,6 +630,12 @@ export interface OnGetSourceFixAllCodeActionsEvent {
*/
actions: SourceFixAllCodeAction[];
}
export type BeforeProvideSourceFixAllCodeActionsEvent = ProvideSourceFixAllCodeActionsEvent;
export type AfterProvideSourceFixAllCodeActionsEvent = ProvideSourceFixAllCodeActionsEvent;
/**
* @deprecated use `ProvideSourceFixAllCodeActionsEvent` instead
*/
export type OnGetSourceFixAllCodeActionsEvent = ProvideSourceFixAllCodeActionsEvent;


export interface ProvideCompletionsEvent<TFile extends BscFile = BscFile> {
Expand All @@ -625,6 +657,7 @@ export interface BeforeBuildProgramEvent {
files: BscFile[];
editor: Editor;
}
export type BuildProgramEvent = BeforeBuildProgramEvent;
export type AfterBuildProgramEvent = BeforeBuildProgramEvent;

export interface ProvideHoverEvent {
Expand Down Expand Up @@ -926,12 +959,14 @@ export interface BeforeAddFileEvent<TFile extends BscFile = BscFile> {
file: TFile;
program: Program;
}
export type AddFileEvent<TFile extends BscFile = BscFile> = BeforeAddFileEvent<TFile>;
export type AfterAddFileEvent<TFile extends BscFile = BscFile> = BeforeAddFileEvent<TFile>;

export interface BeforeRemoveFileEvent<TFile extends BscFile = BscFile> {
file: TFile;
program: Program;
}
export type RemoveFileEvent<TFile extends BscFile = BscFile> = BeforeRemoveFileEvent<TFile>;
export type AfterRemoveFileEvent<TFile extends BscFile = BscFile> = BeforeRemoveFileEvent<TFile>;

export type BeforePrepareProgramEvent = PrepareProgramEvent;
Expand Down Expand Up @@ -1020,6 +1055,7 @@ export interface BeforeWriteProgramEvent {
outDir: string;
files: Map<BscFile, SerializedFile[]>;
}
export type WriteProgramEvent = BeforeWriteProgramEvent;
export type AfterWriteProgramEvent = BeforeWriteProgramEvent;


Expand Down
Loading