2323 from ..protocol import RobotLanguageServerProtocol
2424
2525from ..configuration import RoboTidyConfig
26+ from ..utils .version import create_version_from_str
2627from .model_helper import ModelHelperMixin
2728from .protocol_part import RobotLanguageServerProtocolPart
2829
@@ -43,8 +44,8 @@ def __init__(self, parent: RobotLanguageServerProtocol) -> None:
4344
4445 parent .formatting .format .add (self .format )
4546
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 )
4849
4950 self .space_count = 4
5051 self .use_pipes = False
@@ -82,19 +83,37 @@ async def format(
8283 RE_LINEBREAKS = re .compile (r"\r\n|\r|\n" )
8384
8485 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
8687 ) -> Optional [List [TextEdit ]]:
8788
8889 from difflib import SequenceMatcher
8990
9091 from robotidy .api import RobotidyAPI
92+ from robotidy .version import __version__
9193
9294 try :
9395 model = await self .parent .documents_cache .get_model (document , False )
96+ if model is None :
97+ return None
9498
9599 robot_tidy = RobotidyAPI (document .uri .to_path (), None )
96100
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 )
98117
99118 if not changed :
100119 return None
@@ -173,8 +192,9 @@ async def format_internal(
173192 async def format_range (
174193 self , sender : Any , document : TextDocument , range : Range , options : FormattingOptions , ** further_options : Any
175194 ) -> 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+
180200 return None
0 commit comments