Skip to content

Commit

Permalink
(refs #3)Search by AND if query words are separated by whitespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
takezoe committed Jul 17, 2013
1 parent 4796d7f commit 2767052
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
19 changes: 11 additions & 8 deletions src/main/scala/app/IndexController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,24 @@ 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>")
list.append((treeWalk.getPathString, highlightText))
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))
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/scala/util/StringUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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;")

}
24 changes: 13 additions & 11 deletions src/main/twirl/search/code.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
@import context._
@import view.helpers._
@html.main("Search Results", Some(repository)){
@if(files.isEmpty){
<h4>We couldn't find any code matching '@query'</h4>
} else {
<h4>We've found @files.size code @plural(files.size, "result")</h4>
}
@files.map { file =>
<div>
<div><a href="@url(repository)/blob/@repository.repository.defaultBranch/@file.path">@file.path</a></div>
<div class="muted">@datetime(file.lastModified)</div>
<pre>@Html(file.highlightText)</pre>
</div>
@menu("code", query, repository){
@if(files.isEmpty){
<h4>We couldn't find any code matching '@query'</h4>
} else {
<h4>We've found @files.size code @plural(files.size, "result")</h4>
}
@files.map { file =>
<div>
<div><a href="@url(repository)/blob/@repository.repository.defaultBranch/@file.path">@file.path</a></div>
<div class="muted">@datetime(file.lastModified)</div>
<pre>@Html(file.highlightText)</pre>
</div>
}
}
}
21 changes: 21 additions & 0 deletions src/main/twirl/search/menu.scala.html
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit 2767052

Please sign in to comment.