PR feedback

This commit is contained in:
Matt Rubens 2025-02-25 15:53:30 -05:00
parent 77a6e2be6d
commit 3592539fe5
3 changed files with 86 additions and 13 deletions

View file

@ -16,6 +16,7 @@ import { vscode } from "../../utils/vscode"
import { WebviewMessage } from "../../../../src/shared/WebviewMessage"
import { Mode, getAllModes } from "../../../../src/shared/modes"
import { CaretIcon } from "../common/CaretIcon"
import { convertToMentionPath } from "../../utils/path-mentions"
interface ChatTextAreaProps {
inputValue: string
@ -589,19 +590,8 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
const files = Array.from(e.dataTransfer.files)
const text = e.dataTransfer.getData("text")
if (text) {
let mentionText = text
const normalizedText = text.replace(/\\/g, "/")
const normalizedCwd = cwd ? cwd.replace(/\\/g, "/") : ""
// Always use case-insensitive comparison for path matching
if (normalizedCwd) {
const lowerText = normalizedText.toLowerCase()
const lowerCwd = normalizedCwd.toLowerCase()
if (lowerText.startsWith(lowerCwd)) {
mentionText = "@" + normalizedText.substring(normalizedCwd.length)
}
}
// Convert the path to a mention-friendly format
const mentionText = convertToMentionPath(text, cwd)
const newValue =
inputValue.slice(0, cursorPosition) + mentionText + " " + inputValue.slice(cursorPosition)

View file

@ -0,0 +1,45 @@
import { convertToMentionPath } from "../path-mentions"
describe("path-mentions", () => {
describe("convertToMentionPath", () => {
it("should convert an absolute path to a mention path when it starts with cwd", () => {
// Windows-style paths
expect(convertToMentionPath("C:\\Users\\user\\project\\file.txt", "C:\\Users\\user\\project")).toBe(
"@/file.txt",
)
// Unix-style paths
expect(convertToMentionPath("/Users/user/project/file.txt", "/Users/user/project")).toBe("@/file.txt")
})
it("should handle paths with trailing slashes in cwd", () => {
expect(convertToMentionPath("/Users/user/project/file.txt", "/Users/user/project/")).toBe("@/file.txt")
})
it("should be case-insensitive when matching paths", () => {
expect(convertToMentionPath("/Users/User/Project/file.txt", "/users/user/project")).toBe("@/file.txt")
})
it("should return the original path when cwd is not provided", () => {
expect(convertToMentionPath("/Users/user/project/file.txt")).toBe("/Users/user/project/file.txt")
})
it("should return the original path when it does not start with cwd", () => {
expect(convertToMentionPath("/Users/other/project/file.txt", "/Users/user/project")).toBe(
"/Users/other/project/file.txt",
)
})
it("should normalize backslashes to forward slashes", () => {
expect(convertToMentionPath("C:\\Users\\user\\project\\subdir\\file.txt", "C:\\Users\\user\\project")).toBe(
"@/subdir/file.txt",
)
})
it("should handle nested paths correctly", () => {
expect(convertToMentionPath("/Users/user/project/nested/deeply/file.txt", "/Users/user/project")).toBe(
"@/nested/deeply/file.txt",
)
})
})
})

View file

@ -0,0 +1,38 @@
/**
* Utilities for handling path-related operations in mentions
*/
/**
* Converts an absolute path to a mention-friendly path
* If the provided path starts with the current working directory,
* it's converted to a relative path prefixed with @
*
* @param path The path to convert
* @param cwd The current working directory
* @returns A mention-friendly path
*/
export function convertToMentionPath(path: string, cwd?: string): string {
const normalizedPath = path.replace(/\\/g, "/")
let normalizedCwd = cwd ? cwd.replace(/\\/g, "/") : ""
if (!normalizedCwd) {
return path
}
// Remove trailing slash from cwd if it exists
if (normalizedCwd.endsWith("/")) {
normalizedCwd = normalizedCwd.slice(0, -1)
}
// Always use case-insensitive comparison for path matching
const lowerPath = normalizedPath.toLowerCase()
const lowerCwd = normalizedCwd.toLowerCase()
if (lowerPath.startsWith(lowerCwd)) {
const relativePath = normalizedPath.substring(normalizedCwd.length)
// Ensure there's a slash after the @ symbol when we create the mention path
return "@" + (relativePath.startsWith("/") ? relativePath : "/" + relativePath)
}
return path
}