55from pathlib import Path
66from typing import Any , Optional
77
8+ from codeflash .lsp .helpers import replace_quotes_with_backticks , simplify_worktree_paths
9+
810json_primitive_types = (str , float , int , bool )
11+ max_code_lines_before_collapse = 45
912
1013
1114@dataclass
@@ -28,6 +31,15 @@ def type(self) -> str:
2831 raise NotImplementedError
2932
3033 def serialize (self ) -> str :
34+ if isinstance (self , LspTextMessage ):
35+ self .text = simplify_worktree_paths (self .text )
36+ self .text = replace_quotes_with_backticks (self .text )
37+ if isinstance (self , LspCodeMessage ):
38+ self .file_name = simplify_worktree_paths (str (self .file_name ), highlight = False )
39+ if isinstance (self , LspMarkdownMessage ):
40+ self .markdown = simplify_worktree_paths (self .markdown )
41+ self .markdown = replace_quotes_with_backticks (self .markdown )
42+
3143 data = self ._loop_through (asdict (self ))
3244 # Important: keep type as the first key, for making it easy and fast for the client to know if this is a lsp message before parsing it
3345 ordered = {"type" : self .type (), ** data }
@@ -47,10 +59,19 @@ class LspCodeMessage(LspMessage):
4759 code : str
4860 file_name : Optional [Path ] = None
4961 function_name : Optional [str ] = None
62+ collapsed : bool = False
63+ lines_count : Optional [int ] = None
5064
5165 def type (self ) -> str :
5266 return "code"
5367
68+ def serialize (self ) -> str :
69+ code_lines_length = len (self .code .split ("\n " ))
70+ self .lines_count = code_lines_length
71+ if code_lines_length > max_code_lines_before_collapse :
72+ self .collapsed = True
73+ return super ().serialize ()
74+
5475
5576@dataclass
5677class LspMarkdownMessage (LspMessage ):
0 commit comments