diff --git a/src/plugins/unified_search/public/query_string_input/text_based_languages_editor/helpers.test.ts b/src/plugins/unified_search/public/query_string_input/text_based_languages_editor/helpers.test.ts index bf34735abd4b6b..b00afb894cf30c 100644 --- a/src/plugins/unified_search/public/query_string_input/text_based_languages_editor/helpers.test.ts +++ b/src/plugins/unified_search/public/query_string_input/text_based_languages_editor/helpers.test.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { parseErrors } from './helpers'; +import { parseErrors, getInlineEditorText } from './helpers'; describe('helpers', function () { describe('parseErrors', function () { @@ -102,4 +102,30 @@ describe('helpers', function () { ]); }); }); + + describe('getInlineEditorText', function () { + it('should return the entire query if it is one liner', function () { + const text = getInlineEditorText( + 'SELECT field1, count(*) FROM index1 ORDER BY field1', + false + ); + expect(text).toEqual(text); + }); + + it('should return the query on one line with extra space if is multiliner', function () { + const text = getInlineEditorText( + 'SELECT field1, count(*)\nFROM index1 ORDER BY field1', + true + ); + expect(text).toEqual('SELECT field1, count(*) FROM index1 ORDER BY field1'); + }); + + it('should return the query on one line with extra spaces removed if is multiliner', function () { + const text = getInlineEditorText( + 'SELECT field1, count(*)\nFROM index1 \n ORDER BY field1', + true + ); + expect(text).toEqual('SELECT field1, count(*) FROM index1 ORDER BY field1'); + }); + }); }); diff --git a/src/plugins/unified_search/public/query_string_input/text_based_languages_editor/helpers.ts b/src/plugins/unified_search/public/query_string_input/text_based_languages_editor/helpers.ts index 112d1a364e3ee2..8715d7228fe5e1 100644 --- a/src/plugins/unified_search/public/query_string_input/text_based_languages_editor/helpers.ts +++ b/src/plugins/unified_search/public/query_string_input/text_based_languages_editor/helpers.ts @@ -132,3 +132,7 @@ export const getDocumentationSections = async (language: string) => { }; } }; + +export const getInlineEditorText = (queryString: string, isMultiLine: boolean) => { + return isMultiLine ? queryString.replace(/\r?\n|\r/g, ' ').replace(/ +/g, ' ') : queryString; +}; diff --git a/src/plugins/unified_search/public/query_string_input/text_based_languages_editor/index.tsx b/src/plugins/unified_search/public/query_string_input/text_based_languages_editor/index.tsx index d29a8cbcd8f309..93ec5711a9e4c1 100644 --- a/src/plugins/unified_search/public/query_string_input/text_based_languages_editor/index.tsx +++ b/src/plugins/unified_search/public/query_string_input/text_based_languages_editor/index.tsx @@ -37,7 +37,12 @@ import { EDITOR_MIN_HEIGHT, } from './text_based_languages_editor.styles'; import { MemoizedDocumentation, DocumentationSections } from './documentation'; -import { useDebounceWithOptions, parseErrors, getDocumentationSections } from './helpers'; +import { + useDebounceWithOptions, + parseErrors, + getInlineEditorText, + getDocumentationSections, +} from './helpers'; import { EditorFooter } from './editor_footer'; import './overwrite.scss'; @@ -233,8 +238,7 @@ export const TextBasedLanguagesEditor = memo(function TextBasedLanguagesEditor({ if (hasLines && !updateLinesFromModel) { setLines(queryString.split(/\r|\n/).length); } - const trimmedText = queryString.replace(/\r?\n|\r/g, ''); - const text = hasLines ? trimmedText : queryString; + const text = getInlineEditorText(queryString, Boolean(hasLines)); const queryLength = text.length; const unusedSpace = errors && errors.length