Skip to content

Commit a9e9c02

Browse files
committed
feat: new robotcode.robotidy.ignoreGitDir and robotcode.robotidy.config setting to set the config file for _robotidy_ and to ignore git files if searching for config files for _robotidy_
see also: https://robotidy.readthedocs.io/
1 parent d48b629 commit a9e9c02

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,18 @@
641641
"markdownDescription": "Enables 'robotidy' code formatting, if installed. See [robotidy](https://github.com/MarketSquare/robotframework-tidy)",
642642
"scope": "resource"
643643
},
644+
"robotcode.robotidy.ignoreGitDir": {
645+
"type": "boolean",
646+
"default": true,
647+
"markdownDescription": "Ignore .git directories when searching for the default configuration file. Corresponds to the `--ignore-git-dir` of _robotidy_ See [robotidy](https://github.com/MarketSquare/robotframework-tidy)",
648+
"scope": "resource"
649+
},
650+
"robotcode.robotidy.config": {
651+
"type": "string",
652+
"default": "",
653+
"markdownDescription": "Read configuration from FILE path. Corresponds to the `--config` of _robotidy_ See [robotidy](https://github.com/MarketSquare/robotframework-tidy)",
654+
"scope": "resource"
655+
},
644656
"robotcode.analysis.findUnusedReferences": {
645657
"type": "boolean",
646658
"default": false,

packages/language_server/src/robotcode/language_server/robotframework/configuration.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class RoboCopConfig(ConfigBase):
7171
@dataclass
7272
class RoboTidyConfig(ConfigBase):
7373
enabled: bool = True
74+
ignore_git_dir: bool = False
75+
config: Optional[str] = None
7476

7577

7678
@config_section("robotcode.workspace")

packages/language_server/src/robotcode/language_server/robotframework/parts/formatting.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ def __init__(self, parent: RobotLanguageServerProtocol) -> None:
5555
self.short_test_name_length = 18
5656
self.setting_and_variable_name_length = 14
5757

58-
async def get_config(self, document: TextDocument) -> Optional[RoboTidyConfig]:
58+
async def get_config(self, document: TextDocument) -> RoboTidyConfig:
5959
folder = self.parent.workspace.get_workspace_folder(document.uri)
6060
if folder is None:
61-
return None
61+
return RoboTidyConfig()
6262

6363
return await self.parent.workspace.get_configuration(RoboTidyConfig, folder.uri)
6464

@@ -73,8 +73,8 @@ async def format(
7373
) -> Optional[List[TextEdit]]:
7474
config = await self.get_config(document)
7575

76-
if config and (config.enabled or get_robot_version() >= (5, 0)) and robotidy_installed():
77-
return await self.format_robot_tidy(document, options, **further_options)
76+
if (config.enabled or get_robot_version() >= (5, 0)) and robotidy_installed():
77+
return await self.format_robot_tidy(document, options, config=config, **further_options)
7878

7979
if get_robot_version() < (5, 0):
8080
return await self.format_internal(document, options, **further_options)
@@ -93,11 +93,15 @@ async def format_robot_tidy(
9393
document: TextDocument,
9494
options: FormattingOptions,
9595
range: Optional[Range] = None,
96+
config: Optional[RoboTidyConfig] = None,
9697
**further_options: Any,
9798
) -> Optional[List[TextEdit]]:
9899
from robotidy.version import __version__
99100

100101
try:
102+
if config is None:
103+
config = await self.get_config(document)
104+
101105
robotidy_version = create_version_from_str(__version__)
102106

103107
model = await self.parent.documents_cache.get_model(document, False)
@@ -106,7 +110,21 @@ async def format_robot_tidy(
106110
from robotidy.api import get_robotidy
107111
from robotidy.disablers import RegisterDisablers
108112

109-
robot_tidy = get_robotidy(document.uri.to_path(), None)
113+
if robotidy_version >= (4, 2):
114+
robot_tidy = get_robotidy(
115+
document.uri.to_path(),
116+
None,
117+
ignore_git_dir=config.ignore_git_dir,
118+
config=config.config,
119+
)
120+
elif robotidy_version >= (4, 1):
121+
robot_tidy = get_robotidy(
122+
document.uri.to_path(),
123+
None,
124+
ignore_git_dir=config.ignore_git_dir,
125+
)
126+
else:
127+
robot_tidy = get_robotidy(document.uri.to_path(), None)
110128

111129
if range is not None:
112130
robot_tidy.config.formatting.start_line = range.start.line + 1
@@ -168,6 +186,7 @@ async def format_robot_tidy(
168186
raise
169187
except BaseException as e:
170188
self._logger.exception(e)
189+
self.parent.window.show_message(f"Executing `robotidy` failed: {e}", MessageType.ERROR)
171190
return None
172191

173192
async def format_internal(
@@ -218,7 +237,7 @@ async def format_range(
218237
**further_options: Any,
219238
) -> Optional[List[TextEdit]]:
220239
config = await self.get_config(document)
221-
if config and config.enabled and robotidy_installed():
222-
return await self.format_robot_tidy(document, options, range=range, **further_options)
240+
if config.enabled and robotidy_installed():
241+
return await self.format_robot_tidy(document, options, range=range, config=config, **further_options)
223242

224243
return None

0 commit comments

Comments
 (0)