Skip to content
Merged
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
27 changes: 9 additions & 18 deletions src/vs/workbench/contrib/chat/common/chatWordCounter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,27 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

const wordSeparatorCharPattern = /[\s\|\-]/;

export interface IWordCountResult {
value: string;
actualWordCount: number;
isFullString: boolean;
}

export function getNWords(str: string, numWordsToCount: number): IWordCountResult {
let wordCount = numWordsToCount;
let i = 0;
while (i < str.length && wordCount > 0) {
// Consume word separator chars
while (i < str.length && str[i].match(wordSeparatorCharPattern)) {
i++;
}
// Match words and markdown style links
const allWordMatches = Array.from(str.matchAll(/\[([^\]]+)\]\(([^)]+)\)|[^\s\|\-]+/g));

// Consume word chars
while (i < str.length && !str[i].match(wordSeparatorCharPattern)) {
i++;
}
const targetWords = allWordMatches.slice(0, numWordsToCount);

wordCount--;
}
const endIndex = numWordsToCount > allWordMatches.length
? str.length // Reached end of string
: targetWords.length ? targetWords.at(-1)!.index + targetWords.at(-1)![0].length : 0;

const value = str.substring(0, i);
const value = str.substring(0, endIndex);
return {
value,
actualWordCount: numWordsToCount - wordCount,
isFullString: i >= str.length
actualWordCount: targetWords.length === 0 ? (value.length ? 1 : 0) : targetWords.length,
isFullString: endIndex >= str.length
};
}

Expand Down
11 changes: 11 additions & 0 deletions src/vs/workbench/contrib/chat/test/common/chatWordCounter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,15 @@ suite('ChatWordCounter', () => {

cases.forEach(([str, nWords, result]) => doTest(str, nWords, result));
});

test('getNWords, matching links', () => {
const cases: [string, number, string][] = [
['[hello](https://example.com) world', 1, '[hello](https://example.com)'],
['[hello](https://example.com) world', 2, '[hello](https://example.com) world'],
['oh [hello](https://example.com "title") world', 1, 'oh'],
['oh [hello](https://example.com "title") world', 2, 'oh [hello](https://example.com "title")'],
];

cases.forEach(([str, nWords, result]) => doTest(str, nWords, result));
});
});