(refs #3)Improve presentation for code search results.

This commit is contained in:
takezoe
2013-07-17 21:13:53 +09:00
parent 900e91e101
commit d46e90dcdb
2 changed files with 28 additions and 15 deletions

View File

@@ -40,11 +40,13 @@ trait IndexControllerBase extends ControllerBase { self: RepositoryService
} }
get("/:owner/:repository/search")(referrersOnly { repository => get("/:owner/:repository/search")(referrersOnly { repository =>
val query = params("q") val query = params("q").trim
val target = params.getOrElse("type", "code") val target = params.getOrElse("type", "code")
target.toLowerCase match { target.toLowerCase match {
case "issue" => { case "issue" => if(query.isEmpty){
search.html.issues(Nil, "", repository)
} else {
search.html.issues(queryIssues(repository.owner, repository.name, query).map { case (issue, commentCount, content) => search.html.issues(queryIssues(repository.owner, repository.name, query).map { case (issue, commentCount, content) =>
IssueSearchResult( IssueSearchResult(
issue.issueId, issue.issueId,
@@ -52,12 +54,13 @@ trait IndexControllerBase extends ControllerBase { self: RepositoryService
issue.openedUserName, issue.openedUserName,
issue.registeredDate, issue.registeredDate,
commentCount, commentCount,
getHighlightText(content, query)) getHighlightText(content, query)._1)
}, query, repository) }, query, repository)
} }
case _ => { case _ => if(query.isEmpty){
// TODO move to JGitUtil? // TODO move to JGitUtil?
search.html.code(Nil, "", repository)
} else {
JGitUtil.withGit(getRepositoryDir(repository.owner, repository.name)){ git => JGitUtil.withGit(getRepositoryDir(repository.owner, repository.name)){ git =>
val revWalk = new RevWalk(git.getRepository) val revWalk = new RevWalk(git.getRepository)
val objectId = git.getRepository.resolve("HEAD") val objectId = git.getRepository.resolve("HEAD")
@@ -67,7 +70,7 @@ trait IndexControllerBase extends ControllerBase { self: RepositoryService
treeWalk.addTree(revCommit.getTree) treeWalk.addTree(revCommit.getTree)
val lowerQueries = StringUtil.splitWords(query.toLowerCase) val lowerQueries = StringUtil.splitWords(query.toLowerCase)
val list = new ListBuffer[(String, String)] val list = new ListBuffer[(String, (String, Int))]
while (treeWalk.next()) { while (treeWalk.next()) {
if(treeWalk.getFileMode(0) != FileMode.TREE){ if(treeWalk.getFileMode(0) != FileMode.TREE){
JGitUtil.getContent(git, treeWalk.getObjectId(0), false).foreach { bytes => JGitUtil.getContent(git, treeWalk.getObjectId(0), false).foreach { bytes =>
@@ -88,32 +91,42 @@ trait IndexControllerBase extends ControllerBase { self: RepositoryService
val commits = JGitUtil.getLatestCommitFromPaths(git, list.toList.map(_._1), "HEAD") val commits = JGitUtil.getLatestCommitFromPaths(git, list.toList.map(_._1), "HEAD")
search.html.code(list.toList.map { case (path, highlightText) => search.html.code(list.toList.map { case (path, highlightText) =>
FileSearchResult(path, commits(path).getCommitterIdent.getWhen, highlightText) FileSearchResult(path, commits(path).getCommitterIdent.getWhen, highlightText._1, highlightText._2)
}, query, repository) }, query, repository)
} }
} }
} }
}) })
private def getHighlightText(content: String, query: String): String = { private def getHighlightText(content: String, query: String): (String, Int) = {
val lowerQueries = StringUtil.splitWords(query.toLowerCase) val lowerQueries = StringUtil.splitWords(query.toLowerCase)
val lowerText = content.toLowerCase val lowerText = content.toLowerCase
val indices = lowerQueries.map(lowerText.indexOf _) val indices = lowerQueries.map(lowerText.indexOf _)
if(!indices.exists(_ < 0)){ if(!indices.exists(_ < 0)){
val lineNumber = content.substring(0, indices.min).split("\n").size - 1 val lineNumber = content.substring(0, indices.min).split("\n").size - 1
StringUtil.escapeHtml(content.split("\n").drop(lineNumber).take(5).mkString("\n")) val highlightText = StringUtil.escapeHtml(content.split("\n").drop(lineNumber).take(5).mkString("\n"))
.replaceAll("(?i)(" + lowerQueries.map("\\Q" + _ + "\\E").mkString("|") + ")", .replaceAll("(?i)(" + lowerQueries.map("\\Q" + _ + "\\E").mkString("|") + ")",
"<span style=\"background-color: yellow;\">$1</span>") "<span style=\"background-color: yellow;\">$1</span>")
(highlightText, lineNumber + 1)
} else { } else {
content.split("\n").take(5).mkString("\n") (content.split("\n").take(5).mkString("\n"), 1)
} }
} }
} }
case class IssueSearchResult(issueId: Int, title: String, openedUserName: String, registeredDate: java.util.Date, case class IssueSearchResult(
commentCount: Int, highlightText: String) issueId: Int,
title: String,
openedUserName: String,
registeredDate: java.util.Date,
commentCount: Int,
highlightText: String)
case class FileSearchResult(path: String, lastModified: java.util.Date, highlightText: String) case class FileSearchResult(
path: String,
lastModified: java.util.Date,
highlightText: String,
highlightLineNumber: Int)

View File

@@ -11,8 +11,8 @@
@files.map { file => @files.map { file =>
<div> <div>
<h5><a href="@url(repository)/blob/@repository.repository.defaultBranch/@file.path">@file.path</a></h5> <h5><a href="@url(repository)/blob/@repository.repository.defaultBranch/@file.path">@file.path</a></h5>
<div class="muted">@datetime(file.lastModified)</div> <div class="small muted">Latest commit at @datetime(file.lastModified)</div>
<pre>@Html(file.highlightText)</pre> <pre class="prettyprint linenums:@file.highlightLineNumber" style="padding-left: 25px;">@Html(file.highlightText)</pre>
</div> </div>
} }
} }