From 02bc60220150bb825526f4249a0b00c84702ea0b Mon Sep 17 00:00:00 2001 From: Sekedus Date: Sat, 16 Nov 2024 09:46:24 +0800 Subject: [PATCH] fix: replace line breaks replace: - `\n` with `{empty}` - `\n{2}` with `\n` - `\n+` with `\n` --- src/ext/searchbox.js | 15 +++++++++++---- src/search.js | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/ext/searchbox.js b/src/ext/searchbox.js index 259832c23f..d3530012f4 100644 --- a/src/ext/searchbox.js +++ b/src/ext/searchbox.js @@ -214,7 +214,7 @@ class SearchBox { ? editor.session.getTextRange(this.searchRange) : editor.getValue(); - if (editor.$search.$isMultilineSearch(editor.$search.$options)) + if (editor.$search.$isMultilineSearch(editor.getLastSearchOptions())) value = value.replace(/\r\n|\r|\n/g, "\n"); var offset = editor.session.doc.positionToIndex(editor.selection.anchor); @@ -260,17 +260,24 @@ class SearchBox { } replace() { if (!this.editor.getReadOnly()) - this.editor.replace(this.replaceInput.value); + this.editor.replace(this.replaceMultiline(this.replaceInput.value)); } replaceAndFindNext() { if (!this.editor.getReadOnly()) { - this.editor.replace(this.replaceInput.value); + this.editor.replace(this.replaceMultiline(this.replaceInput.value)); this.findNext(); } } replaceAll() { if (!this.editor.getReadOnly()) - this.editor.replaceAll(this.replaceInput.value); + this.editor.replaceAll(this.replaceMultiline(this.replaceInput.value)); + } + replaceMultiline(text) { + if (this.editor.$search.$isMultilineSearch(this.editor.getLastSearchOptions())) { + return text.replace(/\\n/g, '\n'); + } else { + return text; + } } hide() { diff --git a/src/search.js b/src/search.js index 9214a96eae..3d7b8e92b2 100644 --- a/src/search.js +++ b/src/search.js @@ -176,7 +176,7 @@ class Search { input = input.replace(/\r\n|\r|\n/g, "\n"); var match = re.exec(input); - if (!match || match[0].length != input.length) + if (!match || (!this.$isMultilineSearch(options) && match[0].length != input.length)) return null; if (!options.regExp) { replacement = replacement.replace(/\$/g, "$$$$");