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

Method trackRange

src/shared/vscode/trackers.ts:263–308  ·  view source on GitHub ↗
(document: vs.TextDocument, range: Range, callback: (newRange: Range | undefined) => void)

Source from the content-addressed store, hash-verified

261 }
262
263 public trackRange(document: vs.TextDocument, range: Range, callback: (newRange: Range | undefined) => void): IAmDisposable {
264 let start: Position | undefined = range.start;
265 let end: Position | undefined = range.end;
266
267 // TODO(dantup): If start/end are the same, just short-cut this with one position tracker.
268 // TODO(dantup): Because both the start/end positions move when content is modified before
269 // this range and we track both start/end, we end up firing once after the start moves, then
270 // the end. It would be better if we just atomically update the range once (which might
271 // mean just not using position tracker as-is?)
272
273 const startDisposable = this.positionTracker.trackPosition(document, range.start, (newPos) => {
274 start = newPos;
275 updateRange();
276 });
277
278 const endDisposable = this.positionTracker.trackPosition(document, range.end, (newPos) => {
279 end = newPos;
280 updateRange();
281 });
282
283 const updateRange = () => {
284 const newRange = (start && end) ? { start, end } : undefined;
285 callback(newRange);
286
287 // If we don't have a range because one position went away, be sure to dispose the other tracker.
288 if (!newRange) {
289 startDisposable.dispose();
290 endDisposable.dispose();
291 }
292 };
293
294 const entry: RangeTrackerEntry = {
295 dispose: () => {
296 startDisposable.dispose();
297 endDisposable.dispose();
298 const index = this.rangeTrackers.indexOf(entry);
299 if (index !== -1) {
300 this.rangeTrackers.splice(index, 1);
301 }
302 }
303 };
304
305 this.rangeTrackers.push(entry);
306
307 return entry;
308 }
309
310 public dispose() {
311 this.positionTracker.dispose();

Callers 2

trackRangeForUriMethod · 0.95

Calls 2

trackPositionMethod · 0.80
disposeMethod · 0.65

Tested by

no test coverage detected