MCPcopy Create free account
hub / github.com/Dart-Code/Dart-Code / SingleDocumentPositionTracker

Class SingleDocumentPositionTracker

src/shared/vscode/trackers.ts:6–53  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4import { disposeAll } from "../utils";
5
6export class SingleDocumentPositionTracker implements IAmDisposable {
7 // TODO(dantup): Deprecate and remove this in favour of DocumentPositionTracker
8 private readonly disposables: IAmDisposable[] = [];
9 private readonly tracker: SingleDocumentOffsetTracker = new SingleDocumentOffsetTracker();
10 private readonly positionMap: Map<vs.Position, number> = new Map<vs.Position, number>();
11
12 private onPositionsChangedEmitter = new vs.EventEmitter<[vs.TextDocument, Map<vs.Position, vs.Position>]>();
13 public readonly onPositionsChanged = this.onPositionsChangedEmitter.event;
14
15 constructor() {
16 this.disposables.push(this.tracker);
17
18 this.tracker.onOffsetsChanged(([doc, offsets]) => {
19 // Map all our original positions onto new positions based on their
20 // new offsets.
21 const newPositions = new Map<vs.Position, vs.Position>();
22 for (const position of this.positionMap.keys()) {
23 const currentOffset = this.positionMap.get(position)!;
24 const newOffset = offsets.get(currentOffset);
25 if (newOffset)
26 newPositions.set(position, doc.positionAt(newOffset));
27 else
28 newPositions.delete(position);
29 }
30
31 this.onPositionsChangedEmitter.fire([doc, newPositions]);
32 });
33 }
34
35 public clear(): void {
36 this.positionMap.clear();
37 this.tracker.clear();
38 }
39
40 public trackDoc(document: vs.TextDocument, positions: vs.Position[]) {
41 // Stash all positions as offsets.
42 this.positionMap.clear();
43 for (const position of positions)
44 this.positionMap.set(position, document.offsetAt(position));
45
46 // Track via the offset tracker.
47 this.tracker.trackDoc(document, [...this.positionMap.values()]);
48 }
49
50 public dispose() {
51 disposeAll(this.disposables);
52 }
53}
54
55export class SingleDocumentOffsetTracker implements IAmDisposable {
56 // TODO(dantup): Deprecate and remove this in favour of DocumentPositionTracker

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected