|
28 | 28 | languages: string[]
|
29 | 29 | }
|
30 | 30 |
|
31 |
| - const extensionsCompartment = new Compartment() |
32 |
| -
|
33 | 31 | const defaultTheme = EditorView.theme({
|
34 | 32 | '&': {
|
35 | 33 | height: '100%',
|
|
42 | 40 | lineHeight: '1rem',
|
43 | 41 | fontFamily: 'var(--code-font-family)',
|
44 | 42 | fontSize: 'var(--code-font-size)',
|
| 43 | + overflow: 'auto', |
45 | 44 | },
|
46 | 45 | '.cm-content': {
|
47 | 46 | padding: 0,
|
|
106 | 105 | defaultTheme,
|
107 | 106 | linkify,
|
108 | 107 | ]
|
109 |
| -
|
110 |
| - function configureSyntaxHighlighting(content: string, lsif: string): Extension { |
111 |
| - return lsif ? syntaxHighlight.of({ content, lsif }) : [] |
112 |
| - } |
113 |
| -
|
114 |
| - function configureMiscSettings({ wrapLines }: { wrapLines: boolean }): Extension { |
115 |
| - return [wrapLines ? EditorView.lineWrapping : []] |
116 |
| - } |
117 | 108 | </script>
|
118 | 109 |
|
119 | 110 | <script lang="ts">
|
120 | 111 | import '$lib/highlight.scss'
|
121 | 112 |
|
122 |
| - import { Compartment, EditorState, type Extension } from '@codemirror/state' |
| 113 | + import { EditorState, type Extension } from '@codemirror/state' |
123 | 114 | import { EditorView } from '@codemirror/view'
|
124 | 115 | import { createEventDispatcher, onMount } from 'svelte'
|
125 | 116 |
|
|
136 | 127 | linkify,
|
137 | 128 | createCodeIntelExtension,
|
138 | 129 | syncSelection,
|
139 |
| - temporaryTooltip, |
140 | 130 | showBlame as showBlameColumn,
|
141 | 131 | blameData as blameDataFacet,
|
142 | 132 | type BlameHunkData,
|
| 133 | + lockFirstVisibleLine, |
| 134 | + temporaryTooltip, |
143 | 135 | } from '$lib/web'
|
144 | 136 |
|
145 | 137 | import BlameDecoration from './blame/BlameDecoration.svelte'
|
146 | 138 | import { type Range, staticHighlights } from './codemirror/static-highlights'
|
| 139 | + import { |
| 140 | + createCompartments, |
| 141 | + restoreScrollSnapshot, |
| 142 | + type ExtensionType, |
| 143 | + type ScrollSnapshot, |
| 144 | + getScrollSnapshot as getScrollSnapshot_internal, |
| 145 | + } from './codemirror/utils' |
147 | 146 | import { goToDefinition, openImplementations, openReferences } from './repo/blob'
|
148 | 147 |
|
149 | 148 | export let blobInfo: BlobInfo
|
|
152 | 151 | export let selectedLines: LineOrPositionOrRange | null = null
|
153 | 152 | export let codeIntelAPI: CodeIntelAPI
|
154 | 153 | export let staticHighlightRanges: Range[] = []
|
| 154 | + /** |
| 155 | + * The initial scroll position when the editor is first mounted. |
| 156 | + * Changing the value afterwards has no effect. |
| 157 | + */ |
| 158 | + export let initialScrollPosition: ScrollSnapshot | null = null |
155 | 159 |
|
156 | 160 | export let showBlame: boolean = false
|
157 | 161 | export let blameData: BlameHunkData | undefined = undefined
|
158 | 162 |
|
| 163 | + export function getScrollSnapshot(): ScrollSnapshot | null { |
| 164 | + return view ? getScrollSnapshot_internal(view) : null |
| 165 | + } |
| 166 | +
|
159 | 167 | const dispatch = createEventDispatcher<{ selectline: SelectedLineRange }>()
|
| 168 | + const extensionsCompartment = createCompartments({ |
| 169 | + selectableLineNumbers: null, |
| 170 | + syntaxHighlighting: null, |
| 171 | + lineWrapping: null, |
| 172 | + temporaryTooltip, |
| 173 | + codeIntelExtension: null, |
| 174 | + staticExtensions, |
| 175 | + staticHighlightExtension: null, |
| 176 | + blameDataExtension: null, |
| 177 | + blameColumnExtension: null, |
| 178 | + }) |
160 | 179 |
|
161 |
| - let editor: EditorView |
162 | 180 | let container: HTMLDivElement | null = null
|
163 |
| -
|
164 |
| - const lineNumbers = selectableLineNumbers({ |
165 |
| - onSelection(range) { |
166 |
| - dispatch('selectline', range) |
167 |
| - }, |
168 |
| - initialSelection: selectedLines?.line === undefined ? null : selectedLines, |
169 |
| - }) |
| 181 | + let view: EditorView | undefined = undefined |
170 | 182 |
|
171 | 183 | $: documentInfo = {
|
172 | 184 | repoName: blobInfo.repoName,
|
|
198 | 210 | }
|
199 | 211 | },
|
200 | 212 | })
|
201 |
| - $: settings = configureMiscSettings({ wrapLines }) |
202 |
| - $: sh = configureSyntaxHighlighting(blobInfo.content, highlights) |
| 213 | + $: lineWrapping = wrapLines ? EditorView.lineWrapping : [] |
| 214 | + $: syntaxHighlighting = highlights ? syntaxHighlight.of({ content: blobInfo.content, lsif: highlights }) : [] |
203 | 215 | $: staticHighlightExtension = staticHighlights(staticHighlightRanges)
|
204 | 216 |
|
205 | 217 | $: blameColumnExtension = showBlame
|
|
216 | 228 | : []
|
217 | 229 | $: blameDataExtension = blameDataFacet(blameData)
|
218 | 230 |
|
219 |
| - $: extensions = [ |
220 |
| - sh, |
221 |
| - settings, |
222 |
| - lineNumbers, |
223 |
| - temporaryTooltip, |
224 |
| - codeIntelExtension, |
225 |
| - staticExtensions, |
226 |
| - staticHighlightExtension, |
227 |
| - blameColumnExtension, |
228 |
| - blameDataExtension, |
229 |
| - ] |
| 231 | + // Reinitialize the editor when its content changes. Update only the extensions when they change. |
| 232 | + $: update(view => { |
| 233 | + // blameColumnExtension is omitted here. It's updated separately below because we need to |
| 234 | + // apply additional effects when it changes (but only when it changes). |
| 235 | + const extensions: Partial<ExtensionType<typeof extensionsCompartment>> = { |
| 236 | + codeIntelExtension, |
| 237 | + lineWrapping, |
| 238 | + syntaxHighlighting, |
| 239 | + staticHighlightExtension, |
| 240 | + blameDataExtension, |
| 241 | + } |
| 242 | + if (view.state.sliceDoc() !== blobInfo.content) { |
| 243 | + view.setState(createEditorState(blobInfo, extensions)) |
| 244 | + } else { |
| 245 | + extensionsCompartment.update(view, extensions) |
| 246 | + } |
| 247 | + }) |
230 | 248 |
|
231 |
| - function update(blobInfo: BlobInfo, extensions: Extension, range: LineOrPositionOrRange | null) { |
232 |
| - if (editor) { |
233 |
| - // TODO(fkling): Find a way to combine this into a single transaction. |
234 |
| - if (editor.state.sliceDoc() !== blobInfo.content) { |
235 |
| - editor.setState( |
236 |
| - EditorState.create({ doc: blobInfo.content, extensions: extensionsCompartment.of(extensions) }) |
237 |
| - ) |
238 |
| - } else { |
239 |
| - editor.dispatch({ effects: [extensionsCompartment.reconfigure(extensions)] }) |
240 |
| - } |
241 |
| - editor.dispatch({ |
242 |
| - effects: setSelectedLines.of(range?.line && isValidLineRange(range, editor.state.doc) ? range : null), |
243 |
| - }) |
| 249 | + // Show/hide the blame column and ensure that the style changes do not change the scroll position |
| 250 | + $: update(view => { |
| 251 | + extensionsCompartment.update(view, { blameColumnExtension }, ...lockFirstVisibleLine(view)) |
| 252 | + }) |
244 | 253 |
|
245 |
| - if (range) { |
246 |
| - syncSelection(editor, range) |
247 |
| - } |
| 254 | + // Update the selected lines. This will scroll the selected lines into view. Also set the editor's |
| 255 | + // selection (essentially the cursor position) to the selected lines. This is necessary in case the |
| 256 | + // selected range references a symbol. |
| 257 | + $: update(view => { |
| 258 | + view.dispatch({ |
| 259 | + effects: setSelectedLines.of( |
| 260 | + selectedLines?.line && isValidLineRange(selectedLines, view.state.doc) ? selectedLines : null |
| 261 | + ), |
| 262 | + }) |
| 263 | + if (selectedLines) { |
| 264 | + syncSelection(view, selectedLines) |
248 | 265 | }
|
249 |
| - } |
250 |
| -
|
251 |
| - $: update(blobInfo, extensions, selectedLines) |
| 266 | + }) |
252 | 267 |
|
253 | 268 | onMount(() => {
|
254 | 269 | if (container) {
|
255 |
| - editor = new EditorView({ |
256 |
| - state: EditorState.create({ doc: blobInfo.content, extensions: extensionsCompartment.of(extensions) }), |
| 270 | + view = new EditorView({ |
| 271 | + // On first render initialize all extensions |
| 272 | + state: createEditorState(blobInfo, { |
| 273 | + codeIntelExtension, |
| 274 | + lineWrapping, |
| 275 | + syntaxHighlighting, |
| 276 | + staticHighlightExtension, |
| 277 | + blameDataExtension, |
| 278 | + blameColumnExtension, |
| 279 | + }), |
257 | 280 | parent: container,
|
258 | 281 | })
|
259 | 282 | if (selectedLines) {
|
260 |
| - syncSelection(editor, selectedLines) |
| 283 | + syncSelection(view, selectedLines) |
261 | 284 | }
|
| 285 | + if (initialScrollPosition) { |
| 286 | + restoreScrollSnapshot(view, initialScrollPosition) |
| 287 | + } |
| 288 | + } |
| 289 | + return () => { |
| 290 | + view?.destroy() |
262 | 291 | }
|
263 | 292 | })
|
| 293 | +
|
| 294 | + // Helper function to update the editor state whithout depending on the view variable |
| 295 | + // (those updates should only run on subsequent updates) |
| 296 | + function update(updater: (view: EditorView) => void) { |
| 297 | + if (view) { |
| 298 | + updater(view) |
| 299 | + } |
| 300 | + } |
| 301 | +
|
| 302 | + function createEditorState(blobInfo: BlobInfo, extensions: Partial<ExtensionType<typeof extensionsCompartment>>) { |
| 303 | + return EditorState.create({ |
| 304 | + doc: blobInfo.content, |
| 305 | + extensions: extensionsCompartment.init({ |
| 306 | + selectableLineNumbers: selectableLineNumbers({ |
| 307 | + onSelection(range) { |
| 308 | + dispatch('selectline', range) |
| 309 | + }, |
| 310 | + initialSelection: selectedLines?.line === undefined ? null : selectedLines, |
| 311 | + // We don't want to scroll the selected line into view when a scroll position is explicitly set. |
| 312 | + skipInitialScrollIntoView: initialScrollPosition !== null, |
| 313 | + }), |
| 314 | + ...extensions, |
| 315 | + }), |
| 316 | + selection: { |
| 317 | + anchor: 0, |
| 318 | + }, |
| 319 | + }) |
| 320 | + } |
264 | 321 | </script>
|
265 | 322 |
|
266 | 323 | {#if browser}
|
|
273 | 330 |
|
274 | 331 | <style lang="scss">
|
275 | 332 | .root {
|
276 |
| - display: contents; |
277 | 333 | --blame-decoration-width: 400px;
|
278 | 334 | --blame-recency-width: 4px;
|
| 335 | +
|
| 336 | + height: 100%; |
279 | 337 | }
|
280 | 338 | pre {
|
281 | 339 | margin: 0;
|
|
0 commit comments