-
Notifications
You must be signed in to change notification settings - Fork 1
#[222] No info to user about invalid regex #253
Changes from 8 commits
51dcc95
2fb2903
4b25da4
8424a36
055491d
4fccd3a
06b1d8c
da623ec
43ea7c1
61bc938
0662fe1
226c345
d1c9125
4fa55fa
5ae9c6c
9bcc0f6
71e262a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,35 +9,66 @@ import cats.syntax.option._ | |
| import codesearch.core.config.{Config, SnippetConfig} | ||
| import codesearch.core.index.directory.СindexDirectory | ||
| import codesearch.core.index.repository.Extensions | ||
| import codesearch.core.search.Search.{CSearchPage, CSearchResult, CodeSnippet, Package, PackageResult, snippetConfig} | ||
| import codesearch.core.regex.PreciseMatch | ||
| import codesearch.core.regex.space.SpaceInsensitiveString | ||
| import codesearch.core.search.Search.{CSearchResult, CodeSnippet, Package, PackageResult, snippetConfig} | ||
| import codesearch.core.search.SnippetsGrouper.SnippetInfo | ||
| import codesearch.core.util.Helper.readFileAsync | ||
| import fs2.{Pipe, Stream} | ||
| import io.chrisdavenport.log4cats.SelfAwareStructuredLogger | ||
| import io.chrisdavenport.log4cats.slf4j.Slf4jLogger | ||
| import codesearch.core.regex.RegexConstructor | ||
|
|
||
| import scala.sys.process.Process | ||
| 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] = { | ||
| for { | ||
| lines <- csearch(request) | ||
| results <- Stream | ||
| .emits(lines) | ||
| .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), lines.size) | ||
| checkRegexpForValid(request.query) match { | ||
| 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) => { | ||
|
Kabowyad marked this conversation as resolved.
Outdated
|
||
| val message = exception.getMessage | ||
| IO( | ||
| CSearchPage(Seq.empty[Search.PackageResult], | ||
| 0, | ||
| ErrorResponse(message.substring(0, 1).toUpperCase + message.substring(1, message.size)))) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Does Scala have a linter that catches those things? (Also it's
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @neongreen
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant a linter that we can run during CI. I have just tried to run SonarQube on our code, but it didn't find the |
||
| } | ||
| } | ||
| } | ||
|
|
||
| private def csearch(request: SearchRequest): SearchByIndexResult = { | ||
| val indexDir = cindexDir.indexDirAs[String] | ||
| val env = ("CSEARCHINDEX", indexDir) | ||
| var stderr = new String | ||
| val log = ProcessLogger((o: String) => o, (e: String) => stderr = e) | ||
| val test = for { | ||
| _ <- logger.debug(s"running CSEARCHINDEX=$indexDir ${arguments(request).mkString(" ")}") | ||
| results <- IO((Process(arguments(request), None, env) #| Seq("head", "-1001")).lineStream_!(log).toList) | ||
| } yield SearchByIndexResult(results, ErrorResponse(stderr)) | ||
| test.unsafeRunSync() | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -65,16 +96,6 @@ trait Search { | |
| */ | ||
| protected def buildRepUrl(packageName: String, version: String): String | ||
|
|
||
| private def csearch(request: SearchRequest): IO[List[String]] = { | ||
| val indexDir = cindexDir.indexDirAs[String] | ||
| val env = ("CSEARCHINDEX", indexDir) | ||
|
|
||
| for { | ||
| _ <- logger.debug(s"running CSEARCHINDEX=$indexDir ${arguments(request).mkString(" ")}") | ||
| results <- IO((Process(arguments(request), None, env) #| Seq("head", "-1001")).lineStream.toList) | ||
| } yield results | ||
| } | ||
|
|
||
| private def arguments(request: SearchRequest): List[String] = { | ||
| def extensionsRegex: String = extensions.sourceExtensions.mkString(".*\\.(", "|", ")$") | ||
|
|
||
|
|
@@ -83,8 +104,10 @@ trait Search { | |
| case None => if (request.sourcesOnly) extensionsRegex else ".*" | ||
| } | ||
|
|
||
| val query: String = | ||
| RegexConstructor(request.query, request.insensitive, request.spaceInsensitive, request.preciseMatch) | ||
| val query: String = { | ||
| val preciseMatch: String = if (request.preciseMatch) PreciseMatch(request.query) else request.query | ||
| if (request.spaceInsensitive) SpaceInsensitiveString(preciseMatch) else preciseMatch | ||
| } | ||
|
|
||
| request.filter match { | ||
| case Some(filter) => List("csearch", "-n", "-f", forExtensions, query, filter) | ||
|
|
@@ -150,11 +173,6 @@ object Search { | |
| * @param data code snippets grouped by package | ||
| * @param total number of total matches | ||
| */ | ||
| final case class CSearchPage( | ||
| data: Seq[PackageResult], | ||
| total: Int | ||
| ) | ||
|
|
||
| /** | ||
| * | ||
| * @param relativePath path into package sources | ||
|
|
@@ -200,3 +218,11 @@ object Search { | |
| result: CodeSnippet | ||
| ) | ||
| } | ||
| sealed trait Response | ||
| final case class SearchByIndexResult(lists: List[String], error: ErrorResponse) | ||
|
Kabowyad marked this conversation as resolved.
|
||
| final case class ErrorResponse(message: String) extends Response | ||
| final case class CSearchPage( | ||
|
Kabowyad marked this conversation as resolved.
|
||
| data: Seq[PackageResult], | ||
| total: Int, | ||
| error: ErrorResponse | ||
|
Kabowyad marked this conversation as resolved.
Outdated
|
||
| ) extends Response | ||

Uh oh!
There was an error while loading. Please reload this page.