@arkts/language-plugin
Version:
ArkTS Volar Language Plugin.
131 lines (127 loc) • 3.61 kB
JavaScript
import path from "node:path";
import "@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 = path.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
export { ETSLanguagePlugin };
//# sourceMappingURL=index.mjs.map