@@ -70,6 +70,19 @@ const plugin = createLanguageServicePlugin((ts, info) => {
7070 config ,
7171 ) ;
7272 if ( info . session ) {
73+ // Register protocol handlers for "Request Forwarding to tsserver".
74+ // See https://github.com/mizdra/css-modules-kit/pull/207 for more details.
75+
76+ // `info.session.addProtocolHandler` cannot register multiple handlers with the same command name.
77+ // Attempting to do so will result in an error.
78+ //
79+ // By the way, tsserver creates one ConfiguredProject for each tsconfig.json file. Then, tsserver
80+ // initializes each plugin for each ConfiguredProject. This means that if there are multiple
81+ // tsconfig.json files, the handler will be registered multiple times.
82+ //
83+ // Therefore, we will do the following:
84+ // - Implement the handler to handle files from different projects
85+ // - Skip registration if the handler is already registered
7386 try {
7487 info . session . addProtocolHandler ( '_css-modules-kit:rename' , createRenameHandler ( ts , info . project ) ) ;
7588 info . session . addProtocolHandler ( '_css-modules-kit:renameInfo' , createRenameInfoHandler ( ts , info . project ) ) ;
@@ -83,6 +96,8 @@ const plugin = createLanguageServicePlugin((ts, info) => {
8396 ) ;
8497 }
8598 } else {
99+ // When a plugin is used via tsserver from the editor, the session is always available.
100+ // However, when a plugin is used via the TypeScript Compiler API, the session may not be available.
86101 info . project . projectService . logger . info (
87102 '[@css-modules-kit/ts-plugin] info: Skipping protocol handler registration because session is not available.' ,
88103 ) ;
0 commit comments