@gi-tcg/gts-language-plugin 0.2.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/dist/index.d.ts +16 -0
- package/dist/index.js +101 -0
- package/package.json +24 -0
- package/src/env.d.ts +10 -0
- package/src/index.ts +2 -0
- package/src/language_plugin.ts +52 -0
- package/src/virtual_code.ts +60 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { LanguagePlugin } from "@volar/language-core";
|
|
2
|
+
import { URI } from "vscode-uri";
|
|
3
|
+
import * as import_8myeez from "typescript";
|
|
4
|
+
declare function createGtsLanguagePlugin(ts: typeof import_8myeez): LanguagePlugin<URI | string>;
|
|
5
|
+
import { GtsTranspilerError, GtsConfig } from "@gi-tcg/gts-transpiler";
|
|
6
|
+
import { CodeMapping, VirtualCode } from "@volar/language-core";
|
|
7
|
+
import * as ts from "typescript";
|
|
8
|
+
declare class GtsVirtualCode implements VirtualCode {
|
|
9
|
+
id: string;
|
|
10
|
+
languageId: string;
|
|
11
|
+
mappings: CodeMapping[];
|
|
12
|
+
snapshot: ts.IScriptSnapshot;
|
|
13
|
+
errors: GtsTranspilerError[];
|
|
14
|
+
constructor(filename: string, snapshot: ts.IScriptSnapshot, config: Required<GtsConfig>);
|
|
15
|
+
}
|
|
16
|
+
export { createGtsLanguagePlugin, GtsVirtualCode };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// packages/language-plugin/src/language_plugin.ts
|
|
2
|
+
import { resolveGtsConfigSync } from "@gi-tcg/gts-transpiler";
|
|
3
|
+
|
|
4
|
+
// packages/language-plugin/src/virtual_code.ts
|
|
5
|
+
import {
|
|
6
|
+
GtsTranspilerError,
|
|
7
|
+
transpileForVolar
|
|
8
|
+
} from "@gi-tcg/gts-transpiler";
|
|
9
|
+
|
|
10
|
+
class GtsVirtualCode {
|
|
11
|
+
id = "root";
|
|
12
|
+
languageId = "gaming-ts";
|
|
13
|
+
mappings;
|
|
14
|
+
snapshot;
|
|
15
|
+
errors = [];
|
|
16
|
+
constructor(filename, snapshot, config) {
|
|
17
|
+
const source = snapshot.getText(0, snapshot.getLength());
|
|
18
|
+
try {
|
|
19
|
+
const { code, mappings } = transpileForVolar(source, filename, config);
|
|
20
|
+
this.errors = [];
|
|
21
|
+
this.mappings = mappings;
|
|
22
|
+
this.snapshot = {
|
|
23
|
+
getText: (start, end) => code.slice(start, end),
|
|
24
|
+
getLength: () => code.length,
|
|
25
|
+
getChangeRange: () => {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
} catch (e) {
|
|
30
|
+
if (e instanceof GtsTranspilerError) {
|
|
31
|
+
this.errors = [e];
|
|
32
|
+
} else {
|
|
33
|
+
this.errors = [new GtsTranspilerError(e?.message, null)];
|
|
34
|
+
}
|
|
35
|
+
const emptyGeneration = source.split(`
|
|
36
|
+
`).map((line) => " ".repeat(line.length)).join(`
|
|
37
|
+
`);
|
|
38
|
+
this.mappings = [
|
|
39
|
+
{
|
|
40
|
+
sourceOffsets: [0],
|
|
41
|
+
generatedOffsets: [0],
|
|
42
|
+
lengths: [emptyGeneration.length],
|
|
43
|
+
data: {
|
|
44
|
+
verification: true
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
];
|
|
48
|
+
this.snapshot = {
|
|
49
|
+
getText: (start, end) => emptyGeneration.substring(start, end),
|
|
50
|
+
getLength: () => emptyGeneration.length,
|
|
51
|
+
getChangeRange: () => {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// packages/language-plugin/src/language_plugin.ts
|
|
60
|
+
function createGtsLanguagePlugin(ts) {
|
|
61
|
+
return {
|
|
62
|
+
getLanguageId(uri) {
|
|
63
|
+
const path = typeof uri === "string" ? uri : uri.path;
|
|
64
|
+
if (path.endsWith(".gts")) {
|
|
65
|
+
return "gaming-ts";
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
createVirtualCode(uri, languageId, snapshot) {
|
|
69
|
+
const filename = typeof uri === "string" ? uri : uri.path;
|
|
70
|
+
if (languageId === "gaming-ts") {
|
|
71
|
+
const resolvedConfig = resolveGtsConfigSync(filename, {}, {
|
|
72
|
+
cwd: ts.sys.getCurrentDirectory(),
|
|
73
|
+
readFileFn: (path, encoding) => ts.sys.readFile(path, encoding) || ""
|
|
74
|
+
});
|
|
75
|
+
return new GtsVirtualCode(filename, snapshot, resolvedConfig);
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
typescript: {
|
|
79
|
+
extraFileExtensions: [
|
|
80
|
+
{
|
|
81
|
+
extension: "gts",
|
|
82
|
+
isMixedContent: false,
|
|
83
|
+
scriptKind: 7
|
|
84
|
+
}
|
|
85
|
+
],
|
|
86
|
+
getServiceScript(root) {
|
|
87
|
+
if (root.languageId === "gaming-ts") {
|
|
88
|
+
return {
|
|
89
|
+
code: root,
|
|
90
|
+
extension: ".ts",
|
|
91
|
+
scriptKind: 3
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
export {
|
|
99
|
+
createGtsLanguagePlugin,
|
|
100
|
+
GtsVirtualCode
|
|
101
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gi-tcg/gts-language-plugin",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"repository": "https://github.com/piovium/gts.git",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"bun": "./src/index.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"src",
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@gi-tcg/gts-transpiler": "0.2.0",
|
|
20
|
+
"@volar/language-core": "~2.4.0",
|
|
21
|
+
"@volar/typescript": "~2.4.0",
|
|
22
|
+
"vscode-uri": "^3.0.8"
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/env.d.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { type LanguagePlugin } from "@volar/language-core";
|
|
2
|
+
import type {} from "@volar/typescript";
|
|
3
|
+
import type * as ts from "typescript";
|
|
4
|
+
import { URI } from "vscode-uri";
|
|
5
|
+
import { resolveGtsConfigSync, type GtsConfig } from "@gi-tcg/gts-transpiler";
|
|
6
|
+
import { GtsVirtualCode } from "./virtual_code";
|
|
7
|
+
|
|
8
|
+
export function createGtsLanguagePlugin(
|
|
9
|
+
ts: typeof import("typescript"),
|
|
10
|
+
): LanguagePlugin<URI | string> {
|
|
11
|
+
return {
|
|
12
|
+
getLanguageId(uri) {
|
|
13
|
+
const path = typeof uri === "string" ? uri : uri.path;
|
|
14
|
+
if (path.endsWith(".gts")) {
|
|
15
|
+
return "gaming-ts";
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
createVirtualCode(uri, languageId, snapshot) {
|
|
19
|
+
const filename = typeof uri === "string" ? uri : uri.path;
|
|
20
|
+
if (languageId === "gaming-ts") {
|
|
21
|
+
const resolvedConfig = resolveGtsConfigSync(
|
|
22
|
+
filename,
|
|
23
|
+
{},
|
|
24
|
+
{
|
|
25
|
+
cwd: ts.sys.getCurrentDirectory(),
|
|
26
|
+
readFileFn: (path, encoding) =>
|
|
27
|
+
ts.sys.readFile(path, encoding) || "",
|
|
28
|
+
},
|
|
29
|
+
);
|
|
30
|
+
return new GtsVirtualCode(filename, snapshot, resolvedConfig);
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
typescript: {
|
|
34
|
+
extraFileExtensions: [
|
|
35
|
+
{
|
|
36
|
+
extension: "gts",
|
|
37
|
+
isMixedContent: false,
|
|
38
|
+
scriptKind: 7 satisfies ts.ScriptKind.Deferred,
|
|
39
|
+
},
|
|
40
|
+
],
|
|
41
|
+
getServiceScript(root) {
|
|
42
|
+
if (root.languageId === "gaming-ts") {
|
|
43
|
+
return {
|
|
44
|
+
code: root,
|
|
45
|
+
extension: ".ts",
|
|
46
|
+
scriptKind: 3 satisfies ts.ScriptKind.TS,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import {
|
|
2
|
+
GtsTranspilerError,
|
|
3
|
+
transpileForVolar,
|
|
4
|
+
type GtsConfig,
|
|
5
|
+
} from "@gi-tcg/gts-transpiler";
|
|
6
|
+
import { type CodeMapping, type VirtualCode } from "@volar/language-core";
|
|
7
|
+
import type * as ts from "typescript";
|
|
8
|
+
|
|
9
|
+
export class GtsVirtualCode implements VirtualCode {
|
|
10
|
+
id = "root";
|
|
11
|
+
languageId = "gaming-ts";
|
|
12
|
+
mappings: CodeMapping[];
|
|
13
|
+
snapshot: ts.IScriptSnapshot;
|
|
14
|
+
errors: GtsTranspilerError[] = [];
|
|
15
|
+
|
|
16
|
+
constructor(
|
|
17
|
+
filename: string,
|
|
18
|
+
snapshot: ts.IScriptSnapshot,
|
|
19
|
+
config: Required<GtsConfig>,
|
|
20
|
+
) {
|
|
21
|
+
const source = snapshot.getText(0, snapshot.getLength());
|
|
22
|
+
try {
|
|
23
|
+
const { code, mappings } = transpileForVolar(source, filename, config);
|
|
24
|
+
this.errors = [];
|
|
25
|
+
this.mappings = mappings;
|
|
26
|
+
this.snapshot = {
|
|
27
|
+
getText: (start, end) => code.slice(start, end),
|
|
28
|
+
getLength: () => code.length,
|
|
29
|
+
getChangeRange: () => void 0,
|
|
30
|
+
};
|
|
31
|
+
} catch (e) {
|
|
32
|
+
if (e instanceof GtsTranspilerError) {
|
|
33
|
+
this.errors = [e];
|
|
34
|
+
} else {
|
|
35
|
+
this.errors = [new GtsTranspilerError((e as Error)?.message, null)];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const emptyGeneration = source
|
|
39
|
+
.split("\n")
|
|
40
|
+
.map((line) => " ".repeat(line.length))
|
|
41
|
+
.join("\n");
|
|
42
|
+
this.mappings = [
|
|
43
|
+
{
|
|
44
|
+
sourceOffsets: [0],
|
|
45
|
+
generatedOffsets: [0],
|
|
46
|
+
lengths: [emptyGeneration.length],
|
|
47
|
+
data: {
|
|
48
|
+
verification: true,
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
];
|
|
52
|
+
|
|
53
|
+
this.snapshot = {
|
|
54
|
+
getText: (start, end) => emptyGeneration.substring(start, end),
|
|
55
|
+
getLength: () => emptyGeneration.length,
|
|
56
|
+
getChangeRange: () => void 0,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|