forked from kaycoinsUSA/sourcegraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeEditor.ts
More file actions
75 lines (63 loc) · 2.84 KB
/
Copy pathcodeEditor.ts
File metadata and controls
75 lines (63 loc) · 2.84 KB
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
import { ProxyValue, proxyValueSymbol } from '@sourcegraph/comlink'
import { TextDocumentDecoration } from '@sourcegraph/extension-api-types'
import { flatten, values } from 'lodash'
import { BehaviorSubject, Observable, Subscription } from 'rxjs'
import { ProvideTextDocumentDecorationSignature } from '../services/decoration'
import { FeatureProviderRegistry } from '../services/registry'
import { TextDocumentIdentifier } from '../types/textDocument'
/** @internal */
export interface ClientCodeEditorAPI extends ProxyValue {
$setDecorations(resource: string, decorationType: string, decorations: TextDocumentDecoration[]): void
}
interface PreviousDecorations {
[resource: string]: {
[decorationType: string]: TextDocumentDecoration[]
}
}
/** @internal */
export class ClientCodeEditor implements ClientCodeEditorAPI {
public readonly [proxyValueSymbol] = true
private subscriptions = new Subscription()
/** Map of document URI to its decorations (last published by the server). */
private decorations = new Map<string, BehaviorSubject<TextDocumentDecoration[]>>()
private previousDecorations: PreviousDecorations = {}
constructor(private registry: FeatureProviderRegistry<undefined, ProvideTextDocumentDecorationSignature>) {
this.subscriptions.add(
this.registry.registerProvider(
undefined,
(textDocument: TextDocumentIdentifier): Observable<TextDocumentDecoration[]> =>
this.getDecorationsSubject(textDocument.uri)
)
)
}
public $setDecorations(resource: string, decorationType: string, decorations: TextDocumentDecoration[]): void {
this.getDecorationsSubject(resource, decorationType, decorations)
}
private getDecorationsSubject(
resource: string,
decorationType?: string,
decorations?: TextDocumentDecoration[]
): BehaviorSubject<TextDocumentDecoration[]> {
let subject = this.decorations.get(resource)
if (!subject) {
subject = new BehaviorSubject<TextDocumentDecoration[]>(decorations || [])
this.decorations.set(resource, subject)
this.previousDecorations[resource] = {}
}
if (decorations !== undefined) {
// Replace previous decorations for this resource + decorationType
this.previousDecorations[resource][decorationType!] = decorations
// Merge decorations for all types for this resource, and emit them
const nextDecorations = flatten(values(this.previousDecorations[resource]))
subject.next(nextDecorations)
}
return subject
}
public unsubscribe(): void {
// Clear decorations.
for (const subject of this.decorations.values()) {
subject.next([])
}
this.subscriptions.unsubscribe()
}
}