Skip to content

Commit

Permalink
[Unified search] Improve the way that spaces are handled on inline mo…
Browse files Browse the repository at this point in the history
…de (#137678)

* [Unified search] Improve the way that spaces are handled on multiline mode

* Update src/plugins/unified_search/public/query_string_input/text_based_languages_editor/helpers.ts

Co-authored-by: Jean-Louis Leysens <jloleysens@gmail.com>

Co-authored-by: Jean-Louis Leysens <jloleysens@gmail.com>
  • Loading branch information
stratoula and jloleysens committed Aug 2, 2022
1 parent 2091184 commit daba7d2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import { parseErrors } from './helpers';
import { parseErrors, getInlineEditorText } from './helpers';

describe('helpers', function () {
describe('parseErrors', function () {
Expand Down Expand Up @@ -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');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit daba7d2

Please sign in to comment.