@@ -1334,6 +1334,189 @@ function createTSServerInstance() {
13341334 return sendCommand ( command ) ;
13351335 }
13361336
1337+ /**
1338+ * Sends a 'closeExternalProject' request to the TypeScript Server. This command closes an external project
1339+ * that was previously opened. The server responds with an acknowledgement. Closing a project is important
1340+ * for managing resources and keeping the server's context accurate, especially in dynamic environments.
1341+ *
1342+ * @param {string } projectFileName - The name or path of the external project file to close.
1343+ *
1344+ * @returns {Promise<Object> } A promise that resolves when the server has acknowledged closing the project.
1345+ * The response object contains standard response fields such as:
1346+ * - `success`: A boolean indicating whether the request was successful.
1347+ * - `request_seq`: The sequence number of the request.
1348+ * - `command`: The command requested.
1349+ * - `message`: An optional success or error message.
1350+ *
1351+ * Example usage:
1352+ * ```
1353+ * closeExternalProject('path/to/external/project').then(response => {
1354+ * console.log('External project closed:', response);
1355+ * });
1356+ * ```
1357+ * This function is essential for managing the lifecycle of external projects in various development environments.
1358+ */
1359+ function closeExternalProject ( projectFileName ) {
1360+ const command = {
1361+ command : "closeExternalProject" ,
1362+ arguments : { projectFileName}
1363+ } ;
1364+ return sendCommand ( command ) ;
1365+ }
1366+
1367+ /**
1368+ * Sends an 'updateOpen' request to the TypeScript Server. This command updates the set of open files,
1369+ * including newly opened files, changed files, and files to be closed. It synchronizes the server's
1370+ * view of files with the current state in the development environment.
1371+ *
1372+ * @param {Object[] } openFiles - Array of objects for newly opened files. Each object includes:
1373+ * - `fileName`: The file name.
1374+ * - `fileContent`: (Optional) The current content of the file.
1375+ * - `scriptKindName`: (Optional) The kind of script ('TS', 'JS', 'TSX', 'JSX').
1376+ * - `projectRootPath`: (Optional) Root path for project configuration file search.
1377+ * @param {Object[] } changedFiles - Array of objects for changed files. Each object includes:
1378+ * - `fileName`: The file name.
1379+ * - `textChanges`: Array of changes, each with `start`, `end`, and `newText`.
1380+ * @param {string[] } closedFiles - Array of file names that should be closed.
1381+ *
1382+ * @returns {Promise<void> } A promise that resolves when the server has processed the update.
1383+ *
1384+ * Example usage:
1385+ * ```
1386+ * updateOpen(
1387+ * [{ fileName: 'path/to/openedFile.ts', fileContent: 'file content', scriptKindName: 'TS' }],
1388+ * [{ fileName: 'path/to/changedFile.ts', textChanges: [{ start: { line: 1, offset: 1 }, end: { line: 1, offset: 10 }, newText: 'updated content' }] }],
1389+ * ['path/to/closedFile.ts']
1390+ * ).then(() => {
1391+ * console.log('Open files updated');
1392+ * });
1393+ * ```
1394+ * This function is crucial for keeping the TypeScript server in sync with the file changes in the development environment.
1395+ */
1396+ // TODO: write working test case and figure out the behavior
1397+ function updateOpen ( openFiles , changedFiles , closedFiles ) {
1398+ const command = {
1399+ command : "updateOpen" ,
1400+ arguments : {
1401+ openFiles,
1402+ changedFiles,
1403+ closedFiles
1404+ }
1405+ } ;
1406+ return sendCommand ( command ) ;
1407+ }
1408+
1409+ /**
1410+ * Sends a 'getOutliningSpans' request to the TypeScript Server. This command retrieves outlining spans
1411+ * (code folding regions) for a specified file. These spans help editors to create collapsible regions,
1412+ * enhancing code readability and navigation, especially in large files.
1413+ *
1414+ * @param {string } fileName - The name of the file for which outlining spans are requested.
1415+ *
1416+ * @returns {Promise<Object[]> } A promise that resolves with an array of outlining span objects.
1417+ * Each outlining span object includes:
1418+ * - `textSpan`: The span of the document to collapse, with start and end locations.
1419+ * - `hintSpan`: The span to display as a hint when the user hovers over the collapsed span.
1420+ * - `bannerText`: The text to display in the editor for the collapsed region.
1421+ * - `autoCollapse`: Indicates whether the region should automatically collapse in certain conditions.
1422+ * - `kind`: The kind of outlining span, such as 'comment', 'region', 'code', or 'imports'.
1423+ *
1424+ * Example usage:
1425+ * ```
1426+ * getOutliningSpans('path/to/file.ts').then(spans => {
1427+ * console.log('Outlining spans:', spans);
1428+ * });
1429+ * ```
1430+ * This function is vital for code editors, providing the necessary information for code folding features,
1431+ * helping developers manage visibility in large code files.
1432+ */
1433+ function getOutliningSpans ( fileName ) {
1434+ const command = {
1435+ command : "getOutliningSpans" ,
1436+ arguments : { file : fileName }
1437+ } ;
1438+ return sendCommand ( command ) ;
1439+ }
1440+
1441+ /**
1442+ * Sends a 'todoComments' request to the TypeScript Server. This command searches for TODO comments
1443+ * in a specified file based on given descriptors. Each descriptor includes a text to match (like 'TODO')
1444+ * and a priority level.
1445+ *
1446+ * @param {string } fileName - The name of the file to search for TODO comments.
1447+ * @param {Object[] } descriptors - Array of descriptors for TODO comments. Each descriptor includes:
1448+ * - `text`: The text of the TODO comment (e.g., 'TODO', 'FIXME').
1449+ * - `priority`: The priority level of the comment.
1450+ *
1451+ * @returns {Promise<Object[]> } A promise that resolves with an array of TODO comment objects.
1452+ * Each object includes:
1453+ * - `descriptor`: The descriptor of the TODO comment.
1454+ * - `message`: The text of the TODO comment.
1455+ * - `position`: The position of the comment in the file.
1456+ *
1457+ * Example usage:
1458+ * ```
1459+ * todoComments('path/to/file.ts', [{ text: 'TODO', priority: 1 }, { text: 'FIXME', priority: 2 }])
1460+ * .then(comments => {
1461+ * console.log('TODO comments:', comments);
1462+ * });
1463+ * ```
1464+ * This function is useful for identifying and listing TODO comments and other annotations in the code.
1465+ */
1466+ function todoComments ( fileName , descriptors ) {
1467+ const command = {
1468+ command : "todoComments" ,
1469+ arguments : {
1470+ file : fileName ,
1471+ descriptors : descriptors
1472+ }
1473+ } ;
1474+ return sendCommand ( command ) ;
1475+ }
1476+
1477+ /**
1478+ * Sends an 'indentation' request to the TypeScript Server. This command calculates the indentation level
1479+ * for a specific location in a file, taking into account optional editor settings. It's useful for maintaining
1480+ * consistent formatting in code editors or IDEs.
1481+ *
1482+ * @param {string } fileName - The name of the file to calculate indentation for.
1483+ * @param {number } line - The line number (1-based) to calculate the indentation.
1484+ * @param {number } offset - The character offset (1-based) in the line for indentation calculation.
1485+ * @param {Object } [options] - Optional editor settings to use for computing indentation. Includes:
1486+ * - `baseIndentSize`: The base indent size in spaces.
1487+ * - `indentSize`: The size of an indentation level in spaces.
1488+ * - `tabSize`: The size of a tab character in spaces.
1489+ * - `newLineCharacter`: The character(s) to use for a newline.
1490+ * - `convertTabsToSpaces`: Whether to convert tabs to spaces.
1491+ * - `indentStyle`: The style of indentation ('None', 'Block', 'Smart').
1492+ * - `trimTrailingWhitespace`: Whether to trim trailing whitespace.
1493+ *
1494+ * @returns {Promise<Object> } A promise that resolves with the indentation result, including:
1495+ * - `position`: The base position in the document for the indent.
1496+ * - `indentation`: The number of columns for the indent relative to the position's column.
1497+ *
1498+ * Example usage:
1499+ * ```
1500+ * indentation('path/to/file.ts', 10, 4, { indentSize: 4, tabSize: 4, convertTabsToSpaces: true })
1501+ * .then(result => {
1502+ * console.log('Indentation result:', result);
1503+ * });
1504+ * ```
1505+ * This function assists in automating code formatting in development environments.
1506+ */
1507+ function indentation ( fileName , line , offset , options ) {
1508+ const command = {
1509+ command : "indentation" ,
1510+ arguments : {
1511+ file : fileName ,
1512+ line : line ,
1513+ offset : offset ,
1514+ options : options
1515+ }
1516+ } ;
1517+ return sendCommand ( command ) ;
1518+ }
1519+
13371520 /**
13381521 * Terminates the TypeScript Server process.
13391522 * Warning: Use this function with caution. Prefer using the exitServer function for a graceful shutdown.
@@ -1344,7 +1527,7 @@ function createTSServerInstance() {
13441527 if ( tsserverProcess ) {
13451528 tsserverProcess . kill ( ) ;
13461529 tsserverProcess = null ;
1347- console . log ( 'tsserver pgetCompletionDetailsrocess terminated' ) ;
1530+ console . log ( 'tsserver terminated' ) ;
13481531 }
13491532 }
13501533
@@ -1386,6 +1569,11 @@ function createTSServerInstance() {
13861569 reloadProjects,
13871570 openExternalProject,
13881571 openExternalProjects,
1572+ closeExternalProject,
1573+ updateOpen,
1574+ getOutliningSpans,
1575+ indentation,
1576+ todoComments,
13891577 exitServer
13901578 } ;
13911579}
0 commit comments