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

@@ -52,7 +52,7 @@ public class ClientSideFormatter extends PrettyFormatter {
@Override
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,

View File

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