Skip to content

Commit 3592539

Browse files
committed
PR feedback
1 parent 77a6e2b commit 3592539

File tree

3 files changed

+86
-13
lines changed

3 files changed

+86
-13
lines changed

webview-ui/src/components/chat/ChatTextArea.tsx

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { vscode } from "../../utils/vscode"
1616
import { WebviewMessage } from "../../../../src/shared/WebviewMessage"
1717
import { Mode, getAllModes } from "../../../../src/shared/modes"
1818
import { CaretIcon } from "../common/CaretIcon"
19+
import { convertToMentionPath } from "../../utils/path-mentions"
1920

2021
interface ChatTextAreaProps {
2122
inputValue: string
@@ -589,19 +590,8 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
589590
const files = Array.from(e.dataTransfer.files)
590591
const text = e.dataTransfer.getData("text")
591592
if (text) {
592-
let mentionText = text
593-
const normalizedText = text.replace(/\\/g, "/")
594-
const normalizedCwd = cwd ? cwd.replace(/\\/g, "/") : ""
595-
596-
// Always use case-insensitive comparison for path matching
597-
if (normalizedCwd) {
598-
const lowerText = normalizedText.toLowerCase()
599-
const lowerCwd = normalizedCwd.toLowerCase()
600-
601-
if (lowerText.startsWith(lowerCwd)) {
602-
mentionText = "@" + normalizedText.substring(normalizedCwd.length)
603-
}
604-
}
593+
// Convert the path to a mention-friendly format
594+
const mentionText = convertToMentionPath(text, cwd)
605595

606596
const newValue =
607597
inputValue.slice(0, cursorPosition) + mentionText + " " + inputValue.slice(cursorPosition)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { convertToMentionPath } from "../path-mentions"
2+
3+
describe("path-mentions", () => {
4+
describe("convertToMentionPath", () => {
5+
it("should convert an absolute path to a mention path when it starts with cwd", () => {
6+
// Windows-style paths
7+
expect(convertToMentionPath("C:\\Users\\user\\project\\file.txt", "C:\\Users\\user\\project")).toBe(
8+
"@/file.txt",
9+
)
10+
11+
// Unix-style paths
12+
expect(convertToMentionPath("/Users/user/project/file.txt", "/Users/user/project")).toBe("@/file.txt")
13+
})
14+
15+
it("should handle paths with trailing slashes in cwd", () => {
16+
expect(convertToMentionPath("/Users/user/project/file.txt", "/Users/user/project/")).toBe("@/file.txt")
17+
})
18+
19+
it("should be case-insensitive when matching paths", () => {
20+
expect(convertToMentionPath("/Users/User/Project/file.txt", "/users/user/project")).toBe("@/file.txt")
21+
})
22+
23+
it("should return the original path when cwd is not provided", () => {
24+
expect(convertToMentionPath("/Users/user/project/file.txt")).toBe("/Users/user/project/file.txt")
25+
})
26+
27+
it("should return the original path when it does not start with cwd", () => {
28+
expect(convertToMentionPath("/Users/other/project/file.txt", "/Users/user/project")).toBe(
29+
"/Users/other/project/file.txt",
30+
)
31+
})
32+
33+
it("should normalize backslashes to forward slashes", () => {
34+
expect(convertToMentionPath("C:\\Users\\user\\project\\subdir\\file.txt", "C:\\Users\\user\\project")).toBe(
35+
"@/subdir/file.txt",
36+
)
37+
})
38+
39+
it("should handle nested paths correctly", () => {
40+
expect(convertToMentionPath("/Users/user/project/nested/deeply/file.txt", "/Users/user/project")).toBe(
41+
"@/nested/deeply/file.txt",
42+
)
43+
})
44+
})
45+
})
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Utilities for handling path-related operations in mentions
3+
*/
4+
5+
/**
6+
* Converts an absolute path to a mention-friendly path
7+
* If the provided path starts with the current working directory,
8+
* it's converted to a relative path prefixed with @
9+
*
10+
* @param path The path to convert
11+
* @param cwd The current working directory
12+
* @returns A mention-friendly path
13+
*/
14+
export function convertToMentionPath(path: string, cwd?: string): string {
15+
const normalizedPath = path.replace(/\\/g, "/")
16+
let normalizedCwd = cwd ? cwd.replace(/\\/g, "/") : ""
17+
18+
if (!normalizedCwd) {
19+
return path
20+
}
21+
22+
// Remove trailing slash from cwd if it exists
23+
if (normalizedCwd.endsWith("/")) {
24+
normalizedCwd = normalizedCwd.slice(0, -1)
25+
}
26+
27+
// Always use case-insensitive comparison for path matching
28+
const lowerPath = normalizedPath.toLowerCase()
29+
const lowerCwd = normalizedCwd.toLowerCase()
30+
31+
if (lowerPath.startsWith(lowerCwd)) {
32+
const relativePath = normalizedPath.substring(normalizedCwd.length)
33+
// Ensure there's a slash after the @ symbol when we create the mention path
34+
return "@" + (relativePath.startsWith("/") ? relativePath : "/" + relativePath)
35+
}
36+
37+
return path
38+
}

0 commit comments

Comments
 (0)