Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(command): 改进代码编辑内容提取的引号处理#416

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
phodal merged 1 commit intounit-mesh:masterfromlevicode:dev/mcp
Jun 16, 2025
Merged
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(command): 改进代码编辑内容提取的引号处理
重构 EditFileCommand 中的代码编辑内容解析逻辑,新增 extractCodeEditContent 方法来正确处理嵌套引号和转义字符,替换原有的简单正则表达式匹配方式。
  • Loading branch information
@levicode
levicode committedJun 16, 2025
commitb2e994ea26029b283024e71b3e0083d35affa77a
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -140,18 +140,13 @@ class EditFileCommand(private val project: Project) {
val instructionsRegex =
"""instructions["\s]*[:=]["\s]*["']([^"']*?)["']""".toRegex(RegexOption.DOT_MATCHES_ALL)

val codeEditPattern = """code_edit["\s]*[:=]["\s]*["'](.*?)["']""".toRegex(RegexOption.DOT_MATCHES_ALL)

val targetFileMatch = targetFileRegex.find(content)
val instructionsMatch = instructionsRegex.find(content)
val codeEditMatch = codeEditPattern.find(content)

if (targetFileMatch != null && codeEditMatch != null) {
val codeEditContent = codeEditMatch.groupValues[1]
.replace("\\n", "\n") // Handle escaped newlines
.replace("\\\"", "\"") // Handle escaped quotes
.replace("\\'", "'") // Handle escaped single quotes
// Extract code_edit content more carefully to handle nested quotes
val codeEditContent = extractCodeEditContent(content)

if (targetFileMatch != null && codeEditContent != null) {
EditRequest(
targetFile = targetFileMatch.groupValues[1],
instructions = instructionsMatch?.groupValues?.get(1) ?: "",
Expand All@@ -164,6 +159,43 @@ class EditFileCommand(private val project: Project) {
null
}
}

private fun extractCodeEditContent(content: String): String? {
// Look for code_edit field
val codeEditStart = """code_edit["\s]*[:=]["\s]*["']""".toRegex().find(content) ?: return null
val startIndex = codeEditStart.range.last + 1

if (startIndex >= content.length) return null

// Determine the quote type used to open the string
val openingQuote = content[startIndex - 1]

// Find the matching closing quote, handling escaped quotes
var index = startIndex
var escapeNext = false

while (index < content.length) {
val char = content[index]

if (escapeNext) {
escapeNext = false
} else if (char == '\\') {
escapeNext = true
} else if (char == openingQuote) {
// Found the closing quote
val extractedContent = content.substring(startIndex, index)
return extractedContent
.replace("\\n", "\n") // Handle escaped newlines
.replace("\\\"", "\"") // Handle escaped quotes
.replace("\\'", "'") // Handle escaped single quotes
.replace("\\\\", "\\") // Handle escaped backslashes
}

index++
}

return null
}
}

@Serializable
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp