Skip to content
This repository was archived by the owner on Dec 11, 2019. It is now read-only.
38 changes: 23 additions & 15 deletions core/src/main/scala/codesearch/core/search/Search.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,37 @@ import io.chrisdavenport.log4cats.SelfAwareStructuredLogger
import io.chrisdavenport.log4cats.slf4j.Slf4jLogger

import scala.sys.process.{Process, ProcessLogger}
import com.google.re2j._

import scala.util.{Success, Try}

trait Search {

protected def cindexDir: СindexDirectory
protected def extensions: Extensions
protected val logger: SelfAwareStructuredLogger[IO] = Slf4jLogger.unsafeCreate[IO]

def checkRegexpForValid(regexp: String): Try[Pattern] = {
Try(Pattern.compile(regexp))
}

def search(request: SearchRequest): IO[CSearchPage] = {
val entity = csearch(request)
if (entity.error.message.isEmpty) {
for {
results <- Stream
.emits(entity.lists)
.through(SnippetsGrouper.groupLines(snippetConfig))
.drop(snippetConfig.pageSize * (request.page - 1))
.take(snippetConfig.pageSize)
.evalMap(createSnippet)
.through(groupByPackage)
.compile
.toList
} yield CSearchPage(results.sortBy(_.pack.name), entity.lists.size, ErrorResponse(""))
} else {
IO(CSearchPage(Seq.empty[Search.PackageResult], 0, entity.error))
checkRegexpForValid(request.query) match {
Comment thread
Kabowyad marked this conversation as resolved.
Outdated
case Success(_) => {
val entity = csearch(request)
for {
results <- Stream
.emits(entity.lists)
.through(SnippetsGrouper.groupLines(snippetConfig))
.drop(snippetConfig.pageSize * (request.page - 1))
.take(snippetConfig.pageSize)
.evalMap(createSnippet)
.through(groupByPackage)
.compile
.toList
} yield CSearchPage(results.sortBy(_.pack.name), entity.lists.size, ErrorResponse(""))
}
case scala.util.Failure(exception) => IO(CSearchPage(Seq.empty[Search.PackageResult], 0, ErrorResponse(exception.getMessage)))
}
}

Expand Down
3 changes: 2 additions & 1 deletion project/Builder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ object Builder {
"org.codehaus.janino" % "janino" % "3.0.11",
"com.typesafe.play" %% "play-json" % "2.6.9",
"com.github.mpilquist" %% "simulacrum" % "0.13.0",
"org.typelevel" %% "cats-core" % "1.2.0"
"org.typelevel" %% "cats-core" % "1.2.0",
"com.google.re2j" % "re2j" % "1.2"
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ trait SearchController[V <: DefaultTable] { self: InjectedController =>

db.updated.flatMap { updated =>
searchEngine.search(searchRequest) map {
case CSearchPage(results, total, errorMessage) =>
case CSearchPage(results, total, errorResponse) =>
Ok(
views.html.searchResults(
updated = TimeAgo.using(updated.getTime),
Expand All @@ -66,7 +66,7 @@ trait SearchController[V <: DefaultTable] { self: InjectedController =>
totalMatches = total,
callURI = searchRequest.callURI(host).toString,
lang = lang,
errorMessageT = errorMessage
errorMessageT = errorResponse.message
)
)
} unsafeToFuture
Expand Down
5 changes: 3 additions & 2 deletions web-server/app/views/searchBox.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
<div class="col-lg-6 mx-auto p-3 h7 justify-content-center" style="position: relative;">
<form>
<section class="warning warning-query">Please provide a string to search for.</section>
<section class="error error-query">You have error @{errorMessageT}</section>
<section class="error error-hide">@{errorMessageT}</section>
@if(errorMessageT.nonEmpty) {
<section class="error error-query" style="color:red;">⚠️@{errorMessageT}</section>
}
<div class="input-group mb-2">
<input type="text" class="form-control" name="query" value="@query" class="query">
<div class="input-group-append">
Expand Down