-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
BlobPanel.tsx
312 lines (287 loc) · 12.8 KB
/
BlobPanel.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import React, { useEffect, useCallback, useMemo } from 'react'
import * as H from 'history'
import { EMPTY, from, Observable, ReplaySubject, Subscription } from 'rxjs'
import { distinct, map, mapTo, switchMap, tap } from 'rxjs/operators'
import { DocumentSelector } from 'sourcegraph'
import {
BuiltinTabbedPanelDefinition,
BuiltinTabbedPanelView,
useBuiltinTabbedPanelViews,
} from '@sourcegraph/branded/src/components/panel/TabbedPanelContent'
import { ReferenceParameters, TextDocumentPositionParameters } from '@sourcegraph/client-api'
import { MaybeLoadingResult } from '@sourcegraph/codeintellify'
import { isErrorLike } from '@sourcegraph/common'
import * as clientType from '@sourcegraph/extension-api-types'
import { wrapRemoteObservable } from '@sourcegraph/shared/src/api/client/api/common'
import { FetchFileParameters } from '@sourcegraph/shared/src/backend/file'
import { ExtensionsControllerProps } from '@sourcegraph/shared/src/extensions/controller'
import { Scalars } from '@sourcegraph/shared/src/graphql-operations'
import { PlatformContextProps } from '@sourcegraph/shared/src/platform/context'
import { Settings, SettingsCascadeOrError, SettingsCascadeProps } from '@sourcegraph/shared/src/settings/settings'
import { TelemetryProps } from '@sourcegraph/shared/src/telemetry/telemetryService'
import { ThemeProps } from '@sourcegraph/shared/src/theme'
import { AbsoluteRepoFile, ModeSpec, parseQueryAndHash, UIPositionSpec } from '@sourcegraph/shared/src/util/url'
import { useObservable } from '@sourcegraph/wildcard'
import { CodeIntelligenceProps } from '../../../codeintel'
import { ReferencesPanelWithMemoryRouter } from '../../../codeintel/ReferencesPanel'
import { RepoRevisionSidebarCommits } from '../../RepoRevisionSidebarCommits'
interface Props
extends AbsoluteRepoFile,
ModeSpec,
SettingsCascadeProps,
ExtensionsControllerProps,
ThemeProps,
PlatformContextProps,
Pick<CodeIntelligenceProps, 'useCodeIntel'>,
TelemetryProps {
location: H.Location
history: H.History
repoID: Scalars['ID']
repoName: string
commitID: string
fetchHighlightedFileLineRanges: (parameters: FetchFileParameters, force?: boolean) => Observable<string[][]>
}
export type BlobPanelTabID = 'info' | 'def' | 'references' | 'impl' | 'typedef' | 'history'
/** The subject (what the contextual information refers to). */
interface PanelSubject extends AbsoluteRepoFile, ModeSpec, Partial<UIPositionSpec> {
repoID: string
/**
* Include the full URI fragment here because it represents the state of panels, and we want
* panels to be re-rendered when this state changes.
*/
hash: string
history: H.History
location: H.Location
}
/**
* A React hook that registers panel views for the blob.
*/
function useBlobPanelViews({
extensionsController,
repoName,
commitID,
revision,
mode,
filePath,
repoID,
location,
history,
settingsCascade,
isLightTheme,
platformContext,
useCodeIntel,
telemetryService,
fetchHighlightedFileLineRanges,
}: Props): void {
const subscriptions = useMemo(() => new Subscription(), [])
// Keep active code editor position subscription active to prevent empty loading state
// (for main thread -> ext host -> main thread roundtrip for editor position)
const activeCodeEditorPositions = useMemo(() => new ReplaySubject<TextDocumentPositionParameters | null>(1), [])
useObservable(
useMemo(() => {
if (extensionsController === null) {
return EMPTY
}
return from(extensionsController.extHostAPI).pipe(
switchMap(extensionHostAPI => wrapRemoteObservable(extensionHostAPI.getActiveCodeEditorPosition())),
tap(parameters => activeCodeEditorPositions.next(parameters)),
mapTo(undefined)
)
}, [activeCodeEditorPositions, extensionsController])
)
const maxPanelResults = maxPanelResultsFromSettings(settingsCascade)
const preferAbsoluteTimestamps = preferAbsoluteTimestampsFromSettings(settingsCascade)
const isTabbedReferencesPanelEnabled =
!isErrorLike(settingsCascade.final) &&
settingsCascade.final !== null &&
settingsCascade.final['codeIntel.referencesPanel'] === 'tabbed'
// Creates source for definition and reference panels
const createLocationProvider = useCallback(
<P extends TextDocumentPositionParameters>(
id: string,
title: string,
priority: number,
provideLocations: (parameters: P) => Observable<MaybeLoadingResult<clientType.Location[]>>,
extraParameters?: {
selector: DocumentSelector | null
}
): Observable<BuiltinTabbedPanelView | null> =>
activeCodeEditorPositions.pipe(
distinct(),
map(textDocumentPositionParameters => {
if (!textDocumentPositionParameters) {
return null
}
return {
title,
content: '',
selector: extraParameters?.selector ?? null,
priority,
maxLocationResults: id === 'references' || id === 'def' ? maxPanelResults : undefined,
// This disable directive is necessary because TypeScript is not yet smart
// enough to know that (typeof params & typeof extraParams) is P.
//
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
locationProvider: provideLocations({
...textDocumentPositionParameters,
} as P),
}
})
),
[activeCodeEditorPositions, maxPanelResults]
)
// Source for history panel
const panelSubject = useMemo(() => {
const parsedHash = parseQueryAndHash(location.search, location.hash)
return {
repoID,
repoName,
commitID,
revision,
filePath,
mode,
position:
parsedHash.line !== undefined
? { line: parsedHash.line, character: parsedHash.character || 0 }
: undefined,
hash: location.hash,
history,
location,
}
}, [commitID, filePath, history, location, mode, repoID, repoName, revision])
const panelSubjectChanges = useMemo(() => new ReplaySubject<PanelSubject>(1), [])
useEffect(() => {
panelSubjectChanges.next(panelSubject)
}, [panelSubject, panelSubjectChanges])
useBuiltinTabbedPanelViews(
useMemo(() => {
const panelDefinitions: BuiltinTabbedPanelDefinition[] = [
{
id: 'history',
provider: panelSubjectChanges.pipe(
map(({ repoID, revision, filePath, history, location }) => ({
title: 'History',
content: '',
priority: 150,
selector: null,
locationProvider: undefined,
reactElement: (
<RepoRevisionSidebarCommits
key="commits"
repoID={repoID}
revision={revision}
filePath={filePath}
history={history}
location={location}
preferAbsoluteTimestamps={preferAbsoluteTimestamps}
/>
),
}))
),
},
]
if (isTabbedReferencesPanelEnabled && extensionsController !== null) {
panelDefinitions.push(
...[
{
id: 'def',
provider: createLocationProvider('def', 'Definition', 190, parameters =>
from(extensionsController.extHostAPI).pipe(
switchMap(extensionHostAPI =>
wrapRemoteObservable(extensionHostAPI.getDefinition(parameters))
)
)
),
},
{
id: 'references',
provider: createLocationProvider<ReferenceParameters>(
'references',
'References',
180,
parameters =>
from(extensionsController.extHostAPI).pipe(
switchMap(extensionHostAPI =>
wrapRemoteObservable(
extensionHostAPI.getReferences(parameters, {
includeDeclaration: false,
})
)
)
)
),
},
]
)
} else {
panelDefinitions.push({
id: 'references',
provider: panelSubjectChanges.pipe(
map(({ position, history, location }) => ({
title: 'References',
content: '',
priority: 180,
selector: null,
locationProvider: undefined,
// The new reference panel contains definitoins, references, and implementations. We need it to
// match all these IDs so it shows up when one of the IDs is used as `#tab=<ID>` in the URL.
matchesTabID: (id: string): boolean =>
id === 'def' || id === 'references' || id.startsWith('implementations_'),
// This panel doesn't need a wrapper
noWrapper: true,
reactElement: position ? (
<ReferencesPanelWithMemoryRouter
settingsCascade={settingsCascade}
platformContext={platformContext}
isLightTheme={isLightTheme}
extensionsController={extensionsController}
telemetryService={telemetryService}
key="references"
externalHistory={history}
externalLocation={location}
fetchHighlightedFileLineRanges={fetchHighlightedFileLineRanges}
useCodeIntel={useCodeIntel}
/>
) : (
<></>
),
}))
),
})
}
return panelDefinitions
}, [
panelSubjectChanges,
isTabbedReferencesPanelEnabled,
extensionsController,
preferAbsoluteTimestamps,
createLocationProvider,
settingsCascade,
platformContext,
isLightTheme,
telemetryService,
fetchHighlightedFileLineRanges,
useCodeIntel,
])
)
useEffect(() => () => subscriptions.unsubscribe(), [subscriptions])
}
function maxPanelResultsFromSettings(settingsCascade: SettingsCascadeOrError<Settings>): number | undefined {
if (settingsCascade.final && !isErrorLike(settingsCascade.final)) {
return settingsCascade.final['codeIntelligence.maxPanelResults'] as number
}
return undefined
}
function preferAbsoluteTimestampsFromSettings(settingsCascade: SettingsCascadeOrError<Settings>): boolean {
if (settingsCascade.final && !isErrorLike(settingsCascade.final)) {
return settingsCascade.final['history.preferAbsoluteTimestamps'] as boolean
}
return false
}
/**
* Registers built-in tabbed panel views and renders `null`.
*/
export const BlobPanel: React.FunctionComponent<Props> = props => {
useBlobPanelViews(props)
return null
}