From f80b121398fc063c11b5963f0f8c51284cc66b3f Mon Sep 17 00:00:00 2001 From: Kellen Date: Sun, 8 Sep 2024 12:27:02 +0300 Subject: [PATCH] escaped brackets andcomma double avoidance --- web/js/common/autocomplete.js | 40 +++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/web/js/common/autocomplete.js b/web/js/common/autocomplete.js index d1c17fe..d4c461a 100644 --- a/web/js/common/autocomplete.js +++ b/web/js/common/autocomplete.js @@ -611,24 +611,28 @@ export class TextAreaAutoComplete { const item = $el( "div.pysssss-autocomplete-item", { - onclick: () => { - this.el.focus(); - let value = wordInfo.value ?? wordInfo.text; - const use_replacer = wordInfo.use_replacer ?? true; - if (TextAreaAutoComplete.replacer && use_replacer) { - value = TextAreaAutoComplete.replacer(value); - } - this.helper.insertAtCursor(value + this.separator, -before.length, wordInfo.caretOffset); - setTimeout(() => { - this.#update(); - }, 150); - }, - onmousemove: () => { - this.#setSelected(wordInfo); - }, + onclick: () => { + this.el.focus(); + let value = wordInfo.value ?? wordInfo.text; + const use_replacer = wordInfo.use_replacer ?? true; + if (TextAreaAutoComplete.replacer && use_replacer) { + value = TextAreaAutoComplete.replacer(value); + } + value = this.#escapeParentheses(value); + const afterCursor = this.helper.getAfterCursor(); + const shouldAddSeparator = !afterCursor.trim().startsWith(this.separator.trim()); + this.helper.insertAtCursor( + value + (shouldAddSeparator ? this.separator : ''), + -before.length, + wordInfo.caretOffset + ); + setTimeout(() => { + this.#update(); + }, 150); + }, }, parts - ); + ); if (wordInfo === this.selected) { hasSelected = true; @@ -653,6 +657,10 @@ export class TextAreaAutoComplete { this.dropdown.style.maxHeight = (window.innerHeight - position.top) + "px"; } + #escapeParentheses(text) { + return text.replace(/\(/g, '\\(').replace(/\)/g, '\\)'); + } + #hide() { this.selected = null; this.dropdown.remove();