@@ -7,7 +7,7 @@ import {fileURLToPath} from 'url';
77 * Creates a new instance of TypeScript Server.
88 * @returns {Object } An object containing methods to interact with TypeScript Server.
99 */
10- function createTSServerInstance ( ) {
10+ function createTSServerInstance ( inferredProject = true ) {
1111 let tsserverProcess = null ;
1212 let seqNumber = 0 ;
1313 const pendingCommands = new Map ( ) ;
@@ -34,7 +34,7 @@ function createTSServerInstance() {
3434 const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
3535 const tsserverPath = ( tsServer ) ? tsServer : path . join ( __dirname , '..' , '..' , 'node_modules' , 'typescript' , 'bin' , 'tsserver' ) ;
3636 const nodePath = ( ! node ) ? 'node' : node ;
37- tsserverProcess = spawn ( nodePath , [ tsserverPath ] ) ;
37+ tsserverProcess = spawn ( nodePath , [ tsserverPath , ( inferredProject ) ? '--useInferredProjectPerProjectRoot' : "" ] ) ;
3838 tsserverProcess . stdout . setEncoding ( 'utf8' ) ;
3939
4040 pendingCommands . set ( CONNECT_MESSAGE_KEY , { resolve, reject} ) ;
@@ -1517,6 +1517,82 @@ function createTSServerInstance() {
15171517 return sendCommand ( command ) ;
15181518 }
15191519
1520+ /**
1521+ * Sends a 'docCommentTemplate' request to the TypeScript Server. This command generates a JSDoc comment template
1522+ * at a specified line and character offset in a file. It's useful for quickly inserting standardized documentation comments.
1523+ *
1524+ * @param {string } fileName - The absolute path of the file in which to generate the comment template.
1525+ * @param {number } line - The line number (1-based) where the template should be generated.
1526+ * @param {number } offset - The character offset (1-based) on the line for the template.
1527+ *
1528+ * @returns {Promise<Object> } A promise that resolves with the generated comment template. The result includes:
1529+ * - `newText`: The text of the generated documentation comment.
1530+ * - `caretOffset`: The position in the newText where the caret should be placed.
1531+ *
1532+ * Example usage:
1533+ * ```
1534+ * docCommentTemplate('path/to/file.ts', 10, 5).then(template => {
1535+ * console.log('Documentation comment template:', template);
1536+ * });
1537+ * ```
1538+ * This function assists developers in maintaining consistent documentation standards in their codebase.
1539+ */
1540+ function docCommentTemplate ( fileName , line , offset ) {
1541+ const command = {
1542+ command : "docCommentTemplate" ,
1543+ arguments : {
1544+ file : fileName ,
1545+ line : line ,
1546+ offset : offset
1547+ }
1548+ } ;
1549+ return sendCommand ( command ) ;
1550+ }
1551+
1552+ /**
1553+ * Sends a 'compilerOptionsForInferredProjects' request to the TypeScript Server. This command sets the compiler
1554+ * options for inferred projects. An inferred project is created when a loose file, not part of any other project,
1555+ * is opened. These projects are grouped based on their root directory if 'useInferredProjectPerProjectRoot' is enabled.
1556+ *
1557+ * When 'useInferredProjectPerProjectRoot' is enabled, the TypeScript server creates a separate inferred project for each
1558+ * directory root. This allows for isolated handling of files in different folders, each treated as a distinct project
1559+ * with its own settings. This setting is crucial for large workspaces or monorepos where different directories may have
1560+ * different TypeScript configurations.
1561+ *
1562+ * @param {Object } options - Compiler options for inferred projects, similar to tsconfig.json settings.
1563+ * @param {string } [projectRootPath] - Optional root path to scope the compiler options. Required if the server is started
1564+ * with 'useInferredProjectPerProjectRoot' enabled.
1565+ *
1566+ * @returns {Promise<void> } A promise that resolves when the server acknowledges the update.
1567+ *
1568+ * Example usage:
1569+ * ```
1570+ * const compilerOptions = {
1571+ * allowJs: true,
1572+ * strict: true,
1573+ * // ... other options
1574+ * };
1575+ * const projectRoot = 'path/to/project/root'; // Optional
1576+ * setCompilerOptionsForInferredProjects(compilerOptions, projectRoot)
1577+ * .then(() => {
1578+ * console.log('Compiler options for inferred projects updated');
1579+ * });
1580+ * ```
1581+ * This function is essential for setting up appropriate behaviors for files in inferred projects,
1582+ * especially in complex workspaces with multiple disjoint projects.
1583+ */
1584+
1585+ function setCompilerOptionsForInferredProjects ( options , projectRootPath ) {
1586+ const command = {
1587+ command : "compilerOptionsForInferredProjects" ,
1588+ arguments : {
1589+ options : options ,
1590+ projectRootPath : projectRootPath
1591+ }
1592+ } ;
1593+ return sendCommand ( command ) ;
1594+ }
1595+
15201596 /**
15211597 * Terminates the TypeScript Server process.
15221598 * Warning: Use this function with caution. Prefer using the exitServer function for a graceful shutdown.
@@ -1574,6 +1650,8 @@ function createTSServerInstance() {
15741650 getOutliningSpans,
15751651 indentation,
15761652 todoComments,
1653+ docCommentTemplate,
1654+ setCompilerOptionsForInferredProjects,
15771655 exitServer
15781656 } ;
15791657}
0 commit comments