-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathPositionConverter.ts
More file actions
215 lines (201 loc) · 9.77 KB
/
PositionConverter.ts
File metadata and controls
215 lines (201 loc) · 9.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Copyright (c) 2018-2023, NWO-I CWI and Swat.engineering
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
import * as vscode from "vscode";
import { SourceLocation } from '../RascalTerminalLinkProvider';
/**
* This class allows to convert text locations between VS Code and Rascal.
* Some characters, which VS Code counts as two characters because it uses UTF-16 encoding,
* are only a single character for Rascal, which uses UTF-32 encoding.
*
* Assuming UTF-16 encoding, because this code is executed in TypeScript,
* all methods in this class therefore iterate character-wise over a text to find pairs of characters
* that would be encoded as a single character in UTF-32.
*
* If they find such a pair, they change the position to encount for the difference in encoding:
* If the position comes from Rascal, it is too short and must be increased by 1.
* If the position comes from VS Code, it is too long and must be decreased by 1.
*/
export class PositionConverter {
/***************************************
* Rascal -> VS Code *
***************************************/
/**
* Converts the column position from Rascal (UTF-32) to VS Code (UTF-16).
* @param td the text document where the column information is located in.
* @param line the line in which the column to be changed is located.
* @param rascalColumn the column as given by Rascal.
* @returns the column as understood by VS Code.
*/
static rascalToVSCodeColumn(td: vscode.TextDocument, line: number, rascalColumn: number): number {
const fullLine = td.lineAt(line).text;
let result = rascalColumn;
for (let i = 0; i < fullLine.length && i < result; i++) {
const c = fullLine.charCodeAt(i);
if (PositionConverter.isHighSurrogate(c) && (i + 1) < fullLine.length && PositionConverter.isLowSurrogate(fullLine.charCodeAt(i + 1))) {
i++;
result++;
}
}
return result;
}
/**
* Converts the offset and length position from Rascal (UTF-32) to VS Code (UTF-16).
* @param td the text document where the information is located in.
* @param offset the offset as given by Rascal.
* @param length the length as given by Rascal.
* @returns the offset and length as understood by VS Code.
*/
static rascalToVSCodeOffsetLength(td: vscode.TextDocument, offset: number, length: number): [number, number] {
const fullText = td.getText();
let endOffset = offset + length;
for (let i = 0; i < fullText.length && i < endOffset; i++) {
const c = fullText.charCodeAt(i);
if (PositionConverter.isHighSurrogate(c) && (i + 1) < fullText.length && PositionConverter.isLowSurrogate(fullText.charCodeAt(i + 1))) {
if (i <= offset) { // the character comes before the offset, so it must shift the offset
offset++;
}
endOffset++;
i++;
}
}
return [offset, endOffset];
}
/**
* Converts a range from Rascal (UTF-32) to VS Code (UTF-16).
* A range is given in the form of a SourceLocation which can encode a range
* either as an offset and length or using pairs of line and column
* for begin and end of the range.
* @param td the text document where the information is located in.
* @param sloc a source location as given by Rascal.
* @returns the range as understood by VS Code or `undefined`, if the range is not specified correctly.
*/
static rascalToVSCodeRange(td: vscode.TextDocument, sloc: SourceLocation): vscode.Range | undefined {
if (sloc.beginLineColumn && sloc.endLineColumn) {
const beginLine = sloc.beginLineColumn[0] - 1;
const endLine = sloc.endLineColumn[0] - 1;
return new vscode.Range(
beginLine,
PositionConverter.rascalToVSCodeColumn(td, beginLine, sloc.beginLineColumn[1]),
endLine,
PositionConverter.rascalToVSCodeColumn(td, endLine, sloc.endLineColumn[1])
);
}
else if (sloc.offsetLength) {
const rangePositions = PositionConverter.rascalToVSCodeOffsetLength(td, sloc.offsetLength[0], sloc.offsetLength[1]);
return new vscode.Range(
td.positionAt(rangePositions[0]),
td.positionAt(rangePositions[1])
);
}
return undefined;
}
/***************************************
* VS Code -> Rascal *
***************************************/
/**
* Converts the column position from VS Code (UTF-16) to Rascal (UTF-32).
* @param td the text document where the column information is located in.
* @param line the line in which the column to be changed is located.
* @param columnVSCode the column as given by VS Code.
* @returns the column as understood by Rascal.
*/
static vsCodeToRascalColumn(td: vscode.TextDocument, line: number, columnVSCode: number): number {
const fullLine = td.lineAt(line).text;
let lengthRascal = columnVSCode;
for (let i = 0; i < columnVSCode - 1; i++) {
const c = fullLine.charCodeAt(i);
if (PositionConverter.isHighSurrogate(c) && PositionConverter.isLowSurrogate(fullLine.charCodeAt(i + 1))) {
lengthRascal--;
i++; // the following letter is known to be the low surrogate -> we can skip it
}
}
return lengthRascal;
}
/**
* Converts the offset and length position from VS Code (UTF-16) to Rascal (UTF-32).
* @param td the text document where the information is located in.
* @param offset the offset as given by VS Code.
* @param length the length as given by VS Code.
* @returns the offset and length as understood by Rascal.
*/
static vsCodeToRascalOffsetLength(td: vscode.TextDocument, offset: number, length: number): [number, number] {
const fullText = td.getText();
const endOffset = offset + length;
let newEndOffset = endOffset;
for (let i = 0; i < endOffset - 1; i++) {
const c = fullText.charCodeAt(i);
if (PositionConverter.isHighSurrogate(c) && PositionConverter.isLowSurrogate(fullText.charCodeAt(i + 1))) {
if (i <= offset) {
offset--;
}
newEndOffset--;
i++; // the following letter is known to be the low surrogate -> we can skip it
}
}
return [offset, newEndOffset];
}
/**
* Converts a range from VS Code (UTF-16) to Rascal (UTF-32).
* A range is given in the form of a SourceLocation which can encode a range
* either as an offset and length or using pairs of line and column
* for begin and end of the range.
* @param td the text document where the information is located in.
* @param sloc a source location as given by VS Code.
* @returns the range as understood by Rascal or `undefined`, if the range is not specified correctly.
*/
static vsCodeToRascalRange(td: vscode.TextDocument, sloc: SourceLocation): vscode.Range | undefined {
if (sloc.beginLineColumn && sloc.endLineColumn) {
const beginLine = sloc.beginLineColumn[0];
const endLine = sloc.endLineColumn[0];
return new vscode.Range(
beginLine,
PositionConverter.vsCodeToRascalColumn(td, beginLine, sloc.beginLineColumn[1]),
endLine,
PositionConverter.vsCodeToRascalColumn(td, endLine, sloc.endLineColumn[1])
);
}
else if (sloc.offsetLength) {
const rangePositions = PositionConverter.vsCodeToRascalOffsetLength(td, sloc.offsetLength[0], sloc.offsetLength[1]);
return new vscode.Range(
td.positionAt(rangePositions[0]),
td.positionAt(rangePositions[1])
);
}
return undefined;
}
/***************************************
* Util *
***************************************/
// from https://github.com/microsoft/vscode/blob/main/src/vs/base/common/strings.ts
static isHighSurrogate(charCode: number): boolean {
return (55296 <= charCode && charCode <= 56319);
}
// from https://github.com/microsoft/vscode/blob/main/src/vs/base/common/strings.ts
static isLowSurrogate(charCode: number): boolean {
return (56320 <= charCode && charCode <= 57343);
}
}