5
5
import os
6
6
from typing import TYPE_CHECKING , Any , List , Optional , cast
7
7
8
+ from ....utils .async_tools import threaded
8
9
from ....utils .logging import LoggingDescriptor
9
10
from ...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
+ )
11
18
from ...common .text_document import TextDocument
19
+ from ..utils .version import get_robot_version
12
20
13
21
if TYPE_CHECKING :
14
22
from ..protocol import RobotLanguageServerProtocol
@@ -33,6 +41,7 @@ def __init__(self, parent: RobotLanguageServerProtocol) -> None:
33
41
super ().__init__ (parent )
34
42
35
43
parent .formatting .format .add (self .format )
44
+
36
45
# TODO implement range formatting
37
46
# parent.formatting.format_range.add(self.format_range)
38
47
@@ -50,14 +59,24 @@ async def get_config(self, document: TextDocument) -> Optional[RoboTidyConfig]:
50
59
return await self .parent .workspace .get_configuration (RoboTidyConfig , folder .uri )
51
60
52
61
@language_id ("robotframework" )
53
- @_logger .call (entering = True , exiting = True , exception = True )
62
+ @threaded ()
63
+ @_logger .call
54
64
async def format (
55
65
self , sender : Any , document : TextDocument , options : FormattingOptions , ** further_options : Any
56
66
) -> Optional [List [TextEdit ]]:
57
67
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 ():
59
70
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
61
80
62
81
async def format_robot_tidy (
63
82
self , document : TextDocument , options : FormattingOptions , ** further_options : Any
0 commit comments