11from __future__ import annotations
22
3+ import json
34import logging
4- import os
55from contextlib import contextmanager
66from itertools import cycle
7- from typing import TYPE_CHECKING
7+ from typing import TYPE_CHECKING , Any
88
99from rich .console import Console
1010from rich .logging import RichHandler
2020
2121from codeflash .cli_cmds .console_constants import SPINNER_TYPES
2222from codeflash .cli_cmds .logging_config import BARE_LOGGING_FORMAT
23- from codeflash .lsp .helpers import lsp_log
23+ from codeflash .lsp .helpers import enhanced_log , is_LSP_enabled
2424
2525if TYPE_CHECKING :
2626 from collections .abc import Generator
3131
3232console = Console ()
3333
34- if os . getenv ( "CODEFLASH_LSP" ):
34+ if is_LSP_enabled ( ):
3535 console .quiet = True
3636
3737logging .basicConfig (
4343logger = logging .getLogger ("rich" )
4444logging .getLogger ("parso" ).setLevel (logging .WARNING )
4545
46- real_info_log_fn = logger .info
47- logger .info = lambda msg , * args , ** kwargs : lsp_log (msg , real_info_log_fn , * args , ** kwargs )
46+ # override the logger to reformat the messages for the lsp
47+ for level in ("info" , "debug" , "warning" , "error" ):
48+ real_fn = getattr (logger , level )
49+ setattr (logger , level , lambda msg , * args , _real_fn = real_fn , ** kwargs : enhanced_log (msg , _real_fn , * args , ** kwargs ))
4850
49- real_debug_log_fn = logger .debug
50- logger .debug = lambda msg , * args , ** kwargs : lsp_log (msg , real_debug_log_fn , * args , ** kwargs )
51+
52+ def lsp_log (serializable : dict [str , Any ], * args : Any , ** kwargs : Any ) -> None : # noqa: ANN401
53+ if not is_LSP_enabled ():
54+ return
55+ msg_str = json .dumps (serializable )
56+ logger .info (msg_str , * args , ** kwargs )
5157
5258
5359def paneled_text (
@@ -69,8 +75,13 @@ def code_print(code_str: str) -> None:
6975 """Print code with syntax highlighting."""
7076 from rich .syntax import Syntax
7177
78+ formatted_code = Syntax (code_str , "python" , line_numbers = True , theme = "github-dark" )
79+ # if is_LSP_enabled():
80+ # logger.debug(formatted_code.__str__())
81+ # return
82+
7283 console .rule ()
73- console .print (Syntax ( code_str , "python" , line_numbers = True , theme = "github-dark" ) )
84+ console .print (formatted_code )
7485 console .rule ()
7586
7687
0 commit comments