@astrojs/ts-plugin 0.2.1 → 0.3.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 +1 -1
- package/CHANGELOG.md +6 -0
- package/README.md +1 -1
- package/dist/astro-snapshots.js +1 -1
- package/dist/astro-sys.js +1 -1
- package/dist/astro2tsx.js +2 -22
- package/dist/index.js +2 -2
- package/dist/language-service/completions.js +1 -1
- package/dist/language-service/definition.js +1 -1
- package/dist/language-service/diagnostics.js +1 -1
- package/dist/language-service/find-references.js +1 -1
- package/dist/language-service/implementation.js +1 -1
- package/dist/language-service/index.js +1 -1
- package/dist/language-service/rename.js +1 -1
- package/dist/logger.js +1 -1
- package/dist/module-loader.js +1 -1
- package/dist/source-mapper.js +2 -6
- package/dist/utils.js +1 -1
- package/package.json +25 -26
- package/src/astro-snapshots.ts +277 -289
- package/src/astro-sys.ts +21 -21
- package/src/astro2tsx.ts +27 -31
- package/src/index.ts +42 -53
- package/src/language-service/completions.ts +44 -63
- package/src/language-service/definition.ts +37 -39
- package/src/language-service/diagnostics.ts +31 -31
- package/src/language-service/find-references.ts +66 -75
- package/src/language-service/implementation.ts +29 -31
- package/src/language-service/index.ts +27 -27
- package/src/language-service/rename.ts +37 -47
- package/src/logger.ts +32 -36
- package/src/module-loader.ts +102 -110
- package/src/source-mapper.ts +83 -99
- package/src/utils.ts +39 -39
package/src/source-mapper.ts
CHANGED
|
@@ -8,116 +8,100 @@ export type FileMapping = LineMapping[];
|
|
|
8
8
|
type LineMapping = CharacterMapping[]; // FileMapping[generated_line_index] = LineMapping
|
|
9
9
|
|
|
10
10
|
type CharacterMapping = [
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
number, // generated character
|
|
12
|
+
number, // original file
|
|
13
|
+
number, // original line
|
|
14
|
+
number // original index
|
|
15
15
|
];
|
|
16
16
|
|
|
17
|
-
type ReorderedChar = [
|
|
18
|
-
original_character: number,
|
|
19
|
-
generated_line: number,
|
|
20
|
-
generated_character: number
|
|
21
|
-
];
|
|
17
|
+
type ReorderedChar = [original_character: number, generated_line: number, generated_character: number];
|
|
22
18
|
|
|
23
19
|
interface ReorderedMap {
|
|
24
|
-
|
|
20
|
+
[original_line: number]: ReorderedChar[];
|
|
25
21
|
}
|
|
26
22
|
|
|
27
23
|
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;
|
|
24
|
+
function binaryInsert<T extends Record<any, number> | number[]>(array: T[], value: T, key: keyof T): void;
|
|
33
25
|
function binaryInsert<A extends Array<Record<any, number>> | number[]>(
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
26
|
+
array: A,
|
|
27
|
+
value: A[any],
|
|
28
|
+
key?: keyof (A[any] & object)
|
|
37
29
|
) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
30
|
+
if (0 === key) key = '0' as keyof A[any];
|
|
31
|
+
const index = 1 + binarySearch(array, (key ? value[key] : value) as number, key);
|
|
32
|
+
let i = array.length;
|
|
33
|
+
while (index !== i--) array[1 + i] = array[i];
|
|
34
|
+
array[index] = value;
|
|
43
35
|
}
|
|
44
36
|
|
|
45
|
-
function binarySearch<T extends object | number>(
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
else high = i - 1;
|
|
60
|
-
}
|
|
61
|
-
if ((low = ~low) < 0) low = ~low - 1;
|
|
62
|
-
return low;
|
|
37
|
+
function binarySearch<T extends object | number>(array: T[], target: number, key?: keyof (T & object)) {
|
|
38
|
+
if (!array || 0 === array.length) return -1;
|
|
39
|
+
if (0 === key) key = '0' as keyof T;
|
|
40
|
+
let low = 0;
|
|
41
|
+
let high = array.length - 1;
|
|
42
|
+
while (low <= high) {
|
|
43
|
+
const i = low + ((high - low) >> 1);
|
|
44
|
+
const item = undefined === key ? array[i] : array[i][key];
|
|
45
|
+
if (item === target) return i;
|
|
46
|
+
if (item < target) low = i + 1;
|
|
47
|
+
else high = i - 1;
|
|
48
|
+
}
|
|
49
|
+
if ((low = ~low) < 0) low = ~low - 1;
|
|
50
|
+
return low;
|
|
63
51
|
}
|
|
64
52
|
|
|
65
53
|
export class SourceMapper {
|
|
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
|
-
}
|
|
54
|
+
private mappings: FileMapping;
|
|
55
|
+
private reverseMappings?: ReorderedMap;
|
|
56
|
+
|
|
57
|
+
constructor(mappings: FileMapping | string) {
|
|
58
|
+
if (typeof mappings === 'string') this.mappings = decode(mappings) as FileMapping;
|
|
59
|
+
else this.mappings = mappings;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
getOriginalPosition(position: LineChar): LineChar {
|
|
63
|
+
const lineMap = this.mappings[position.line];
|
|
64
|
+
if (!lineMap) {
|
|
65
|
+
return { line: -1, character: -1 };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const closestMatch = binarySearch(lineMap, position.character, 0);
|
|
69
|
+
const match = lineMap[closestMatch];
|
|
70
|
+
if (!match) {
|
|
71
|
+
return { line: -1, character: -1 };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const { 2: line, 3: character } = match;
|
|
75
|
+
return { line, character };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
getGeneratedPosition(position: LineChar): LineChar {
|
|
79
|
+
if (!this.reverseMappings) this.computeReversed();
|
|
80
|
+
const lineMap = this.reverseMappings![position.line];
|
|
81
|
+
if (!lineMap) {
|
|
82
|
+
return { line: -1, character: -1 };
|
|
83
|
+
}
|
|
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
|
+
|
|
91
|
+
const { 1: line, 2: character } = match;
|
|
92
|
+
return { line, character };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
private computeReversed() {
|
|
96
|
+
this.reverseMappings = {} as ReorderedMap;
|
|
97
|
+
for (let generated_line = 0; generated_line !== this.mappings.length; generated_line++) {
|
|
98
|
+
for (const { 0: generated_index, 2: original_line, 3: original_character_index } of this.mappings[
|
|
99
|
+
generated_line
|
|
100
|
+
]) {
|
|
101
|
+
const reordered_char: ReorderedChar = [original_character_index, generated_line, generated_index];
|
|
102
|
+
if (original_line in this.reverseMappings) binaryInsert(this.reverseMappings[original_line], reordered_char, 0);
|
|
103
|
+
else this.reverseMappings[original_line] = [reordered_char];
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
package/src/utils.ts
CHANGED
|
@@ -1,66 +1,66 @@
|
|
|
1
1
|
export function isAstroFilePath(filePath: string) {
|
|
2
|
-
|
|
2
|
+
return filePath.endsWith('.astro');
|
|
3
3
|
}
|
|
4
4
|
|
|
5
5
|
export function isVirtualAstroFilePath(filePath: string) {
|
|
6
|
-
|
|
6
|
+
return filePath.endsWith('.astro.ts');
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
export function toRealAstroFilePath(filePath: string) {
|
|
10
|
-
|
|
10
|
+
return filePath.slice(0, -'.ts'.length);
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
export function ensureRealAstroFilePath(filePath: string) {
|
|
14
|
-
|
|
14
|
+
return isVirtualAstroFilePath(filePath) ? toRealAstroFilePath(filePath) : filePath;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
export function isNotNullOrUndefined<T>(val: T | undefined | null): val is T {
|
|
18
|
-
|
|
18
|
+
return val !== undefined && val !== null;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
|
-
* Checks if this a section that should be completely ignored
|
|
23
|
-
* because it's purely generated.
|
|
24
|
-
*/
|
|
22
|
+
* Checks if this a section that should be completely ignored
|
|
23
|
+
* because it's purely generated.
|
|
24
|
+
*/
|
|
25
25
|
export function isInGeneratedCode(text: string, start: number, end: number) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
26
|
+
const lineStart = text.lastIndexOf('\n', start);
|
|
27
|
+
const lineEnd = text.indexOf('\n', end);
|
|
28
|
+
const lastStart = text.substring(lineStart, start).lastIndexOf('/*Ωignore_startΩ*/');
|
|
29
|
+
const lastEnd = text.substring(lineStart, start).lastIndexOf('/*Ωignore_endΩ*/');
|
|
30
|
+
return lastStart > lastEnd && text.substring(end, lineEnd).includes('/*Ωignore_endΩ*/');
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
|
-
* Checks that this isn't a text span that should be completely ignored
|
|
35
|
-
* because it's purely generated.
|
|
36
|
-
*/
|
|
34
|
+
* Checks that this isn't a text span that should be completely ignored
|
|
35
|
+
* because it's purely generated.
|
|
36
|
+
*/
|
|
37
37
|
export function isNoTextSpanInGeneratedCode(text: string, span: ts.TextSpan) {
|
|
38
|
-
|
|
38
|
+
return !isInGeneratedCode(text, span.start, span.start + span.length);
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
/**
|
|
42
|
-
* Replace all occurrences of a string within an object with another string,
|
|
43
|
-
*/
|
|
42
|
+
* Replace all occurrences of a string within an object with another string,
|
|
43
|
+
*/
|
|
44
44
|
export function replaceDeep<T extends Record<string, any>>(
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
obj: T,
|
|
46
|
+
searchStr: string | RegExp,
|
|
47
|
+
replacementStr: string
|
|
48
48
|
): T {
|
|
49
|
-
|
|
49
|
+
return _replaceDeep(obj);
|
|
50
50
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
51
|
+
function _replaceDeep(_obj: any): any {
|
|
52
|
+
if (typeof _obj === 'string') {
|
|
53
|
+
return _obj.replace(searchStr, replacementStr);
|
|
54
|
+
}
|
|
55
|
+
if (Array.isArray(_obj)) {
|
|
56
|
+
return _obj.map((entry) => _replaceDeep(entry));
|
|
57
|
+
}
|
|
58
|
+
if (typeof _obj === 'object') {
|
|
59
|
+
return Object.keys(_obj).reduce((_o, key) => {
|
|
60
|
+
_o[key] = _replaceDeep(_obj[key]);
|
|
61
|
+
return _o;
|
|
62
|
+
}, {} as any);
|
|
63
|
+
}
|
|
64
|
+
return _obj;
|
|
65
|
+
}
|
|
66
|
+
}
|