@arkts/language-plugin
Version:
ArkTS Volar Language Plugin.
222 lines (216 loc) • 6.76 kB
JavaScript
//#region rolldown:runtime
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
//#endregion
let ts_macro = require("ts-macro");
let node_path = require("node:path");
node_path = __toESM(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;
(0, ts_macro.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 ts_macro.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 = node_path.default.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 = node_path.default.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 = node_path.default.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
exports.$$thisFixerPlugin = $$thisFixerPlugin;
exports.ESObjectPlugin = ESObjectPlugin;
exports.ETSLanguagePlugin = ETSLanguagePlugin;
exports.ETSVirtualCode = ETSVirtualCode;
//# sourceMappingURL=index.cjs.map