Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Console Migration] Add action for auto indentation #181613

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export const MonacoEditor = ({ initialTextValue }: EditorProps) => {
return actionsProvider.current!.getDocumentationLink(docLinkVersion);
}, [docLinkVersion]);

const autoIndentCallback = useCallback(async (event: React.MouseEvent) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the argument "event" is now unused and can be deleted

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! I removed it in 3e1264f.

return actionsProvider.current!.autoIndent(event);
}, []);

const sendRequestsCallback = useCallback(async () => {
await actionsProvider.current?.sendRequests(toasts, dispatch, trackUiMetric, http);
}, [dispatch, http, toasts, trackUiMetric]);
Expand Down Expand Up @@ -119,7 +123,7 @@ export const MonacoEditor = ({ initialTextValue }: EditorProps) => {
<ConsoleMenu
getCurl={getCurlCallback}
getDocumentation={getDocumenationLink}
autoIndent={() => {}}
autoIndent={autoIndentCallback}
notifications={notifications}
/>
</EuiFlexItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
trackSentRequests,
tokenizeRequestUrl,
getDocumentationLinkFromAutocompleteContext,
getAutoIndentedRequests,
} from './utils';

const selectedRequestsClass = 'console__monaco_editor__selectedRequests';
Expand Down Expand Up @@ -265,4 +266,23 @@ export class MonacoEditorActionsProvider {

return getDocumentationLinkFromAutocompleteContext(context, docLinkVersion);
}

public async autoIndent(event: React.MouseEvent) {
event.preventDefault();
ElenaStoeva marked this conversation as resolved.
Show resolved Hide resolved
const requests = await this.getRequests();
const { range } = await this.getSelectedParsedRequestsAndRange();

if (requests.length < 1) {
return;
}

const autoIndentedRequests = getAutoIndentedRequests(requests);

this.editor.executeEdits('', [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think for completeness sake we should use some kind of string to indicate that the edit was made by the auto-indent button

Copy link
Contributor Author

@ElenaStoeva ElenaStoeva May 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I wasn't sure how this string would be used... will it be visible to the user somehow or do you think we'll only need it for internal use? I added a translated string (in case it will be displayed to the user), let me know what you think.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the string might be used for undo/redo actions which I don't think we currently have. I believe the value won't be displayed in the UI to the user though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see... Then it might be good to store this in a constant in case it's needed somewhere. I added this change with 3e1264f.

{
range,
text: autoIndentedRequests,
},
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import {
getAutoIndentedRequests,
getCurlRequest,
getDocumentationLinkFromAutocompleteContext,
removeTrailingWhitespaces,
Expand Down Expand Up @@ -224,4 +225,57 @@ describe('monaco editor utils', () => {
expect(link).toBe(expectedLink);
});
});

describe('getAutoIndentedRequests', () => {
const TEST_REQUEST_1 = {
method: 'GET',
url: '_search',
data: ['{\n "query": {\n "match_all": {}\n }\n}'],
};

const TEST_REQUEST_2 = {
method: 'GET',
url: '_all',
data: [],
};

const TEST_REQUEST_3 = {
method: 'POST',
url: '/_bulk',
data: ['{\n "index": {\n "_index": "books"\n }\n}', '{\n "name": "1984"\n}'],
};

it('correctly auto-indents a single request with data', () => {
const formattedData = getAutoIndentedRequests([TEST_REQUEST_1]);
const expectedResult = 'GET _search\n{\n "query": {\n "match_all": {}\n }\n}';
expect(formattedData).toBe(expectedResult);
});

it('correctly auto-indents a single request with no data', () => {
const formattedData = getAutoIndentedRequests([TEST_REQUEST_2]);
const expectedResult = 'GET _all';

expect(formattedData).toBe(expectedResult);
});

it('correctly auto-indents a single request with multiple data', () => {
const formattedData = getAutoIndentedRequests([TEST_REQUEST_3]);
const expectedResult =
'POST /_bulk\n{\n "index": {\n "_index": "books"\n }\n}\n{\n "name": "1984"\n}';

expect(formattedData).toBe(expectedResult);
});

it('correctly auto-indents multiple request', () => {
const formattedData = getAutoIndentedRequests([
TEST_REQUEST_1,
TEST_REQUEST_2,
TEST_REQUEST_3,
]);
const expectedResult =
'GET _search\n{\n "query": {\n "match_all": {}\n }\n}\n\nGET _all\n\nPOST /_bulk\n{\n "index": {\n "_index": "books"\n }\n}\n{\n "name": "1984"\n}';

expect(formattedData).toBe(expectedResult);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,19 @@ export const getDocumentationLinkFromAutocompleteContext = (
}
return null;
};

export const getAutoIndentedRequests = (requests: EditorRequest[]): string => {
const formattedRequestsText: string[] = [];

requests.forEach((request, index) => {
const firstLine = (index > 0 ? '\n' : '') + request.method + ' ' + request.url;
formattedRequestsText.push(firstLine);

if (request.data && request.data.length > 0) {
// The request data is already parsed as JSON
formattedRequestsText.push(...request.data);
}
});

return formattedRequestsText.join('\n');
};