Coverage for src/robotide/editor/editorcreator.py: 100%
49 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-06 10:40 +0100
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-06 10:40 +0100
1# Copyright 2008-2015 Nokia Networks
2# Copyright 2016- Robot Framework Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
17from .. import robotapi 1ab
18from .. import controller 1ab
19from ..controller.dataloader import TestDataDirectoryWithExcludes 1ab
20from ..controller import filecontrollers 1ab
21from ..controller.settingcontrollers import VariableController 1ab
22from .editors import (InitFileEditor, TestCaseFileEditor, WelcomePage, ResourceFileEditor) 1ab
23from .macroeditors import TestCaseEditor, UserKeywordEditor 1ab
26def variable_editor_chooser(plugin, parent, _controller, tree): 1ab
27 _controller = _controller.datafile_controller 1dc
28 editor_class = plugin.get_editor(_controller.data.__class__) 1dc
29 return editor_class(plugin, parent, _controller, tree) 1dc
32class EditorCreator(object): 1ab
33 # DEBUG: Should not use robot.model classes here
34 _EDITORS = ((robotapi.TestDataDirectory, InitFileEditor), 1ab
35 (robotapi.ResourceFile, ResourceFileEditor),
36 (robotapi.TestCase, TestCaseEditor),
37 (robotapi.TestCaseFile, TestCaseFileEditor),
38 (robotapi.UserKeyword, UserKeywordEditor),
39 (robotapi.Variable, variable_editor_chooser),
40 (TestDataDirectoryWithExcludes, InitFileEditor))
42 def __init__(self, editor_registerer): 1ab
43 self._editor_registerer = editor_registerer
44 self._editor = None
46 def register_editors(self): 1ab
47 for item, editorclass in self._EDITORS:
48 self._editor_registerer(item, editorclass)
50 def editor_for(self, plugin, editor_panel, tree): 1ab
51 self._editor = self._create_editor(editor_panel, plugin, tree) 1agdiefchjk
52 return self._editor 1agdiefchjk
54 def _create_editor(self, editor_panel, plugin, tree): 1ab
55 _controller = plugin.get_selected_item() 1agdiefchjk
56 if self._invalid(_controller): 1agdiefchjk
57 # http://code.google.com/p/robotframework-ride/issues/detail?id=1092
58 if self._editor and tree and (not tree.datafile_nodes or 1aihjk
59 self._only_resource_files(tree)):
60 self._editor.w_destroy()
61 self._editor = None
62 return None
63 if self._editor: 1aihjk
64 return self._editor 1h
65 return WelcomePage(editor_panel) 1aihjk
66 if self._should_use_old_editor(_controller): 1gdefc
67 return self._editor 1c
68 return self._create_new_editor(_controller, editor_panel, plugin, tree) 1gdefc
70 @staticmethod 1ab
71 def _invalid(_controller): 1ab
72 return not _controller or _controller.data is None or \ 1agdiefchjk
73 isinstance(_controller, controller.Project) or \
74 isinstance(_controller, filecontrollers.ExcludedDirectoryController)
76 def _should_use_old_editor(self, _controller): 1ab
77 return self._editor and \ 1gdefc
78 isinstance(_controller, VariableController) and \
79 _controller.datafile_controller is self._editor.controller
81 def _create_new_editor(self, _controller, editor_panel, plugin, tree): 1ab
82 editor_class = plugin.get_editor(_controller.data.__class__) 1gdefc
83 if self._editor: 1gdefc
84 self._editor.w_destroy() 1ef
85 editor_panel.Show(False) 1gdefc
86 return editor_class(plugin, editor_panel, _controller, tree) 1gdefc
88 @staticmethod 1ab
89 def _only_resource_files(tree): 1ab
90 return all([tree.node_is_resource_file(node) 1l
91 for node in tree.datafile_nodes])