Refactoring AccountDiffPreference vs PatchScriptSettings+PrettySettings

There was some code duplication in these classes. This refactoring
removes the PatchScriptSettings and PrettySettings classes and uses
AccountDiffPreference instead.

Issue: bug 629
Change-Id: I57ab1522b0023503d0cbd29620236ea68b7717ed
Signed-off-by: Sasa Zivkov <zivkov@gmail.com>
This commit is contained in:
Sasa Zivkov 2010-07-21 15:45:07 +02:00 committed by Shawn O. Pearce
parent 228e8dd509
commit 8e33d76853
15 changed files with 130 additions and 310 deletions

View File

@ -16,6 +16,7 @@ package com.google.gerrit.common.data;
import com.google.gerrit.common.auth.SignInRequired; import com.google.gerrit.common.auth.SignInRequired;
import com.google.gerrit.reviewdb.Account; import com.google.gerrit.reviewdb.Account;
import com.google.gerrit.reviewdb.AccountDiffPreference;
import com.google.gerrit.reviewdb.ApprovalCategoryValue; import com.google.gerrit.reviewdb.ApprovalCategoryValue;
import com.google.gerrit.reviewdb.Change; import com.google.gerrit.reviewdb.Change;
import com.google.gerrit.reviewdb.Patch; import com.google.gerrit.reviewdb.Patch;
@ -34,7 +35,7 @@ import java.util.Set;
@RpcImpl(version = Version.V2_0) @RpcImpl(version = Version.V2_0)
public interface PatchDetailService extends RemoteJsonService { public interface PatchDetailService extends RemoteJsonService {
void patchScript(Patch.Key key, PatchSet.Id a, PatchSet.Id b, void patchScript(Patch.Key key, PatchSet.Id a, PatchSet.Id b,
PatchScriptSettings settings, AsyncCallback<PatchScript> callback); AccountDiffPreference diffPrefs, AsyncCallback<PatchScript> callback);
@SignInRequired @SignInRequired
void saveDraft(PatchLineComment comment, void saveDraft(PatchLineComment comment,

View File

@ -17,7 +17,6 @@ package com.google.gerrit.common.data;
import com.google.gerrit.prettify.client.ClientSideFormatter; import com.google.gerrit.prettify.client.ClientSideFormatter;
import com.google.gerrit.prettify.common.EditList; import com.google.gerrit.prettify.common.EditList;
import com.google.gerrit.prettify.common.PrettyFormatter; import com.google.gerrit.prettify.common.PrettyFormatter;
import com.google.gerrit.prettify.common.PrettySettings;
import com.google.gerrit.prettify.common.SparseFileContent; import com.google.gerrit.prettify.common.SparseFileContent;
import com.google.gerrit.prettify.common.SparseHtmlFile; import com.google.gerrit.prettify.common.SparseHtmlFile;
import com.google.gerrit.reviewdb.AccountDiffPreference; import com.google.gerrit.reviewdb.AccountDiffPreference;
@ -40,7 +39,7 @@ public class PatchScript {
protected String oldName; protected String oldName;
protected String newName; protected String newName;
protected List<String> header; protected List<String> header;
protected PatchScriptSettings settings; protected AccountDiffPreference diffPrefs;
protected SparseFileContent a; protected SparseFileContent a;
protected SparseFileContent b; protected SparseFileContent b;
protected List<Edit> edits; protected List<Edit> edits;
@ -52,7 +51,7 @@ public class PatchScript {
protected boolean intralineDifference; protected boolean intralineDifference;
public PatchScript(final Change.Key ck, final ChangeType ct, final String on, public PatchScript(final Change.Key ck, final ChangeType ct, final String on,
final String nn, final List<String> h, final PatchScriptSettings s, final String nn, final List<String> h, final AccountDiffPreference dp,
final SparseFileContent ca, final SparseFileContent cb, final SparseFileContent ca, final SparseFileContent cb,
final List<Edit> e, final DisplayMethod ma, final DisplayMethod mb, final List<Edit> e, final DisplayMethod ma, final DisplayMethod mb,
final CommentDetail cd, final List<Patch> hist, final boolean hf, final CommentDetail cd, final List<Patch> hist, final boolean hf,
@ -62,7 +61,7 @@ public class PatchScript {
oldName = on; oldName = on;
newName = nn; newName = nn;
header = h; header = h;
settings = s; diffPrefs = dp;
a = ca; a = ca;
b = cb; b = cb;
edits = e; edits = e;
@ -113,12 +112,12 @@ public class PatchScript {
return history; return history;
} }
public PatchScriptSettings getSettings() { public AccountDiffPreference getDiffPrefs() {
return settings; return diffPrefs;
} }
public void setSettings(PatchScriptSettings s) { public void setDiffPrefs(AccountDiffPreference dp) {
settings = s; diffPrefs = dp;
} }
public boolean isHugeFile() { public boolean isHugeFile() {
@ -126,7 +125,7 @@ public class PatchScript {
} }
public boolean isIgnoreWhitespace() { public boolean isIgnoreWhitespace() {
return settings.getWhitespace() != Whitespace.IGNORE_NONE; return diffPrefs.getIgnoreWhitespace() != Whitespace.IGNORE_NONE;
} }
public boolean hasIntralineDifference() { public boolean hasIntralineDifference() {
@ -142,12 +141,12 @@ public class PatchScript {
} }
public SparseHtmlFile getSparseHtmlFileA() { public SparseHtmlFile getSparseHtmlFileA() {
PrettySettings s = new PrettySettings(settings.getPrettySettings()); AccountDiffPreference dp = new AccountDiffPreference(diffPrefs);
s.setFileName(a.getPath()); dp.setShowWhitespaceErrors(false);
s.setShowWhiteSpaceErrors(false);
PrettyFormatter f = ClientSideFormatter.FACTORY.get(); PrettyFormatter f = ClientSideFormatter.FACTORY.get();
f.setPrettySettings(s); f.setDiffPrefs(dp);
f.setFileName(a.getPath());
f.setEditFilter(PrettyFormatter.A); f.setEditFilter(PrettyFormatter.A);
f.setEditList(edits); f.setEditList(edits);
f.format(a); f.format(a);
@ -155,15 +154,15 @@ public class PatchScript {
} }
public SparseHtmlFile getSparseHtmlFileB() { public SparseHtmlFile getSparseHtmlFileB() {
PrettySettings s = new PrettySettings(settings.getPrettySettings()); AccountDiffPreference dp = new AccountDiffPreference(diffPrefs);
s.setFileName(b.getPath());
PrettyFormatter f = ClientSideFormatter.FACTORY.get(); PrettyFormatter f = ClientSideFormatter.FACTORY.get();
f.setPrettySettings(s); f.setDiffPrefs(dp);
f.setFileName(b.getPath());
f.setEditFilter(PrettyFormatter.B); f.setEditFilter(PrettyFormatter.B);
f.setEditList(edits); f.setEditList(edits);
if (s.isSyntaxHighlighting() && a.isWholeFile() && !b.isWholeFile()) { if (dp.isSyntaxHighlighting() && a.isWholeFile() && !b.isWholeFile()) {
f.format(b.apply(a, edits)); f.format(b.apply(a, edits));
} else { } else {
f.format(b); f.format(b);
@ -176,7 +175,7 @@ public class PatchScript {
} }
public Iterable<EditList.Hunk> getHunks() { public Iterable<EditList.Hunk> getHunks() {
int ctx = settings.getContext(); int ctx = diffPrefs.getContext();
if (ctx == AccountDiffPreference.WHOLE_FILE_CONTEXT) { if (ctx == AccountDiffPreference.WHOLE_FILE_CONTEXT) {
ctx = Math.max(a.size(), b.size()); ctx = Math.max(a.size(), b.size());
} }

View File

@ -1,63 +0,0 @@
// Copyright (C) 2009 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.
package com.google.gerrit.common.data;
import com.google.gerrit.prettify.common.PrettySettings;
import com.google.gerrit.reviewdb.AccountDiffPreference;
import com.google.gerrit.reviewdb.AccountDiffPreference.Whitespace;
public class PatchScriptSettings {
protected int context;
protected Whitespace whitespace;
protected PrettySettings pretty;
public PatchScriptSettings() {
context = AccountDiffPreference.DEFAULT_CONTEXT;
whitespace = Whitespace.IGNORE_NONE;
pretty = new PrettySettings();
}
public PatchScriptSettings(final PatchScriptSettings s) {
context = s.context;
whitespace = s.whitespace;
pretty = new PrettySettings(s.pretty);
}
public PrettySettings getPrettySettings() {
return pretty;
}
public void setPrettySettings(PrettySettings s) {
pretty = s;
}
public int getContext() {
return context;
}
public void setContext(final int ctx) {
assert 0 <= ctx || ctx == AccountDiffPreference.WHOLE_FILE_CONTEXT;
context = ctx;
}
public Whitespace getWhitespace() {
return whitespace;
}
public void setWhitespace(final Whitespace ws) {
assert ws != null;
whitespace = ws;
}
}

View File

@ -27,7 +27,6 @@ import com.google.gerrit.client.ui.InlineHyperlink;
import com.google.gerrit.client.ui.Screen; import com.google.gerrit.client.ui.Screen;
import com.google.gerrit.common.PageLinks; import com.google.gerrit.common.PageLinks;
import com.google.gerrit.common.data.PatchScript; import com.google.gerrit.common.data.PatchScript;
import com.google.gerrit.common.data.PatchScriptSettings;
import com.google.gerrit.common.data.PatchSetDetail; import com.google.gerrit.common.data.PatchSetDetail;
import com.google.gerrit.prettify.client.ClientSideFormatter; import com.google.gerrit.prettify.client.ClientSideFormatter;
import com.google.gerrit.prettify.common.PrettyFactory; import com.google.gerrit.prettify.common.PrettyFactory;
@ -81,9 +80,9 @@ public abstract class PatchScreen extends Screen implements
public Unified(final Patch.Key id, final int patchIndex, public Unified(final Patch.Key id, final int patchIndex,
final PatchSetDetail patchSetDetail, final PatchTable patchTable) { final PatchSetDetail patchSetDetail, final PatchTable patchTable) {
super(id, patchIndex, patchSetDetail, patchTable); super(id, patchIndex, patchSetDetail, patchTable);
final PatchScriptSettings s = settingsPanel.getValue(); final AccountDiffPreference dp = settingsPanel.getValue();
s.getPrettySettings().setSyntaxHighlighting(false); dp.setSyntaxHighlighting(false);
settingsPanel.setValue(s); settingsPanel.setValue(dp);
} }
@Override @Override
@ -181,9 +180,9 @@ public abstract class PatchScreen extends Screen implements
settingsPanel = new PatchScriptSettingsPanel(); settingsPanel = new PatchScriptSettingsPanel();
settingsPanel settingsPanel
.addValueChangeHandler(new ValueChangeHandler<PatchScriptSettings>() { .addValueChangeHandler(new ValueChangeHandler<AccountDiffPreference>() {
@Override @Override
public void onValueChange(ValueChangeEvent<PatchScriptSettings> event) { public void onValueChange(ValueChangeEvent<AccountDiffPreference> event) {
update(event.getValue()); update(event.getValue());
} }
}); });
@ -206,9 +205,9 @@ public abstract class PatchScreen extends Screen implements
lastScript = null; lastScript = null;
} }
private void update(PatchScriptSettings s) { private void update(AccountDiffPreference dp) {
if (lastScript != null && canReuse(s, lastScript)) { if (lastScript != null && canReuse(dp, lastScript)) {
lastScript.setSettings(s); lastScript.setDiffPrefs(dp);
RpcStatus.INSTANCE.onRpcStart(null); RpcStatus.INSTANCE.onRpcStart(null);
settingsPanel.setEnabled(false); settingsPanel.setEnabled(false);
DeferredCommand.addCommand(new Command() { DeferredCommand.addCommand(new Command() {
@ -226,24 +225,24 @@ public abstract class PatchScreen extends Screen implements
} }
} }
private boolean canReuse(PatchScriptSettings s, PatchScript last) { private boolean canReuse(AccountDiffPreference dp, PatchScript last) {
if (last.getSettings().getWhitespace() != s.getWhitespace()) { if (last.getDiffPrefs().getIgnoreWhitespace() != dp.getIgnoreWhitespace()) {
// Whitespace ignore setting requires server computation. // Whitespace ignore setting requires server computation.
return false; return false;
} }
final int ctx = s.getContext(); final int ctx = dp.getContext();
if (ctx == AccountDiffPreference.WHOLE_FILE_CONTEXT && !last.getA().isWholeFile()) { if (ctx == AccountDiffPreference.WHOLE_FILE_CONTEXT && !last.getA().isWholeFile()) {
// We don't have the entire file here, so we can't render it. // We don't have the entire file here, so we can't render it.
return false; return false;
} }
if (last.getSettings().getContext() < ctx && !last.getA().isWholeFile()) { if (last.getDiffPrefs().getContext() < ctx && !last.getA().isWholeFile()) {
// We don't have sufficient context. // We don't have sufficient context.
return false; return false;
} }
if (s.getPrettySettings().isSyntaxHighlighting() if (dp.isSyntaxHighlighting()
&& !last.getA().isWholeFile()) { && !last.getA().isWholeFile()) {
// We need the whole file to syntax highlight accurately. // We need the whole file to syntax highlight accurately.
return false; return false;

View File

@ -18,8 +18,6 @@ import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.account.Util; import com.google.gerrit.client.account.Util;
import com.google.gerrit.client.rpc.GerritCallback; import com.google.gerrit.client.rpc.GerritCallback;
import com.google.gerrit.client.ui.NpIntTextBox; import com.google.gerrit.client.ui.NpIntTextBox;
import com.google.gerrit.common.data.PatchScriptSettings;
import com.google.gerrit.prettify.common.PrettySettings;
import com.google.gerrit.reviewdb.AccountDiffPreference; import com.google.gerrit.reviewdb.AccountDiffPreference;
import com.google.gerrit.reviewdb.AccountDiffPreference.Whitespace; import com.google.gerrit.reviewdb.AccountDiffPreference.Whitespace;
import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.GWT;
@ -44,13 +42,13 @@ import com.google.gwt.user.client.ui.Widget;
import com.google.gwtjsonrpc.client.VoidResult; import com.google.gwtjsonrpc.client.VoidResult;
public class PatchScriptSettingsPanel extends Composite implements public class PatchScriptSettingsPanel extends Composite implements
HasValueChangeHandlers<PatchScriptSettings> { HasValueChangeHandlers<AccountDiffPreference> {
private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class); private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);
interface MyUiBinder extends UiBinder<Widget, PatchScriptSettingsPanel> { interface MyUiBinder extends UiBinder<Widget, PatchScriptSettingsPanel> {
} }
private PatchScriptSettings value; private AccountDiffPreference value;
private boolean enableIntralineDifference = true; private boolean enableIntralineDifference = true;
private boolean enableSmallFileFeatures = true; private boolean enableSmallFileFeatures = true;
@ -118,15 +116,15 @@ public class PatchScriptSettingsPanel extends Composite implements
colWidth.addKeyPressHandler(onEnter); colWidth.addKeyPressHandler(onEnter);
if (Gerrit.isSignedIn() && Gerrit.getAccountDiffPreference() != null) { if (Gerrit.isSignedIn() && Gerrit.getAccountDiffPreference() != null) {
setValue(createPatchScriptSettings(Gerrit.getAccountDiffPreference())); setValue(Gerrit.getAccountDiffPreference());
} else { } else {
setValue(new PatchScriptSettings()); setValue(AccountDiffPreference.createDefault(null));
} }
} }
@Override @Override
public HandlerRegistration addValueChangeHandler( public HandlerRegistration addValueChangeHandler(
ValueChangeHandler<PatchScriptSettings> handler) { ValueChangeHandler<AccountDiffPreference> handler) {
return super.addHandler(handler, ValueChangeEvent.getType()); return super.addHandler(handler, ValueChangeEvent.getType());
} }
@ -149,9 +147,7 @@ public class PatchScriptSettingsPanel extends Composite implements
public void setEnableSmallFileFeatures(final boolean on) { public void setEnableSmallFileFeatures(final boolean on) {
enableSmallFileFeatures = on; enableSmallFileFeatures = on;
if (enableSmallFileFeatures) { if (enableSmallFileFeatures) {
final PrettySettings p = getValue().getPrettySettings(); syntaxHighlighting.setValue(value.isSyntaxHighlighting());
syntaxHighlighting.setValue(p.isSyntaxHighlighting());
} else { } else {
syntaxHighlighting.setValue(false); syntaxHighlighting.setValue(false);
} }
@ -161,8 +157,7 @@ public class PatchScriptSettingsPanel extends Composite implements
public void setEnableIntralineDifference(final boolean on) { public void setEnableIntralineDifference(final boolean on) {
enableIntralineDifference = on; enableIntralineDifference = on;
if (enableIntralineDifference) { if (enableIntralineDifference) {
final PrettySettings p = getValue().getPrettySettings(); intralineDifference.setValue(value.isIntralineDifference());
intralineDifference.setValue(p.isIntralineDifference());
} else { } else {
intralineDifference.setValue(false); intralineDifference.setValue(false);
} }
@ -182,28 +177,26 @@ public class PatchScriptSettingsPanel extends Composite implements
return reviewed; return reviewed;
} }
public PatchScriptSettings getValue() { public AccountDiffPreference getValue() {
return value; return value;
} }
public void setValue(final PatchScriptSettings s) { public void setValue(final AccountDiffPreference dp) {
final PrettySettings p = s.getPrettySettings(); setIgnoreWhitespace(dp.getIgnoreWhitespace());
setIgnoreWhitespace(s.getWhitespace());
if (enableSmallFileFeatures) { if (enableSmallFileFeatures) {
syntaxHighlighting.setValue(p.isSyntaxHighlighting()); syntaxHighlighting.setValue(dp.isSyntaxHighlighting());
} else { } else {
syntaxHighlighting.setValue(false); syntaxHighlighting.setValue(false);
} }
setContext(s.getContext()); setContext(dp.getContext());
tabWidth.setIntValue(p.getTabSize()); tabWidth.setIntValue(dp.getTabSize());
colWidth.setIntValue(p.getLineLength()); colWidth.setIntValue(dp.getLineLength());
intralineDifference.setValue(p.isIntralineDifference()); intralineDifference.setValue(dp.isIntralineDifference());
whitespaceErrors.setValue(p.isShowWhiteSpaceErrors()); whitespaceErrors.setValue(dp.isShowWhitespaceErrors());
showTabs.setValue(p.isShowTabs()); showTabs.setValue(dp.isShowTabs());
value = s; value = dp;
} }
@UiHandler("update") @UiHandler("update")
@ -212,20 +205,18 @@ public class PatchScriptSettingsPanel extends Composite implements
} }
private void update() { private void update() {
PatchScriptSettings s = new PatchScriptSettings(getValue()); AccountDiffPreference dp = new AccountDiffPreference(value);
PrettySettings p = s.getPrettySettings(); dp.setIgnoreWhitespace(getIgnoreWhitespace());
dp.setContext(getContext());
dp.setTabSize(tabWidth.getIntValue());
dp.setLineLength(colWidth.getIntValue());
dp.setSyntaxHighlighting(syntaxHighlighting.getValue());
dp.setIntralineDifference(intralineDifference.getValue());
dp.setShowWhitespaceErrors(whitespaceErrors.getValue());
dp.setShowTabs(showTabs.getValue());
s.setWhitespace(getIgnoreWhitespace()); value = dp;
s.setContext(getContext()); fireEvent(new ValueChangeEvent<AccountDiffPreference>(dp) {});
p.setTabSize(tabWidth.getIntValue());
p.setLineLength(colWidth.getIntValue());
p.setSyntaxHighlighting(syntaxHighlighting.getValue());
p.setIntralineDifference(intralineDifference.getValue());
p.setShowWhiteSpaceErrors(whitespaceErrors.getValue());
p.setShowTabs(showTabs.getValue());
value = s;
fireEvent(new ValueChangeEvent<PatchScriptSettings>(s) {});
if (Gerrit.isSignedIn()) { if (Gerrit.isSignedIn()) {
persistDiffPreferences(); persistDiffPreferences();
@ -234,19 +225,10 @@ public class PatchScriptSettingsPanel extends Composite implements
private void persistDiffPreferences() { private void persistDiffPreferences() {
setEnabled(false); setEnabled(false);
final AccountDiffPreference diffPref = new AccountDiffPreference(Gerrit.getUserAccount().getId()); Util.ACCOUNT_SVC.changeDiffPreferences(value, new GerritCallback<VoidResult>() {
diffPref.setIgnoreWhitespace(getIgnoreWhitespace());
diffPref.setTabSize(tabWidth.getIntValue());
diffPref.setLineLength(colWidth.getIntValue());
diffPref.setSyntaxHighlighting(syntaxHighlighting.getValue());
diffPref.setShowWhitespaceErrors(whitespaceErrors.getValue());
diffPref.setIntralineDifference(intralineDifference.getValue());
diffPref.setShowTabs(showTabs.getValue());
diffPref.setContext(getContext());
Util.ACCOUNT_SVC.changeDiffPreferences(diffPref, new GerritCallback<VoidResult>() {
@Override @Override
public void onSuccess(VoidResult result) { public void onSuccess(VoidResult result) {
Gerrit.setAccountDiffPreference(diffPref); Gerrit.setAccountDiffPreference(value);
setEnabled(true); setEnabled(true);
} }
@ -285,7 +267,7 @@ public class PatchScriptSettingsPanel extends Composite implements
if (0 <= sel) { if (0 <= sel) {
return Whitespace.valueOf(ignoreWhitespace.getValue(sel)); return Whitespace.valueOf(ignoreWhitespace.getValue(sel));
} }
return value.getWhitespace(); return value.getIgnoreWhitespace();
} }
private void setIgnoreWhitespace(Whitespace s) { private void setIgnoreWhitespace(Whitespace s) {
@ -316,20 +298,4 @@ public class PatchScriptSettingsPanel extends Composite implements
} }
context.setSelectedIndex(0); context.setSelectedIndex(0);
} }
private PatchScriptSettings createPatchScriptSettings(AccountDiffPreference diffPref) {
final PatchScriptSettings s = new PatchScriptSettings();
if (diffPref != null) {
s.setWhitespace(diffPref.getIgnoreWhitespace());
s.setContext(diffPref.getContext());
final PrettySettings p = s.getPrettySettings();
p.setTabSize(diffPref.getTabSize());
p.setLineLength(diffPref.getLineLength());
p.setSyntaxHighlighting(diffPref.isSyntaxHighlighting());
p.setIntralineDifference(diffPref.isIntralineDifference());
p.setShowWhiteSpaceErrors(diffPref.isShowWhitespaceErrors());
p.setShowTabs(diffPref.isShowTabs());
}
return s;
}
} }

View File

@ -75,7 +75,7 @@ public class SideBySideTable extends AbstractPatchContentTable {
final ArrayList<PatchLine> lines = new ArrayList<PatchLine>(); final ArrayList<PatchLine> lines = new ArrayList<PatchLine>();
final SafeHtmlBuilder nc = new SafeHtmlBuilder(); final SafeHtmlBuilder nc = new SafeHtmlBuilder();
final boolean intraline = final boolean intraline =
script.getSettings().getPrettySettings().isIntralineDifference() script.getDiffPrefs().isIntralineDifference()
&& script.hasIntralineDifference(); && script.hasIntralineDifference();
appendHeader(script, nc); appendHeader(script, nc);

View File

@ -133,7 +133,7 @@ public class UnifiedDiffTable extends AbstractPatchContentTable {
} }
final boolean syntaxHighlighting = final boolean syntaxHighlighting =
script.getSettings().getPrettySettings().isSyntaxHighlighting(); script.getDiffPrefs().isSyntaxHighlighting();
final ArrayList<PatchLine> lines = new ArrayList<PatchLine>(); final ArrayList<PatchLine> lines = new ArrayList<PatchLine>();
for (final EditList.Hunk hunk : script.getHunks()) { for (final EditList.Hunk hunk : script.getHunks()) {
appendHunkHeader(nc, hunk); appendHunkHeader(nc, hunk);

View File

@ -20,11 +20,11 @@ import com.google.gerrit.common.data.ApprovalSummarySet;
import com.google.gerrit.common.data.ApprovalTypes; import com.google.gerrit.common.data.ApprovalTypes;
import com.google.gerrit.common.data.PatchDetailService; import com.google.gerrit.common.data.PatchDetailService;
import com.google.gerrit.common.data.PatchScript; import com.google.gerrit.common.data.PatchScript;
import com.google.gerrit.common.data.PatchScriptSettings;
import com.google.gerrit.common.errors.NoSuchEntityException; import com.google.gerrit.common.errors.NoSuchEntityException;
import com.google.gerrit.httpd.rpc.BaseServiceImplementation; import com.google.gerrit.httpd.rpc.BaseServiceImplementation;
import com.google.gerrit.httpd.rpc.Handler; import com.google.gerrit.httpd.rpc.Handler;
import com.google.gerrit.reviewdb.Account; import com.google.gerrit.reviewdb.Account;
import com.google.gerrit.reviewdb.AccountDiffPreference;
import com.google.gerrit.reviewdb.AccountPatchReview; import com.google.gerrit.reviewdb.AccountPatchReview;
import com.google.gerrit.reviewdb.ApprovalCategory; import com.google.gerrit.reviewdb.ApprovalCategory;
import com.google.gerrit.reviewdb.ApprovalCategoryValue; import com.google.gerrit.reviewdb.ApprovalCategoryValue;
@ -92,13 +92,13 @@ class PatchDetailServiceImpl extends BaseServiceImplementation implements
} }
public void patchScript(final Patch.Key patchKey, final PatchSet.Id psa, public void patchScript(final Patch.Key patchKey, final PatchSet.Id psa,
final PatchSet.Id psb, final PatchScriptSettings s, final PatchSet.Id psb, final AccountDiffPreference dp,
final AsyncCallback<PatchScript> callback) { final AsyncCallback<PatchScript> callback) {
if (psb == null) { if (psb == null) {
callback.onFailure(new NoSuchEntityException()); callback.onFailure(new NoSuchEntityException());
return; return;
} }
patchScriptFactoryFactory.create(patchKey, psa, psb, s).to(callback); patchScriptFactoryFactory.create(patchKey, psa, psb, dp).to(callback);
} }
public void saveDraft(final PatchLineComment comment, public void saveDraft(final PatchLineComment comment,

View File

@ -16,7 +16,6 @@ package com.google.gerrit.httpd.rpc.patch;
import com.google.gerrit.common.data.CommentDetail; import com.google.gerrit.common.data.CommentDetail;
import com.google.gerrit.common.data.PatchScript; import com.google.gerrit.common.data.PatchScript;
import com.google.gerrit.common.data.PatchScriptSettings;
import com.google.gerrit.common.data.PatchScript.DisplayMethod; import com.google.gerrit.common.data.PatchScript.DisplayMethod;
import com.google.gerrit.prettify.common.EditList; import com.google.gerrit.prettify.common.EditList;
import com.google.gerrit.prettify.common.SparseFileContent; import com.google.gerrit.prettify.common.SparseFileContent;
@ -66,7 +65,7 @@ class PatchScriptBuilder {
private Repository db; private Repository db;
private Change change; private Change change;
private PatchScriptSettings settings; private AccountDiffPreference diffPrefs;
private ObjectId aId; private ObjectId aId;
private ObjectId bId; private ObjectId bId;
@ -92,10 +91,10 @@ class PatchScriptBuilder {
this.change = c; this.change = c;
} }
void setSettings(final PatchScriptSettings s) { void setDiffPrefs(final AccountDiffPreference dp) {
settings = s; diffPrefs = dp;
context = settings.getContext(); context = diffPrefs.getContext();
if (context == AccountDiffPreference.WHOLE_FILE_CONTEXT) { if (context == AccountDiffPreference.WHOLE_FILE_CONTEXT) {
context = MAX_CONTEXT; context = MAX_CONTEXT;
} else if (context > MAX_CONTEXT) { } else if (context > MAX_CONTEXT) {
@ -117,7 +116,7 @@ class PatchScriptBuilder {
// //
return new PatchScript(change.getKey(), content.getChangeType(), content return new PatchScript(change.getKey(), content.getChangeType(), content
.getOldName(), content.getNewName(), content.getHeaderLines(), .getOldName(), content.getNewName(), content.getHeaderLines(),
settings, a.dst, b.dst, Collections.<Edit> emptyList(), diffPrefs, a.dst, b.dst, Collections.<Edit> emptyList(),
a.displayMethod, b.displayMethod, comments, history, false, false); a.displayMethod, b.displayMethod, comments, history, false, false);
} }
@ -150,24 +149,24 @@ class PatchScriptBuilder {
// IF the file is really large, we disable things to avoid choking // IF the file is really large, we disable things to avoid choking
// the browser client. // the browser client.
// //
settings.setContext(Math.min(25, context)); diffPrefs.setContext((short) Math.min(25, context));
settings.getPrettySettings().setSyntaxHighlighting(false); diffPrefs.setSyntaxHighlighting(false);
context = settings.getContext(); context = diffPrefs.getContext();
hugeFile = true; hugeFile = true;
} else if (settings.getPrettySettings().isSyntaxHighlighting()) { } else if (diffPrefs.isSyntaxHighlighting()) {
// In order to syntax highlight the file properly we need to // In order to syntax highlight the file properly we need to
// give the client the complete file contents. So force our // give the client the complete file contents. So force our
// context temporarily to the complete file size. // context temporarily to the complete file size.
// //
context = MAX_CONTEXT; context = MAX_CONTEXT;
} }
packContent(settings.getWhitespace() != Whitespace.IGNORE_NONE); packContent(diffPrefs.getIgnoreWhitespace() != Whitespace.IGNORE_NONE);
} }
return new PatchScript(change.getKey(), content.getChangeType(), content return new PatchScript(change.getKey(), content.getChangeType(), content
.getOldName(), content.getNewName(), content.getHeaderLines(), .getOldName(), content.getNewName(), content.getHeaderLines(),
settings, a.dst, b.dst, edits, a.displayMethod, b.displayMethod, diffPrefs, a.dst, b.dst, edits, a.displayMethod, b.displayMethod,
comments, history, hugeFile, intralineDifference); comments, history, hugeFile, intralineDifference);
} }

View File

@ -16,9 +16,9 @@ package com.google.gerrit.httpd.rpc.patch;
import com.google.gerrit.common.data.CommentDetail; import com.google.gerrit.common.data.CommentDetail;
import com.google.gerrit.common.data.PatchScript; import com.google.gerrit.common.data.PatchScript;
import com.google.gerrit.common.data.PatchScriptSettings;
import com.google.gerrit.httpd.rpc.Handler; import com.google.gerrit.httpd.rpc.Handler;
import com.google.gerrit.reviewdb.Account; import com.google.gerrit.reviewdb.Account;
import com.google.gerrit.reviewdb.AccountDiffPreference;
import com.google.gerrit.reviewdb.Change; import com.google.gerrit.reviewdb.Change;
import com.google.gerrit.reviewdb.Patch; import com.google.gerrit.reviewdb.Patch;
import com.google.gerrit.reviewdb.PatchLineComment; import com.google.gerrit.reviewdb.PatchLineComment;
@ -62,7 +62,7 @@ class PatchScriptFactory extends Handler<PatchScript> {
PatchScriptFactory create(Patch.Key patchKey, PatchScriptFactory create(Patch.Key patchKey,
@Assisted("patchSetA") PatchSet.Id patchSetA, @Assisted("patchSetA") PatchSet.Id patchSetA,
@Assisted("patchSetB") PatchSet.Id patchSetB, @Assisted("patchSetB") PatchSet.Id patchSetB,
PatchScriptSettings settings); AccountDiffPreference diffPrefs);
} }
private static final Logger log = private static final Logger log =
@ -79,7 +79,7 @@ class PatchScriptFactory extends Handler<PatchScript> {
@Nullable @Nullable
private final PatchSet.Id psa; private final PatchSet.Id psa;
private final PatchSet.Id psb; private final PatchSet.Id psb;
private final PatchScriptSettings settings; private final AccountDiffPreference diffPrefs;
private final PatchSet.Id patchSetId; private final PatchSet.Id patchSetId;
private final Change.Id changeId; private final Change.Id changeId;
@ -102,7 +102,7 @@ class PatchScriptFactory extends Handler<PatchScript> {
@Assisted final Patch.Key patchKey, @Assisted final Patch.Key patchKey,
@Assisted("patchSetA") @Nullable final PatchSet.Id patchSetA, @Assisted("patchSetA") @Nullable final PatchSet.Id patchSetA,
@Assisted("patchSetB") final PatchSet.Id patchSetB, @Assisted("patchSetB") final PatchSet.Id patchSetB,
@Assisted final PatchScriptSettings settings) { @Assisted final AccountDiffPreference diffPrefs) {
this.repoManager = grm; this.repoManager = grm;
this.builderFactory = builderFactory; this.builderFactory = builderFactory;
this.patchListCache = patchListCache; this.patchListCache = patchListCache;
@ -113,7 +113,7 @@ class PatchScriptFactory extends Handler<PatchScript> {
this.patchKey = patchKey; this.patchKey = patchKey;
this.psa = patchSetA; this.psa = patchSetA;
this.psb = patchSetB; this.psb = patchSetB;
this.settings = settings; this.diffPrefs = diffPrefs;
patchSetId = patchKey.getParentKey(); patchSetId = patchKey.getParentKey();
changeId = patchSetId.getParentKey(); changeId = patchSetId.getParentKey();
@ -143,7 +143,7 @@ class PatchScriptFactory extends Handler<PatchScript> {
throw new NoSuchChangeException(changeId, e); throw new NoSuchChangeException(changeId, e);
} }
try { try {
final PatchList list = listFor(keyFor(settings.getWhitespace())); final PatchList list = listFor(keyFor(diffPrefs.getIgnoreWhitespace()));
final boolean intraline = list.hasIntralineDifference(); final boolean intraline = list.hasIntralineDifference();
final PatchScriptBuilder b = newBuilder(list, git); final PatchScriptBuilder b = newBuilder(list, git);
final PatchListEntry content = list.get(patchKey.getFileName()); final PatchListEntry content = list.get(patchKey.getFileName());
@ -172,11 +172,11 @@ class PatchScriptFactory extends Handler<PatchScript> {
} }
private PatchScriptBuilder newBuilder(final PatchList list, Repository git) { private PatchScriptBuilder newBuilder(final PatchList list, Repository git) {
final PatchScriptSettings s = new PatchScriptSettings(settings); final AccountDiffPreference dp = new AccountDiffPreference(diffPrefs);
final PatchScriptBuilder b = builderFactory.get(); final PatchScriptBuilder b = builderFactory.get();
b.setRepository(git); b.setRepository(git);
b.setChange(change); b.setChange(change);
b.setSettings(s); b.setDiffPrefs(dp);
b.setTrees(list.getOldId(), list.getNewId()); b.setTrees(list.getOldId(), list.getNewId());
return b; return b;
} }

View File

@ -44,6 +44,12 @@ limitations under the License.
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.google.gerrit</groupId>
<artifactId>gerrit-reviewdb</artifactId>
<version>${project.version}</version>
</dependency>
<dependency> <dependency>
<groupId>com.google.gwt</groupId> <groupId>com.google.gwt</groupId>
<artifactId>gwt-user</artifactId> <artifactId>gwt-user</artifactId>

View File

@ -52,7 +52,7 @@ public class ClientSideFormatter extends PrettyFormatter {
@Override @Override
protected String prettify(String html, String type) { protected String prettify(String html, String type) {
return go(prettify.getContext(), html, type, settings.getTabSize()); return go(prettify.getContext(), html, type, diffPrefs.getTabSize());
} }
private static native String go(JavaScriptObject ctx, String srcText, private static native String go(JavaScriptObject ctx, String srcText,

View File

@ -14,6 +14,7 @@
package com.google.gerrit.prettify.common; package com.google.gerrit.prettify.common;
import com.google.gerrit.reviewdb.AccountDiffPreference;
import com.google.gwtexpui.safehtml.client.SafeHtml; import com.google.gwtexpui.safehtml.client.SafeHtml;
import com.google.gwtexpui.safehtml.client.SafeHtmlBuilder; import com.google.gwtexpui.safehtml.client.SafeHtmlBuilder;
@ -71,7 +72,8 @@ public abstract class PrettyFormatter implements SparseHtmlFile {
protected SparseFileContent content; protected SparseFileContent content;
protected EditFilter side; protected EditFilter side;
protected List<Edit> edits; protected List<Edit> edits;
protected PrettySettings settings; protected AccountDiffPreference diffPrefs;
protected String fileName;
protected Set<Integer> trailingEdits; protected Set<Integer> trailingEdits;
private int col; private int col;
@ -105,8 +107,12 @@ public abstract class PrettyFormatter implements SparseHtmlFile {
edits = all; edits = all;
} }
public void setPrettySettings(PrettySettings how) { public void setDiffPrefs(AccountDiffPreference how) {
settings = how; diffPrefs = how;
}
public void setFileName(String fileName) {
this.fileName = fileName;
} }
/** /**
@ -122,7 +128,7 @@ public abstract class PrettyFormatter implements SparseHtmlFile {
String html = toHTML(src); String html = toHTML(src);
if (settings.isSyntaxHighlighting() && getFileType() != null if (diffPrefs.isSyntaxHighlighting() && getFileType() != null
&& src.isWholeFile()) { && src.isWholeFile()) {
// The prettify parsers don't like &#39; as an entity for the // The prettify parsers don't like &#39; as an entity for the
// single quote character. Replace them all out so we don't // single quote character. Replace them all out so we don't
@ -205,7 +211,7 @@ public abstract class PrettyFormatter implements SparseHtmlFile {
cleanText(txt, pos, start); cleanText(txt, pos, start);
pos = txt.indexOf(';', start + 1) + 1; pos = txt.indexOf(';', start + 1) + 1;
if (settings.getLineLength() <= col) { if (diffPrefs.getLineLength() <= col) {
buf.append("<br />"); buf.append("<br />");
col = 0; col = 0;
} }
@ -219,14 +225,14 @@ public abstract class PrettyFormatter implements SparseHtmlFile {
private void cleanText(String txt, int pos, int end) { private void cleanText(String txt, int pos, int end) {
while (pos < end) { while (pos < end) {
int free = settings.getLineLength() - col; int free = diffPrefs.getLineLength() - col;
if (free <= 0) { if (free <= 0) {
// The current line is full. Throw an explicit line break // The current line is full. Throw an explicit line break
// onto the end, and we'll continue on the next line. // onto the end, and we'll continue on the next line.
// //
buf.append("<br />"); buf.append("<br />");
col = 0; col = 0;
free = settings.getLineLength(); free = diffPrefs.getLineLength();
} }
int n = Math.min(end - pos, free); int n = Math.min(end - pos, free);
@ -305,7 +311,7 @@ public abstract class PrettyFormatter implements SparseHtmlFile {
private String toHTML(SparseFileContent src) { private String toHTML(SparseFileContent src) {
SafeHtml html; SafeHtml html;
if (settings.isIntralineDifference()) { if (diffPrefs.isIntralineDifference()) {
html = colorLineEdits(src); html = colorLineEdits(src);
} else { } else {
SafeHtmlBuilder b = new SafeHtmlBuilder(); SafeHtmlBuilder b = new SafeHtmlBuilder();
@ -321,7 +327,7 @@ public abstract class PrettyFormatter implements SparseHtmlFile {
html = html.replaceAll("\r([^\n])", r); html = html.replaceAll("\r([^\n])", r);
} }
if (settings.isShowWhiteSpaceErrors()) { if (diffPrefs.isShowWhitespaceErrors()) {
// We need to do whitespace errors before showing tabs, because // We need to do whitespace errors before showing tabs, because
// these patterns rely on \t as a literal, before it expands. // these patterns rely on \t as a literal, before it expands.
// //
@ -329,8 +335,8 @@ public abstract class PrettyFormatter implements SparseHtmlFile {
html = showTrailingWhitespace(html); html = showTrailingWhitespace(html);
} }
if (settings.isShowTabs()) { if (diffPrefs.isShowTabs()) {
String t = 1 < settings.getTabSize() ? "\t" : ""; String t = 1 < diffPrefs.getTabSize() ? "\t" : "";
html = html.replaceAll("\t", "<span class=\"vt\">\u00BB</span>" + t); html = html.replaceAll("\t", "<span class=\"vt\">\u00BB</span>" + t);
} }
@ -496,17 +502,17 @@ public abstract class PrettyFormatter implements SparseHtmlFile {
private String expandTabs(String html) { private String expandTabs(String html) {
StringBuilder tmp = new StringBuilder(); StringBuilder tmp = new StringBuilder();
int i = 0; int i = 0;
if (settings.isShowTabs()) { if (diffPrefs.isShowTabs()) {
i = 1; i = 1;
} }
for (; i < settings.getTabSize(); i++) { for (; i < diffPrefs.getTabSize(); i++) {
tmp.append("&nbsp;"); tmp.append("&nbsp;");
} }
return html.replaceAll("\t", tmp.toString()); return html.replaceAll("\t", tmp.toString());
} }
private String getFileType() { private String getFileType() {
String srcType = settings.getFilename(); String srcType = fileName;
if (srcType == null) { if (srcType == null) {
return null; return null;
} }

View File

@ -1,106 +0,0 @@
// Copyright (C) 2010 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.
package com.google.gerrit.prettify.common;
/** Settings to configure a {@link PrettyFormatter}. */
public class PrettySettings {
protected String fileName;
protected boolean showWhiteSpaceErrors;
protected int lineLength;
protected int tabSize;
protected boolean showTabs;
protected boolean syntaxHighlighting;
protected boolean intralineDifference;
public PrettySettings() {
showWhiteSpaceErrors = true;
lineLength = 100;
tabSize = 8;
showTabs = true;
syntaxHighlighting = true;
intralineDifference = true;
}
public PrettySettings(PrettySettings pretty) {
fileName = pretty.fileName;
showWhiteSpaceErrors = pretty.showWhiteSpaceErrors;
lineLength = pretty.lineLength;
tabSize = pretty.tabSize;
showTabs = pretty.showTabs;
syntaxHighlighting = pretty.syntaxHighlighting;
intralineDifference = pretty.intralineDifference;
}
public String getFilename() {
return fileName;
}
public PrettySettings setFileName(final String name) {
fileName = name;
return this;
}
public boolean isShowWhiteSpaceErrors() {
return showWhiteSpaceErrors;
}
public PrettySettings setShowWhiteSpaceErrors(final boolean show) {
showWhiteSpaceErrors = show;
return this;
}
public int getLineLength() {
return lineLength;
}
public PrettySettings setLineLength(final int len) {
lineLength = len;
return this;
}
public int getTabSize() {
return tabSize;
}
public PrettySettings setTabSize(final int len) {
tabSize = len;
return this;
}
public boolean isShowTabs() {
return showTabs;
}
public PrettySettings setShowTabs(final boolean show) {
showTabs = show;
return this;
}
public boolean isSyntaxHighlighting() {
return syntaxHighlighting;
}
public void setSyntaxHighlighting(final boolean on) {
syntaxHighlighting = on;
}
public boolean isIntralineDifference() {
return intralineDifference;
}
public void setIntralineDifference(final boolean on) {
intralineDifference = on;
}
}

View File

@ -103,6 +103,18 @@ public class AccountDiffPreference {
this.accountId = accountId; this.accountId = accountId;
} }
public AccountDiffPreference(AccountDiffPreference p) {
this.accountId = p.accountId;
this.ignoreWhitespace = p.ignoreWhitespace;
this.tabSize = p.tabSize;
this.lineLength = p.lineLength;
this.syntaxHighlighting = p.syntaxHighlighting;
this.showWhitespaceErrors = p.showWhitespaceErrors;
this.intralineDifference = p.intralineDifference;
this.showTabs = p.showTabs;
this.context = p.context;
}
public Account.Id getAccountId() { public Account.Id getAccountId() {
return accountId; return accountId;
} }
@ -169,7 +181,8 @@ public class AccountDiffPreference {
} }
/** Set the number of lines of context when viewing a patch. */ /** Set the number of lines of context when viewing a patch. */
public void setContext(final short s) { public void setContext(final short context) {
context = s; assert 0 <= context || context == WHOLE_FILE_CONTEXT;
this.context = context;
} }
} }