feat: replace large text pastes with [Pasted Text: X lines] placeholder (#16422)

This commit is contained in:
Jack Wotherspoon
2026-01-21 21:09:24 -05:00
committed by GitHub
parent 27d21f9921
commit 75e4f492ab
7 changed files with 619 additions and 28 deletions

View File

@@ -4,21 +4,28 @@
* SPDX-License-Identifier: Apache-2.0
*/
import {
type Transformation,
PASTED_TEXT_PLACEHOLDER_REGEX,
} from '../components/shared/text-buffer.js';
import { LRUCache } from 'mnemonist';
import type { Transformation } from '../components/shared/text-buffer.js';
import { cpLen, cpSlice } from './textUtils.js';
import { LRU_BUFFER_PERF_CACHE_LIMIT } from '../constants.js';
export type HighlightToken = {
text: string;
type: 'default' | 'command' | 'file';
type: 'default' | 'command' | 'file' | 'paste';
};
// Matches slash commands (e.g., /help) and @ references (files or MCP resource URIs).
// Matches slash commands (e.g., /help), @ references (files or MCP resource URIs),
// and large paste placeholders (e.g., [Pasted Text: 6 lines]).
// The @ pattern uses a negated character class to support URIs like `@file:///example.txt`
// which contain colons. It matches any character except delimiters: comma, whitespace,
// semicolon, common punctuation, and brackets.
const HIGHLIGHT_REGEX = /(^\/[a-zA-Z0-9_-]+|@(?:\\ |[^,\s;!?()[\]{}])+)/g;
const HIGHLIGHT_REGEX = new RegExp(
`(^/[a-zA-Z0-9_-]+|@(?:\\\\ |[^,\\s;!?()\\[\\]{}])+|${PASTED_TEXT_PLACEHOLDER_REGEX.source})`,
'g',
);
const highlightCache = new LRUCache<string, readonly HighlightToken[]>(
LRU_BUFFER_PERF_CACHE_LIMIT,
@@ -66,7 +73,11 @@ export function parseInputForHighlighting(
tokens.push({ text: text.slice(last, matchIndex), type: 'default' });
}
const type = fullMatch.startsWith('/') ? 'command' : 'file';
const type = fullMatch.startsWith('/')
? 'command'
: fullMatch.startsWith('@')
? 'file'
: 'paste';
if (type === 'command' && index !== 0) {
tokens.push({ text: fullMatch, type: 'default' });
} else {