提交 a27776bf 编写于 作者: A Alex Dima

Validate offset before passing it down

上级 5507f635
......@@ -1079,6 +1079,7 @@ export interface ITextBuffer {
getValueInRange(range: Range, eol: EndOfLinePreference): string;
getValueLengthInRange(range: Range, eol: EndOfLinePreference): number;
getLength(): number;
getLineCount(): number;
getLinesContent(): string[];
getLineContent(lineNumber: number): string;
......
......@@ -198,6 +198,10 @@ export class LinesTextBuffer implements ITextBuffer {
return this._lines.slice(0);
}
public getLength(): number {
return this._lineStarts.getTotalValue();
}
public getLineContent(lineNumber: number): string {
return this._lines[lineNumber - 1];
}
......
......@@ -630,8 +630,9 @@ export class TextModel extends Disposable implements model.ITextModel {
return this._buffer.getOffsetAt(position.lineNumber, position.column);
}
public getPositionAt(offset: number): Position {
public getPositionAt(rawOffset: number): Position {
this._assertNotDisposed();
let offset = (Math.min(this._buffer.getLength(), Math.max(0, rawOffset)));
return this._buffer.getPositionAt(offset);
}
......@@ -890,7 +891,8 @@ export class TextModel extends Disposable implements model.ITextModel {
public modifyPosition(rawPosition: IPosition, offset: number): Position {
this._assertNotDisposed();
return this.getPositionAt(this.getOffsetAt(rawPosition) + offset);
let candidate = this.getOffsetAt(rawPosition) + offset;
return this.getPositionAt(Math.min(this._buffer.getLength(), Math.max(0, candidate)));
}
public getFullModelRange(): Range {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册