-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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
Changes from 13 commits
aef7615
e9d191c
147f53d
27237f9
7654c46
b37881f
18dc2c4
6929c0e
09f5333
8fc44d3
e01aaa9
8f79193
e0fe0c4
50363cc
30fd2d0
addacb6
ff3a57b
3e1264f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ import { | |
replaceRequestVariables, | ||
stringifyRequest, | ||
trackSentRequests, | ||
getAutoIndentedRequests, | ||
} from './utils'; | ||
|
||
const selectedRequestsClass = 'console__monaco_editor__selectedRequests'; | ||
|
@@ -43,9 +44,10 @@ export interface EditorRequest { | |
method: string; | ||
url: string; | ||
data: string[]; | ||
text: string; | ||
} | ||
|
||
interface AdjustedParsedRequest extends ParsedRequest { | ||
export interface AdjustedParsedRequest extends ParsedRequest { | ||
startLineNumber: number; | ||
endLineNumber: number; | ||
} | ||
|
@@ -340,4 +342,47 @@ export class MonacoEditorActionsProvider { | |
): monaco.languages.ProviderResult<monaco.languages.CompletionList> { | ||
return this.getSuggestions(model, position); | ||
} | ||
|
||
private getTextInRange(selectionRange: monaco.IRange): string { | ||
const model = this.editor.getModel(); | ||
if (!model || !selectionRange) { | ||
return ''; | ||
} | ||
const { startLineNumber, startColumn, endLineNumber, endColumn } = selectionRange; | ||
const text = model.getValueInRange({ | ||
startLineNumber, | ||
startColumn, | ||
endLineNumber, | ||
endColumn, | ||
}); | ||
return text; | ||
} | ||
|
||
public async autoIndent(event: React.MouseEvent) { | ||
event.preventDefault(); | ||
ElenaStoeva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const parsedRequests = await this.getSelectedParsedRequests(); | ||
const selectionStartLineNumber = parsedRequests[0].startLineNumber; | ||
const selectionEndLineNumber = parsedRequests[parsedRequests.length - 1].endLineNumber; | ||
const selectedRange = new monaco.Range( | ||
selectionStartLineNumber, | ||
1, | ||
selectionEndLineNumber, | ||
this.editor.getModel()?.getLineMaxColumn(selectionEndLineNumber) ?? 1 | ||
); | ||
|
||
if (parsedRequests.length < 1) { | ||
return; | ||
} | ||
|
||
const selectedText = this.getTextInRange(selectedRange); | ||
|
||
const autoIndentedText = getAutoIndentedRequests(parsedRequests, selectedText); | ||
|
||
this.editor.executeEdits('', [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: selectedRange, | ||
text: autoIndentedText, | ||
}, | ||
]); | ||
} | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.