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 committed Nov 11, 2020
1 parent 4f763a9 commit 3123124
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
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
17 changes: 17 additions & 0 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 Down Expand Up @@ -86,6 +87,7 @@ export class MonacoEditorProvider {
@inject(MonacoWorkspace) protected readonly workspace: MonacoWorkspace,
@inject(MonacoCommandServiceFactory) protected readonly commandServiceFactory: MonacoCommandServiceFactory,
@inject(EditorPreferences) protected readonly editorPreferences: EditorPreferences,
@inject(FileSystemPreferences) protected readonly filePreferences: FileSystemPreferences,
@inject(MonacoQuickOpenService) protected readonly quickOpenService: MonacoQuickOpenService,
@inject(MonacoDiffNavigatorFactory) protected readonly diffNavigatorFactory: MonacoDiffNavigatorFactory,
/** @deprecated since 1.6.0 */
Expand Down Expand Up @@ -276,6 +278,7 @@ export class MonacoEditorProvider {
}
}));
toDispose.push(editor.onLanguageChanged(() => this.updateMonacoEditorOptions(editor)));
editor.document.onWillSaveModel(event => event.waitUntil(this.removeTrailingWhiteSpace(editor, event)));
editor.document.onWillSaveModel(event => event.waitUntil(this.formatOnSave(editor, event)));
return editor;
}
Expand Down Expand Up @@ -312,6 +315,20 @@ export class MonacoEditorProvider {
return true;
}

protected async removeTrailingWhiteSpace(editor: MonacoEditor, event: WillSaveMonacoModelEvent): Promise<monaco.editor.IIdentifiedSingleEditOperation[]> {
if (!this.shouldFormat(editor, event)) {
return [];
}
const overrideIdentifier = editor.document.languageId;
const uri = editor.uri.toString();
const shouldRemoveWhiteSpace = this.filePreferences.get({ preferenceName: 'files.trimTrailingWhitespace', overrideIdentifier }, undefined, uri);
if (!shouldRemoveWhiteSpace) {
return [];
}
await editor.runAction('editor.action.trimTrailingWhitespace');
return [];
}

protected async formatOnSave(editor: MonacoEditor, event: WillSaveMonacoModelEvent): Promise<monaco.editor.IIdentifiedSingleEditOperation[]> {
if (!this.shouldFormat(editor, event)) {
return [];
Expand Down

0 comments on commit 3123124

Please sign in to comment.