@@ -7,88 +7,129 @@ function Search-Sourcegraph {
77 <#
88 . SYNOPSIS
99 Get users on a Sourcegraph instance
10- . PARAMETER Username
11- Get only the user with the given username
10+ . PARAMETER Query
11+ The search query.
12+ . PARAMETER CaseSensitive
13+ Match the query case-sensitive. Only for regexp and literal search.
14+ . PARAMETER Structural
15+ Interpret the query as a structural search query.
16+ . PARAMETER RegularExpression
17+ Interpret the query as a regular expression.
1218 . PARAMETER Endpoint
1319 The endpoint URL of the Sourcegraph instance (default https://sourcegraph.com)
1420 . PARAMETER Token
1521 The authentication token (if needed). Go to the settings page on Sourcegraph to generate one.
1622 #>
17- [CmdletBinding (SupportsPaging )]
23+ [CmdletBinding (SupportsPaging , DefaultParameterSetName = ' literal ' )]
1824 param (
1925 [Parameter (Mandatory , Position = 0 )]
2026 [string ] $Query ,
2127
28+ [Parameter (ParameterSetName = ' regexp' )]
29+ [Parameter (ParameterSetName = ' literal' )]
30+ [switch ] $CaseSensitive ,
31+
32+ [Parameter (ParameterSetName = ' regexp' , Mandatory )]
33+ [Alias (' Regexp' )]
34+ [switch ] $RegularExpression ,
35+
36+ [Parameter (ParameterSetName = ' structural' , Mandatory )]
37+ [switch ] $Structural ,
38+
2239 [Uri ] $Endpoint = ' https://sourcegraph.com' ,
2340 [string ] $Token
2441 )
2542
26- $data = Invoke-SourcegraphApiRequest - Query $SearchQuery - Variables @ { query = $Query } - Endpoint $Endpoint - Token $Token
27- if ($data.search.results.cloning.Count -gt 0 ) {
28- Write-Warning " Cloning:"
29- $data.search.results.cloning.name | Write-Warning
30- }
31- if ($data.search.results.missing.Count -gt 0 ) {
32- Write-Warning " Missing:"
33- $data.search.results.missing.name | Write-Warning
34- }
35- if ($data.search.results.timedout.Count -gt 0 ) {
36- Write-Warning " Timed out:"
37- $data.search.results.timedout.name | Write-Warning
38- }
39- if ($PSCmdlet.PagingParameters.IncludeTotalCount ) {
40- $PSCmdlet.PagingParameters.NewTotalCount ($data.search.results.resultCount , 1 )
41- }
42- if ($data.search.results.limitHit ) {
43- Write-Warning " Result limit hit"
44- }
43+ process {
44+ if ($CaseSensitive ) {
45+ $Query += ' case:yes'
46+ }
47+ $variables = @ {
48+ query = $Query
49+ patternType = $PSCmdlet.ParameterSetName
50+ }
51+ $data = Invoke-SourcegraphApiRequest - Query $SearchQuery - Variables $variables - Endpoint $Endpoint - Token $Token
52+ if ($data.search.results.cloning.Count -gt 0 ) {
53+ Write-Warning " Cloning:"
54+ $data.search.results.cloning.name | Write-Warning
55+ }
56+ if ($data.search.results.missing.Count -gt 0 ) {
57+ Write-Warning " Missing:"
58+ $data.search.results.missing.name | Write-Warning
59+ }
60+ if ($data.search.results.timedout.Count -gt 0 ) {
61+ Write-Warning " Timed out:"
62+ $data.search.results.timedout.name | Write-Warning
63+ }
64+ if ($PSCmdlet.PagingParameters.IncludeTotalCount ) {
65+ $PSCmdlet.PagingParameters.NewTotalCount ($data.search.results.resultCount , 1 )
66+ }
67+ if ($data.search.results.limitHit ) {
68+ Write-Warning " Result limit hit"
69+ }
4570
46- foreach ($result in $data.search.results.results ) {
47- $result.PSObject.TypeNames.Insert (0 , ' Sourcegraph.' + $result.__typename )
48- # Make the metadata accessible from the match objects
49- Add-Member - InputObject $result - MemberType NoteProperty - Name ' SearchResults' - Value $data.search.results
50- if ($result.__typename -eq ' FileMatch' ) {
51- # Make URL absolute
52- $result.File.Url = [Uri ]::new($Endpoint , $result.File.Url )
53- $result.Repository.Url = [Uri ]::new($Endpoint , $result.Repository.Url )
54-
55- if ($result.LineMatches -or $result.Symbols ) {
56- # Instead of nesting LineMatches and Symbols in FileMatches, we flat out the list and let PowerShell formatting do the grouping
57- foreach ($lineMatch in $result.LineMatches ) {
58- $lineMatch.PSObject.TypeNames.Insert (0 , ' Sourcegraph.LineMatch' )
59- Add-Member - InputObject $lineMatch - MemberType NoteProperty - Name ' FileMatch' - Value $result
60- $lineMatch
61- }
62- foreach ($symbol in $result.Symbols ) {
63- $symbol.PSObject.TypeNames.Insert (0 , ' Sourcegraph.Symbol' )
64- Add-Member - InputObject $symbol - MemberType NoteProperty - Name ' FileMatch' - Value $result
65- $symbol
71+ foreach ($result in $data.search.results.results ) {
72+ $result.PSObject.TypeNames.Insert (0 , ' Sourcegraph.' + $result.__typename )
73+ # Make the metadata accessible from the match objects
74+ Add-Member - InputObject $result - MemberType NoteProperty - Name ' SearchResults' - Value $data.search.results
75+ if ($result.__typename -eq ' FileMatch' ) {
76+ # Make URL absolute
77+ $result.File.Url = [Uri ]::new($Endpoint , $result.File.Url )
78+ $result.Repository.Url = [Uri ]::new($Endpoint , $result.Repository.Url )
79+
80+ if ($result.LineMatches -or $result.Symbols ) {
81+ # Instead of nesting LineMatches and Symbols in FileMatches, we flat out the list and let PowerShell formatting do the grouping
82+ foreach ($lineMatch in $result.LineMatches ) {
83+ $lineMatch.PSObject.TypeNames.Insert (0 , ' Sourcegraph.LineMatch' )
84+ Add-Member - InputObject $lineMatch - MemberType NoteProperty - Name ' FileMatch' - Value $result
85+ $lineMatch
86+ }
87+ foreach ($symbol in $result.Symbols ) {
88+ $symbol.PSObject.TypeNames.Insert (0 , ' Sourcegraph.Symbol' )
89+ Add-Member - InputObject $symbol - MemberType NoteProperty - Name ' FileMatch' - Value $result
90+ $symbol
91+ }
92+ } else {
93+ # The FileMatch has no line or symbol matches, which means the file name matched, so add the FileMatch itself as a result
94+ $result
6695 }
6796 } else {
68- # The FileMatch has no line or symbol matches, which means the file name matched, so add the FileMatch itself as a result
6997 $result
7098 }
71- } else {
72- $result
7399 }
74100 }
75101}
76102Set-Alias Search-Src Search-Sourcegraph
77103
78104function Get-SourcegraphSearchSuggestions {
79- [CmdletBinding ()]
105+ [CmdletBinding (DefaultParameterSetName = ' literal ' )]
80106 param (
81107 [Parameter (Mandatory )]
82108 [string ] $Query ,
83109
110+ [Parameter (ParameterSetName = ' regexp' )]
111+ [Parameter (ParameterSetName = ' literal' )]
112+ [switch ] $CaseSensitive ,
113+
114+ [Parameter (ParameterSetName = ' regexp' , Mandatory )]
115+ [Alias (' Regexp' )]
116+ [switch ] $RegularExpression ,
117+
118+ [Parameter (ParameterSetName = ' structural' , Mandatory )]
119+ [switch ] $Structural ,
120+
84121 [Uri ] $Endpoint = ' https://sourcegraph.com' ,
85122 [string ] $Token
86123 )
87124
88125 process {
126+ if ($CaseSensitive ) {
127+ $Query += ' case:yes'
128+ }
89129 $vars = @ {
90130 query = $Query
91131 first = 10
132+ patternType = $PSCmdlet.ParameterSetName
92133 }
93134 Invoke-SourcegraphApiRequest - Query $SuggestionsQuery - Variables $vars - Endpoint $Endpoint - Token $Token |
94135 ForEach-Object { $_.search.suggestions }
@@ -117,6 +158,15 @@ Register-ArgumentCompleter -CommandName Search-Sourcegraph -ParameterName Query
117158 if ($params.ContainsKey (' Endpoint' )) {
118159 $suggestionParams.Endpoint = $params.Endpoint
119160 }
161+ if ($params.ContainsKey (' CaseSensitive' )) {
162+ $suggestionParams.CaseSensitive = $params.CaseSensitive
163+ }
164+ if ($params.ContainsKey (' RegularExpression' )) {
165+ $suggestionParams.RegularExpression = $params.RegularExpression
166+ }
167+ if ($params.ContainsKey (' Structural' )) {
168+ $suggestionParams.Structural = $params.Structural
169+ }
120170
121171 Get-SourcegraphSearchSuggestions @suggestionParams - Query $wordToComplete.Trim (@ (" '" , ' "' )) |
122172 ForEach-Object {
0 commit comments