twoslash icon indicating copy to clipboard operation
twoslash copied to clipboard

[Question] Can we use tsserver plugins with shiki-twoslash?

Open znck opened this issue 3 years ago • 4 comments

znck avatar Oct 22 '22 03:10 znck

It doesn't seem massively outside of the scope for supporting it IMO, we do host a TSServer.

If the diff is reasonable and well tested, I'd be open to support.

orta avatar Oct 22 '22 09:10 orta

As far as I understand @typescript/vfs creates a language service using createLanguageService(), I don't see TSServer.

Where should I look to start a PR?

znck avatar Oct 22 '22 16:10 znck

I think we might need to emulate some the commands sent from the language service to look like the ones sent from the tsserver process - I think they might be pretty much pass throughs in the tsc codebase

orta avatar Oct 22 '22 18:10 orta

PluginModule.create() expects project and serverHost. I am not sure how to emulate project.

diff --git a/packages/typescript-vfs/src/index.ts b/packages/typescript-vfs/src/index.ts
index 453dcf9b..e7c99c39 100755
--- a/packages/typescript-vfs/src/index.ts
+++ b/packages/typescript-vfs/src/index.ts
@@ -5,6 +5,7 @@ type LanguageServiceHost = import("typescript").LanguageServiceHost
 type CompilerHost = import("typescript").CompilerHost
 type SourceFile = import("typescript").SourceFile
 type TS = typeof import("typescript")
+type PluginModuleFactory = import("typescript/lib/tsserverlibrary").server.PluginModuleFactory
 
 let hasLocalStorage = false
 try {
@@ -39,7 +40,8 @@ export function createVirtualTypeScriptEnvironment(
   rootFiles: string[],
   ts: TS,
   compilerOptions: CompilerOptions = {},
-  customTransformers?: CustomTransformers
+  customTransformers?: CustomTransformers,
+  plugins: PluginModuleFactory[] = []
 ): VirtualTypeScriptEnvironment {
   const mergedCompilerOpts = { ...defaultCompilerOptions(ts), ...compilerOptions }
 
@@ -50,7 +52,16 @@ export function createVirtualTypeScriptEnvironment(
     ts,
     customTransformers
   )
-  const languageService = ts.createLanguageService(languageServiceHost)
+  const languageService = plugins.reduce((languageService, plugin) => {
+    return plugin({ typescript: ts }).create({
+      languageService,
+      languageServiceHost,
+      project: null as any, // ??
+      config: {},
+      serverHost: {} as any, // create severHost
+    })
+  }, ts.createLanguageService(languageServiceHost))
+  // TODO: Enable plugins
   const diagnostics = languageService.getCompilerOptionsDiagnostics()
 
   if (diagnostics.length) {

znck avatar Oct 23 '22 09:10 znck