Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
protected addOnSaveHandler(editorWidget: EditorWidget): void {
const monacoEditor = MonacoEditor.get(editorWidget);
if (monacoEditor) {
monacoEditor.document.onWillSaveModel(event => {
event.waitUntil(new Promise(resolve => {
const uri = monacoEditor.uri.toString();
const properties = this.properties[uri];
const edits = [];
edits.push(...this.getEditsTrimmingTrailingWhitespaces(monacoEditor, properties, event.reason));
const edit = this.getEditInsertingFinalNewLine(monacoEditor, properties);
if (edit) {
edits.push(edit);
// get current cursor position
const cursor = monacoEditor.cursor;
organizeImports(): void {
const editor = MonacoEditor.get(this.currentEditor);
if (editor) {
// tslint:disable-next-line:no-any
const action = editor.getControl().getAction('editor.action.organizeImports') as any;
// workaround isSupported check
action._run();
}
}
private getEditsTrimmingTrailingWhitespaces(editor: MonacoEditor, properties: KnownProps, saveReason?: TextDocumentSaveReason): monaco.editor.IIdentifiedSingleEditOperation[] {
const edits = [];
if (this.isSet(properties.trim_trailing_whitespace)) {
if (MonacoEditor.get(this.editorManager.activeEditor) === editor) {
const trimReason = (saveReason !== TextDocumentSaveReason.Manual) ? 'auto-save' : undefined;
editor.commandService.executeCommand('editor.action.trimTrailingWhitespace', {
reason: trimReason
});
return [];
}
const lines = editor.document.lineCount;
for (let i = 1; i <= lines; i++) {
const line = editor.document.textEditorModel.getLineContent(i);
const trimmedLine = line.trimRight();
if (line.length !== trimmedLine.length) {
edits.push({
forceMoveMarkers: false,
range: new monaco.Range(i, trimmedLine.length + 1, i, line.length + 1),
private onEditorCreated(editor: EditorWidget): void {
const monacoEditor = MonacoEditor.get(editor);
if (monacoEditor) {
this.onEditorAdded(monacoEditor);
editor.disposed.connect(e => this.onEditorRemoved(monacoEditor));
}
}