mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-10-09 13:18:29 +08:00
R=szuend@chromium.org Bug: 1050549 Change-Id: Ic068b0178efcd5bafd46e8e054a77315ff9083f5 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2644904 Commit-Queue: Tim van der Lippe <tvanderlippe@chromium.org> Reviewed-by: Simon Zünd <szuend@chromium.org>
60 lines
1.4 KiB
JavaScript
60 lines
1.4 KiB
JavaScript
// Copyright 2020 The Chromium Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import * as Platform from '../platform/platform.js';
|
|
|
|
export class TextCursor {
|
|
/**
|
|
* @param {!Array<number>} lineEndings
|
|
*/
|
|
constructor(lineEndings) {
|
|
this._lineEndings = lineEndings;
|
|
this._offset = 0;
|
|
this._lineNumber = 0;
|
|
this._columnNumber = 0;
|
|
}
|
|
|
|
/**
|
|
* @param {number} offset
|
|
*/
|
|
advance(offset) {
|
|
this._offset = offset;
|
|
while (this._lineNumber < this._lineEndings.length && this._lineEndings[this._lineNumber] < this._offset) {
|
|
++this._lineNumber;
|
|
}
|
|
this._columnNumber = this._lineNumber ? this._offset - this._lineEndings[this._lineNumber - 1] - 1 : this._offset;
|
|
}
|
|
|
|
/**
|
|
* @return {number}
|
|
*/
|
|
offset() {
|
|
return this._offset;
|
|
}
|
|
|
|
/**
|
|
* @param {number} offset
|
|
*/
|
|
resetTo(offset) {
|
|
this._offset = offset;
|
|
this._lineNumber =
|
|
Platform.ArrayUtilities.lowerBound(this._lineEndings, offset, Platform.ArrayUtilities.DEFAULT_COMPARATOR);
|
|
this._columnNumber = this._lineNumber ? this._offset - this._lineEndings[this._lineNumber - 1] - 1 : this._offset;
|
|
}
|
|
|
|
/**
|
|
* @return {number}
|
|
*/
|
|
lineNumber() {
|
|
return this._lineNumber;
|
|
}
|
|
|
|
/**
|
|
* @return {number}
|
|
*/
|
|
columnNumber() {
|
|
return this._columnNumber;
|
|
}
|
|
}
|