Send a JSON-RPC request to the LSP server
(self, method: str, params: dict = None)
| 53 | self.initialized = False |
| 54 | |
| 55 | async def send_request(self, method: str, params: dict = None) -> dict: |
| 56 | """Send a JSON-RPC request to the LSP server""" |
| 57 | if not self.process or not self.stdin or not self.stdout: |
| 58 | raise RuntimeError('LSP server not started') |
| 59 | |
| 60 | self.message_id += 1 |
| 61 | request_id = self.message_id |
| 62 | request = { |
| 63 | 'jsonrpc': '2.0', |
| 64 | 'id': request_id, |
| 65 | 'method': method, |
| 66 | 'params': params or {} |
| 67 | } |
| 68 | |
| 69 | content = json.dumps(request) |
| 70 | message = f'Content-Length: {len(content)}\r\n\r\n{content}' |
| 71 | |
| 72 | try: |
| 73 | self.stdin.write(message.encode('utf-8')) |
| 74 | await self.stdin.drain() |
| 75 | |
| 76 | max_retries = 20 |
| 77 | for _ in range(max_retries): |
| 78 | msg = await self._read_message() |
| 79 | |
| 80 | # Check if it's the response we're waiting for |
| 81 | if 'id' in msg and msg['id'] == request_id: |
| 82 | return msg |
| 83 | |
| 84 | # It's a notification (no id) or response for different request |
| 85 | # Log and continue reading |
| 86 | if 'method' in msg: |
| 87 | logger.debug( |
| 88 | f"Received notification during request: {msg.get('method')}" |
| 89 | ) |
| 90 | continue |
| 91 | |
| 92 | logger.warning( |
| 93 | f'No response received for request {request_id} after {max_retries} attempts' |
| 94 | ) |
| 95 | return {'error': 'No response received'} |
| 96 | |
| 97 | except Exception as e: |
| 98 | logger.error(f'Error sending LSP request: {e}') |
| 99 | return {'error': str(e)} |
| 100 | |
| 101 | async def send_notification(self, method: str, params: dict = None): |
| 102 | """Send a JSON-RPC notification to the LSP server""" |
no test coverage detected