Files
gerrit/polygerrit-ui/app/elements/diff/gr-diff-preferences/gr-diff-preferences.js
Wyatt Allen efc81216a1 Add ignore-whitespace control
When a diff request includes an ignore-whitespace level, the same diff
content is returned, but a number of diff chunks are marked as "common"
with the understanding that those chunks only contain the kind of
whitespace that can be ignored. Thus, actually ignoring these chunks is
up to the frontend.

In this commit, support is added for editing the preferred whitespace
level, and the preferred level is included in diff requests. When a diff
is loaded with an ignore-whitespace level, gr-diff-host transforms the
marked deltas into shared chunks. In this way, when the transformed diff
is passed down into gr-diff, it can be rendered like normal and the
unwanted whitespace deltas do not appear.

To transform a diff with chunks to be ignored, the strategy is as
follows. If a marked delta chunk has revision (a right-side), then it's
converted to a shared chunk where both sides are made up of the revision
content. (If a marked delta chunk has no revision content, it's merely
omitted.) Finally, adjacent shared chunks that result from modifying
the marked chunks are merged together so that gr-diff will properly
position context-control barriers.

Feature: Issue 6198
Change-Id: I1e4cc1075edf34f5ce87ee6bfc2cf415a3b98d94
2018-09-14 13:43:19 -07:00

145 lines
4.0 KiB
JavaScript

/**
* @license
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function() {
'use strict';
Polymer({
is: 'gr-diff-preferences',
properties: {
prefs: {
type: Object,
notify: true,
},
localPrefs: {
type: Object,
notify: true,
},
disabled: {
type: Boolean,
value: false,
reflectToAttribute: true,
},
/** @type {?} */
_newPrefs: Object,
_newLocalPrefs: Object,
},
observers: [
'_prefsChanged(prefs.*)',
'_localPrefsChanged(localPrefs.*)',
],
getFocusStops() {
return {
start: this.$.contextSelect,
end: this.$.saveButton,
};
},
resetFocus() {
this.$.contextSelect.focus();
},
_prefsChanged(changeRecord) {
const prefs = changeRecord.base;
// NOTE: Object.assign is NOT automatically a deep copy. If prefs adds
// an object as a value, it must be marked enumerable.
this._newPrefs = Object.assign({}, prefs);
this.$.contextSelect.value = prefs.context;
this.$.showTabsInput.checked = prefs.show_tabs;
this.$.showTrailingWhitespaceInput.checked = prefs.show_whitespace_errors;
this.$.lineWrappingInput.checked = prefs.line_wrapping;
this.$.syntaxHighlightInput.checked = prefs.syntax_highlighting;
this.$.automaticReviewInput.checked = !prefs.manual_review;
this.$.ignoreWhitespace.value = prefs.ignore_whitespace;
},
_localPrefsChanged(changeRecord) {
const localPrefs = changeRecord.base || {};
this._newLocalPrefs = Object.assign({}, localPrefs);
},
_handleContextSelectChange(e) {
const selectEl = Polymer.dom(e).rootTarget;
this.set('_newPrefs.context', parseInt(selectEl.value, 10));
},
_handleIgnoreWhitespaceChange(e) {
const selectEl = Polymer.dom(e).rootTarget;
this.set('_newPrefs.ignore_whitespace', selectEl.value);
},
_handleShowTabsTap(e) {
this.set('_newPrefs.show_tabs', Polymer.dom(e).rootTarget.checked);
},
_handleShowTrailingWhitespaceTap(e) {
this.set('_newPrefs.show_whitespace_errors',
Polymer.dom(e).rootTarget.checked);
},
_handleSyntaxHighlightTap(e) {
this.set('_newPrefs.syntax_highlighting',
Polymer.dom(e).rootTarget.checked);
},
_handlelineWrappingTap(e) {
this.set('_newPrefs.line_wrapping', Polymer.dom(e).rootTarget.checked);
},
_handleAutomaticReviewTap(e) {
this.set('_newPrefs.manual_review', !Polymer.dom(e).rootTarget.checked);
},
_handleSave(e) {
e.stopPropagation();
this.prefs = this._newPrefs;
this.localPrefs = this._newLocalPrefs;
const el = Polymer.dom(e).rootTarget;
el.disabled = true;
this.$.storage.savePreferences(this._localPrefs);
this._saveDiffPreferences().then(response => {
el.disabled = false;
if (!response.ok) { return response; }
this.$.prefsOverlay.close();
}).catch(err => {
el.disabled = false;
});
},
_handleCancel(e) {
e.stopPropagation();
this.$.prefsOverlay.close();
},
open() {
this.$.prefsOverlay.open().then(() => {
const focusStops = this.getFocusStops();
this.$.prefsOverlay.setFocusStops(focusStops);
this.resetFocus();
});
},
_saveDiffPreferences() {
return this.$.restAPI.saveDiffPreferences(this.prefs);
},
});
})();