@@ -2,6 +2,7 @@ import {spawn} from 'child_process';
22import path from 'path' ;
33import { fileURLToPath } from 'url' ;
44
5+ // @INCLUDE_IN_API_DOCS
56/**
67 * Creates a new instance of TypeScript Server.
78 * @returns {Object } An object containing methods to interact with TypeScript Server.
@@ -309,6 +310,141 @@ function createTSServerInstance() {
309310 return sendCommand ( command ) ;
310311 }
311312
313+ /**
314+ * Sends a 'completionEntryDetails' request to the TypeScript Server.
315+ * @param {string } filePath - The path to the file.
316+ * @param {number } line - The line number of the position.
317+ * @param {number } offset - The offset in the line of the position.
318+ * @param {string } entryName - The name of the completion entry.
319+ * @returns {Promise<Object> } A promise that resolves with the response from tsserver.
320+ */
321+ function getCompletionDetails ( filePath , line , offset , entryName ) {
322+ const command = {
323+ command : "completionEntryDetails" ,
324+ arguments : {
325+ file : filePath ,
326+ line : line ,
327+ offset : offset ,
328+ entryNames : [ entryName ]
329+ }
330+ } ;
331+ return sendCommand ( command ) ;
332+ }
333+
334+ /**
335+ * Sends a 'compileOnSaveAffectedFileList' request to the TypeScript Server.
336+ * @param {string } filePath - The path to the file.
337+ * @returns {Promise<Object> } A promise that resolves with the response from tsserver.
338+ */
339+ function getCompileOnSaveAffectedFileList ( filePath ) {
340+ const command = {
341+ command : "compileOnSaveAffectedFileList" ,
342+ arguments : {
343+ file : filePath
344+ }
345+ } ;
346+ return sendCommand ( command ) ;
347+ }
348+
349+ /**
350+ * Sends a 'compileOnSaveEmitFile' command to the TypeScript Server.
351+ * @param {string } filePath - The path to the TypeScript file.
352+ * @param {boolean } [forced=false] - Force emit even if there are errors.
353+ * @param {boolean } [includeLinePosition=false] - Include line position in the response.
354+ * @param {boolean } [richResponse=false] - If true, returns response as an object with detailed emit results.
355+ * @returns {Promise<boolean | EmitResult> } A promise that resolves with a boolean indicating success
356+ * or an EmitResult object containing detailed information about the emit process.
357+ * - If a boolean: true if the emit was successful, false otherwise.
358+ * - If an EmitResult object:
359+ * - `emitSkipped`: A boolean indicating whether the emit was skipped.
360+ * - `diagnostics`: An array of Diagnostic or DiagnosticWithLinePosition objects, providing
361+ * detailed information about each diagnostic message.
362+ * - `Diagnostic`: An object representing a diagnostic message, typically containing:
363+ * - `start`: The starting position of the diagnostic message.
364+ * - `length`: The length of the diagnostic message.
365+ * - `text`: The text of the diagnostic message.
366+ * - `DiagnosticWithLinePosition`: An extended version of Diagnostic, including line and
367+ * character position information:
368+ * - `start`: The starting position of the diagnostic message (line and character).
369+ * - `end`: The ending position of the diagnostic message (line and character).
370+ * - `text`: The text of the diagnostic message.
371+ */
372+ function compileOnSaveEmitFile ( filePath , forced = false , includeLinePosition = false , richResponse = false ) {
373+ const command = {
374+ command : "compileOnSaveEmitFile" ,
375+ arguments : {
376+ file : filePath ,
377+ forced : forced ,
378+ includeLinePosition : includeLinePosition ,
379+ richResponse : richResponse
380+ }
381+ } ;
382+ return sendCommand ( command ) ;
383+ }
384+
385+ /**
386+ * Sends a 'definitionAndBoundSpan' request to the TypeScript Server.
387+ * @param {string } filePath - The path to the file.
388+ * @param {number } line - The line number of the position.
389+ * @param {number } offset - The offset in the line of the position.
390+ * @returns {Promise<Object> } A promise that resolves with the response from tsserver.
391+ */
392+ function getDefinitionAndBoundSpan ( filePath , line , offset ) {
393+ const command = {
394+ command : "definitionAndBoundSpan" ,
395+ arguments : {
396+ file : filePath ,
397+ line : line ,
398+ offset : offset
399+ }
400+ } ;
401+ return sendCommand ( command ) ;
402+ }
403+
404+ /**
405+ * Sends an 'implementation' request to the TypeScript Server.
406+ * @param {string } filePath - The path to the file.
407+ * @param {number } line - The line number of the position.
408+ * @param {number } offset - The offset in the line of the position.
409+ * @returns {Promise<Object> } A promise that resolves with the response from tsserver.
410+ */
411+ function getImplementations ( filePath , line , offset ) {
412+ const command = {
413+ command : "implementation" ,
414+ arguments : {
415+ file : filePath ,
416+ line : line ,
417+ offset : offset
418+ }
419+ } ;
420+ return sendCommand ( command ) ;
421+ }
422+
423+ /**
424+ * Sends a 'format' request to the TypeScript Server.
425+ * @param {string } filePath - The path to the file.
426+ * @param {number } startLine - The starting line number of the format range.
427+ * @param {number } startOffset - The starting offset in the start line.
428+ * @param {number } endLine - The ending line number of the format range.
429+ * @param {number } endOffset - The ending offset in the end line.
430+ * @param {object } [formatOptions] - Optional formatting options.
431+ * @returns {Promise<Object> } A promise that resolves with the response from tsserver.
432+ */
433+ function format ( filePath , startLine , startOffset , endLine , endOffset , formatOptions = { } ) {
434+ const command = {
435+ command : "format" ,
436+ arguments : {
437+ file : filePath ,
438+ line : startLine ,
439+ offset : startOffset ,
440+ endLine : endLine ,
441+ endOffset : endOffset ,
442+ options : formatOptions
443+ }
444+ } ;
445+ return sendCommand ( command ) ;
446+ }
447+
312448 /**
313449 * Sends an 'exit' command to the TypeScript Server to gracefully shut it down.
314450 * @function exitServer
@@ -323,6 +459,50 @@ function createTSServerInstance() {
323459 tsserverProcess = null ;
324460 }
325461
462+ /**
463+ * Sends a 'formatonkey' request to the TypeScript Server. This command is used
464+ * for formatting code at a specific position in a file, typically in response
465+ * to a keystroke. It's commonly used for auto-formatting a line of code when
466+ * a specific key (like a closing brace or semi-colon) is pressed.
467+ *
468+ * @param {string } filePath - The path to the TypeScript file. The path should
469+ * be absolute or relative to the TypeScript server's
470+ * current working directory.
471+ * @param {number } line - The 1-based line number in the file where the key was
472+ * pressed. This and the offset together point to the
473+ * specific position in the file.
474+ * @param {number } offset - The 1-based character offset in the line where the
475+ * key was pressed. This is typically the position
476+ * right after where the key was pressed.
477+ * @param {string } key - The character corresponding to the key pressed. This
478+ * is typically a single character like ';' or '}' that
479+ * triggers the formatting action.
480+ * @param {object } [formatOptions] - Optional formatting options to customize
481+ * the formatting behavior. These options might
482+ * include settings like tab size, indent size,
483+ * whether to insert spaces, and so on.
484+ * Example: { tabSize: 4, indentSize: 4, insertSpace: true }
485+ *
486+ * @returns {Promise<Object> } A promise that resolves with the response from tsserver.
487+ * The response typically includes an array of code edits
488+ * that should be applied to the file to achieve the
489+ * desired formatting. Each edit suggests changes like
490+ * text insertions, deletions, or replacements.
491+ */
492+ function formatOnKey ( filePath , line , offset , key , formatOptions = { } ) {
493+ const command = {
494+ command : "formatonkey" ,
495+ arguments : {
496+ file : filePath ,
497+ line : line ,
498+ offset : offset ,
499+ key : key ,
500+ options : formatOptions
501+ }
502+ } ;
503+ return sendCommand ( command ) ;
504+ }
505+
326506 /**
327507 * Terminates the TypeScript Server process.
328508 * Warning: Use this function with caution. Prefer using the exitServer function for a graceful shutdown.
@@ -333,7 +513,7 @@ function createTSServerInstance() {
333513 if ( tsserverProcess ) {
334514 tsserverProcess . kill ( ) ;
335515 tsserverProcess = null ;
336- console . log ( 'tsserver process terminated' ) ;
516+ console . log ( 'tsserver pgetCompletionDetailsrocess terminated' ) ;
337517 }
338518 }
339519
@@ -348,6 +528,13 @@ function createTSServerInstance() {
348528 getQuickInfo,
349529 findSourceDefinition,
350530 getCompletionInfo,
531+ getCompletionDetails,
532+ getCompileOnSaveAffectedFileList,
533+ compileOnSaveEmitFile,
534+ getDefinitionAndBoundSpan,
535+ getImplementations,
536+ format,
537+ formatOnKey,
351538 exitServer
352539 } ;
353540}
0 commit comments