Skip to content

Commit 2fc069b

Browse files
committed
Refactor reconstruct_raw_value_from_node to correctly handle keyword nodes that contain trailing text, ensuring accurate raw value reconstruction when text is appended to keywords
1 parent 50ba5e1 commit 2fc069b

1 file changed

Lines changed: 48 additions & 18 deletions

File tree

‎packages/ui/src/components/editor/panel/PromptField/hooks/use-handlers.ts‎

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,38 @@ const reconstruct_raw_value_from_node = (node: Node): string => {
2727
const path = el.dataset.path
2828
if (!path) return ''
2929
const filename = path.split('/').pop() || path
30-
if (el.textContent === filename) {
31-
return `\`${path}\``
30+
if (el.textContent?.startsWith(filename)) {
31+
const extra = el.textContent.substring(filename.length)
32+
return `\`${path}\`${extra}`
3233
}
3334
break
3435
}
3536
case 'changes-keyword': {
3637
const branchName = el.dataset.branchName
3738
if (!branchName) return ''
38-
if (el.textContent === `Diff with ${branchName}`) {
39-
return `#Changes:${branchName}`
39+
const expected_text = `Diff with ${branchName}`
40+
if (el.textContent?.startsWith(expected_text)) {
41+
const extra = el.textContent.substring(expected_text.length)
42+
return `#Changes:${branchName}${extra}`
4043
}
4144
break
4245
}
4346
case 'saved-context-keyword': {
4447
const contextType = el.dataset.contextType
4548
const contextName = el.dataset.contextName
4649
if (!contextType || !contextName) return ''
47-
if (el.textContent === `Context "${contextName}"`) {
48-
return `#SavedContext:${contextType} "${contextName}"`
50+
const expected_text = `Context "${contextName}"`
51+
if (el.textContent?.startsWith(expected_text)) {
52+
const extra = el.textContent.substring(expected_text.length)
53+
return `#SavedContext:${contextType} "${contextName}"${extra}`
4954
}
5055
break
5156
}
5257
case 'selection-keyword': {
53-
if (el.textContent === 'Selection') {
54-
return '#Selection'
58+
const expected_text = 'Selection'
59+
if (el.textContent?.startsWith(expected_text)) {
60+
const extra = el.textContent.substring(expected_text.length)
61+
return `#Selection${extra}`
5562
}
5663
break
5764
}
@@ -63,8 +70,9 @@ const reconstruct_raw_value_from_node = (node: Node): string => {
6370
return ''
6471
}
6572
const short_hash = commit_hash.substring(0, 7)
66-
if (el.textContent === short_hash) {
67-
return `#Commit:${repo_name}:${commit_hash} "${commit_message}"`
73+
if (el.textContent?.startsWith(short_hash)) {
74+
const extra = el.textContent.substring(short_hash.length)
75+
return `#Commit:${repo_name}:${commit_hash} "${commit_message}"${extra}`
6876
}
6977
break
7078
}
@@ -541,19 +549,42 @@ export const use_handlers = (
541549
}
542550

543551
const range = selection.getRangeAt(0)
552+
const { startContainer, startOffset } = range
544553
let node_before_cursor: Node | null = null
545554

546555
if (
547-
range.startContainer.nodeType === Node.TEXT_NODE &&
548-
range.startOffset === 0
556+
startContainer.nodeType == Node.TEXT_NODE &&
557+
startOffset == 0
549558
) {
550-
node_before_cursor = range.startContainer.previousSibling
559+
node_before_cursor = startContainer.previousSibling
551560
} else if (
552-
range.startContainer.nodeType === Node.ELEMENT_NODE &&
553-
range.startOffset > 0
561+
startContainer.nodeType == Node.ELEMENT_NODE &&
562+
startOffset > 0
554563
) {
555-
node_before_cursor =
556-
range.startContainer.childNodes[range.startOffset - 1]
564+
node_before_cursor = startContainer.childNodes[startOffset - 1]
565+
} else if (
566+
startContainer.nodeType == Node.TEXT_NODE &&
567+
startOffset == startContainer.textContent?.length
568+
) {
569+
let parent = startContainer.parentElement
570+
while (parent && parent !== input_ref.current) {
571+
if (
572+
parent.dataset.type == 'file-keyword' ||
573+
parent.dataset.type == 'changes-keyword' ||
574+
parent.dataset.type == 'selection-keyword' ||
575+
parent.dataset.type == 'saved-context-keyword' ||
576+
parent.dataset.type == 'commit-keyword'
577+
) {
578+
const rangeAfter = document.createRange()
579+
rangeAfter.selectNodeContents(parent)
580+
rangeAfter.setStart(startContainer, startOffset)
581+
if (rangeAfter.toString().trim() == '') {
582+
node_before_cursor = parent
583+
break
584+
}
585+
}
586+
parent = parent.parentElement
587+
}
557588
}
558589

559590
if (
@@ -691,4 +722,3 @@ export const use_handlers = (
691722
handle_input_click
692723
}
693724
}
694-

0 commit comments

Comments
 (0)