Elffuss BraidHub

kikocis/continue public

Merge pull request #5744 from saimadib/saim/improve-command-extraction

Fix: improve command extraction in RunInTerminalButton (#5724)
Changesetc394cc5a9fd9a18e8e78a597d75b3ec10fefae41f26e12a94c7cb7e24a33d394
AuthorDallin Romney <dallinromney@gmail.com> · 2025-05-21 09:52 -07:00
CommitterGitHub <noreply@github.com> · 2025-05-21 09:52 -07:00
Parentse84747fb3c59, 71db21646609

Browse the files at this changeset

3 files changed

modified gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/RunInTerminalButton.tsx +1 −12
@@ -3,6 +3,7 @@
33 import { lightGray, vscForeground } from "../..";
44 import { IdeMessengerContext } from "../../../context/IdeMessenger";
55 import { isJetBrains } from "../../../util";
6+import { extractCommand } from "../utils/commandExtractor";
67
78 interface RunInTerminalButtonProps {
89 command: string;
@@ -16,18 +17,6 @@
1617 return null;
1718 }
1819
19- // Extract just the command line (the line after $ or the first line)
20- function extractCommand(cmd: string): string {
21- // If the command contains a $ prompt, extract the line after it
22- if (cmd.includes("$")) {
23- const match = cmd.match(/\$\s*([^\n]+)/);
24- return match ? match[1].trim() : "";
25- }
26-
27- // Otherwise, just take the first line
28- return cmd.split("\n")[0].trim();
29- }
30-
3120 function runInTerminal() {
3221 // Extract just the command line
3322 const extractedCommand = extractCommand(command);
added gui/src/components/StyledMarkdownPreview/utils/commandExtractor.test.ts +80 −0
@@ -0,0 +1,80 @@
1+import { extractCommand } from './commandExtractor';
2+
3+describe('extractCommand', () => {
4+ // Test basic command extraction
5+ it('should extract command from $ prompt', () => {
6+ expect(extractCommand('$ echo hello')).toBe('echo hello');
7+ expect(extractCommand('$ echo hello')).toBe('echo hello');
8+ expect(extractCommand('$echo hello')).toBe('echo hello');
9+ });
10+
11+ // Test comment removal
12+ it('should remove comments at beginning of lines', () => {
13+ expect(extractCommand('# shell comment\necho hello')).toBe('echo hello');
14+ expect(extractCommand('// js comment\necho hello')).toBe('echo hello');
15+ expect(extractCommand('/* multi-line\ncomment */\necho hello')).toBe('echo hello');
16+ });
17+
18+ // Test multiline comment removal
19+ it('should remove multiline comments anywhere in the text', () => {
20+ expect(extractCommand('echo /* comment */ hello')).toBe('echo hello');
21+ expect(extractCommand('echo hello /* comment */')).toBe('echo hello');
22+ expect(extractCommand('echo /* multiple\nline\ncomment */ hello')).toBe('echo hello');
23+ });
24+
25+ // Test line continuation handling
26+ it('should handle line continuations correctly', () => {
27+ expect(extractCommand('echo hello && \\\necho world')).toBe('echo hello && echo world');
28+ expect(extractCommand('echo hello | \\\ngrep h')).toBe('echo hello | grep h');
29+ expect(extractCommand('echo hello \\\nworld')).toBe('echo hello world');
30+ });
31+
32+ // Test complex command combinations
33+ it('should handle complex command combinations', () => {
34+ expect(extractCommand(`
35+ # shell comment
36+ echo hello /* inline comment */ && \\
37+ // js comment
38+ echo world
39+ `)).toBe('echo hello && echo world');
40+ });
41+
42+ // Test empty and whitespace handling
43+ it('should handle empty and whitespace inputs', () => {
44+ expect(extractCommand('')).toBe('');
45+ expect(extractCommand(' \n \t ')).toBe('');
46+ });
47+
48+ // Test comment-only inputs
49+ it('should handle comment-only inputs', () => {
50+ expect(extractCommand('/* comment */')).toBe('');
51+ expect(extractCommand('# comment')).toBe('');
52+ expect(extractCommand('// comment')).toBe('');
53+ expect(extractCommand('/* multi\nline\ncomment */')).toBe('');
54+ });
55+
56+ // Test inline comments
57+ it('should handle inline comments correctly according to implementation', () => {
58+ // But removes multiline comments even when inline
59+ expect(extractCommand('/* inline */ echo hello world')).toBe('echo hello world');
60+ });
61+
62+ // Test multiple line continuations
63+ it('should handle multiple line continuations', () => {
64+ expect(extractCommand(`
65+ echo hello && \\
66+ echo world && \\
67+ echo test
68+ `)).toBe('echo hello && echo world && echo test');
69+ });
70+
71+ // Test nested line continuations
72+ it('should handle nested line continuations', () => {
73+ expect(extractCommand(`
74+ echo hello && \\
75+ (echo nested && \\
76+ echo more) && \\
77+ echo final
78+ `)).toBe('echo hello && (echo nested && echo more) && echo final');
79+ });
80+});
No newline at the end of the file
added gui/src/components/StyledMarkdownPreview/utils/commandExtractor.ts +50 −0
@@ -0,0 +1,50 @@
1+/**
2+ * Extracts and processes command strings, handling comments and line continuations.
3+ * @param cmd The command string to process
4+ * @returns The processed command string
5+ */
6+export function extractCommand(cmd: string): string {
7+ if (!cmd || cmd.trim() === '') {
8+ return '';
9+ }
10+
11+ // Remove multi-line comments (/* ... */) and replace with a single space
12+ cmd = cmd.replace(/\/\*[\s\S]*?\*\//g, " ");
13+
14+ // Handle the $ prompt case first
15+ if (cmd.includes("$")) {
16+ const match = cmd.match(/\$\s*([^\n]+)/);
17+ if (match) {
18+ // Return just the command after $ without further processing
19+ return match[1].trim();
20+ }
21+ }
22+
23+ // Process lines - remove single-line comments only at beginning of lines
24+ const lines = cmd.split('\n')
25+ .map(line => line.replace(/^\s*#.*$/, '').replace(/^\s*\/\/.*$/, ''))
26+ .map(line => line.trim())
27+ .filter(line => line);
28+
29+ // Join lines with proper handling of continuations
30+ let result = '';
31+ for (let i = 0; i < lines.length; i++) {
32+ let line = lines[i];
33+
34+ // Remove trailing continuation characters and join with next line
35+ while (
36+ (line.endsWith('&&') || line.endsWith('|') || line.endsWith('\\')) &&
37+ i < lines.length - 1
38+ ) {
39+ // Remove the continuation character(s) and any trailing spaces
40+ line = line.replace(/(&&|\||\\)\s*$/, '').trim();
41+ // Join with the next line
42+ i++;
43+ line += ' ' + lines[i];
44+ }
45+
46+ result += line + ' ';
47+ }
48+
49+ return result.trim().replace(/\s+/g, ' ');
50+}
No newline at the end of the file