@arkts/language-plugin
Version:
ArkTS Volar Language Plugin.
154 lines (149 loc) • 4.64 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
const node_path = __toESM(require("node:path"));
require("@volar/typescript");
//#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) {
return {
id: "root",
languageId,
snapshot: {
getText: () => "",
getLength: () => 0,
getChangeRange: () => void 0
},
mappings: [{
sourceOffsets: [0],
generatedOffsets: [0],
lengths: [snapshot.getLength()],
data
}]
};
}
//#endregion
//#region src/utils.ts
const SYMBOL_UNINITIALIZED = Symbol("UNINITIALIZED");
/**
* Store a lazy getter function, and return the cached value.
*
* When first time calling the returned function, the lazy getter function will be executed,
* and the result will be cached.
*
* When second time calling the returned function, the cached value will be returned and the lazy getter function will not be executed again.
*
* @param fn - The lazy getter function.
* @returns The cached value.
*/
function createLazyGetter(fn) {
let value = SYMBOL_UNINITIALIZED;
return () => {
if (value === SYMBOL_UNINITIALIZED) value = fn();
return value;
};
}
//#endregion
//#region src/language-plugin.ts
function isEts(tsOrEts) {
return "ETS" in tsOrEts.ScriptKind && tsOrEts.ScriptKind.ETS === 8;
}
function ETSLanguagePlugin(tsOrEts, { sdkPaths = [], tsdk = "" } = {}) {
const isETSServerMode = isEts(tsOrEts);
const isTSPluginMode = !isETSServerMode;
return {
getLanguageId(uri) {
const filePath = typeof uri === "string" ? uri : uri.fsPath;
if (filePath.endsWith(".ets")) return "ets";
if (filePath.endsWith(".ts")) return "typescript";
return void 0;
},
createVirtualCode(uri, languageId, snapshot) {
const filePath = node_path.default.resolve(typeof uri === "string" ? uri : uri.fsPath);
const isInSdkPath = sdkPaths.some((sdkPath) => filePath.startsWith(sdkPath));
const isInTsdkPath = filePath.startsWith(tsdk);
const isDTS = filePath.endsWith(".d.ts");
const isDETS = filePath.endsWith(".d.ets");
const getFullVitrualCode = createLazyGetter(() => createVirtualCode(snapshot, languageId, {
completion: true,
format: true,
navigation: true,
semantic: true,
structure: true,
verification: true
}));
const getDisabledVirtualCode = createLazyGetter(() => createVirtualCode(snapshot, languageId, {
completion: false,
format: false,
navigation: false,
semantic: false,
structure: false,
verification: false
}));
if (languageId === "ets" && filePath.endsWith(".ets")) return getFullVitrualCode();
if (isETSServerMode && !(isDTS || isDETS) && !isInSdkPath) return getDisabledVirtualCode();
if (isTSPluginMode && (isDTS || isDETS) && isInSdkPath) return createEmptyVirtualCode(snapshot, languageId, {
completion: false,
format: false,
navigation: false,
semantic: false,
structure: false,
verification: false
});
if (isETSServerMode && (isDTS || isDETS) && isInTsdkPath) return getDisabledVirtualCode();
},
typescript: {
extraFileExtensions: isETSServerMode ? [{
extension: "ets",
isMixedContent: false,
scriptKind: 8
}, {
extension: "d.ets",
isMixedContent: false,
scriptKind: 8
}] : [],
resolveHiddenExtensions: true,
getServiceScript(root) {
return {
code: root,
extension: ".ets",
scriptKind: 3
};
}
}
};
}
//#endregion
exports.ETSLanguagePlugin = ETSLanguagePlugin;
//# sourceMappingURL=index.js.map