Skip to content

Support mentioning filenames with spaces #3044

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

Merged
merged 2 commits into from
Apr 30, 2025
Merged

Conversation

jr
Copy link
Collaborator

@jr jr commented Apr 29, 2025

Allows spaces to be escaped in mentions with \\. Does not attempt more comprehensive escape handling. (IMO a more structured representation or moving to something like @[something complex] is probably a better long term approach, but the scope of that seems daunting.

Adapted from 8955d45 / #2363.

Context

Addresses #2361

Screenshots

before

spaces-in-mentions-not-working.mov

after

spaces-in-mentions-escaped.mov

Important

Adds support for file path mentions with spaces by allowing spaces to be escaped with backslashes, updating regex patterns, and enhancing path handling functions.

  • Behavior:
    • Supports file path mentions with spaces by allowing spaces to be escaped with \\ in parseMentions() and openMention() in index.ts.
    • Updates mentionRegex in context-mentions.ts to match paths with escaped spaces.
    • insertMention() in context-mentions.ts now escapes spaces in file paths.
  • Tests:
    • Adds tests for escaped spaces in index.test.ts, context-mentions.test.ts, and path-mentions.test.ts.
    • Verifies correct handling of escaped spaces in mentions and context menu options.
  • Utilities:
    • Adds escapeSpaces() to path-mentions.ts to escape spaces in paths.
    • Updates convertToMentionPath() to handle paths with spaces.

This description was created by Ellipsis for 07bb1f1. You can customize this summary. It will automatically update as commits are pushed.

Allows spaces to be escaped in mentions with \. Does not attempt more
comprehensive escape handling. (IMO a more structured representation or
moving to something like @[something complex] is probably a better long
term approach, but the scope of that seems problematic.

Adapted from 8955d45.

Co-authored-by: Matt Rubens <mrubens@users.noreply.github.com>
Copy link

changeset-bot bot commented Apr 29, 2025

⚠️ No Changeset found

Latest commit: 07bb1f1

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. enhancement New feature or request labels Apr 29, 2025
* @returns A path with spaces escaped
*/
export function escapeSpaces(path: string): string {
return path.replace(/ /g, "\\ ")

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This does not escape backslash characters in the input.

Copilot Autofix

AI 4 days ago

To fix the issue, we need to ensure that backslashes in the input are escaped before escaping spaces. This can be achieved by first replacing all backslashes (\) with double backslashes (\\) and then replacing spaces with \ . This ensures that any existing backslashes are properly escaped, avoiding ambiguity or incorrect output.

The fix involves modifying the escapeSpaces function to handle backslashes in addition to spaces. Specifically:

  1. Replace all backslashes (\) with double backslashes (\\) using a regular expression with the global flag.
  2. Replace all spaces with \ as before.

No additional imports or dependencies are required for this fix.


Suggested changeset 1
webview-ui/src/utils/path-mentions.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/webview-ui/src/utils/path-mentions.ts b/webview-ui/src/utils/path-mentions.ts
--- a/webview-ui/src/utils/path-mentions.ts
+++ b/webview-ui/src/utils/path-mentions.ts
@@ -11,3 +11,4 @@
 export function escapeSpaces(path: string): string {
-	return path.replace(/ /g, "\\ ")
+	// Escape backslashes first, then spaces
+	return path.replace(/\\/g, "\\\\").replace(/ /g, "\\ ");
 }
EOF
@@ -11,3 +11,4 @@
export function escapeSpaces(path: string): string {
return path.replace(/ /g, "\\ ")
// Escape backslashes first, then spaces
return path.replace(/\\/g, "\\\\").replace(/ /g, "\\ ");
}
Copilot is powered by AI and may make mistakes. Always verify output.
// Check if there's any unescaped whitespace after the '@'
// We need to check for whitespace that isn't preceded by a backslash
// Using a negative lookbehind to ensure the space isn't escaped
const hasUnescapedSpace = /(?<!\\)\s/.test(textAfterAt)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The negative lookbehind regex /(?<!\)\s/ in shouldShowContextMenu might not be supported in older JS engines.

Suggested change
const hasUnescapedSpace = /(?<!\\)\s/.test(textAfterAt)
const hasUnescapedSpace = /(^|[^\\])\s/.test(textAfterAt)

@elianiva
Copy link

IMO a more structured representation or moving to something like @[something complex] is probably a better long term approach, but the scope of that seems daunting

fwiw i started working on this in #2881, and yes the scope of it is quite big, haven't got the time to continue but will do it over the weekends
we can go with this solution for now, that PR will take quite some time before being actually usable

Copy link
Collaborator

@cte cte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the CodeQL error, but otherwise looks good, especially the test to non-test code ratio :)

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Apr 29, 2025
Copy link
Collaborator

@mrubens mrubens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@mrubens mrubens merged commit 34d7dcf into main Apr 30, 2025
13 of 14 checks passed
@mrubens mrubens deleted the jr/spaces-in-mentions branch April 30, 2025 02:27
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Apr 30, 2025
@samhvw8
Copy link
Collaborator

samhvw8 commented Apr 30, 2025

@cte last time i change regex to string and using replaceAll codeql make that pass

'''
String.replaceAll(' ', '\ ')
'''

Something like this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request lgtm This PR has been approved by a maintainer size:XL This PR changes 500-999 lines, ignoring generated files.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants