Skip to content
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

Add Trim Trailing Whitespace Preference #8742

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
vince-fugnitto marked this conversation as resolved.
Show resolved Hide resolved
}
}
};
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