Skip to content

Commit 910e9ba

Browse files
committed
bug: space in folder and file name
1 parent 9c1869a commit 910e9ba

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

src/core/mentions/__tests__/index.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,17 @@ Detailed commit message with multiple lines
155155
jest.spyOn(fs, "readFile").mockResolvedValue(fileContent)
156156
jest.spyOn(fs, "stat").mockResolvedValue({ isFile: () => true, isDirectory: () => false } as any)
157157

158+
// Test with a file path containing spaces
158159
const filePath = "/path/with spaces/my file.txt"
160+
161+
// First, verify that the regex pattern correctly matches the entire path
162+
// Import the regex pattern directly to test it
163+
const { mentionRegexGlobal } = require("../../../shared/context-mentions")
164+
const mentionMatch = `@${filePath}`.match(mentionRegexGlobal)
165+
expect(mentionMatch).not.toBeNull()
166+
expect(mentionMatch![0]).toBe(`@${filePath}`)
167+
168+
// Now test the full parseMentions function
159169
const result = await parseMentions(`Check out this file @${filePath}`, mockCwd, mockUrlContentFetcher)
160170

161171
// Verify the file path with spaces was correctly parsed

src/shared/context-mentions.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Mention regex:
99
- `/@`:
1010
- **@**: The mention must start with the '@' symbol.
1111
12-
- `((?:\/|\w+:\/\/)(?:[^\s]|\s(?=[^\s]))+?|problems\b|git-changes\b)`:
12+
- `((?:\/|\w+:\/\/)[^\r\n]*?(?=\s*$|\s+@|[.,;:!?](?=[\s\r\n]|$))|problems\b|git-changes\b)`:
1313
- **Capturing Group (`(...)`)**: Captures the part of the string that matches one of the specified patterns.
1414
- `(?:\/|\w+:\/\/)`:
1515
- **Non-Capturing Group (`(?:...)`)**: Groups the alternatives without capturing them for back-referencing.
@@ -18,10 +18,11 @@ Mention regex:
1818
- `|`: Logical OR.
1919
- `\w+:\/\/`:
2020
- **Protocol (`\w+://`)**: Matches URLs that start with a word character sequence followed by '://', such as 'http://', 'https://', 'ftp://', etc.
21-
- `(?:[^\s]|\s(?=[^\s]))+?`:
22-
- **Character Pattern**: Matches either a non-whitespace character OR a whitespace character that is followed by a non-whitespace character.
23-
- **This allows spaces within file paths while preventing trailing spaces**.
24-
- **Non-Greedy (`+?`)**: Ensures the smallest possible match, preventing the inclusion of trailing punctuation.
21+
- `[^\r\n]*?(?=\s*$|\s+@|[.,;:!?](?=[\s\r\n]|$))`:
22+
- **Character Pattern**: Matches any characters except line breaks.
23+
- **Followed by a lookahead**: Ensures the match ends at the end of the line, before another @ symbol, or before punctuation followed by whitespace or end of line.
24+
- **This allows spaces within file paths while properly handling path boundaries**.
25+
- **Non-Greedy (`*?`)**: Ensures the smallest possible match.
2526
- `|`: Logical OR.
2627
- `problems\b`:
2728
- **Exact Word ('problems')**: Matches the exact word 'problems'.
@@ -40,6 +41,7 @@ Mention regex:
4041
- **Summary**:
4142
- The regex effectively matches:
4243
- Mentions that are file or folder paths starting with '/' and can contain spaces within the path (e.g., 'my folder/my file.txt').
44+
The regex now properly handles paths with multiple spaces and ensures the entire path is captured.
4345
- URLs that start with a protocol (like 'http://') followed by any non-whitespace characters (including query parameters).
4446
- The exact word 'problems'.
4547
- The exact word 'git-changes'.
@@ -51,7 +53,7 @@ Mention regex:
5153
5254
*/
5355
export const mentionRegex =
54-
/@((?:\/|\w+:\/\/)(?:[^\s]|\s(?=[^\s]))+?|[a-f0-9]{7,40}\b|problems\b|git-changes\b|terminal\b)(?=[.,;:!?]?(?=[\s\r\n]|$))/
56+
/@((?:\/|\w+:\/\/)[^\r\n]*?(?=\s*$|\s+@|[.,;:!?](?=[\s\r\n]|$))|[a-f0-9]{7,40}\b|problems\b|git-changes\b|terminal\b)(?=[.,;:!?]?(?=[\s\r\n]|$))/
5557
export const mentionRegexGlobal = new RegExp(mentionRegex.source, "g")
5658

5759
export interface MentionSuggestion {

webview-ui/src/utils/context-mentions.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,9 @@ export function shouldShowContextMenu(text: string, position: number): boolean {
280280

281281
const textAfterAt = beforeCursor.slice(atIndex + 1)
282282

283-
// Check if there's any whitespace after the '@'
284-
if (/\s/.test(textAfterAt)) return false
283+
// Check if there's any whitespace immediately after the '@'
284+
// This only checks the first character after @ to allow for paths with spaces
285+
if (textAfterAt.startsWith(" ")) return false
285286

286287
// Don't show the menu if it's clearly a URL
287288
if (textAfterAt.toLowerCase().startsWith("http")) {

0 commit comments

Comments
 (0)