Skip to content

Commit fea6b55

Browse files
committed
update readme
1 parent 53f6249 commit fea6b55

File tree

2 files changed

+134
-46
lines changed

2 files changed

+134
-46
lines changed

README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
1-
21
# TypeScript Server (TSserver) Documentation Project
32

4-
TSserver is a powerful tool that enhances TypeScript coding and project management. However, we recognize that the lack of human-readable documentation can be a barrier to effectively utilizing its full potential. To address this challenge, our project focuses on generating simple, accessible JSDoc documentation. Our goal is to make TSserver more user-friendly, allowing anyone to leverage its capabilities without the need to decipher complex TypeScript code syntaxes.
3+
TSserver is a powerful tool that enhances TypeScript coding and project management. However, we recognize that the lack
4+
of human-readable documentation can be a barrier to effectively utilizing its full potential. To address this challenge,
5+
our project focuses on generating simple, accessible JSDoc documentation. Our goal is to make TSserver more
6+
user-friendly, allowing anyone to leverage its capabilities without the need to decipher complex TypeScript code
7+
syntaxes.
58

69
## Project Objective
710

8-
Our main objective is to transform the complex and cryptic syntaxes of TSserver into easy-to-understand documentation. We aim to provide clear, concise, and practical guides that cater to both beginners and experienced TypeScript developers.
11+
Our main objective is to transform the complex and cryptic syntaxes of TSserver into easy-to-understand documentation.
12+
We aim to provide clear, concise, and practical guides that cater to both beginners and experienced TypeScript
13+
developers.
14+
15+
## Phoenix code LSP experiments
16+
17+
Please see `src/experiments/readme.md` for experiments and results for future LSP integration
18+
into Phoenix Code.
919

1020
## Getting Started
1121

1222
To get started with TSserver using our JSDoc documentation:
1323

14-
- Open the `utils/server.js` file in your project.
15-
- View the JSDoc comments above each function to understand their usage and capabilities.
24+
- Open the `utils/server.js` file in your project.
25+
- View the JSDoc comments above each function to understand their usage and capabilities.
1626

1727
## Contributing
1828

19-
We welcome contributions to improve and expand the documentation. If you have insights, examples, or enhancements, please feel free to contribute. Check out our [Contributing Guide](CONTRIBUTING.md) for guidelines on how to make submissions.
29+
We welcome contributions to improve and expand the documentation. If you have insights, examples, or enhancements,
30+
please feel free to contribute. Check out our [Contributing Guide](CONTRIBUTING.md) for guidelines on how to make
31+
submissions.
2032

2133
## License
2234

src/experiments/readme.md

Lines changed: 116 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,70 @@
1-
# TypeScript Language Server Client
1+
# TypeScript Language Server Integration
22

3-
A Node.js CLI tool for getting TypeScript/JavaScript code completions using the Language Server Protocol (LSP), inspired by Zed editor's implementation.
3+
## Overview
4+
5+
This experiments folder explores TypeScript/JavaScript code completions and language features using the Language Server Protocol
6+
(LSP).
7+
8+
## Experiments
9+
10+
### TypeScript Language Server
11+
12+
We have a working implementation in the `experiments` folder that uses the Language Server Protocol with
13+
[typescript-language-server](https://github.com/typescript-language-server/typescript-language-server). This approach,
14+
inspired by Zed editor's implementation, proved to be more straightforward and effective than alternatives.
15+
16+
The experiment demonstrates:
17+
18+
- Functional code completions for JavaScript and TypeScript files
19+
- Property access completion with proper context
20+
- Integration with project-specific TypeScript installations
21+
- Robust message handling for LSP communication
22+
23+
See the [experiments README](./experiments/README.md) for implementation details and lessons learned.
24+
25+
### Alternative Approaches
26+
27+
We also investigated using the TypeScript server directly, based on VS Code's TypeScript implementation. However, this
28+
approach proved unnecessarily complex compared to the LSP-based solution. The typescript-language-server abstraction
29+
provided a cleaner interface while maintaining all the functionality we needed.
30+
31+
## Key Challenges Solved
32+
33+
One of the main problems we encountered was getting relevant code hints. The language service initially returned either:
34+
35+
1. Too many irrelevant completions without proper context
36+
2. No completions at all in certain scenarios (particularly with property access)
37+
38+
The critical fix was properly specifying trigger characters and trigger kinds in completion requests. See the
39+
documentation below for details on this and other challenges we overcame.
40+
41+
## Phoenix Code Integration Recommendations
42+
43+
Based on our findings, here are the recommendations for integrating with Phoenix Code:
44+
45+
1. The existing Brackets LSP code in Phoenix Code already handles most cases for:
46+
47+
- Code completions
48+
- Jump to definition/declarations
49+
- Hover information
50+
- Other language features
51+
52+
2. For integration, the main work needed is:
53+
- Implement Node.js connectors in Phoenix Code
54+
- Configure communication with the TypeScript language server
55+
- Ensure proper trigger character handling for completions
56+
57+
The existing LSP infrastructure in Phoenix Code provides a solid foundation, and with proper connections to the
58+
TypeScript language server, you should be able to achieve full functionality without significant architectural changes.
59+
60+
# Getting Started
61+
62+
See the docs below for setup instructions and example usage of the prototype implementation.
63+
64+
## TypeScript Language Server Client
65+
66+
A Node.js CLI tool for getting TypeScript/JavaScript code completions using the Language Server Protocol (LSP), inspired
67+
by Zed editor's implementation.
468

569
## Quick Start
670

@@ -13,19 +77,20 @@ node lsp.js /path/to/project /path/to/file.js 10:15
1377
```
1478

1579
Where:
16-
- `/path/to/project` is the root directory of your project
17-
- `/path/to/file.js` is the specific file you want completions for
18-
- `10:15` is the line and character position (zero-based) where you want completions
80+
81+
- `/path/to/project` is the root directory of your project
82+
- `/path/to/file.js` is the specific file you want completions for
83+
- `10:15` is the line and character position (zero-based) where you want completions
1984

2085
## Key Features
2186

22-
- Works with JavaScript, TypeScript, JSX, and TSX files
23-
- Automatically detects project configuration (tsconfig.json/jsconfig.json)
24-
- Uses locally installed TypeScript if available
25-
- Falls back to typescript-language-server's bundled TypeScript if needed
26-
- Provides proper property completions for imported modules
27-
- Robust message handling for LSP communication
28-
- Timeouts to prevent hanging on incomplete responses
87+
- Works with JavaScript, TypeScript, JSX, and TSX files
88+
- Automatically detects project configuration (tsconfig.json/jsconfig.json)
89+
- Uses locally installed TypeScript if available
90+
- Falls back to typescript-language-server's bundled TypeScript if needed
91+
- Provides proper property completions for imported modules
92+
- Robust message handling for LSP communication
93+
- Timeouts to prevent hanging on incomplete responses
2994

3095
## Common Challenges and Solutions
3196

@@ -34,10 +99,11 @@ Where:
3499
**Challenge**: The Language Server Protocol uses a specific message format with headers and JSON body.
35100

36101
**Solution**: Implemented proper binary buffer handling with Content-Length parsing:
102+
37103
```javascript
38104
// Format for sending
39105
const jsonMessage = JSON.stringify(message);
40-
const contentLength = Buffer.byteLength(jsonMessage, 'utf8');
106+
const contentLength = Buffer.byteLength(jsonMessage, "utf8");
41107
const header = `Content-Length: ${contentLength}\r\n\r\n`;
42108
serverProcess.stdin.write(header + jsonMessage);
43109

@@ -50,6 +116,7 @@ serverProcess.stdin.write(header + jsonMessage);
50116
**Challenge**: LSP has two types of messages (requests that need responses and notifications that don't).
51117

52118
**Solution**: Implemented separate methods for each type:
119+
53120
```javascript
54121
// For requests (require response)
55122
sendRequest: function(method, params) {
@@ -67,6 +134,7 @@ sendNotification: function(method, params) {
67134
**Challenge**: The server sometimes sends very large messages that can cause the client to hang.
68135

69136
**Solution**: Implemented binary buffer handling with progress tracking and timeouts:
137+
70138
```javascript
71139
// Binary buffer concatenation
72140
buffer = Buffer.concat([buffer, chunk]);
@@ -77,28 +145,32 @@ const percentComplete = ((buffer.length / contentLength) * 100).toFixed(1);
77145
// Timeout mechanism for completion requests
78146
const completionPromise = getCompletions(server, documentUri, line, character);
79147
const timeoutPromise = new Promise((_, reject) =>
80-
setTimeout(() => reject(new Error('Completion request timed out')), 10000)
148+
setTimeout(() => reject(new Error("Completion request timed out")), 10000)
81149
);
82150
```
83151

84152
### 4. Import Resolution
85153

86154
**Challenge**: Code completions for imported modules weren't working initially.
87155

88-
**Solution**: While we implemented a `syncRelatedFiles` function to provide context to the language server, we discovered that this wasn't actually the main issue. The key fix was properly specifying the trigger character (see below).
156+
**Solution**: While we implemented a `syncRelatedFiles` function to provide context to the language server, we
157+
discovered that this wasn't actually the main issue. The key fix was properly specifying the trigger character (see
158+
below).
89159

90-
**Note**: The file syncing code is currently prepared but may not be necessary in most cases as long as the trigger character is correctly specified:
160+
**Note**: The file syncing code is currently prepared but may not be necessary in most cases as long as the trigger
161+
character is correctly specified:
91162

92163
```javascript
93164
// Find and open all related files in the directory
94165
async function syncRelatedFiles(server, projectRoot, targetFile) {
95-
// Opens JS/TS files in the same directory
166+
// Opens JS/TS files in the same directory
96167
}
97168
```
98169

99170
### 5. Completion Parameters and Trigger Characters
100171

101-
**Challenge**: Property completions for object members weren't showing up at all. For example, with code like `config.se<cursor>rver.port`, no completions were being returned, even though VS Code would show property suggestions.
172+
**Challenge**: Property completions for object members weren't showing up at all. For example, with code like
173+
`config.se<cursor>rver.port`, no completions were being returned, even though VS Code would show property suggestions.
102174

103175
**Solution**: Adding the correct trigger character (".") to completion requests was critical:
104176

@@ -121,27 +193,28 @@ async function syncRelatedFiles(server, projectRoot, targetFile) {
121193
```
122194

123195
**Important**: The trigger character should match the character that triggered the completion:
124-
- For property access (`config.se<cursor>rver`), the trigger character should be "."
125-
- For regular identifier completions (`con<cursor>fig`), you should omit the trigger character
126-
- Using the wrong trigger character can result in irrelevant completions or no completions at all
127196

128-
This was the single most important fix that made property completions work correctly.
197+
- For property access (`config.se<cursor>rver`), the trigger character should be "."
198+
- For regular identifier completions (`con<cursor>fig`), you should omit the trigger character
199+
- Using the wrong trigger character can result in irrelevant completions or no completions at all
129200

201+
This was the single most important fix that made property completions work correctly.
130202

131203
### 6. TypeScript Detection
132204

133205
**Challenge**: Needed to detect and use the local project's TypeScript installation.
134206

135207
**Solution**: Checked common installation paths and provided fallbacks:
208+
136209
```javascript
137210
// Check for TypeScript in common locations
138-
const yarnTsPath = path.join(projectPath, '.yarn/sdks/typescript/lib');
139-
const npmTsPath = path.join(projectPath, 'node_modules/typescript/lib');
211+
const yarnTsPath = path.join(projectPath, ".yarn/sdks/typescript/lib");
212+
const npmTsPath = path.join(projectPath, "node_modules/typescript/lib");
140213

141214
if (fs.existsSync(yarnTsPath)) {
142-
tsdk = '.yarn/sdks/typescript/lib';
215+
tsdk = ".yarn/sdks/typescript/lib";
143216
} else if (fs.existsSync(npmTsPath)) {
144-
tsdk = 'node_modules/typescript/lib';
217+
tsdk = "node_modules/typescript/lib";
145218
}
146219
```
147220

@@ -150,6 +223,7 @@ if (fs.existsSync(yarnTsPath)) {
150223
### Trigger Kinds
151224

152225
The LSP defines three trigger kinds for completion requests:
226+
153227
1. **Invoked (1)**: Manual completion (Ctrl+Space)
154228
2. **TriggerCharacter (2)**: Automatic completion after typing a specific character
155229
3. **TriggerForIncompleteCompletions (3)**: Request for more items from a previous result
@@ -158,22 +232,24 @@ The LSP defines three trigger kinds for completion requests:
158232

159233
Common trigger characters in TypeScript/JavaScript and when to use them:
160234

161-
| Character | Context | Example | When to Include |
162-
|-----------|---------|---------|----------------|
163-
| `.` | Property access | `object.prop` | When completing properties after a dot |
164-
| `"`, `'` | String literals/imports | `import "./` | When completing paths in quotes |
165-
| `/` | Path completions | `import "./folder/` | When completing folders/files |
166-
| `@` | Decorators | `@Decorator` | When completing TypeScript decorators |
167-
| `<` | JSX tags | `<Component` | When completing JSX/TSX tags |
168-
| `(` | Function calls | `function(` | When completing function parameters |
169-
| `[` | Bracket notation | `object[` | When completing key access |
170-
| (none) | Regular identifiers | `cons` | When completing variable/function names |
235+
| Character | Context | Example | When to Include |
236+
| --------- | ----------------------- | ------------------- | --------------------------------------- |
237+
| `.` | Property access | `object.prop` | When completing properties after a dot |
238+
| `"`, `'` | String literals/imports | `import "./` | When completing paths in quotes |
239+
| `/` | Path completions | `import "./folder/` | When completing folders/files |
240+
| `@` | Decorators | `@Decorator` | When completing TypeScript decorators |
241+
| `<` | JSX tags | `<Component` | When completing JSX/TSX tags |
242+
| `(` | Function calls | `function(` | When completing function parameters |
243+
| `[` | Bracket notation | `object[` | When completing key access |
244+
| (none) | Regular identifiers | `cons` | When completing variable/function names |
171245

172-
**Important**: Do not include a trigger character for regular identifier completions - it will filter results incorrectly. Only include the trigger character that was actually typed by the user to trigger the completion.
246+
**Important**: Do not include a trigger character for regular identifier completions - it will filter results
247+
incorrectly. Only include the trigger character that was actually typed by the user to trigger the completion.
173248

174249
## Next Steps
175250

176251
Potential improvements:
252+
177253
1. Add support for document changes (editing files)
178254
2. Implement completion item resolution for more details
179255
3. Add signature help support
@@ -184,6 +260,6 @@ Potential improvements:
184260

185261
## References
186262

187-
- [Language Server Protocol Specification](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/)
188-
- [TypeScript Language Server](https://github.com/typescript-language-server/typescript-language-server)
189-
- [Zed Editor's TypeScript LSP implementation](https://github.com/zed-industries/zed/blob/148131786f5b15e8fdcb9e550abbf9edfd3dd0f8/crates/project/src/lsp_command.rs#L2035)
263+
- [Language Server Protocol Specification](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/)
264+
- [TypeScript Language Server](https://github.com/typescript-language-server/typescript-language-server)
265+
- [Zed Editor's TypeScript LSP implementation](https://github.com/zed-industries/zed/blob/148131786f5b15e8fdcb9e550abbf9edfd3dd0f8/crates/project/src/lsp_command.rs#L2035)

0 commit comments

Comments
 (0)