Skip to content

Commit aa20305

Browse files
committed
feat: added support for docCommentTemplate, setCompilerOptionsForInferredProjects
1 parent 65b5b2b commit aa20305

File tree

4 files changed

+154
-3
lines changed

4 files changed

+154
-3
lines changed

src/exp.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,13 @@ for (let file of FILES) {
120120
const indentationsResponse = await tsServer.indentation(file.indentation.fileName, file.indentation.line, file.indentation.offset, file.indentation.options);
121121
console.log('indentationsResponse', JSON.stringify(indentationsResponse));
122122

123+
await tsServer.openFile(file.docCommentTemplate.fileName);
124+
const docCommentTemplateResponse = await tsServer.docCommentTemplate(file.docCommentTemplate.fileName, file.docCommentTemplate.line, file.docCommentTemplate.offset);
125+
console.log('docCommentTemplateResponse', JSON.stringify(docCommentTemplateResponse));
126+
127+
const setCompilerOptionsForInferredProjectsResponse = await tsServer.setCompilerOptionsForInferredProjects(file.setCompilerOptionsForInferredProjects.options, file.setCompilerOptionsForInferredProjects.projectRootPath);
128+
console.log('setCompilerOptionsForInferredProjectsResponse', JSON.stringify(setCompilerOptionsForInferredProjectsResponse));
129+
130+
123131
}
124132
//tsServer.exitServer();

src/test/sample/doctemplate.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// file.ts
2+
3+
export class ExampleClass {
4+
private value: number;
5+
constructor(value: number) {
6+
this.value = value;}
7+
8+
/**
9+
* This is where we will want to generate a documentation template.
10+
* Let's assume our docCommentTemplate function targets this method.
11+
*/
12+
addNumbers(a: number, b: number): number {
13+
return a + b;
14+
}
15+
}

src/utils/server.js

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/utils/testconfig.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
1+
const compilerOption = {
2+
// Allow JavaScript files to be compiled
3+
allowJs: true,
4+
5+
// Specify the module system, common options are "CommonJS" or "ESNext" for ES Modules
6+
module: "CommonJS",
7+
8+
// Target specific ECMAScript version for output (e.g., "ES2020")
9+
target: "ES2020",
10+
11+
// Enable strict mode for type checking (if you're using TypeScript features in .js files)
12+
strict: true,
13+
14+
// Include type definitions for Node.js (if your project is a Node.js project)
15+
types: ["node"],
16+
17+
// Enable synthetic default imports (e.g., to import React as `import React from 'react'`)
18+
allowSyntheticDefaultImports: true,
19+
20+
// Optionally, set the module resolution strategy
21+
moduleResolution: "node",
22+
23+
// Enable emitting decorator metadata (useful if you're using decorators)
24+
experimentalDecorators: true,
25+
26+
// Emit BOM for UTF-8 files
27+
emitBOM: false
28+
29+
// If using JSX (React), set this to "React"
30+
// jsx: "React"
31+
32+
// Other options as needed for your project...
33+
};
134
export const FILES = [{
235
filepath: '/home/charly/repo/tsIntelligence/src/exp.js',
336
definition: {line: 11, offset: 49},
@@ -121,8 +154,16 @@ export const FILES = [{
121154
tabSize: 4,
122155
convertTabsToSpaces: true
123156
}
157+
},
158+
docCommentTemplate: {
159+
fileName: '/home/charly/repo/tsIntelligence/src/test/sample/doctemplate.ts',
160+
line: 12,
161+
offset: 5
162+
},
163+
setCompilerOptionsForInferredProjects: {
164+
options: compilerOption,
165+
projectRootPath: '/home/charly/repo/tsIntelligence/'
124166
}
125-
126167
}, {
127168
filepath: '/home/charly/repo/vscode/extensions/typescript-language-features/web/src/webServer.ts',
128169
definition: {line: 49, offset: 36},
@@ -254,6 +295,15 @@ export const FILES = [{
254295
tabSize: 4,
255296
convertTabsToSpaces: true
256297
}
298+
},
299+
docCommentTemplate: {
300+
fileName: '/home/charly/repo/vscode/extensions/typescript-language-features/web/src/webServer.ts',
301+
line: 47,
302+
offset: 1
303+
},
304+
setCompilerOptionsForInferredProjects: {
305+
options: compilerOption,
306+
projectRootPath: '/home/charly/repo/tsIntelligence/'
257307
}
258308
// TODO: add testcase for update and open
259309
}];

0 commit comments

Comments
 (0)