Merge "Add lines count parameter to the inlineComments function."

This commit is contained in:
Shawn Pearce
2012-06-05 22:05:38 -07:00
committed by gerrit code review
3 changed files with 69 additions and 8 deletions

View File

@@ -78,6 +78,10 @@ public class CommentSender extends ReplyToChangeSender {
}
public String getInlineComments() {
return getInlineComments(1);
}
public String getInlineComments(int lines) {
StringBuilder cmts = new StringBuilder();
final Repository repo = getRepository();
@@ -121,19 +125,29 @@ public class CommentSender extends ReplyToChangeSender {
}
}
cmts.append("Line " + lineNbr);
if (currentFileData != null) {
int maxLines;
try {
final String lineStr = currentFileData.getLine(side, lineNbr);
cmts.append(": ");
cmts.append(lineStr);
} catch (Throwable cce) {
// Don't quote the line if we can't safely convert it.
maxLines = currentFileData.getLineCount(side);
} catch (Throwable e) {
maxLines = lineNbr;
}
final int startLine = Math.max(1, lineNbr - lines + 1);
final int stopLine = Math.min(maxLines, lineNbr + lines);
for (int line = startLine; line <= lineNbr; ++line) {
appendFileLine(cmts, currentFileData, side, line);
}
cmts.append("\n");
cmts.append(c.getMessage().trim());
cmts.append("\n");
for (int line = lineNbr + 1; line < stopLine; ++line) {
appendFileLine(cmts, currentFileData, side, line);
}
}
cmts.append("\n\n");
}
} finally {
@@ -144,6 +158,18 @@ public class CommentSender extends ReplyToChangeSender {
return cmts.toString();
}
private void appendFileLine(StringBuilder cmts, PatchFile fileData, short side, int line) {
cmts.append("Line " + line);
try {
final String lineStr = fileData.getLine(side, line);
cmts.append(": ");
cmts.append(lineStr);
} catch (Throwable e) {
// Don't quote the line if we can't safely convert it.
}
cmts.append("\n");
}
private Repository getRepository() {
try {
return args.server.openRepository(projectState.getProject().getNameKey());

View File

@@ -111,6 +111,35 @@ public class PatchFile {
}
}
/**
* Return number of lines in file.
*
* @param file the file index to extract.
* @return number of lines in file.
* @throws CorruptEntityException the patch cannot be read.
* @throws IOException the patch or complete file content cannot be read.
* @throws NoSuchEntityException the file is not exist.
*/
public int getLineCount(final int file)
throws CorruptEntityException, IOException, NoSuchEntityException {
switch (file) {
case 0:
if (a == null) {
a = load(aTree, entry.getOldName());
}
return a.size();
case 1:
if (b == null) {
b = load(bTree, entry.getNewName());
}
return b.size();
default:
throw new NoSuchEntityException();
}
}
private Text load(final ObjectId tree, final String path)
throws MissingObjectException, IncorrectObjectTypeException,
CorruptObjectException, IOException {

View File

@@ -43,5 +43,11 @@ Change subject: $change.subject
$email.coverLetter
#end
##
## It is possible to increase the span of the quoted lines by using the line
## count parameter when calling $email.inlineComments as a function.
##
## Example: #if($email.inlineComments)$email.getInlineComments(5)#end
##
#if($email.inlineComments)$email.inlineComments#end
#end