-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Highlight @file references and command options in message text #1145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0eb1497
feat(coding-agent): highlight @path references and --flags in editor,…
snimu 9fa8752
docs(tui): add changelog entry for editor source-coordinate styling h…
snimu f87b0ff
fix(coding-agent): keep line structure for multi-line quoted @refs an…
snimu f9081c5
feat(coding-agent): highlight bare -- end-of-options separator in sla…
snimu 36b7baa
chore: merge main and convert changelog entry to fragment
snimu 85cbe7e
Merge remote-tracking branch 'origin/main' into feat/queue-path-arg-h…
snimu 66359df
fix(coding-agent): keep arg-token color past the cursor reset mid-token
snimu 8435a49
refactor(coding-agent): dedupe the editor command-token pattern
snimu aac41f2
fix(coding-agent): forward table-cell selection regions through Highl…
snimu 3e0e277
fix(coding-agent): identity-based mask placeholders so copied table c…
snimu 8e6790d
fix(coding-agent): gate the bare -- separator on argument-taking comm…
snimu f08133f
fix(coding-agent): normalize tabs before token masking to match Markd…
snimu 285ccf4
refactor(coding-agent): condense highlight comments and table-drive t…
snimu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
packages/coding-agent/.changes/feat-queue-path-arg-highlighting.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| - Highlighted `@path` file references and `--flags` in the editor, queued message previews, and sent user messages, plus the bare `--` end-of-options separator in recognized slash commands. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
198 changes: 198 additions & 0 deletions
198
packages/coding-agent/src/modes/interactive/components/prompt-highlight.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| import { visibleWidth } from "@earendil-works/pi-tui"; | ||
| import { type ThemeColor, theme } from "../theme/theme.js"; | ||
|
|
||
| const ARG_TOKEN_PATTERN = /@"[^"\n]*"|@[^\s\x1b]+|--[A-Za-z0-9][A-Za-z0-9-]*/g; | ||
| /** Also matches a bare `--` end-of-options separator; only used for argument-taking slash commands. */ | ||
| const ARG_TOKEN_PATTERN_WITH_SEPARATOR = /@"[^"\n]*"|@[^\s\x1b]+|--[A-Za-z0-9][A-Za-z0-9-]*|--(?=\s|$)/g; | ||
| const FG_SGR_PATTERN = /\x1b\[(?:0|39|3[0-7]|9[0-7]|38;[0-9;]+)m/g; | ||
| /** Escape sequences the editor splices into displayed text (cursor highlight, IME marker). */ | ||
| const CURSOR_ESCAPE_PATTERN = /\x1b\[[0-9;]*m|\x1b_[^\x07]*\x07/g; | ||
|
|
||
| const MASK_BASE_START = 0xe000; | ||
| /** Each masked grapheme gets its own private-use base char, so restoring is a lookup, not positional. */ | ||
| const MASK_CAPACITY = 0xf8ff - MASK_BASE_START + 1; | ||
| const MASK_EXTRA_WIDTH = "\uFF9E"; | ||
| const MASK_PATTERN = /[\uE000-\uF8FF]\uFF9E*/gu; | ||
| /** Literal mask-range characters would alias generated placeholders; messages containing them skip masking. */ | ||
| const MASK_LITERAL_PATTERN = /[\uE000-\uF8FF\uFF9E]/u; | ||
|
|
||
| const graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" }); | ||
|
|
||
| interface ArgTokenSpan { | ||
| start: number; | ||
| end: number; | ||
| color: ThemeColor; | ||
| } | ||
|
|
||
| function tokenColor(token: string): ThemeColor { | ||
| return token.startsWith("@") ? "success" : "mdLink"; | ||
| } | ||
|
|
||
| function hasTokenBoundary(text: string, index: number): boolean { | ||
| return index === 0 || /\s/.test(text.charAt(index - 1)); | ||
| } | ||
|
|
||
| function findArgTokens(text: string, fromIndex = 0, includeBareSeparator = false): ArgTokenSpan[] { | ||
| const spans: ArgTokenSpan[] = []; | ||
| const pattern = includeBareSeparator ? ARG_TOKEN_PATTERN_WITH_SEPARATOR : ARG_TOKEN_PATTERN; | ||
| for (const match of text.matchAll(pattern)) { | ||
| if (match.index < fromIndex || !hasTokenBoundary(text, match.index)) continue; | ||
| spans.push({ start: match.index, end: match.index + match[0].length, color: tokenColor(match[0]) }); | ||
| } | ||
| return spans; | ||
| } | ||
|
|
||
| /** Foreground SGR active at index; theme.fg() closes with \x1b[39m, so it must be re-emitted. */ | ||
| function activeFgBefore(line: string, index: number): string { | ||
| let active = ""; | ||
| for (const sgr of line.slice(0, index).matchAll(FG_SGR_PATTERN)) { | ||
| active = sgr[0] === "\x1b[0m" ? "" : sgr[0]; | ||
| } | ||
| return active; | ||
| } | ||
|
|
||
| export function styleArgumentTokens( | ||
| text: string, | ||
| styleOther: (segment: string) => string = (segment) => segment, | ||
| includeBareSeparator = false, | ||
| ): string { | ||
| let result = ""; | ||
| let offset = 0; | ||
| for (const token of findArgTokens(text, 0, includeBareSeparator)) { | ||
| result += styleOther(text.slice(offset, token.start)) + theme.fg(token.color, text.slice(token.start, token.end)); | ||
| offset = token.end; | ||
| } | ||
| return result + styleOther(text.slice(offset)); | ||
| } | ||
|
|
||
| /** Masks the slash command and @path/--flag tokens with same-width placeholders before markdown layout. */ | ||
| export class PromptTokenMask { | ||
| readonly text: string; | ||
| private graphemes: { segment: string; color: ThemeColor }[] = []; | ||
|
|
||
| constructor(source: string, commandEnd = 0, includeBareSeparator = false) { | ||
| // Markdown turns tabs into three spaces; a masked raw tab would be restored into a three-column layout. | ||
| source = source.replace(/\t/g, " "); | ||
| if (MASK_LITERAL_PATTERN.test(source)) { | ||
| this.text = source; | ||
| return; | ||
| } | ||
|
snimu marked this conversation as resolved.
|
||
| const tokens: ArgTokenSpan[] = []; | ||
| if (commandEnd > 0) { | ||
| tokens.push({ start: 0, end: commandEnd, color: "accent" }); | ||
| } | ||
| tokens.push(...findArgTokens(source, commandEnd, includeBareSeparator)); | ||
|
|
||
| let text = ""; | ||
| let cursor = 0; | ||
| for (const token of tokens) { | ||
| text += source.slice(cursor, token.start); | ||
| for (const { segment } of graphemeSegmenter.segment(source.slice(token.start, token.end))) { | ||
| const width = visibleWidth(segment); | ||
| if (width === 0) { | ||
| // Zero-width graphemes stay literal: invisible either way, and extracted text stays exact. | ||
| text += segment; | ||
| continue; | ||
| } | ||
| if (this.graphemes.length === MASK_CAPACITY) { | ||
| this.text = source; | ||
| this.graphemes = []; | ||
| return; | ||
| } | ||
| text += String.fromCharCode(MASK_BASE_START + this.graphemes.length) + MASK_EXTRA_WIDTH.repeat(width - 1); | ||
| this.graphemes.push({ segment, color: token.color }); | ||
| } | ||
| cursor = token.end; | ||
| } | ||
| this.text = text + source.slice(cursor); | ||
| } | ||
|
|
||
| private graphemeFor(placeholder: string): { segment: string; color: ThemeColor } | undefined { | ||
| return this.graphemes[placeholder.charCodeAt(0) - MASK_BASE_START]; | ||
| } | ||
|
|
||
| /** Restores masked graphemes in text extracted from a render, e.g. selection-region cell content. */ | ||
| restoreText(text: string): string { | ||
| return text.replace(MASK_PATTERN, (placeholder) => this.graphemeFor(placeholder)?.segment ?? placeholder); | ||
| } | ||
|
|
||
| restoreLine(line: string): string { | ||
| let result = ""; | ||
| let copied = 0; | ||
| let run: { start: number; end: number; color: ThemeColor; text: string } | undefined; | ||
| const flush = () => { | ||
| if (!run) return; | ||
| result += line.slice(copied, run.start) + theme.fg(run.color, run.text) + activeFgBefore(line, run.start); | ||
| copied = run.end; | ||
| run = undefined; | ||
| }; | ||
| for (const match of line.matchAll(MASK_PATTERN)) { | ||
|
macroscopeapp[bot] marked this conversation as resolved.
|
||
| const grapheme = this.graphemeFor(match[0]); | ||
| if (!grapheme) continue; // literal mask-range character from an unmasked source; leave it untouched | ||
| if (run && run.color === grapheme.color && run.end === match.index) { | ||
| run.text += grapheme.segment; | ||
| run.end += match[0].length; | ||
| } else { | ||
| flush(); | ||
| run = { | ||
| start: match.index, | ||
| end: match.index + match[0].length, | ||
| color: grapheme.color, | ||
| text: grapheme.segment, | ||
| }; | ||
| } | ||
| } | ||
| flush(); | ||
| return result + line.slice(copied); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| /** Styles tokens in laid-out editor lines from spans on the logical source lines; reset() before each render pass. */ | ||
| export class ArgTokenHighlighter { | ||
| private spans: ArgTokenSpan[][] = []; | ||
|
|
||
| reset(lines: readonly string[], includeBareSeparator = false): void { | ||
| this.spans = lines.map((line) => findArgTokens(line, 0, includeBareSeparator)); | ||
| } | ||
|
|
||
| /** displayText is chunkText with cursor escapes spliced in; chunkText starts at sourceStart within sourceLine. */ | ||
| highlightLine(displayText: string, chunkText: string, sourceLine: number, sourceStart: number): string { | ||
| const rangeEnd = sourceStart + chunkText.length; | ||
| const spans: ArgTokenSpan[] = []; | ||
| for (const span of this.spans[sourceLine] ?? []) { | ||
| if (span.end <= sourceStart) continue; | ||
| if (span.start >= rangeEnd) break; | ||
| spans.push({ | ||
| start: Math.max(span.start, sourceStart) - sourceStart, | ||
| end: Math.min(span.end, rangeEnd) - sourceStart, | ||
| color: span.color, | ||
| }); | ||
| } | ||
| if (spans.length === 0) return displayText; | ||
|
|
||
| // Maps visible code-unit offsets to displayText offsets, skipping the editor's cursor escapes. | ||
| const visibleStart: number[] = []; | ||
| let pos = 0; | ||
| for (const seq of displayText.matchAll(CURSOR_ESCAPE_PATTERN)) { | ||
| for (; pos < seq.index; pos++) visibleStart.push(pos); | ||
| pos = seq.index + seq[0].length; | ||
| } | ||
| for (; pos < displayText.length; pos++) visibleStart.push(pos); | ||
|
|
||
| let result = ""; | ||
| let copied = 0; | ||
| for (const span of spans) { | ||
| const start = visibleStart[span.start] ?? displayText.length; | ||
| const end = (visibleStart[span.end - 1] ?? displayText.length - 1) + 1; | ||
| // The cursor splice may carry a full reset mid-span; wrap each segment so the token color survives it. | ||
| const styled = displayText | ||
| .slice(start, end) | ||
| .split("\x1b[0m") | ||
| .map((segment) => theme.fg(span.color, segment)) | ||
| .join("\x1b[0m"); | ||
| result += displayText.slice(copied, start) + styled; | ||
| copied = end; | ||
| } | ||
| return result + displayText.slice(copied); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
macroscopeapp[bot] marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.