Skip to content

Commit

Permalink
[prompt snippets]: move move codec files around
Browse files Browse the repository at this point in the history
  • Loading branch information
legomushroom committed Nov 22, 2024
1 parent 9b242e6 commit 356d78b
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 140 deletions.
18 changes: 17 additions & 1 deletion src/vs/editor/common/codecs/baseToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Range } from '../../../editor/common/core/range.js';
* Base class for all tokens with a `range` that
* reflects token position in the original data.
*/
export class BaseToken {
export abstract class BaseToken {
constructor(
public readonly range: Range,
) { }
Expand All @@ -20,4 +20,20 @@ export class BaseToken {
public sameRange(other: Range): boolean {
return this.range.equalsRange(other);
}

/**
* Returns a string representation of the token.
*/
public abstract toString(): string;

/**
* Check if this token is equal to another one.
*/
public equals<T extends BaseToken>(other: T): boolean {
if (!(other instanceof this.constructor)) {
return false;
}

return this.sameRange(other.range);
}
}
8 changes: 6 additions & 2 deletions src/vs/editor/common/codecs/linesCodec/tokens/line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ export class Line extends BaseToken {
/**
* Check if this token is equal to another one.
*/
public equals(other: Line): boolean {
if (!super.sameRange(other.range)) {
public override equals<T extends BaseToken>(other: T): boolean {
if (!super.equals(other)) {
return false;
}

if (!(other instanceof Line)) {
return false;
}

Expand Down
7 changes: 0 additions & 7 deletions src/vs/editor/common/codecs/linesCodec/tokens/newLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ export class NewLine extends BaseToken {
);
}

/**
* Check if this token is equal to another one.
*/
public equals(other: NewLine): boolean {
return super.sameRange(other.range);
}

/**
* Returns a string representation of the token.
*/
Expand Down
7 changes: 0 additions & 7 deletions src/vs/editor/common/codecs/simpleCodec/tokens/space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ export class Space extends BaseToken {
));
}

/**
* Check if this token is equal to another one.
*/
public equals(other: Space): boolean {
return super.sameRange(other.range);
}

/**
* Returns a string representation of the token.
*/
Expand Down
7 changes: 0 additions & 7 deletions src/vs/editor/common/codecs/simpleCodec/tokens/tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ export class Tab extends BaseToken {
));
}

/**
* Check if this token is equal to another one.
*/
public equals(other: Tab): boolean {
return super.sameRange(other.range);
}

/**
* Returns a string representation of the token.
*/
Expand Down
8 changes: 6 additions & 2 deletions src/vs/editor/common/codecs/simpleCodec/tokens/word.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ export class Word extends BaseToken {
/**
* Check if this token is equal to another one.
*/
public equals(other: Word): boolean {
if (!super.sameRange(other.range)) {
public override equals<T extends BaseToken>(other: T): boolean {
if (!super.equals(other)) {
return false;
}

if (!(other instanceof Word)) {
return false;
}

Expand Down
92 changes: 4 additions & 88 deletions src/vs/editor/test/common/utils/testDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import { BaseToken } from '../../../common/codecs/baseToken.js';
import { Disposable } from '../../../../base/common/lifecycle.js';
import { WriteableStream } from '../../../../base/common/stream.js';
import { BaseDecoder } from '../../../../base/common/codecs/baseDecoder.js';
import { Line, NewLine } from '../../../common/codecs/linesCodec/tokens/index.js';
import { Space, Tab, Word } from '../../../common/codecs/simpleCodec/tokens/index.js';
import { FileReference } from '../../../common/codecs/chatbotPromptCodec/tokens/fileReference.js';

/**
* (pseudo)Random boolean generator.
Expand Down Expand Up @@ -99,91 +96,10 @@ export class TestDecoder<T extends BaseToken, D extends BaseDecoder<T>> extends
const expectedToken = expectedTokens[i];
const receivedtoken = receivedTokens[i];

if (expectedToken instanceof Line) {
assert(
receivedtoken instanceof Line,
`Token '${i}' must be a 'Line', got '${receivedtoken}'.`,
);

assert(
receivedtoken.equals(expectedToken),
`Expected token '${i}' to be '${expectedToken}', got '${receivedtoken}'.`,
);

continue;
}

if (expectedToken instanceof NewLine) {
assert(
receivedtoken instanceof NewLine,
`Token '${i}' must be a 'NewLine', got '${receivedtoken}'.`,
);

assert(
receivedtoken.equals(expectedToken),
`Expected token '${i}' be '${expectedToken}', got '${receivedtoken}'.`,
);

continue;
}

if (expectedToken instanceof Space) {
assert(
receivedtoken instanceof Space,
`Token '${i}' must be a 'Space', got '${receivedtoken}'.`,
);

assert(
receivedtoken.equals(expectedToken),
`Expected token '${i}' be '${expectedToken}', got '${receivedtoken}'.`,
);

continue;
}

if (expectedToken instanceof Word) {
assert(
receivedtoken instanceof Word,
`Token '${i}' must be a 'Word', got '${receivedtoken}'.`,
);

assert(
receivedtoken.equals(expectedToken),
`Expected token '${i}' be '${expectedToken}', got '${receivedtoken}'.`,
);

continue;
}

if (expectedToken instanceof Tab) {
assert(
receivedtoken instanceof Tab,
`Token '${i}' must be a 'Tab ', got '${receivedtoken}'.`,
);

assert(
receivedtoken.equals(expectedToken),
`Expected token '${i}' be '${expectedToken}', got '${receivedtoken}'.`,
);

continue;
}

if (expectedToken instanceof FileReference) {
assert(
receivedtoken instanceof FileReference,
`Token '${i}' must be a 'FileReference ', got '${receivedtoken}'.`,
);

assert(
receivedtoken.equals(expectedToken),
`Expected token '${i}' be '${expectedToken}', got '${receivedtoken}'.`,
);

continue;
}

throw new Error(`Unexpected token type for '${expectedToken}'.`);
assert(
receivedtoken.equals(expectedToken),
`Expected token '${i}' to be '${expectedToken}', got '${receivedtoken}'.`,
);
}

assert.strictEqual(
Expand Down
4 changes: 3 additions & 1 deletion src/vs/workbench/contrib/chat/browser/chatInputPart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,12 @@ export class ChatInputPart extends Disposable implements IHistoryNavigationWidge
chatWidget: IChatWidget,
): IChatRequestVariableEntry[] {
const contextArr = [...this.attachmentModel.attachments];

if (this._implicitContext?.enabled && this._implicitContext.value) {
const mainEntry = this._implicitContext.toBaseEntry();

contextArr.push(mainEntry);

// if the implicit context is a file, it can have nested
// file references that should be included in the context
if (this._implicitContext.validFileReferenceUris) {
Expand All @@ -166,7 +169,6 @@ export class ChatInputPart extends Disposable implements IHistoryNavigationWidge
}
}


// factor in nested references of dynamic variables into the implicit attached context
for (const part of chatWidget.parsedInput.parts) {
if (!(part instanceof ChatRequestDynamicVariablePart)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { VSBuffer } from '../../../../base/common/buffer.js';
import { Disposable } from '../../../../base/common/lifecycle.js';
import { ReadableStream } from '../../../../base/common/stream.js';
import { ICodec } from '../../../../base/common/codecs/types/ICodec.js';
import { VSBuffer } from '../../../../../../base/common/buffer.js';
import { Disposable } from '../../../../../../base/common/lifecycle.js';
import { ReadableStream } from '../../../../../../base/common/stream.js';
import { ICodec } from '../../../../../../base/common/codecs/types/ICodec.js';
import { ChatbotPromptDecoder, TChatbotPromptToken } from './chatbotPromptDecoder.js';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { Word } from '../simpleCodec/tokens/index.js';
import { FileReference } from './tokens/fileReference.js';
import { VSBuffer } from '../../../../base/common/buffer.js';
import { ReadableStream } from '../../../../base/common/stream.js';
import { BaseDecoder } from '../../../../base/common/codecs/baseDecoder.js';
import { SimpleDecoder, TSimpleToken } from '../simpleCodec/simpleDecoder.js';
import { VSBuffer } from '../../../../../../base/common/buffer.js';
import { ReadableStream } from '../../../../../../base/common/stream.js';
import { BaseDecoder } from '../../../../../../base/common/codecs/baseDecoder.js';
import { Word } from '../../../../../../editor/common/codecs/simpleCodec/tokens/index.js';
import { SimpleDecoder, TSimpleToken } from '../../../../../../editor/common/codecs/simpleCodec/simpleDecoder.js';

/**
* Tokens handled by the `ChatbotPromptDecoder` decoder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { BaseToken } from '../../baseToken.js';
import { Word } from '../../simpleCodec/tokens/index.js';
import { assert } from '../../../../../base/common/assert.js';
import { Range } from '../../../../../editor/common/core/range.js';
import { assert } from '../../../../../../../base/common/assert.js';
import { Range } from '../../../../../../../editor/common/core/range.js';
import { BaseToken } from '../../../../../../../editor/common/codecs/baseToken.js';
import { Word } from '../../../../../../../editor/common/codecs/simpleCodec/tokens/index.js';


// Start sequence for a file reference token in a prompt.
const TOKEN_START: string = '#file:';
Expand Down Expand Up @@ -77,11 +78,15 @@ export class FileReference extends BaseToken {
/**
* Check if this token is equal to another one.
*/
public equals(other: FileReference): boolean {
public override equals<T extends BaseToken>(other: T): boolean {
if (!super.sameRange(other.range)) {
return false;
}

if (!(other instanceof FileReference)) {
return false;
}

return this.text === other.text;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { Emitter } from '../../../../base/common/event.js';
import { extUri } from '../../../../base/common/resources.js';
import { Disposable } from '../../../../base/common/lifecycle.js';
import { Location } from '../../../../editor/common/languages.js';
import { ChatbotPromptCodec } from './codecs/chatbotPromptCodec/chatbotPromptCodec.js';
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.js';
import { FileOpenFailed, NotPromptSnippetFile, RecursiveReference } from './promptFileReferenceErrors.js';
import { ChatbotPromptCodec } from '../../../../editor/common/codecs/chatbotPromptCodec/chatbotPromptCodec.js';
import { FileChangesEvent, FileChangeType, IFileService, IFileStreamContent } from '../../../../platform/files/common/files.js';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { TestDecoder } from '../utils/testDecoder.js';
import { Range } from '../../../common/core/range.js';
import { VSBuffer } from '../../../../base/common/buffer.js';
import { newWriteableStream } from '../../../../base/common/stream.js';
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';
import { VSBuffer } from '../../../../../../base/common/buffer.js';
import { Range } from '../../../../../../editor/common/core/range.js';
import { newWriteableStream } from '../../../../../../base/common/stream.js';
import { TestDecoder } from '../../../../../../editor/test/common/utils/testDecoder.js';
import { FileReference } from '../../../common/codecs/chatbotPromptCodec/tokens/fileReference.js';
import { ChatbotPromptCodec } from '../../../common/codecs/chatbotPromptCodec/chatbotPromptCodec.js';
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../../base/test/common/utils.js';
import { ChatbotPromptDecoder, TChatbotPromptToken } from '../../../common/codecs/chatbotPromptCodec/chatbotPromptDecoder.js';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { TestDecoder } from '../utils/testDecoder.js';
import { Range } from '../../../common/core/range.js';
import { VSBuffer } from '../../../../base/common/buffer.js';
import { newWriteableStream } from '../../../../base/common/stream.js';
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';
import { VSBuffer } from '../../../../../../base/common/buffer.js';
import { Range } from '../../../../../../editor/common/core/range.js';
import { newWriteableStream } from '../../../../../../base/common/stream.js';
import { TestDecoder } from '../../../../../../editor/test/common/utils/testDecoder.js';
import { FileReference } from '../../../common/codecs/chatbotPromptCodec/tokens/fileReference.js';
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../../base/test/common/utils.js';
import { ChatbotPromptDecoder, TChatbotPromptToken } from '../../../common/codecs/chatbotPromptCodec/chatbotPromptDecoder.js';

/**
Expand Down

0 comments on commit 356d78b

Please sign in to comment.