(refs #3)Search by AND if query words are separated by whitespace.

This commit is contained in:
takezoe
2013-07-17 11:52:28 +09:00
parent 4796d7f450
commit 27670525a3
4 changed files with 48 additions and 19 deletions

View File

@@ -62,19 +62,22 @@ trait IndexControllerBase extends ControllerBase { self: RepositoryService
treeWalk.setRecursive(true)
treeWalk.addTree(revCommit.getTree)
val lowerQuery = query.toLowerCase
val lowerQueries = query.toLowerCase.split("[ \\t ]+")
val list = new ListBuffer[(String, String)]
while (treeWalk.next()) {
if(treeWalk.getFileMode(0) != FileMode.TREE){
JGitUtil.getContent(git, treeWalk.getObjectId(0), false).foreach { bytes =>
if(FileUtil.isText(bytes)){
val text = new String(bytes, "UTF-8")
val index = text.toLowerCase.indexOf(lowerQuery)
if(index >= 0){
val lineNumber = text.substring(0, index).split("\n").size - 1
val highlightText = text.split("\n").drop(lineNumber).take(5).mkString("\n")
.replace("&", "&amp;").replace("<", "&gt;").replace(">", "&gt;").replace("\"", "&quot;")
.replaceAll("(?i)(\\Q" + query + "\\E)", "<span style=\"background-color: yellow;\">$1</span>")
val lowerText = text.toLowerCase
val indices = lowerQueries.map { lowerQuery =>
lowerText.indexOf(lowerQuery)
}
if(!indices.exists(_ < 0)){
val lineNumber = text.substring(0, indices.min).split("\n").size - 1
val highlightText = StringUtil.escapeHtml(text.split("\n").drop(lineNumber).take(5).mkString("\n"))
.replaceAll("(?i)(" + lowerQueries.map("\\Q" + _ + "\\E").mkString("|") + ")",
"<span style=\"background-color: yellow;\">$1</span>")
list.append((treeWalk.getPathString, highlightText))
}
}

View File

@@ -20,4 +20,7 @@ object StringUtil {
def urlDecode(value: String): String = URLDecoder.decode(value, "UTF-8")
def escapeHtml(value: String): String =
value.replace("&", "&amp;").replace("<", "&gt;").replace(">", "&gt;").replace("\"", "&quot;")
}

View File

@@ -2,6 +2,7 @@
@import context._
@import view.helpers._
@html.main("Search Results", Some(repository)){
@menu("code", query, repository){
@if(files.isEmpty){
<h4>We couldn't find any code matching '@query'</h4>
} else {
@@ -14,4 +15,5 @@
<pre>@Html(file.highlightText)</pre>
</div>
}
}
}

View File

@@ -0,0 +1,21 @@
@(active: String, query: String, repository: service.RepositoryService.RepositoryInfo)(body: Html)(implicit context: app.Context)
@import context._
@import view.helpers._
@html.header("", repository)
<div class="row-fluid">
<div class="span3">
<div class="box">
<ul class="nav nav-tabs nav-stacked side-menu">
<li@if(active=="code"){ class="active"}>
<a href="@url(repository)/search?q=@urlEncode(query)&type=Code">Code</a>
</li>
<li@if(active=="issue"){ class="active"}>
<a href="@url(repository)/search?q=@urlEncode(query)&type=Issue">Issue</a>
</li>
</ul>
</div>
</div>
<div class="span9">
@body
</div>
</div>