11import * as vscode from 'vscode'
22import { SearchPatternType } from './scanner'
3- import { searchQueryResult } from '../queries/searchQuery'
3+ import { searchQueryResult , SearchResultNode } from '../queries/searchQuery'
44
55export async function searchHtml (
66 host : string ,
@@ -12,70 +12,111 @@ export async function searchHtml(
1212 const html : string [ ] = [ ]
1313 const nodes = result ?. data ?. search ?. results ?. results
1414 for ( const node of nodes || [ ] ) {
15- const url = node ?. file ?. url
16- if ( ! url ) {
15+ formatFileMatch ( host , node , html )
16+ formatRepository ( host , node , html )
17+ formatCommit ( host , node , html )
18+ }
19+ return html . join ( '' )
20+ }
21+
22+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
23+ function formatCommit ( host : string , node : SearchResultNode , html : string [ ] ) : void {
24+ if ( node . __typename !== 'CommitSearchResult' ) {
25+ return
26+ }
27+ // Not supported
28+ }
29+
30+ function formatRepository ( host : string , node : SearchResultNode , html : string [ ] ) : void {
31+ if ( node . __typename !== 'Repository' ) {
32+ return
33+ }
34+ if ( ! node . name ) {
35+ return
36+ }
37+ const url = `sourcegraph://${ host } /${ node . name } `
38+ const stars = formatStars ( node . stars )
39+ html . push ( '<p>' )
40+ html . push (
41+ `<code><a style='cursor:pointer' class='sourcegraph-location' id="${ url } ">${ node . name } </a>${ stars } </code>`
42+ )
43+ html . push ( '</p>' )
44+ }
45+
46+ function formatFileMatch ( host : string , node : SearchResultNode , html : string [ ] ) : void {
47+ if ( node . __typename !== 'FileMatch' ) {
48+ return
49+ }
50+ const url = node ?. file ?. url
51+ if ( ! url ) {
52+ return
53+ }
54+ const lineMatches = node . lineMatches || [ ]
55+ if ( lineMatches . length === 0 ) {
56+ return
57+ }
58+ let first = true
59+ let filenameMatchesCount = 0
60+ for ( const [ lineMatchIndex , lineMatch ] of lineMatches . entries ( ) ) {
61+ const line = lineMatch . lineNumber
62+ if ( ! line ) {
1763 continue
1864 }
19- const starCount = node ?. repository ?. stars
20- const stars = starCount ? ` ⭐ ${ formatStarCount ( starCount ) } ` : ''
21- const lineMatches = node . lineMatches || [ ]
22- if ( lineMatches . length === 0 ) {
65+ const preview = lineMatch . preview
66+ if ( ! preview ) {
2367 continue
2468 }
25- let first = true
26- let filenameMatchesCount = 0
27- for ( const [ lineMatchIndex , lineMatch ] of lineMatches . entries ( ) ) {
28- const line = lineMatch . lineNumber
29- if ( ! line ) {
30- continue
31- }
32- const preview = lineMatch . preview
33- if ( ! preview ) {
34- continue
35- }
36- let index = 0
37- const highlightedPreview : string [ ] = [ ]
38- if ( lineMatchIndex > 0 ) {
39- highlightedPreview . push ( '\n' )
40- }
41- highlightedPreview . push ( `L${ line } : ` )
42- let character = 0
43- for ( const offsetsAndLength of lineMatch . offsetAndLengths || [ ] ) {
44- const [ start , length ] = offsetsAndLength
45- if ( ! character ) {
46- // Position the cursor at the first match on the line.
47- character = start
48- }
49- const end = start + length
50- highlightedPreview . push ( escapeHtml ( preview . slice ( index , start ) ) )
51- highlightedPreview . push ( '<mark>' )
52- highlightedPreview . push ( escapeHtml ( preview . slice ( start , end ) ) )
53- highlightedPreview . push ( '</mark>' )
54- index = end
55- }
56- highlightedPreview . push ( escapeHtml ( preview . slice ( index , preview . length ) ) )
57- if ( first ) {
58- first = false
59- html . push ( '<p>' )
60- html . push ( `<code>${ url } ${ stars } </code>` )
61- html . push ( '<pre>' )
62- }
63- const uri = `sourcegraph://${ host } ${ url } ?L${ line + 1 } :${ character } `
64- html . push (
65- `<a id='${ uri } ' style='cursor:pointer' class='sourcegraph-location'>${ highlightedPreview . join ( '' ) } </a>`
66- )
67- filenameMatchesCount ++
68- if ( filenameMatchesCount > 5 ) {
69- break
69+ let index = 0
70+ const highlightedPreview : string [ ] = [ ]
71+ if ( lineMatchIndex > 0 ) {
72+ highlightedPreview . push ( '\n' )
73+ }
74+ highlightedPreview . push ( `L${ line } : ` )
75+ let character = 0
76+ for ( const offsetsAndLength of lineMatch . offsetAndLengths || [ ] ) {
77+ const [ start , length ] = offsetsAndLength
78+ if ( ! character ) {
79+ // Position the cursor at the first match on the line.
80+ character = start
7081 }
82+ const end = start + length
83+ highlightedPreview . push ( escapeHtml ( preview . slice ( index , start ) ) )
84+ highlightedPreview . push ( '<mark>' )
85+ highlightedPreview . push ( escapeHtml ( preview . slice ( start , end ) ) )
86+ highlightedPreview . push ( '</mark>' )
87+ index = end
88+ }
89+ highlightedPreview . push ( escapeHtml ( preview . slice ( index , preview . length ) ) )
90+ if ( first ) {
91+ first = false
92+ html . push ( '<p>' )
93+ html . push ( `<code>${ url } ${ formatStars ( node ?. repository ?. stars ) } </code>` )
94+ html . push ( '<pre>' )
95+ }
96+ const uri = `sourcegraph://${ host } ${ url } ?L${ line + 1 } :${ character } `
97+ html . push (
98+ `<a id='${ uri } ' style='cursor:pointer' class='sourcegraph-location'>${ highlightedPreview . join ( '' ) } </a>`
99+ )
100+ filenameMatchesCount ++
101+ if ( filenameMatchesCount > 5 ) {
102+ break
71103 }
72- html . push ( '</pre>' )
73- html . push ( '</p>' )
74104 }
75- return html . join ( '' )
105+ html . push ( '</pre>' )
106+ html . push ( '</p>' )
76107}
77108
78- function formatStarCount ( starCount : number ) : string {
109+ function formatStars ( starCount : number | undefined ) : string {
110+ const count = formatStarCount ( starCount )
111+ if ( count ) {
112+ return ` ⭐${ count } `
113+ }
114+ return count
115+ }
116+ function formatStarCount ( starCount : number | undefined ) : string {
117+ if ( ! starCount ) {
118+ return ''
119+ }
79120 if ( starCount > 1000 ) {
80121 return `${ Math . round ( starCount / 1000 ) } k`
81122 }
0 commit comments