@@ -1784,6 +1784,111 @@ function createTSServerInstance(inferredProject = true) {
17841784 return sendCommand ( command ) ;
17851785 }
17861786
1787+ /**
1788+ * Sends a 'GetMoveToRefactoringFileSuggestions' request to the TypeScript Server. This command retrieves a list of
1789+ * existing file paths as suggestions for moving a specific piece of code as part of a refactoring process.
1790+ * The function handles both single-location requests (FileLocationRequestArgs) and range-based requests (FileRangeRequestArgs).
1791+ *
1792+ * @param {string } filePath - The absolute path to the TypeScript file.
1793+ * @param {number } startLine - The starting line number of the range or location (1-based).
1794+ * @param {number } startOffset - The starting character offset on the start line (1-based).
1795+ * @param {number } [endLine] - The ending line number of the range (1-based). Optional for single location.
1796+ * @param {number } [endOffset] - The ending character offset on the end line (1-based). Optional for single location.
1797+ * @param {string } [kind] - Optional. The kind of refactoring to apply.
1798+ * @returns {Promise<Object> } A promise that resolves with a list of file paths suggested for the refactoring.
1799+ * @example
1800+ * // Example usage for a range in a file
1801+ * getMoveToRefactoringFileSuggestions('/path/to/file.ts', 5, 1, 7, 1)
1802+ * .then(suggestions => {
1803+ * console.log('Refactoring File Suggestions:', suggestions);
1804+ * })
1805+ * .catch(error => {
1806+ * console.error('Error getting file suggestions for refactoring:', error);
1807+ * });
1808+ */
1809+ function getMoveToRefactoringFileSuggestions ( filePath , startLine , startOffset , endLine , endOffset , kind ) {
1810+ const args = endLine !== undefined && endOffset !== undefined
1811+ ? { startLine, startOffset, endLine, endOffset} // FileRangeRequestArgs
1812+ : { line : startLine , offset : startOffset } ; // FileLocationRequestArgs
1813+
1814+ const command = {
1815+ command : "getMoveToRefactoringFileSuggestions" ,
1816+ arguments : {
1817+ file : filePath ,
1818+ ...args ,
1819+ kind : kind
1820+ }
1821+ } ;
1822+ return sendCommand ( command ) ;
1823+ }
1824+
1825+ /**
1826+ * Sends an 'organizeImports' request to the TypeScript Server. This command organizes the imports in a TypeScript file by:
1827+ * 1) Removing unused imports.
1828+ * 2) Coalescing imports from the same module.
1829+ * 3) Sorting imports.
1830+ * The scope of the request is limited to a single file. The function allows specifying the mode of import organization.
1831+ *
1832+ * @param {string } filePath - The absolute path to the TypeScript file.
1833+ * @param {string } [mode] - The mode of import organization, which can be 'All', 'SortAndCombine', or 'RemoveUnused'. Default is 'All'.
1834+ * @param {string } [projectFileName] - Optional. The name of the project that contains the file (e.g., path to 'tsconfig.json').
1835+ * @returns {Promise<Object> } A promise that resolves with an array of file code edits suggested by the TypeScript server. Each edit includes the file name and an array of text changes.
1836+ * @example
1837+ * // Organizing imports in a file with all modes
1838+ * organizeImports('/path/to/file.ts', 'All', '/path/to/project/tsconfig.json')
1839+ * .then(edits => {
1840+ * console.log('Organize Imports Edits:', edits);
1841+ * })
1842+ * .catch(error => {
1843+ * console.error('Error organizing imports:', error);
1844+ * });
1845+ */
1846+ function organizeImports ( filePath , mode = 'All' , projectFileName ) {
1847+ const command = {
1848+ command : "organizeImports" ,
1849+ arguments : {
1850+ scope : {
1851+ type : "file" ,
1852+ args : { file : filePath , projectFileName : projectFileName }
1853+ } ,
1854+ mode : mode
1855+ }
1856+ } ;
1857+ return sendCommand ( command ) ;
1858+ }
1859+
1860+ /**
1861+ * Sends a 'getEditsForFileRename' request to the TypeScript Server. This function is used to retrieve
1862+ * the necessary code edits required when a file is renamed. It updates import paths and references in
1863+ * other files within the project to reflect the new file name.
1864+ *
1865+ * @param {string } oldFilePath - The original path of the file before renaming.
1866+ * @param {string } newFilePath - The new path of the file after renaming.
1867+ * @returns {Promise } A promise that resolves with an array of file code edits. Each element in the array
1868+ * is an object containing the file name and an array of text changes. Each text change
1869+ * is an object with 'start', 'end', and 'newText' properties indicating how the text
1870+ * should be modified.
1871+ * @example
1872+ * getEditsForFileRename('/path/to/oldFile.ts', '/path/to/newFile.ts')
1873+ * .then(edits => {
1874+ * console.log('File Rename Edits:', edits);
1875+ * })
1876+ * .catch(error => {
1877+ * console.error('Error getting edits for file rename:', error);
1878+ * });
1879+ */
1880+ function getEditsForFileRename ( oldFilePath , newFilePath ) {
1881+ const command = {
1882+ command : "getEditsForFileRename" ,
1883+ arguments : {
1884+ oldFilePath : oldFilePath ,
1885+ newFilePath : newFilePath
1886+ }
1887+ } ;
1888+ return sendCommand ( command ) ;
1889+ }
1890+
1891+
17871892 /**
17881893 * Terminates the TypeScript Server process.
17891894 * Warning: Use this function with caution. Prefer using the exitServer function for a graceful shutdown.
@@ -1848,6 +1953,9 @@ function createTSServerInstance(inferredProject = true) {
18481953 getSupportedCodeFixes,
18491954 getApplicableRefactors,
18501955 getEditsForRefactor,
1956+ getMoveToRefactoringFileSuggestions,
1957+ organizeImports,
1958+ getEditsForFileRename,
18511959 exitServer
18521960 } ;
18531961}
0 commit comments