Skip to content

Commit

Permalink
add trim trailing whitespace preference
Browse files Browse the repository at this point in the history
Signed-off-by: Colin Grant <colin.grant@ericsson.com>
  • Loading branch information
colin-grant-work committed Dec 3, 2020
1 parent 75bf8df commit 67df732
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/common/preferences/preference-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export namespace PreferenceScope {
return PreferenceScope.Folder;
case 'resource':
return PreferenceScope.Folder;
case 'language-overridable':
return PreferenceScope.Folder;
}
}
}
7 changes: 7 additions & 0 deletions packages/filesystem/src/browser/filesystem-preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ These have precedence over the default associations of the languages installed.'
type: 'number',
default: MAX_FILE_SIZE_MB,
markdownDescription: 'Controls the max file size in MB which is possible to open.'
},
'files.trimTrailingWhitespace': {
'type': 'boolean',
'default': false,
'description': 'When enabled, will trim trailing whitespace when saving a file.',
'scope': 'language-overridable'
}
}
};
Expand All @@ -91,6 +97,7 @@ export interface FileSystemConfiguration {
'files.autoGuessEncoding': boolean;
'files.participants.timeout': number;
'files.maxFileSizeMB': number;
'files.trimTrailingWhitespace': boolean;
}

export const FileSystemPreferences = Symbol('FileSystemPreferences');
Expand Down
21 changes: 14 additions & 7 deletions packages/monaco/src/browser/monaco-editor-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { MonacoResolvedKeybinding } from './monaco-resolved-keybinding';
import { HttpOpenHandlerOptions } from '@theia/core/lib/browser/http-open-handler';
import { MonacoToProtocolConverter } from './monaco-to-protocol-converter';
import { ProtocolToMonacoConverter } from './protocol-to-monaco-converter';
import { FileSystemPreferences } from '@theia/filesystem/lib/browser';

export const MonacoEditorFactory = Symbol('MonacoEditorFactory');
export interface MonacoEditorFactory {
Expand All @@ -67,6 +68,9 @@ export class MonacoEditorProvider {
@inject(OpenerService)
protected readonly openerService: OpenerService;

@inject(FileSystemPreferences)
protected readonly filePreferences: FileSystemPreferences;

protected _current: MonacoEditor | undefined;
/**
* Returns the last focused MonacoEditor.
Expand Down Expand Up @@ -319,14 +323,17 @@ export class MonacoEditorProvider {
const overrideIdentifier = editor.document.languageId;
const uri = editor.uri.toString();
const formatOnSave = this.editorPreferences.get({ preferenceName: 'editor.formatOnSave', overrideIdentifier }, undefined, uri)!;
if (!formatOnSave) {
return [];
if (formatOnSave) {
const formatOnSaveTimeout = this.editorPreferences.get({ preferenceName: 'editor.formatOnSaveTimeout', overrideIdentifier }, undefined, uri)!;
await Promise.race([
new Promise((_, reject) => setTimeout(() => reject(new Error(`Aborted format on save after ${formatOnSaveTimeout}ms`)), formatOnSaveTimeout)),
editor.runAction('editor.action.formatDocument')
]);
}
const shouldRemoveWhiteSpace = this.filePreferences.get({ preferenceName: 'files.trimTrailingWhitespace', overrideIdentifier }, undefined, uri);
if (shouldRemoveWhiteSpace) {
await editor.runAction('editor.action.trimTrailingWhitespace');
}
const formatOnSaveTimeout = this.editorPreferences.get({ preferenceName: 'editor.formatOnSaveTimeout', overrideIdentifier }, undefined, uri)!;
await Promise.race([
new Promise((_, reject) => setTimeout(() => reject(new Error(`Aborted format on save after ${formatOnSaveTimeout}ms`)), formatOnSaveTimeout)),
editor.runAction('editor.action.formatDocument')
]);
return [];
}

Expand Down

0 comments on commit 67df732

Please sign in to comment.