positionToOffsetImpl.ts ×19

Frontier kind: Code frontier

unlabeled · c_523149fd5aaa

3413 tests · 5563 LOC · 30 files · introduces 0 tests · 71 LOC · 1 file

Introduces — evidence that enters the hierarchy at this concept

Code
19 ranges71 lines · 1 files
Tests
0 tests

Contains — complete concept membership

All code (extent)
841 ranges5563 lines · 30 files · Browse complete extent
All tests (intent)
3413 testsBrowse complete intent

Neighbourhood graph

The orange circle is the focus. Violet and green circles are every ancestor and descendant, broader and narrower, at any distance; blue squares and pink diamonds are the introduced files and exact introduced tests of every visible concept, not only the focus's. Arrows point from broader to narrower concepts and bridge only concepts omitted from this view. Undirected links show source or test introduction. Concept and file size follows LOC; exact test nodes use test-count units.

Introduced files, introduced tests, and structurally relevant concept specialization

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the native relationship evidence on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the native relationship evidence on this page while the interactive map is unavailable.

Native relationship evidence

Every exact file and test below is linked only from the concept that introduces it.

Introduced tests

Every collected test enters the hierarchy at exactly one concept.

No tests are introduced at this concept. Its intent tests are introduced by other concepts.

Introduced code

Every collected source range enters the hierarchy at exactly one concept.

1 file ranked by introduced lines: 71 introduced LOC across 19 ranges. Expand a file to inspect source; the > gutter marks introduced lines.

src/vs/editor/common/core/text/positionToOffsetImpl.ts 71 introduced LOC · 19 ranges

Open complete file

1 > /*--------------------------------------------------------------------------------------------- positionToOffsetImpl.ts
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { findLastIdxMonotonous } from '../../../../base/common/arraysFind.js';
7 > import { StringEdit, StringReplacement } from '../edits/stringEdit.js';
8 > import { OffsetRange } from '../ranges/offsetRange.js';
9 > import { Position } from '../position.js';
10 > import { Range } from '../range.js';
11 > import type { TextReplacement, TextEdit } from '../edits/textEdit.js';
12 > import type { TextLength } from '../text/textLength.js';
13 >
14 > export abstract class PositionOffsetTransformerBase {
15 > abstract getOffset(position: Position): number;
16 >
17 > getOffsetRange(range: Range): OffsetRange {
18 return new OffsetRange(
19 this.getOffset(range.getStartPosition()),
21 );
22 }
24 > abstract getPosition(offset: number): Position;
25 >
26 > getRange(offsetRange: OffsetRange): Range {
27 return Range.fromPositions(
28 this.getPosition(offsetRange.start),
30 );
31 }
33 > getStringEdit(edit: TextEdit): StringEdit {
34 const edits = edit.replacements.map(e => this.getStringReplacement(e));
35 return new Deps.deps.StringEdit(edits);
36 }
38 > getStringReplacement(edit: TextReplacement): StringReplacement {
39 return new Deps.deps.StringReplacement(this.getOffsetRange(edit.range), edit.text);
40 }
42 > getTextReplacement(edit: StringReplacement): TextReplacement {
43 return new Deps.deps.TextReplacement(this.getRange(edit.replaceRange), edit.newText);
44 }
46 > getTextEdit(edit: StringEdit): TextEdit {
47 const edits = edit.replacements.map(e => this.getTextReplacement(e));
48 return new Deps.deps.TextEdit(edits);
49 }
51 >
52 > interface IDeps {
53 > StringEdit: typeof StringEdit;
54 > StringReplacement: typeof StringReplacement;
55 > TextReplacement: typeof TextReplacement;
56 > TextEdit: typeof TextEdit;
57 > TextLength: typeof TextLength;
58 > }
59 >
60 > class Deps {
61 > static _deps: IDeps | undefined = undefined;
62 > static get deps(): IDeps {
63 if (!this._deps) {
64 throw new Error('Dependencies not set. Call _setDependencies first.');
66 return this._deps;
67 }
69 >
70 > /** This is to break circular module dependencies. */
71 > export function _setPositionOffsetTransformerDependencies(deps: IDeps): void {
72 Deps._deps = deps;
73 }
75 > export class PositionOffsetTransformer extends PositionOffsetTransformerBase {
76 > private _lineStartOffsetByLineIdx: number[] | undefined;
77 > private _lineEndOffsetByLineIdx: number[] | undefined;
78 >
79 > constructor(public readonly text: string) {
80 super();
81 }
83 > private get lineStartOffsetByLineIdx(): number[] {
84 if (!this._lineStartOffsetByLineIdx) {
85 this._computeLineOffsets();
87 return this._lineStartOffsetByLineIdx!;
88 }
90 > private get lineEndOffsetByLineIdx(): number[] {
91 if (!this._lineEndOffsetByLineIdx) {
92 this._computeLineOffsets();
94 return this._lineEndOffsetByLineIdx!;
95 }
97 > private _computeLineOffsets(): void {
98 this._lineStartOffsetByLineIdx = [];
99 this._lineEndOffsetByLineIdx = [];
112 this._lineEndOffsetByLineIdx.push(this.text.length);
113 }
115 > override getOffset(position: Position): number {
116 const valPos = this._validatePosition(position);
117 return this.lineStartOffsetByLineIdx[valPos.lineNumber - 1] + valPos.column - 1;
118 }
120 > private _validatePosition(position: Position): Position {
121 if (position.lineNumber < 1) {
122 return new Position(1, 1);
136 return position;
137 }
139 > override getPosition(offset: number): Position {
140 const idx = findLastIdxMonotonous(this.lineStartOffsetByLineIdx, i => i <= offset);
141 const lineNumber = idx + 1;
143 return new Position(lineNumber, column);
144 }
146 > getTextLength(offsetRange: OffsetRange): TextLength {
147 return Deps.deps.TextLength.ofRange(this.getRange(offsetRange));
148 }
150 > get textLength(): TextLength {
151 const lineIdx = this.lineStartOffsetByLineIdx.length - 1;
152 return new Deps.deps.TextLength(lineIdx, this.text.length - this.lineStartOffsetByLineIdx[lineIdx]);
153 }
155 > getLineLength(lineNumber: number): number {
156 return this.lineEndOffsetByLineIdx[lineNumber - 1] - this.lineStartOffsetByLineIdx[lineNumber - 1];
157 }