@arkts/language-plugin
Version:
ArkTS Volar Language Plugin.
195 lines (190 loc) • 5.61 kB
JavaScript
import { TsmVirtualCode, replaceRange } from "ts-macro";
import path from "node:path";
//#region src/$$this-fixer-plugin.ts
function $$thisFixerPlugin() {
const $$thisRegex = /\$\$this/g;
return {
name: "ets:$$this-fixer",
resolveVirtualCode(virtualCode) {
const matches = virtualCode.ast.getText().matchAll($$thisRegex);
for (const match of matches) {
const start = match.index;
const end = start + match[0].length;
replaceRange(virtualCode.codes, start, end, [
"this",
match[0],
start + 2,
{
completion: true,
format: true,
navigation: true,
semantic: true,
structure: true,
verification: true
}
]);
}
}
};
}
//#endregion
//#region src/es-object-plugin.ts
function ESObjectPlugin() {
return {
name: "ets:es-object",
resolveVirtualCode(virtualCode) {
virtualCode.codes.push(`\n/**\n * 应该尽量不使用此类型。\n * ---\n * Should not use this type as much as possible. \n */ \n type ESObject = any`);
return virtualCode;
}
};
}
//#endregion
//#region src/ets-code.ts
function createVirtualCode(snapshot, languageId, data) {
return {
id: "root",
languageId,
snapshot,
mappings: [{
sourceOffsets: [0],
generatedOffsets: [0],
lengths: [snapshot.getLength()],
data
}]
};
}
function createEmptyVirtualCode(snapshot, languageId, data, options) {
return {
id: "root",
languageId,
snapshot: {
getText: () => "",
getLength: () => 0,
getChangeRange: () => void 0
},
mappings: [{
sourceOffsets: [0],
generatedOffsets: [0],
lengths: [snapshot.getLength()],
data
}],
...options
};
}
var ETSVirtualCode = class extends TsmVirtualCode {
filePath;
constructor(filePath, sourceFile, languageId, plugins) {
super(filePath, sourceFile, languageId, plugins);
this.filePath = filePath;
}
};
//#endregion
//#region src/language-plugin.ts
function ETSLanguagePlugin(tsOrEts, { excludePaths = [], tsdk = "" } = {}) {
const isETSServerMode = isEts(tsOrEts);
function getLanguageId(uri) {
const filePath = typeof uri === "string" ? uri : uri.fsPath;
if (filePath.endsWith(".ets")) return "ets";
if (filePath.endsWith(".ts")) return "typescript";
if (filePath.endsWith(".json") || filePath.endsWith(".json5") || filePath.endsWith(".jsonc") || filePath.endsWith(".tsbuildinfo")) return "json";
}
function getScriptKindByFilePath(filePath, defaultExtension = ".ets") {
if (!filePath) return [7, defaultExtension];
if (filePath.endsWith(".d.ts")) return [3, ".d.ts"];
if (filePath.endsWith(".d.ets")) return [8, ".d.ets"];
if (filePath.endsWith(".d.cts")) return [3, ".d.cts"];
if (filePath.endsWith(".d.mts")) return [3, ".d.mts"];
const extension = path.extname(filePath);
switch (extension) {
case ".ts":
case ".cts":
case ".mts": return [3, extension];
case ".ets": return [8, extension];
default: return [7, extension];
}
}
if (isETSServerMode) return {
getLanguageId,
createVirtualCode(uri, languageId, snapshot) {
const filePath = path.resolve(typeof uri === "string" ? uri : uri.fsPath);
if (languageId === "ets") return new ETSVirtualCode(filePath, tsOrEts.createSourceFile(filePath, snapshot.getText(0, snapshot.getLength()), 99), "typescript", [$$thisFixerPlugin(), ESObjectPlugin()]);
if (filePath.endsWith(".json") || filePath.endsWith(".json5") || filePath.endsWith(".jsonc") || languageId === "json" || languageId === "jsonc") return getFullVirtualCode(snapshot, languageId);
if (filePath.startsWith(tsdk)) return getDisabledVirtualCode(snapshot, languageId);
},
typescript: {
extraFileExtensions: [{
extension: "ets",
isMixedContent: false,
scriptKind: 8
}, {
extension: "d.ets",
isMixedContent: false,
scriptKind: 8
}],
resolveHiddenExtensions: true,
getServiceScript(root) {
const [scriptKind, extension] = getScriptKindByFilePath(root.filePath);
return {
code: root,
extension,
scriptKind
};
}
}
};
return {
getLanguageId,
createVirtualCode(uri, languageId, snapshot) {
const filePath = path.resolve(typeof uri === "string" ? uri : uri.fsPath);
const isInExcludePath = excludePaths.some((excludePath) => filePath.startsWith(excludePath));
if ((filePath.endsWith(".d.ts") || filePath.endsWith(".d.ets")) && isInExcludePath) return getFullDisabledVirtualCode(snapshot, languageId, { filePath });
},
typescript: {
extraFileExtensions: [],
getServiceScript(root) {
const [scriptKind, extension] = getScriptKindByFilePath(root.filePath, ".ts");
return {
code: root,
extension,
scriptKind
};
}
}
};
}
function isEts(tsOrEts) {
return "ETS" in tsOrEts.ScriptKind && tsOrEts.ScriptKind.ETS === 8;
}
function getFullVirtualCode(snapshot, languageId) {
return createVirtualCode(snapshot, languageId, {
completion: true,
format: true,
navigation: true,
semantic: true,
structure: true,
verification: true
});
}
function getDisabledVirtualCode(snapshot, languageId) {
return createVirtualCode(snapshot, languageId, {
completion: false,
format: false,
navigation: false,
semantic: false,
structure: false,
verification: false
});
}
function getFullDisabledVirtualCode(snapshot, languageId, options) {
return createEmptyVirtualCode(snapshot, languageId, {
completion: false,
format: false,
navigation: false,
semantic: false,
structure: false,
verification: false
}, options);
}
//#endregion
export { $$thisFixerPlugin, ESObjectPlugin, ETSLanguagePlugin, ETSVirtualCode };
//# sourceMappingURL=index.mjs.map