55import os
66from typing import TYPE_CHECKING , Any , List , Optional , cast
77
8+ from ....utils .async_tools import threaded
89from ....utils .logging import LoggingDescriptor
910from ...common .decorators import language_id
10- from ...common .lsp_types import FormattingOptions , Position , Range , TextEdit
11+ from ...common .lsp_types import (
12+ FormattingOptions ,
13+ MessageType ,
14+ Position ,
15+ Range ,
16+ TextEdit ,
17+ )
1118from ...common .text_document import TextDocument
19+ from ..utils .version import get_robot_version
1220
1321if TYPE_CHECKING :
1422 from ..protocol import RobotLanguageServerProtocol
@@ -33,6 +41,7 @@ def __init__(self, parent: RobotLanguageServerProtocol) -> None:
3341 super ().__init__ (parent )
3442
3543 parent .formatting .format .add (self .format )
44+
3645 # TODO implement range formatting
3746 # parent.formatting.format_range.add(self.format_range)
3847
@@ -50,14 +59,24 @@ async def get_config(self, document: TextDocument) -> Optional[RoboTidyConfig]:
5059 return await self .parent .workspace .get_configuration (RoboTidyConfig , folder .uri )
5160
5261 @language_id ("robotframework" )
53- @_logger .call (entering = True , exiting = True , exception = True )
62+ @threaded ()
63+ @_logger .call
5464 async def format (
5565 self , sender : Any , document : TextDocument , options : FormattingOptions , ** further_options : Any
5666 ) -> Optional [List [TextEdit ]]:
5767 config = await self .get_config (document )
58- if config and config .enabled and robotidy_installed ():
68+
69+ if config and (config .enabled or get_robot_version () >= (5 , 0 )) and robotidy_installed ():
5970 return await self .format_robot_tidy (document , options , ** further_options )
60- return await self .format_internal (document , options , ** further_options )
71+
72+ if get_robot_version () < (5 , 0 ):
73+ return await self .format_internal (document , options , ** further_options )
74+
75+ self .parent .window .show_message (
76+ "RobotFramework formatter is not available, please install 'robotframework-tidy'." , MessageType .ERROR
77+ )
78+
79+ return None
6180
6281 async def format_robot_tidy (
6382 self , document : TextDocument , options : FormattingOptions , ** further_options : Any
0 commit comments