From 3c45cf8494fcd29e1a99b0ee6f253808eb607053 Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Thu, 20 Feb 2020 21:53:55 +0200 Subject: Add detected file language to code search (#10256) Move langauge detection to separate module to be more reusable Add option to disable vendored file exclusion from file search Allways show all language stats for search --- routers/home.go | 13 +++++++++---- routers/repo/search.go | 10 +++++++--- 2 files changed, 16 insertions(+), 7 deletions(-) (limited to 'routers') diff --git a/routers/home.go b/routers/home.go index 77a7e153e2..1b8c4cd17b 100644 --- a/routers/home.go +++ b/routers/home.go @@ -290,6 +290,7 @@ func ExploreCode(ctx *context.Context) { ctx.Data["PageIsExplore"] = true ctx.Data["PageIsExploreCode"] = true + language := strings.TrimSpace(ctx.Query("l")) keyword := strings.TrimSpace(ctx.Query("q")) page := ctx.QueryInt("page") if page <= 0 { @@ -317,8 +318,9 @@ func ExploreCode(ctx *context.Context) { } var ( - total int - searchResults []*code_indexer.Result + total int + searchResults []*code_indexer.Result + searchResultLanguages []*code_indexer.SearchResultLanguages ) // if non-admin login user, we need check UnitTypeCode at first @@ -340,14 +342,14 @@ func ExploreCode(ctx *context.Context) { ctx.Data["RepoMaps"] = rightRepoMap - total, searchResults, err = code_indexer.PerformSearch(repoIDs, keyword, page, setting.UI.RepoSearchPagingNum) + total, searchResults, searchResultLanguages, err = code_indexer.PerformSearch(repoIDs, language, keyword, page, setting.UI.RepoSearchPagingNum) if err != nil { ctx.ServerError("SearchResults", err) return } // if non-login user or isAdmin, no need to check UnitTypeCode } else if (ctx.User == nil && len(repoIDs) > 0) || isAdmin { - total, searchResults, err = code_indexer.PerformSearch(repoIDs, keyword, page, setting.UI.RepoSearchPagingNum) + total, searchResults, searchResultLanguages, err = code_indexer.PerformSearch(repoIDs, language, keyword, page, setting.UI.RepoSearchPagingNum) if err != nil { ctx.ServerError("SearchResults", err) return @@ -377,12 +379,15 @@ func ExploreCode(ctx *context.Context) { } ctx.Data["Keyword"] = keyword + ctx.Data["Language"] = language ctx.Data["SearchResults"] = searchResults + ctx.Data["SearchResultLanguages"] = searchResultLanguages ctx.Data["RequireHighlightJS"] = true ctx.Data["PageIsViewCode"] = true pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5) pager.SetDefaultParams(ctx) + pager.AddParam(ctx, "l", "Language") ctx.Data["Page"] = pager ctx.HTML(200, tplExploreCode) diff --git a/routers/repo/search.go b/routers/repo/search.go index 50a92c1e36..e110ae2a72 100644 --- a/routers/repo/search.go +++ b/routers/repo/search.go @@ -22,26 +22,30 @@ func Search(ctx *context.Context) { ctx.Redirect(ctx.Repo.RepoLink, 302) return } + language := strings.TrimSpace(ctx.Query("l")) keyword := strings.TrimSpace(ctx.Query("q")) page := ctx.QueryInt("page") if page <= 0 { page = 1 } - total, searchResults, err := code_indexer.PerformSearch([]int64{ctx.Repo.Repository.ID}, - keyword, page, setting.UI.RepoSearchPagingNum) + total, searchResults, searchResultLanguages, err := code_indexer.PerformSearch([]int64{ctx.Repo.Repository.ID}, + language, keyword, page, setting.UI.RepoSearchPagingNum) if err != nil { ctx.ServerError("SearchResults", err) return } ctx.Data["Keyword"] = keyword + ctx.Data["Language"] = language ctx.Data["SourcePath"] = setting.AppSubURL + "/" + - path.Join(ctx.Repo.Repository.Owner.Name, ctx.Repo.Repository.Name, "src", "branch", ctx.Repo.Repository.DefaultBranch) + path.Join(ctx.Repo.Repository.Owner.Name, ctx.Repo.Repository.Name) ctx.Data["SearchResults"] = searchResults + ctx.Data["SearchResultLanguages"] = searchResultLanguages ctx.Data["RequireHighlightJS"] = true ctx.Data["PageIsViewCode"] = true pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5) pager.SetDefaultParams(ctx) + pager.AddParam(ctx, "l", "Language") ctx.Data["Page"] = pager ctx.HTML(200, tplSearch) -- cgit v1.2.3