UNPKG

@arkts/language-plugin

Version:
1 lines 13.6 kB
{"version":3,"file":"index.mjs","names":[],"sources":["../src/$$this-fixer-plugin.ts","../src/es-object-plugin.ts","../src/ets-code.ts","../src/language-plugin.ts"],"sourcesContent":["import type { ETSMacroPlugin, ETSVirtualCode } from './ets-code'\nimport { replaceRange } from 'ts-macro'\n\nexport function $$thisFixerPlugin(): ETSMacroPlugin {\n const $$thisRegex = /\\$\\$this/g\n\n return {\n name: 'ets:$$this-fixer',\n resolveVirtualCode(virtualCode: ETSVirtualCode) {\n const text = virtualCode.ast.getText()\n const matches = text.matchAll($$thisRegex)\n for (const match of matches) {\n const start = match.index\n const end = start + match[0].length\n replaceRange(virtualCode.codes, start, end, [\n 'this',\n match[0],\n start + 2,\n {\n completion: true,\n format: true,\n navigation: true,\n semantic: true,\n structure: true,\n verification: true,\n },\n ])\n }\n },\n }\n}\n","import type { ETSMacroPlugin, ETSVirtualCode } from './ets-code'\n\nexport function ESObjectPlugin(): ETSMacroPlugin {\n return {\n name: 'ets:es-object',\n resolveVirtualCode(virtualCode: ETSVirtualCode) {\n virtualCode.codes.push(\n `\\n/**\\n * 应该尽量不使用此类型。\\n * ---\\n * Should not use this type as much as possible. \\n */ \\n type ESObject = any`,\n )\n return virtualCode\n },\n }\n}\n","import type { CodeInformation, VirtualCode } from '@volar/language-core'\nimport type { TsmLanguagePlugin } from 'ts-macro'\nimport type * as ts from 'typescript'\nimport { TsmVirtualCode } from 'ts-macro'\n\nexport function createVirtualCode(snapshot: ts.IScriptSnapshot, languageId: string, data: CodeInformation): VirtualCode {\n return {\n id: 'root',\n languageId,\n snapshot,\n mappings: [{\n sourceOffsets: [0],\n generatedOffsets: [0],\n lengths: [snapshot.getLength()],\n data,\n }],\n }\n}\n\nexport function createEmptyVirtualCode<T extends Record<string, any>>(snapshot: ts.IScriptSnapshot, languageId: string, data: CodeInformation, options?: T): VirtualCode & T {\n return {\n id: 'root',\n languageId,\n snapshot: {\n getText: () => '',\n getLength: () => 0,\n getChangeRange: () => undefined,\n },\n mappings: [{\n sourceOffsets: [0],\n generatedOffsets: [0],\n lengths: [snapshot.getLength()],\n data,\n }],\n ...options,\n } as unknown as VirtualCode & T\n}\n\nexport class ETSVirtualCode extends TsmVirtualCode {\n readonly filePath: string\n\n constructor(filePath: string, sourceFile: ts.SourceFile, languageId: string, plugins: TsmLanguagePlugin[]) {\n super(filePath, sourceFile, languageId, plugins)\n this.filePath = filePath\n }\n}\n\nexport type ETSMacroPlugin = Omit<TsmLanguagePlugin, 'resolveVirtualCode'> & {\n resolveVirtualCode?(virtualCode: ETSVirtualCode): void\n}\n","import type { LanguagePlugin, VirtualCode } from '@volar/language-core'\nimport type { TypeScriptServiceScript } from '@volar/typescript'\nimport type * as ets from 'ohos-typescript'\nimport type * as ts from 'typescript'\nimport type { URI } from 'vscode-uri'\nimport path from 'node:path'\nimport { $$thisFixerPlugin } from './$$this-fixer-plugin'\nimport { ESObjectPlugin } from './es-object-plugin'\nimport { createEmptyVirtualCode, createVirtualCode, ETSVirtualCode } from './ets-code'\n\nexport interface ETSLanguagePluginOptions {\n /**\n * Paths excluded from virtual code. It is very useful when you want to disable files in the `openharmony` & `hms` sdk.\n */\n excludePaths?: string[]\n /**\n * The path to the `tsdk`.\n *\n * Declaration files located within the `tsdk`, such as type declarations in the `lib.dom.d.ts`, are\n * incompatible with the `openharmony` & `hms` sdk and can interfere with it. A series of diagnostics\n * should be specified for the `tsdk` path to eliminate incompatibility.\n */\n tsdk?: string\n}\n\n/**\n * This {@linkcode ETSLanguagePlugin} is used to create the virtual code for the ETS and TS files.\n *\n * It supports the typescript-plugin-side & language-server-side.\n *\n * @see Structure of the virtual code: https://github.com/ohosvscode/arkTS/issues/36#issuecomment-2977236063\n * @param tsOrEts If passed {@linkcode ets} means current mode is language-server-side, otherwise is typescript-plugin-side.\n * @param options - The options for the plugin. See: {@linkcode ETSLanguagePluginOptions}\n */\nexport function ETSLanguagePlugin(tsOrEts: typeof ts, options?: ETSLanguagePluginOptions): LanguagePlugin<URI | string>\nexport function ETSLanguagePlugin(tsOrEts: typeof ets, options?: ETSLanguagePluginOptions): LanguagePlugin<URI | string>\nexport function ETSLanguagePlugin(tsOrEts: typeof ets | typeof ts, { excludePaths = [], tsdk = '' }: ETSLanguagePluginOptions = {}): LanguagePlugin<URI | string> {\n const isETSServerMode = isEts(tsOrEts)\n\n function getLanguageId(uri: URI | string): string | undefined {\n const filePath = typeof uri === 'string' ? uri : uri.fsPath\n if (filePath.endsWith('.ets')) return 'ets'\n if (filePath.endsWith('.ts')) return 'typescript'\n if (filePath.endsWith('.json') || filePath.endsWith('.json5') || filePath.endsWith('.jsonc') || filePath.endsWith('.tsbuildinfo')) return 'json'\n return undefined\n }\n\n function getScriptKindByFilePath(filePath: string, defaultExtension: string = '.ets'): [ets.ScriptKind, string] {\n if (!filePath) return [7 satisfies typeof ets.ScriptKind.Deferred, defaultExtension]\n if (filePath.endsWith('.d.ts')) return [3 satisfies typeof ets.ScriptKind.TS, '.d.ts']\n if (filePath.endsWith('.d.ets')) return [8 satisfies typeof ets.ScriptKind.ETS, '.d.ets']\n if (filePath.endsWith('.d.cts')) return [3 satisfies typeof ets.ScriptKind.TS, '.d.cts']\n if (filePath.endsWith('.d.mts')) return [3 satisfies typeof ets.ScriptKind.TS, '.d.mts']\n\n const extension = path.extname(filePath)\n switch (extension) {\n case '.ts':\n case '.cts':\n case '.mts':\n return [3 satisfies typeof ets.ScriptKind.TS, extension]\n case '.ets':\n return [8 satisfies typeof ets.ScriptKind.ETS, extension]\n default:\n return [7 satisfies typeof ets.ScriptKind.Deferred, extension]\n }\n }\n\n if (isETSServerMode) {\n return {\n getLanguageId,\n createVirtualCode(uri, languageId, snapshot) {\n const filePath = path.resolve(typeof uri === 'string' ? uri : uri.fsPath)\n if (languageId === 'ets') {\n return new ETSVirtualCode(\n filePath,\n tsOrEts.createSourceFile(filePath, snapshot.getText(0, snapshot.getLength()), 99 as any) as unknown as ts.SourceFile,\n 'typescript',\n [$$thisFixerPlugin(), ESObjectPlugin()] as any,\n )\n }\n // json5、json files, directly using full feature virtual code\n if (filePath.endsWith('.json') || filePath.endsWith('.json5') || filePath.endsWith('.jsonc') || languageId === 'json' || languageId === 'jsonc') return getFullVirtualCode(snapshot, languageId)\n // tsdk files we must disable the full feature virtual code but still keep the full content\n if (filePath.startsWith(tsdk)) return getDisabledVirtualCode(snapshot, languageId)\n },\n typescript: {\n extraFileExtensions: [\n // eslint-disable-next-line ts/ban-ts-comment\n // @ts-expect-error\n { extension: 'ets', isMixedContent: false, scriptKind: 8 satisfies ets.ScriptKind.ETS },\n // eslint-disable-next-line ts/ban-ts-comment\n // @ts-expect-error\n { extension: 'd.ets', isMixedContent: false, scriptKind: 8 satisfies ets.ScriptKind.ETS },\n ],\n resolveHiddenExtensions: true,\n getServiceScript(root: VirtualCode & { filePath: string }) {\n const [scriptKind, extension] = getScriptKindByFilePath(root.filePath)\n return {\n code: root,\n extension,\n scriptKind,\n } as unknown as TypeScriptServiceScript\n },\n },\n }\n }\n\n return {\n getLanguageId,\n createVirtualCode(uri, languageId, snapshot) {\n const filePath = path.resolve(typeof uri === 'string' ? uri : uri.fsPath)\n const isInExcludePath = excludePaths.some(excludePath => filePath.startsWith(excludePath))\n if ((filePath.endsWith('.d.ts') || filePath.endsWith('.d.ets')) && isInExcludePath) return getFullDisabledVirtualCode(snapshot, languageId, { filePath })\n },\n typescript: {\n extraFileExtensions: [],\n getServiceScript(root: VirtualCode & { filePath: string }) {\n const [scriptKind, extension] = getScriptKindByFilePath(root.filePath, '.ts')\n return {\n code: root,\n extension,\n scriptKind: scriptKind as ts.ScriptKind,\n }\n },\n },\n }\n}\n\nfunction isEts(tsOrEts: typeof ets | typeof ts): tsOrEts is typeof ets {\n return 'ETS' in tsOrEts.ScriptKind && tsOrEts.ScriptKind.ETS === 8\n}\n\n// full feature virtual code\nfunction getFullVirtualCode(snapshot: ts.IScriptSnapshot, languageId: string): VirtualCode {\n return createVirtualCode(snapshot, languageId, {\n completion: true,\n format: true,\n navigation: true,\n semantic: true,\n structure: true,\n verification: true,\n })\n}\n\n// disabled virtual code, but still keep the full content\nfunction getDisabledVirtualCode(snapshot: ts.IScriptSnapshot, languageId: string): VirtualCode {\n return createVirtualCode(snapshot, languageId, {\n completion: false,\n format: false,\n navigation: false,\n semantic: false,\n structure: false,\n verification: false,\n })\n}\n\n// disabled virtual code, and remove the full content to empty string\nfunction getFullDisabledVirtualCode<T extends Record<string, any>>(snapshot: ts.IScriptSnapshot, languageId: string, options?: T): VirtualCode & T {\n return createEmptyVirtualCode(snapshot, languageId, {\n completion: false,\n format: false,\n navigation: false,\n semantic: false,\n structure: false,\n verification: false,\n }, options)\n}\n"],"mappings":";;;;AAGA,SAAgB,oBAAoC;CAClD,MAAM,cAAc;AAEpB,QAAO;EACL,MAAM;EACN,mBAAmB,aAA6B;GAE9C,MAAM,UADO,YAAY,IAAI,SAAS,CACjB,SAAS,YAAY;AAC1C,QAAK,MAAM,SAAS,SAAS;IAC3B,MAAM,QAAQ,MAAM;IACpB,MAAM,MAAM,QAAQ,MAAM,GAAG;AAC7B,iBAAa,YAAY,OAAO,OAAO,KAAK;KAC1C;KACA,MAAM;KACN,QAAQ;KACR;MACE,YAAY;MACZ,QAAQ;MACR,YAAY;MACZ,UAAU;MACV,WAAW;MACX,cAAc;MACf;KACF,CAAC;;;EAGP;;;;;AC3BH,SAAgB,iBAAiC;AAC/C,QAAO;EACL,MAAM;EACN,mBAAmB,aAA6B;AAC9C,eAAY,MAAM,KAChB,+GACD;AACD,UAAO;;EAEV;;;;;ACNH,SAAgB,kBAAkB,UAA8B,YAAoB,MAAoC;AACtH,QAAO;EACL,IAAI;EACJ;EACA;EACA,UAAU,CAAC;GACT,eAAe,CAAC,EAAE;GAClB,kBAAkB,CAAC,EAAE;GACrB,SAAS,CAAC,SAAS,WAAW,CAAC;GAC/B;GACD,CAAC;EACH;;AAGH,SAAgB,uBAAsD,UAA8B,YAAoB,MAAuB,SAA8B;AAC3K,QAAO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