23
23
from ..protocol import RobotLanguageServerProtocol
24
24
25
25
from ..configuration import RoboTidyConfig
26
+ from ..utils .version import create_version_from_str
26
27
from .model_helper import ModelHelperMixin
27
28
from .protocol_part import RobotLanguageServerProtocolPart
28
29
@@ -43,8 +44,8 @@ def __init__(self, parent: RobotLanguageServerProtocol) -> None:
43
44
44
45
parent .formatting .format .add (self .format )
45
46
46
- # TODO implement range formatting
47
- # parent.formatting.format_range.add(self.format_range)
47
+ if robotidy_installed ():
48
+ parent .formatting .format_range .add (self .format_range )
48
49
49
50
self .space_count = 4
50
51
self .use_pipes = False
@@ -82,19 +83,37 @@ async def format(
82
83
RE_LINEBREAKS = re .compile (r"\r\n|\r|\n" )
83
84
84
85
async def format_robot_tidy (
85
- self , document : TextDocument , options : FormattingOptions , ** further_options : Any
86
+ self , document : TextDocument , options : FormattingOptions , range : Optional [ Range ] = None , ** further_options : Any
86
87
) -> Optional [List [TextEdit ]]:
87
88
88
89
from difflib import SequenceMatcher
89
90
90
91
from robotidy .api import RobotidyAPI
92
+ from robotidy .version import __version__
91
93
92
94
try :
93
95
model = await self .parent .documents_cache .get_model (document , False )
96
+ if model is None :
97
+ return None
94
98
95
99
robot_tidy = RobotidyAPI (document .uri .to_path (), None )
96
100
97
- changed , _ , new = robot_tidy .transform (model )
101
+ if range is not None :
102
+ robot_tidy .formatting_config .start_line = range .start .line
103
+ robot_tidy .formatting_config .end_line = range .end .line + 1
104
+
105
+ if create_version_from_str (__version__ ) >= (2 , 2 ):
106
+ from robotidy .disablers import RegisterDisablers
107
+
108
+ disabler_finder = RegisterDisablers (
109
+ robot_tidy .formatting_config .start_line , robot_tidy .formatting_config .end_line
110
+ )
111
+ disabler_finder .visit (model )
112
+ if disabler_finder .file_disabled :
113
+ return None
114
+ changed , _ , new = robot_tidy .transform (model , disabler_finder .disablers )
115
+ else :
116
+ changed , _ , new = robot_tidy .transform (model )
98
117
99
118
if not changed :
100
119
return None
@@ -173,8 +192,9 @@ async def format_internal(
173
192
async def format_range (
174
193
self , sender : Any , document : TextDocument , range : Range , options : FormattingOptions , ** further_options : Any
175
194
) -> Optional [List [TextEdit ]]:
176
- # TODO implement range formatting
177
- # config = await self.get_config(document)
178
- # if config and config.enabled and robotidy_installed():
179
- # return await self.format_robot_tidy(document, options, range=range, **further_options)
195
+
196
+ config = await self .get_config (document )
197
+ if config and config .enabled and robotidy_installed ():
198
+ return await self .format_robot_tidy (document , options , range = range , ** further_options )
199
+
180
200
return None
0 commit comments