@astrojs/ts-plugin 0.2.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +5 -2
- package/CHANGELOG.md +16 -0
- package/LICENSE +36 -0
- package/README.md +1 -1
- package/dist/astro-snapshots.js +48 -47
- package/dist/astro-sys.js +22 -35
- package/dist/astro2tsx.js +17 -41
- package/dist/index.js +38 -39
- package/dist/language-service/completions.js +42 -42
- package/dist/language-service/definition.js +22 -40
- package/dist/language-service/diagnostics.js +15 -18
- package/dist/language-service/file-references.js +52 -0
- package/dist/language-service/find-references.js +20 -38
- package/dist/language-service/implementation.js +18 -37
- package/dist/language-service/index.js +26 -42
- package/dist/language-service/line-column-offset.js +44 -0
- package/dist/language-service/rename.js +24 -37
- package/dist/logger.js +18 -5
- package/dist/module-loader.js +28 -22
- package/dist/project-astro-files.js +125 -0
- package/dist/utils.js +34 -19
- package/dist/workers/TSXService.js +39 -0
- package/dist/workers/TSXWorker.js +9 -0
- package/package.json +36 -27
- package/src/astro-snapshots.ts +270 -291
- package/src/astro-sys.ts +22 -22
- package/src/astro2tsx.ts +5 -50
- package/src/index.ts +49 -60
- package/src/language-service/completions.ts +56 -64
- package/src/language-service/definition.ts +35 -42
- package/src/language-service/diagnostics.ts +32 -33
- package/src/language-service/file-references.ts +31 -0
- package/src/language-service/find-references.ts +67 -76
- package/src/language-service/implementation.ts +27 -34
- package/src/language-service/index.ts +17 -32
- package/src/language-service/line-column-offset.ts +21 -0
- package/src/language-service/rename.ts +38 -48
- package/src/logger.ts +32 -36
- package/src/module-loader.ts +124 -124
- package/src/project-astro-files.ts +130 -0
- package/src/utils.ts +54 -45
- package/src/workers/TSXService.ts +11 -0
- package/src/workers/TSXWorker.ts +8 -0
- package/test/fixtures/MyAstroComponent.astro +5 -0
- package/test/fixtures/script.ts +9 -0
- package/test/fixtures/tsconfig.json +3 -0
- package/test/runTest.js +41 -0
- package/test/suite/extension.test.js +68 -0
- package/test/suite/index.js +38 -0
- package/test/tsconfig.json +14 -0
- package/test/utils.js +65 -0
- package/tsconfig.json +2 -1
- package/dist/source-mapper.js +0 -114
- package/src/source-mapper.ts +0 -123
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const { expect } = require('chai');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const vscode = require('vscode');
|
|
5
|
+
|
|
6
|
+
suite('Extension Test Suite', () => {
|
|
7
|
+
vscode.window.showInformationMessage('Start all tests.');
|
|
8
|
+
|
|
9
|
+
// TypeScript takes a while to wake up and there's unfortunately no good way to wait for it
|
|
10
|
+
async function waitForTS(command, commandArgs, condition) {
|
|
11
|
+
for (let i = 0; i < 1000; i++) {
|
|
12
|
+
const commandResult = await vscode.commands.executeCommand(command, ...commandArgs);
|
|
13
|
+
if (condition(commandResult)) {
|
|
14
|
+
return commandResult;
|
|
15
|
+
}
|
|
16
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
17
|
+
}
|
|
18
|
+
throw new Error(`TypeScript plugin never started or condition never resolved for command ${command}`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
test('extension is enabled', async () => {
|
|
22
|
+
const ext = vscode.extensions.getExtension('astro-build.astro-vscode');
|
|
23
|
+
const activate = await ext?.activate();
|
|
24
|
+
|
|
25
|
+
assert.notStrictEqual(activate, undefined);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('can find references inside Astro files', async () => {
|
|
29
|
+
const doc = await vscode.workspace.openTextDocument(vscode.Uri.file(path.join(__dirname, '../fixtures/script.ts')));
|
|
30
|
+
|
|
31
|
+
const references = await waitForTS(
|
|
32
|
+
'vscode.executeReferenceProvider',
|
|
33
|
+
[doc.uri, new vscode.Position(0, 18)],
|
|
34
|
+
(result) => result.length > 1
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
const hasAstroRef = references.some((ref) => ref.uri.path.includes('MyAstroComponent.astro'));
|
|
38
|
+
expect(hasAstroRef).to.be.true;
|
|
39
|
+
}).timeout(22000);
|
|
40
|
+
|
|
41
|
+
test('can get completions for Astro components', async () => {
|
|
42
|
+
const doc = await vscode.workspace.openTextDocument(vscode.Uri.file(path.join(__dirname, '../fixtures/script.ts')));
|
|
43
|
+
|
|
44
|
+
const completions = await waitForTS(
|
|
45
|
+
'vscode.executeCompletionItemProvider',
|
|
46
|
+
[doc.uri, new vscode.Position(4, 12)],
|
|
47
|
+
(result) => result.items.length > 0
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
const hasAstroCompletion = completions.items.some((item) => {
|
|
51
|
+
return item.insertText === 'MyAstroComponent';
|
|
52
|
+
});
|
|
53
|
+
expect(hasAstroCompletion).to.be.true;
|
|
54
|
+
}).timeout(12000);
|
|
55
|
+
|
|
56
|
+
test('can get implementations inside Astro files', async () => {
|
|
57
|
+
const doc = await vscode.workspace.openTextDocument(vscode.Uri.file(path.join(__dirname, '../fixtures/script.ts')));
|
|
58
|
+
|
|
59
|
+
const implementations = await waitForTS(
|
|
60
|
+
'vscode.executeImplementationProvider',
|
|
61
|
+
[doc.uri, new vscode.Position(6, 15)],
|
|
62
|
+
(result) => result.length > 1
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
const hasAstroImplementation = implementations.some((impl) => impl.uri.path.includes('MyAstroComponent'));
|
|
66
|
+
expect(hasAstroImplementation).to.be.true;
|
|
67
|
+
}).timeout(12000);
|
|
68
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const Mocha = require('mocha');
|
|
3
|
+
const glob = require('glob');
|
|
4
|
+
|
|
5
|
+
exports.run = function () {
|
|
6
|
+
// Create the mocha test
|
|
7
|
+
const mocha = new Mocha({
|
|
8
|
+
ui: 'tdd',
|
|
9
|
+
color: true,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
const testsRoot = path.resolve(__dirname, '..');
|
|
13
|
+
|
|
14
|
+
return new Promise((c, e) => {
|
|
15
|
+
glob('**/**.test.js', { cwd: testsRoot }, (err, files) => {
|
|
16
|
+
if (err) {
|
|
17
|
+
return e(err);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Add files to the test suite
|
|
21
|
+
files.forEach((f) => mocha.addFile(path.resolve(testsRoot, f)));
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
// Run the mocha test
|
|
25
|
+
mocha.run((failures) => {
|
|
26
|
+
if (failures > 0) {
|
|
27
|
+
e(new Error(`${failures} tests failed.`));
|
|
28
|
+
} else {
|
|
29
|
+
c();
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
33
|
+
} catch (err) {
|
|
34
|
+
e(err);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"module": "ES2020",
|
|
5
|
+
"target": "ES2020",
|
|
6
|
+
"moduleResolution": "node",
|
|
7
|
+
"allowJs": true,
|
|
8
|
+
"checkJs": false,
|
|
9
|
+
"rootDir": ".",
|
|
10
|
+
"emitDeclarationOnly": false,
|
|
11
|
+
"noEmit": true
|
|
12
|
+
},
|
|
13
|
+
"include": ["**/*.js"]
|
|
14
|
+
}
|
package/test/utils.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/* MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Microsoft Corporation. All rights reserved.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE */
|
|
22
|
+
|
|
23
|
+
// @vscode/test-electron doesn't export this, so copying it here
|
|
24
|
+
// https://github.com/microsoft/vscode-test/blob/c6092b087a9c795c6d2097e864ab13e89d825226/lib/util.ts
|
|
25
|
+
|
|
26
|
+
const path = require('path');
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @type {string}
|
|
30
|
+
*/
|
|
31
|
+
let systemDefaultPlatform;
|
|
32
|
+
const windowsPlatforms = new Set(['win32-archive', 'win32-x64-archive', 'win32-arm64-archive']);
|
|
33
|
+
const darwinPlatforms = new Set(['darwin-arm64', 'darwin']);
|
|
34
|
+
|
|
35
|
+
switch (process.platform) {
|
|
36
|
+
case 'darwin':
|
|
37
|
+
systemDefaultPlatform = process.arch === 'arm64' ? 'darwin-arm64' : 'darwin';
|
|
38
|
+
break;
|
|
39
|
+
case 'win32':
|
|
40
|
+
systemDefaultPlatform =
|
|
41
|
+
process.arch === 'arm64'
|
|
42
|
+
? 'win32-arm64-archive'
|
|
43
|
+
: process.arch === 'ia32'
|
|
44
|
+
? 'win32-archive'
|
|
45
|
+
: 'win32-x64-archive';
|
|
46
|
+
break;
|
|
47
|
+
default:
|
|
48
|
+
systemDefaultPlatform =
|
|
49
|
+
process.arch === 'arm64' ? 'linux-arm64' : process.arch === 'arm' ? 'linux-armhf' : 'linux-x64';
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @param {string} dir
|
|
54
|
+
*/
|
|
55
|
+
function downloadDirToExecutablePath(dir) {
|
|
56
|
+
if (windowsPlatforms.has(systemDefaultPlatform)) {
|
|
57
|
+
return path.resolve(dir, 'Code.exe');
|
|
58
|
+
} else if (darwinPlatforms.has(systemDefaultPlatform)) {
|
|
59
|
+
return path.resolve(dir, 'Visual Studio Code.app/Contents/MacOS/Electron');
|
|
60
|
+
} else {
|
|
61
|
+
return path.resolve(dir, 'code');
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
module.exports = { downloadDirToExecutablePath };
|
package/tsconfig.json
CHANGED
package/dist/source-mapper.js
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
var __create = Object.create;
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __markAsModule = (target) => __defProp(target, "__esModule", {value: true});
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, {get: all[name], enumerable: true});
|
|
11
|
-
};
|
|
12
|
-
var __reExport = (target, module2, desc) => {
|
|
13
|
-
if (module2 && typeof module2 === "object" || typeof module2 === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(module2))
|
|
15
|
-
if (!__hasOwnProp.call(target, key) && key !== "default")
|
|
16
|
-
__defProp(target, key, {get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable});
|
|
17
|
-
}
|
|
18
|
-
return target;
|
|
19
|
-
};
|
|
20
|
-
var __toModule = (module2) => {
|
|
21
|
-
return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? {get: () => module2.default, enumerable: true} : {value: module2, enumerable: true})), module2);
|
|
22
|
-
};
|
|
23
|
-
__markAsModule(exports);
|
|
24
|
-
__export(exports, {
|
|
25
|
-
SourceMapper: () => SourceMapper
|
|
26
|
-
});
|
|
27
|
-
var import_sourcemap_codec = __toModule(require("sourcemap-codec"));
|
|
28
|
-
function binaryInsert(array, value, key) {
|
|
29
|
-
if (key === 0)
|
|
30
|
-
key = "0";
|
|
31
|
-
const index = 1 + binarySearch(array, key ? value[key] : value, key);
|
|
32
|
-
let i = array.length;
|
|
33
|
-
while (index !== i--)
|
|
34
|
-
array[1 + i] = array[i];
|
|
35
|
-
array[index] = value;
|
|
36
|
-
}
|
|
37
|
-
function binarySearch(array, target, key) {
|
|
38
|
-
if (!array || array.length === 0)
|
|
39
|
-
return -1;
|
|
40
|
-
if (key === 0)
|
|
41
|
-
key = "0";
|
|
42
|
-
let low = 0;
|
|
43
|
-
let high = array.length - 1;
|
|
44
|
-
while (low <= high) {
|
|
45
|
-
const i = low + (high - low >> 1);
|
|
46
|
-
const item = key === void 0 ? array[i] : array[i][key];
|
|
47
|
-
if (item === target)
|
|
48
|
-
return i;
|
|
49
|
-
if (item < target)
|
|
50
|
-
low = i + 1;
|
|
51
|
-
else
|
|
52
|
-
high = i - 1;
|
|
53
|
-
}
|
|
54
|
-
if ((low = ~low) < 0)
|
|
55
|
-
low = ~low - 1;
|
|
56
|
-
return low;
|
|
57
|
-
}
|
|
58
|
-
class SourceMapper {
|
|
59
|
-
constructor(mappings) {
|
|
60
|
-
if (typeof mappings === "string")
|
|
61
|
-
this.mappings = (0, import_sourcemap_codec.decode)(mappings);
|
|
62
|
-
else
|
|
63
|
-
this.mappings = mappings;
|
|
64
|
-
}
|
|
65
|
-
getOriginalPosition(position) {
|
|
66
|
-
const lineMap = this.mappings[position.line];
|
|
67
|
-
if (!lineMap) {
|
|
68
|
-
return {line: -1, character: -1};
|
|
69
|
-
}
|
|
70
|
-
const closestMatch = binarySearch(lineMap, position.character, 0);
|
|
71
|
-
const match = lineMap[closestMatch];
|
|
72
|
-
if (!match) {
|
|
73
|
-
return {line: -1, character: -1};
|
|
74
|
-
}
|
|
75
|
-
const {2: line, 3: character} = match;
|
|
76
|
-
return {line, character};
|
|
77
|
-
}
|
|
78
|
-
getGeneratedPosition(position) {
|
|
79
|
-
if (!this.reverseMappings)
|
|
80
|
-
this.computeReversed();
|
|
81
|
-
const lineMap = this.reverseMappings[position.line];
|
|
82
|
-
if (!lineMap) {
|
|
83
|
-
return {line: -1, character: -1};
|
|
84
|
-
}
|
|
85
|
-
const closestMatch = binarySearch(lineMap, position.character, 0);
|
|
86
|
-
const match = lineMap[closestMatch];
|
|
87
|
-
if (!match) {
|
|
88
|
-
return {line: -1, character: -1};
|
|
89
|
-
}
|
|
90
|
-
const {1: line, 2: character} = match;
|
|
91
|
-
return {line, character};
|
|
92
|
-
}
|
|
93
|
-
computeReversed() {
|
|
94
|
-
this.reverseMappings = {};
|
|
95
|
-
for (let generated_line = 0; generated_line !== this.mappings.length; generated_line++) {
|
|
96
|
-
for (const {0: generated_index, 2: original_line, 3: original_character_index} of this.mappings[generated_line]) {
|
|
97
|
-
const reordered_char = [
|
|
98
|
-
original_character_index,
|
|
99
|
-
generated_line,
|
|
100
|
-
generated_index
|
|
101
|
-
];
|
|
102
|
-
if (original_line in this.reverseMappings)
|
|
103
|
-
binaryInsert(this.reverseMappings[original_line], reordered_char, 0);
|
|
104
|
-
else
|
|
105
|
-
this.reverseMappings[original_line] = [reordered_char];
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
// Annotate the CommonJS export names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QUFTLFFBQVEsa0JBQWtCO0FBQ3BGLGlCQUFXLENBQUUsR0FBRyxpQkFBaUIsR0FBRyxlQUFlLEdBQUcsNkJBQThCLEtBQy9FLFNBQVMsaUJBQWlCO0FBQzNCLGNBQU0saUJBQWdDO0FBQUEsVUFDbEM7QUFBQSxVQUNBO0FBQUEsVUFDQTtBQUFBO0FBRUosWUFBSSxpQkFBaUIsS0FBSztBQUN0Qix1QkFBYSxLQUFLLGdCQUFnQixnQkFBZ0IsZ0JBQWdCO0FBQUE7QUFDakUsZUFBSyxnQkFBZ0IsaUJBQWlCLENBQUM7QUFBQTtBQUFBO0FBQUE7QUFBQTsiLAogICJuYW1lcyI6IFtdCn0K
|
package/src/source-mapper.ts
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import { decode } from 'sourcemap-codec';
|
|
2
|
-
import type ts from 'typescript/lib/tsserverlibrary';
|
|
3
|
-
|
|
4
|
-
type LineChar = ts.LineAndCharacter;
|
|
5
|
-
|
|
6
|
-
export type FileMapping = LineMapping[];
|
|
7
|
-
|
|
8
|
-
type LineMapping = CharacterMapping[]; // FileMapping[generated_line_index] = LineMapping
|
|
9
|
-
|
|
10
|
-
type CharacterMapping = [
|
|
11
|
-
number, // generated character
|
|
12
|
-
number, // original file
|
|
13
|
-
number, // original line
|
|
14
|
-
number // original index
|
|
15
|
-
];
|
|
16
|
-
|
|
17
|
-
type ReorderedChar = [
|
|
18
|
-
original_character: number,
|
|
19
|
-
generated_line: number,
|
|
20
|
-
generated_character: number
|
|
21
|
-
];
|
|
22
|
-
|
|
23
|
-
interface ReorderedMap {
|
|
24
|
-
[original_line: number]: ReorderedChar[];
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function binaryInsert(array: number[], value: number): void;
|
|
28
|
-
function binaryInsert<T extends Record<any, number> | number[]>(
|
|
29
|
-
array: T[],
|
|
30
|
-
value: T,
|
|
31
|
-
key: keyof T
|
|
32
|
-
): void;
|
|
33
|
-
function binaryInsert<A extends Array<Record<any, number>> | number[]>(
|
|
34
|
-
array: A,
|
|
35
|
-
value: A[any],
|
|
36
|
-
key?: keyof (A[any] & object)
|
|
37
|
-
) {
|
|
38
|
-
if (0 === key) key = '0' as keyof A[any];
|
|
39
|
-
const index = 1 + binarySearch(array, (key ? value[key] : value) as number, key);
|
|
40
|
-
let i = array.length;
|
|
41
|
-
while (index !== i--) array[1 + i] = array[i];
|
|
42
|
-
array[index] = value;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function binarySearch<T extends object | number>(
|
|
46
|
-
array: T[],
|
|
47
|
-
target: number,
|
|
48
|
-
key?: keyof (T & object)
|
|
49
|
-
) {
|
|
50
|
-
if (!array || 0 === array.length) return -1;
|
|
51
|
-
if (0 === key) key = '0' as keyof T;
|
|
52
|
-
let low = 0;
|
|
53
|
-
let high = array.length - 1;
|
|
54
|
-
while (low <= high) {
|
|
55
|
-
const i = low + ((high - low) >> 1);
|
|
56
|
-
const item = undefined === key ? array[i] : array[i][key];
|
|
57
|
-
if (item === target) return i;
|
|
58
|
-
if (item < target) low = i + 1;
|
|
59
|
-
else high = i - 1;
|
|
60
|
-
}
|
|
61
|
-
if ((low = ~low) < 0) low = ~low - 1;
|
|
62
|
-
return low;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export class SourceMapper {
|
|
66
|
-
private mappings: FileMapping;
|
|
67
|
-
private reverseMappings?: ReorderedMap;
|
|
68
|
-
|
|
69
|
-
constructor(mappings: FileMapping | string) {
|
|
70
|
-
if (typeof mappings === 'string') this.mappings = decode(mappings) as FileMapping;
|
|
71
|
-
else this.mappings = mappings;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
getOriginalPosition(position: LineChar): LineChar {
|
|
75
|
-
const lineMap = this.mappings[position.line];
|
|
76
|
-
if (!lineMap) {
|
|
77
|
-
return { line: -1, character: -1 };
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const closestMatch = binarySearch(lineMap, position.character, 0);
|
|
81
|
-
const match = lineMap[closestMatch];
|
|
82
|
-
if (!match) {
|
|
83
|
-
return { line: -1, character: -1 };
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const { 2: line, 3: character } = match;
|
|
87
|
-
return { line, character };
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
getGeneratedPosition(position: LineChar): LineChar {
|
|
91
|
-
if (!this.reverseMappings) this.computeReversed();
|
|
92
|
-
const lineMap = this.reverseMappings![position.line];
|
|
93
|
-
if (!lineMap) {
|
|
94
|
-
return { line: -1, character: -1 };
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const closestMatch = binarySearch(lineMap, position.character, 0);
|
|
98
|
-
const match = lineMap[closestMatch];
|
|
99
|
-
if (!match) {
|
|
100
|
-
return { line: -1, character: -1 };
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const { 1: line, 2: character } = match;
|
|
104
|
-
return { line, character };
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
private computeReversed() {
|
|
108
|
-
this.reverseMappings = {} as ReorderedMap;
|
|
109
|
-
for (let generated_line = 0; generated_line !== this.mappings.length; generated_line++) {
|
|
110
|
-
for (const { 0: generated_index, 2: original_line, 3: original_character_index } of this
|
|
111
|
-
.mappings[generated_line]) {
|
|
112
|
-
const reordered_char: ReorderedChar = [
|
|
113
|
-
original_character_index,
|
|
114
|
-
generated_line,
|
|
115
|
-
generated_index
|
|
116
|
-
];
|
|
117
|
-
if (original_line in this.reverseMappings)
|
|
118
|
-
binaryInsert(this.reverseMappings[original_line], reordered_char, 0);
|
|
119
|
-
else this.reverseMappings[original_line] = [reordered_char];
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|