Changesets · Branches · Tags · Compare
| Changeset | 71db21646609335b575e9e2afc443d21308a401821090fd4e51fa143d12b3061 |
|---|---|
| Author | Saim <saimadib2002@gmail.com> · 2025-05-21 16:41 +05:30 |
| Committer | Saim <saimadib2002@gmail.com> · 2025-05-21 16:41 +05:30 |
| Parents | 097e009e2978 |
3 files changed
modified gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/RunInTerminalButton.tsx +1 −36
| @@ -3,6 +3,7 @@ | ||
| 3 | 3 | import { lightGray, vscForeground } from "../.."; |
| 4 | 4 | import { IdeMessengerContext } from "../../../context/IdeMessenger"; |
| 5 | 5 | import { isJetBrains } from "../../../util"; |
| 6 | +import { extractCommand } from "../utils/commandExtractor"; | |
| 6 | 7 | |
| 7 | 8 | interface RunInTerminalButtonProps { |
| 8 | 9 | command: string; |
| @@ -16,42 +17,6 @@ | ||
| 16 | 17 | return null; |
| 17 | 18 | } |
| 18 | 19 | |
| 19 | - // Extract just the command line (the line after $ or the first line) | |
| 20 | - function extractCommand(cmd: string): string { | |
| 21 | - // First handle the $ prompt case, extract the line after it | |
| 22 | - if (cmd.includes("$")) { | |
| 23 | - const match = cmd.match(/\$\s*([^\n]+)/); | |
| 24 | - if (match) { | |
| 25 | - return match[1].trim(); | |
| 26 | - } | |
| 27 | - } | |
| 28 | - | |
| 29 | - // Process all lines, filtering out comments and empty lines | |
| 30 | - const lines = cmd.split("\n") | |
| 31 | - .map(line => line.trim()) | |
| 32 | - .filter(line => | |
| 33 | - line && | |
| 34 | - !line.startsWith("#") && | |
| 35 | - !line.startsWith("//") && | |
| 36 | - !line.startsWith("/*") | |
| 37 | - ); | |
| 38 | - | |
| 39 | - // Handle multi-line commands | |
| 40 | - let result = ""; | |
| 41 | - for (let i = 0; i < lines.length; i++) { | |
| 42 | - const line = lines[i]; | |
| 43 | - result += line; | |
| 44 | - | |
| 45 | - // Add space for command continuation | |
| 46 | - if (line.endsWith("&&") || line.endsWith("|") || line.endsWith("\\")) { | |
| 47 | - result += " "; | |
| 48 | - } else if (i < lines.length - 1) { | |
| 49 | - result += " "; | |
| 50 | - } | |
| 51 | - } | |
| 52 | - return result.trim(); | |
| 53 | - } | |
| 54 | - | |
| 55 | 20 | function runInTerminal() { |
| 56 | 21 | // Extract just the command line |
| 57 | 22 | 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 |