Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ElenaStoeva committed May 14, 2024
1 parent e0fe0c4 commit 50363cc
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 133 deletions.
2 changes: 0 additions & 2 deletions packages/kbn-monaco/src/console/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ export const createParser = () => {
},
addRequestEnd = function() {
const lastRequest = getLastRequest();
const requestText = text.substring(requestStartOffset, requestEndOffset);
lastRequest.endOffset = requestEndOffset;
lastRequest.text = requestText;
requests.push(lastRequest);
},
error = function (m) {
Expand Down
1 change: 0 additions & 1 deletion packages/kbn-monaco/src/console/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export interface ParsedRequest {
method: string;
url: string;
data?: Array<Record<string, unknown>>;
text: string;
}
export interface ConsoleParserResult {
errors: ErrorAnnotation[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export interface EditorRequest {
method: string;
url: string;
data: string[];
text: string;
}

export interface AdjustedParsedRequest extends ParsedRequest {
Expand Down Expand Up @@ -343,23 +342,29 @@ export class MonacoEditorActionsProvider {
return this.getSuggestions(model, position);
}

private getTextInRange(selectionRange: monaco.IRange): string {
/*
This function returns the text in the provided range.
If no range is provided, it returns all text in the editor.
*/
private getTextInRange(selectionRange?: monaco.IRange): string {
const model = this.editor.getModel();
if (!model || !selectionRange) {
if (!model) {
return '';
}
const { startLineNumber, startColumn, endLineNumber, endColumn } = selectionRange;
const text = model.getValueInRange({
startLineNumber,
startColumn,
endLineNumber,
endColumn,
});
return text;
if (selectionRange) {
const { startLineNumber, startColumn, endLineNumber, endColumn } = selectionRange;
return model.getValueInRange({
startLineNumber,
startColumn,
endLineNumber,
endColumn,
});
}
// If no range is provided, return all text in the editor
return model.getValue();
}

public async autoIndent(event: React.MouseEvent) {
event.preventDefault();
const parsedRequests = await this.getSelectedParsedRequests();
const selectionStartLineNumber = parsedRequests[0].startLineNumber;
const selectionEndLineNumber = parsedRequests[parsedRequests.length - 1].endLineNumber;
Expand All @@ -375,14 +380,20 @@ export class MonacoEditorActionsProvider {
}

const selectedText = this.getTextInRange(selectedRange);
const allText = this.getTextInRange();

const autoIndentedText = getAutoIndentedRequests(parsedRequests, selectedText);
const autoIndentedText = getAutoIndentedRequests(parsedRequests, selectedText, allText);

this.editor.executeEdits('', [
{
range: selectedRange,
text: autoIndentedText,
},
]);
this.editor.executeEdits(
i18n.translate('console.monaco.applyIndentationCall', {
defaultMessage: 'Apply indentations.',
}),
[
{
range: selectedRange,
text: autoIndentedText,
},
]
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { monaco } from '@kbn/monaco';

interface RegisterCommandsParams {
editor: monaco.editor.IStandaloneCodeEditor;
getDocumentationLink: () => Promise<string | null>;
}

/**
* Hook that sets up the autocomplete polling for Console editor.
*
* @param params The {@link RegisterCommandsParams} to use.
*/
export const useRegisterCommands = () => {
return (params: RegisterCommandsParams) => {
const { editor, getDocumentationLink } = params;

const openDocs = async () => {
const documentation = await getDocumentationLink();
if (!documentation) {
return;
}
window.open(documentation, '_blank');
};

editor.addCommand(monaco.KeyCode.Enter, openDocs);
};
};
Loading

0 comments on commit 50363cc

Please sign in to comment.