diff --git a/src/vs/editor/common/codecs/simpleCodec/tokens/brackets.ts b/src/vs/editor/common/codecs/simpleCodec/tokens/brackets.ts index 9137fc586b3..491cb2a588c 100644 --- a/src/vs/editor/common/codecs/simpleCodec/tokens/brackets.ts +++ b/src/vs/editor/common/codecs/simpleCodec/tokens/brackets.ts @@ -18,7 +18,7 @@ export class LeftBracket extends SimpleToken<'['> { /** * Return text representation of the token. */ - public override get text() { + public override get text(): '[' { return LeftBracket.symbol; } @@ -43,7 +43,7 @@ export class RightBracket extends SimpleToken<']'> { /** * Return text representation of the token. */ - public override get text() { + public override get text(): ']' { return RightBracket.symbol; } diff --git a/src/vs/editor/common/codecs/simpleCodec/tokens/exclamationMark.ts b/src/vs/editor/common/codecs/simpleCodec/tokens/exclamationMark.ts index c019bdd256d..32675fdf8a3 100644 --- a/src/vs/editor/common/codecs/simpleCodec/tokens/exclamationMark.ts +++ b/src/vs/editor/common/codecs/simpleCodec/tokens/exclamationMark.ts @@ -18,7 +18,7 @@ export class ExclamationMark extends SimpleToken<'!'> { /** * Return text representation of the token. */ - public override get text() { + public override get text(): '!' { return ExclamationMark.symbol; } diff --git a/src/vs/editor/common/codecs/simpleCodec/tokens/formFeed.ts b/src/vs/editor/common/codecs/simpleCodec/tokens/formFeed.ts index 699574ab46c..df5b8b0d446 100644 --- a/src/vs/editor/common/codecs/simpleCodec/tokens/formFeed.ts +++ b/src/vs/editor/common/codecs/simpleCodec/tokens/formFeed.ts @@ -18,7 +18,7 @@ export class FormFeed extends SimpleToken<'\f'> { /** * Return text representation of the token. */ - public override get text() { + public override get text(): '\f' { return FormFeed.symbol; } diff --git a/src/vs/platform/prompts/test/common/utils/mock.ts b/src/vs/platform/prompts/test/common/utils/mock.ts index 9b9a088be78..c34231bd5e5 100644 --- a/src/vs/platform/prompts/test/common/utils/mock.ts +++ b/src/vs/platform/prompts/test/common/utils/mock.ts @@ -3,7 +3,55 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { mockObject } from '../../../../../base/test/common/testUtils.js'; +import { assertOneOf } from '../../../../../base/common/types.js'; + +/** + * Mocks an `TObject` with the provided `overrides`. + * + * If you need to mock an `Service`, please use {@link mockService} + * instead which provides better type safety guarantees for the case. + * + * @throws Reading non-overridden property or function + * on `TObject` throws an error. + */ +export function mockObject( + overrides: Partial, +): TObject { + // ensure that the overrides object cannot be modified afterward + overrides = Object.freeze(overrides); + + const keys: (keyof Partial)[] = []; + for (const key in overrides) { + if (Object.hasOwn(overrides, key)) { + keys.push(key); + } + } + + const service: object = new Proxy( + {}, + { + get: ( + _target: TObject, + key: string | number | Symbol, + ): TObject[T] => { + + assertOneOf( + key, + keys, + `The '${key}' is not mocked.`, + ); + + // TODO: @legomushroom - add type assertion comment + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions + return overrides[key as T] as TObject[T]; + }, + }); + + // note! it's ok to `as TObject` here, because of the runtime checks + // in the `Proxy` getter + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions + return service as TObject; +} /** * Type for any service. diff --git a/src/vs/workbench/contrib/chat/common/promptSyntax/codecs/tokens/fileReference.ts b/src/vs/workbench/contrib/chat/common/promptSyntax/codecs/tokens/fileReference.ts index 50fc3489823..78c4f7cb6ea 100644 --- a/src/vs/workbench/contrib/chat/common/promptSyntax/codecs/tokens/fileReference.ts +++ b/src/vs/workbench/contrib/chat/common/promptSyntax/codecs/tokens/fileReference.ts @@ -28,7 +28,7 @@ export class FileReference extends PromptVariableWithData { * Create a {@link FileReference} from a {@link PromptVariableWithData} instance. * @throws if variable name is not equal to {@link VARIABLE_NAME}. */ - public static from(variable: PromptVariableWithData) { + public static from(variable: PromptVariableWithData): FileReference { assert( variable.name === VARIABLE_NAME, `Variable name must be '${VARIABLE_NAME}', got '${variable.name}'.`, diff --git a/src/vs/workbench/contrib/chat/common/promptSyntax/contentProviders/filePromptContentsProvider.ts b/src/vs/workbench/contrib/chat/common/promptSyntax/contentProviders/filePromptContentsProvider.ts index 723bf10ea3f..27d9404edef 100644 --- a/src/vs/workbench/contrib/chat/common/promptSyntax/contentProviders/filePromptContentsProvider.ts +++ b/src/vs/workbench/contrib/chat/common/promptSyntax/contentProviders/filePromptContentsProvider.ts @@ -46,7 +46,7 @@ export class FilePromptContentProvider extends PromptContentsProviderBase = {}, + options: Partial, @IFileService private readonly fileService: IFileService, @IModelService private readonly modelService: IModelService, @ILanguageService private readonly languageService: ILanguageService, @@ -152,7 +152,7 @@ export class FilePromptContentProvider extends PromptContentsProviderBase = {}, + options: Partial, @IInstantiationService private readonly initService: IInstantiationService, @ILogService private readonly logService: ILogService, ) { @@ -149,7 +149,7 @@ export class TextModelContentsProvider extends PromptContentsProviderBase { +export const registerPromptFileContributions = (): void => { registerContributions(LANGUAGE_FEATURE_CONTRIBUTIONS); - registerContribution(ConfigMigration); }; @@ -26,9 +25,7 @@ export type TContribution = new (...args: any[]) => IWorkbenchContribution; /** * Register a specific workbench contribution. */ -const registerContribution = ( - contribution: TContribution, -) => { +const registerContribution = (contribution: TContribution): void => { Registry.as(Extensions.Workbench) .registerWorkbenchContribution(contribution, LifecyclePhase.Eventually); }; @@ -36,9 +33,7 @@ const registerContribution = ( /** * Register a specific workbench contribution. */ -const registerContributions = ( - contributions: readonly TContribution[], -) => { +const registerContributions = (contributions: readonly TContribution[]): void => { contributions - .map(registerContribution); + .forEach(registerContribution); }; diff --git a/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/decorationsProvider/decorations/frontMatterDecoration.ts b/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/decorationsProvider/decorations/frontMatterDecoration.ts index b2c0602dff4..e8be111d262 100644 --- a/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/decorationsProvider/decorations/frontMatterDecoration.ts +++ b/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/decorationsProvider/decorations/frontMatterDecoration.ts @@ -8,7 +8,7 @@ import { localize } from '../../../../../../../../../../nls.js'; import { FrontMatterMarkerDecoration } from './frontMatterMarkerDecoration.js'; import { Position } from '../../../../../../../../../../editor/common/core/position.js'; import { BaseToken } from '../../../../../../../../../../editor/common/codecs/baseToken.js'; -import { TAddAccessor, TDecorationStyles, ReactiveDecorationBase, asCssVariable } from './utils/index.js'; +import { TAddAccessor, TDecorationStyles, ReactiveDecorationBase, asCssVariable, IReactiveDecorationClassNames } from './utils/index.js'; import { contrastBorder, editorBackground } from '../../../../../../../../../../platform/theme/common/colorRegistry.js'; import { ColorIdentifier, darken, registerColor } from '../../../../../../../../../../platform/theme/common/colorUtils.js'; import { FrontMatterHeader } from '../../../../../../../../../../editor/common/codecs/markdownExtensionsCodec/tokens/frontMatterHeader.js'; @@ -92,7 +92,7 @@ export class FrontMatterDecoration extends ReactiveDecorationBase { return CssClassNames; } diff --git a/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/decorationsProvider/decorations/frontMatterMarkerDecoration.ts b/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/decorationsProvider/decorations/frontMatterMarkerDecoration.ts index 31ad42e50b4..9c382df0a33 100644 --- a/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/decorationsProvider/decorations/frontMatterMarkerDecoration.ts +++ b/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/decorationsProvider/decorations/frontMatterMarkerDecoration.ts @@ -4,7 +4,7 @@ *--------------------------------------------------------------------------------------------*/ import { CssClassModifiers } from '../types.js'; -import { TDecorationStyles, ReactiveDecorationBase } from './utils/index.js'; +import { TDecorationStyles, ReactiveDecorationBase, IReactiveDecorationClassNames } from './utils/index.js'; import { FrontMatterMarker } from '../../../../../../../../../../editor/common/codecs/markdownExtensionsCodec/tokens/frontMatterMarker.js'; /** @@ -34,7 +34,7 @@ export class FrontMatterMarkerDecoration extends ReactiveDecorationBase { return CssClassNames; } diff --git a/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/decorationsProvider/decorations/utils/reactiveDecorationBase.ts b/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/decorationsProvider/decorations/utils/reactiveDecorationBase.ts index 2d07e97574b..ec78e9736c5 100644 --- a/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/decorationsProvider/decorations/utils/reactiveDecorationBase.ts +++ b/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/decorationsProvider/decorations/utils/reactiveDecorationBase.ts @@ -143,13 +143,13 @@ export abstract class ReactiveDecorationBase< return this; } - protected override get className() { + protected override get className(): TCssClassName { return (this.active) ? this.classNames.main : this.classNames.mainInactive; } - protected override get inlineClassName() { + protected override get inlineClassName(): TCssClassName { return (this.active) ? this.classNames.inline : this.classNames.inlineInactive; diff --git a/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/decorationsProvider/promptDecorationsProvider.ts b/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/decorationsProvider/promptDecorationsProvider.ts index 0b534a21a63..5fa9591504d 100644 --- a/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/decorationsProvider/promptDecorationsProvider.ts +++ b/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/decorationsProvider/promptDecorationsProvider.ts @@ -8,9 +8,9 @@ import { ProviderInstanceBase } from '../providerInstanceBase.js'; import { ITextModel } from '../../../../../../../../../editor/common/model.js'; import { FrontMatterDecoration } from './decorations/frontMatterDecoration.js'; import { toDisposable } from '../../../../../../../../../base/common/lifecycle.js'; -import { ProviderInstanceManagerBase } from '../providerInstanceManagerBase.js'; import { Position } from '../../../../../../../../../editor/common/core/position.js'; import { BaseToken } from '../../../../../../../../../editor/common/codecs/baseToken.js'; +import { ProviderInstanceManagerBase, TProviderInstance } from '../providerInstanceManagerBase.js'; import { registerThemingParticipant } from '../../../../../../../../../platform/theme/common/themeService.js'; import { FrontMatterHeader } from '../../../../../../../../../editor/common/codecs/markdownExtensionsCodec/tokens/frontMatterHeader.js'; import { DecorationBase, ReactiveDecorationBase, type TDecorationClass, type TChangedDecorator } from './decorations/utils/index.js'; @@ -178,7 +178,7 @@ export class PromptDecorator extends ProviderInstanceBase { /** * Returns a string representation of this object. */ - public override toString() { + public override toString(): string { return `text-model-prompt-decorator:${this.model.uri.path}`; } } @@ -198,7 +198,7 @@ registerThemingParticipant((_theme, collector) => { * Provider for prompt syntax decorators on text models. */ export class PromptDecorationsProviderInstanceManager extends ProviderInstanceManagerBase { - protected override get InstanceClass() { + protected override get InstanceClass(): TProviderInstance { return PromptDecorator; } } diff --git a/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/promptHeaderDiagnosticsProvider.ts b/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/promptHeaderDiagnosticsProvider.ts index a2caf8d250a..b7084e159b1 100644 --- a/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/promptHeaderDiagnosticsProvider.ts +++ b/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/promptHeaderDiagnosticsProvider.ts @@ -7,7 +7,7 @@ import { IPromptsService } from '../../../service/types.js'; import { ProviderInstanceBase } from './providerInstanceBase.js'; import { assertNever } from '../../../../../../../../base/common/assert.js'; import { ITextModel } from '../../../../../../../../editor/common/model.js'; -import { ProviderInstanceManagerBase } from './providerInstanceManagerBase.js'; +import { ProviderInstanceManagerBase, TProviderInstance } from './providerInstanceManagerBase.js'; import { TDiagnostic, PromptMetadataError, PromptMetadataWarning } from '../../../parsers/promptHeader/diagnostics.js'; import { IMarkerData, IMarkerService, MarkerSeverity } from '../../../../../../../../platform/markers/common/markers.js'; @@ -61,7 +61,7 @@ class PromptHeaderDiagnosticsProvider extends ProviderInstanceBase { /** * Returns a string representation of this object. */ - public override toString() { + public override toString(): string { return `prompt-link-diagnostics:${this.model.uri.path}`; } } @@ -100,7 +100,7 @@ const toMarker = ( * classes for each specific editor text model. */ export class PromptHeaderDiagnosticsInstanceManager extends ProviderInstanceManagerBase { - protected override get InstanceClass() { + protected override get InstanceClass(): TProviderInstance { return PromptHeaderDiagnosticsProvider; } } diff --git a/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/promptLinkDiagnosticsProvider.ts b/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/promptLinkDiagnosticsProvider.ts index 222a2c7808f..fe40077ceba 100644 --- a/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/promptLinkDiagnosticsProvider.ts +++ b/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/promptLinkDiagnosticsProvider.ts @@ -10,7 +10,7 @@ import { assert } from '../../../../../../../../base/common/assert.js'; import { NotPromptFile } from '../../../../promptFileReferenceErrors.js'; import { ITextModel } from '../../../../../../../../editor/common/model.js'; import { assertDefined } from '../../../../../../../../base/common/types.js'; -import { ProviderInstanceManagerBase } from './providerInstanceManagerBase.js'; +import { ProviderInstanceManagerBase, TProviderInstance } from './providerInstanceManagerBase.js'; import { IMarkerData, IMarkerService, MarkerSeverity } from '../../../../../../../../platform/markers/common/markers.js'; /** @@ -33,7 +33,7 @@ class PromptLinkDiagnosticsProvider extends ProviderInstanceBase { /** * Update diagnostic markers for the current editor. */ - protected override async onPromptSettled() { + protected override async onPromptSettled(): Promise { // ensure that parsing process is settled await this.parser.allSettled(); @@ -72,7 +72,7 @@ class PromptLinkDiagnosticsProvider extends ProviderInstanceBase { /** * Returns a string representation of this object. */ - public override toString() { + public override toString(): string { return `prompt-link-diagnostics:${this.model.uri.path}`; } } @@ -125,7 +125,7 @@ const toMarker = ( * classes for each specific editor text model. */ export class PromptLinkDiagnosticsInstanceManager extends ProviderInstanceManagerBase { - protected override get InstanceClass() { + protected override get InstanceClass(): TProviderInstance { return PromptLinkDiagnosticsProvider; } } diff --git a/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/providerInstanceManagerBase.ts b/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/providerInstanceManagerBase.ts index c766123e204..68670a2b11c 100644 --- a/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/providerInstanceManagerBase.ts +++ b/src/vs/workbench/contrib/chat/common/promptSyntax/contributions/languageFeatures/providers/providerInstanceManagerBase.ts @@ -24,6 +24,11 @@ export interface IPromptFileEditor extends IEditor { readonly getModel: () => ITextModel; } +/** + * TODO: @legomushroom + */ +export type TProviderInstance = new (editor: ITextModel, ...args: any[]) => TInstance; + /** * A generic base class that manages creation and disposal of {@link TInstance} * objects for each specific editor object that is used for reusable prompt files. @@ -37,7 +42,7 @@ export abstract class ProviderInstanceManagerBase TInstance; + protected abstract get InstanceClass(): TProviderInstance; constructor( @IModelService modelService: IModelService, diff --git a/src/vs/workbench/contrib/chat/common/promptSyntax/parsers/basePromptParser.ts b/src/vs/workbench/contrib/chat/common/promptSyntax/parsers/basePromptParser.ts index d796c8761f6..888f5f669ac 100644 --- a/src/vs/workbench/contrib/chat/common/promptSyntax/parsers/basePromptParser.ts +++ b/src/vs/workbench/contrib/chat/common/promptSyntax/parsers/basePromptParser.ts @@ -171,7 +171,7 @@ export class BasePromptParser * The promise is resolved when at least one parse result (a stream or * an error) has been received from the prompt contents provider. */ - private firstParseResult = new FirstParseResult(); + private readonly firstParseResult = new FirstParseResult(); /** * Returned promise is resolved when the parser process is settled. @@ -445,7 +445,7 @@ export class BasePromptParser /** * Dispose all currently held references. */ - private disposeReferences() { + private disposeReferences(): void { for (const reference of [...this._references]) { reference.dispose(); } @@ -753,7 +753,7 @@ export class BasePromptParser /** * @inheritdoc */ - public override dispose() { + public override dispose(): void { if (this.disposed) { return; } @@ -784,7 +784,7 @@ export class PromptReference extends ObservableDisposable implements TPromptRefe constructor( private readonly promptContentsProvider: IPromptContentsProvider, public readonly token: FileReference | MarkdownLink, - options: Partial = {}, + options: Partial, @IInstantiationService initService: IInstantiationService, ) { super(); @@ -945,7 +945,7 @@ export class PromptReference extends ObservableDisposable implements TPromptRefe /** * Returns a string representation of this object. */ - public override toString() { + public override toString(): string { return `prompt-reference/${this.type}:${this.subtype}/${this.token}`; } } @@ -978,7 +978,7 @@ class FirstParseResult extends DeferredPromise { /** * Complete the underlying promise. */ - public override complete() { + public override complete(): Promise { this._gotResult = true; return super.complete(void 0); } diff --git a/src/vs/workbench/contrib/chat/common/promptSyntax/parsers/filePromptParser.ts b/src/vs/workbench/contrib/chat/common/promptSyntax/parsers/filePromptParser.ts index ab4cd1fb7bc..b2078735461 100644 --- a/src/vs/workbench/contrib/chat/common/promptSyntax/parsers/filePromptParser.ts +++ b/src/vs/workbench/contrib/chat/common/promptSyntax/parsers/filePromptParser.ts @@ -17,7 +17,7 @@ import { IInstantiationService } from '../../../../../../platform/instantiation/ export class FilePromptParser extends BasePromptParser { constructor( uri: URI, - options: Partial = {}, + options: Partial, @IInstantiationService initService: IInstantiationService, @IWorkspaceContextService workspaceService: IWorkspaceContextService, @ILogService logService: ILogService, @@ -31,7 +31,7 @@ export class FilePromptParser extends BasePromptParser { constructor( uri: URI, - options: Partial = {}, + options: Partial, @ILogService logService: ILogService, @IModelService modelService: IModelService, @IInstantiationService instaService: IInstantiationService, @@ -75,7 +75,7 @@ export class PromptParser extends BasePromptParser { /** * Returns a string representation of this object. */ - public override toString() { + public override toString(): string { const { sourceName } = this.contentsProvider; return `prompt-parser:${sourceName}:${this.uri.path}`; diff --git a/src/vs/workbench/contrib/chat/common/promptSyntax/parsers/textModelPromptParser.ts b/src/vs/workbench/contrib/chat/common/promptSyntax/parsers/textModelPromptParser.ts index 72f21fd0a5e..e2ebc518235 100644 --- a/src/vs/workbench/contrib/chat/common/promptSyntax/parsers/textModelPromptParser.ts +++ b/src/vs/workbench/contrib/chat/common/promptSyntax/parsers/textModelPromptParser.ts @@ -17,7 +17,7 @@ import { IInstantiationService } from '../../../../../../platform/instantiation/ export class TextModelPromptParser extends BasePromptParser { constructor( model: ITextModel, - options: Partial = {}, + options: Partial, @IInstantiationService initService: IInstantiationService, @IWorkspaceContextService workspaceService: IWorkspaceContextService, @ILogService logService: ILogService, @@ -36,7 +36,7 @@ export class TextModelPromptParser extends BasePromptParser, + options: Omit, ) { this.originalError = options.originalError; this.errorSubject = options.errorSubject; diff --git a/src/vs/workbench/contrib/chat/common/promptSyntax/utils/treeUtils.ts b/src/vs/workbench/contrib/chat/common/promptSyntax/utils/treeUtils.ts index bfbc36075e0..6c596c3ab7f 100644 --- a/src/vs/workbench/contrib/chat/common/promptSyntax/utils/treeUtils.ts +++ b/src/vs/workbench/contrib/chat/common/promptSyntax/utils/treeUtils.ts @@ -39,9 +39,9 @@ export const forEach = ( } for (const child of treeRoot.children ?? []) { - const shouldStop = forEach(callback, child); + const childShouldStop = forEach(callback, child); - if (shouldStop === true) { + if (childShouldStop === true) { return true; } }