Coverage for src/robotide/namespace/suggesters.py: 91%
93 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.
16from functools import total_ordering 1ab
18from robotide import robotapi 1ab
21class SuggestionSource(object): 1ab
23 def __init__(self, plugin, controller): 1ab
24 self._plugin = plugin 1anpcihjkl
25 self._controller = controller 1anpcihjkl
27 def get_suggestions(self, value, row=None): 1ab
28 # print(f"DEBUG: suggesters.py SuggestionSource get_suggestions ENTER value={value}")
29 start = value 1npcihjkl
30 while start and start[-1] in [']', '}', '=', ',']: 30 ↛ 31line 30 didn't jump to line 31 because the condition on line 30 was never true1npcihjkl
31 start = start[:-1]
32 # print(f"DEBUG: suggesters.py SuggestionSource get_suggestions SEARCHING start={start}")
33 # If we have a space separated value, try first the value and then the last word
34 key = start.split(' ')[-1] 1npcihjkl
35 keys = [start, key] if start != key else [start] 1npcihjkl
36 sugs = set() 1npcihjkl
37 for initial in keys: 1npcihjkl
38 if self._controller: 1npcihjkl
39 try: 1ncihjkl
40 sugs.update(self._controller.get_local_namespace_for_row(row).get_suggestions(initial)) 1ncihjkl
41 except AttributeError: 1ihjkl
42 try: 1ihjkl
43 sugs.update(self._controller.get_local_namespace.get_suggestions(initial)) 1ihjkl
44 except AttributeError: # For example TestCaseFileController 1ihjkl
45 pass 1ihjkl
46 # return list(sugs)
47 if self._plugin: 1npcihjkl
48 sugs.update(self._plugin.content_assist_values(initial)) # DEBUG: Remove old functionality when no more needed 1pch
49 # print(f"DEBUG: suggesters.py SuggestionSource get_suggestions IN LOOP initial ={initial} len sugs={len(sugs)}")
50 return list(sugs) 1npcihjkl
52 def update_from_local(self, words: list, language:str): 1ab
53 from ..lib.compat.parsing.languages import Language 1ac
54 if isinstance(language, list): 54 ↛ 56line 54 didn't jump to line 56 because the condition on line 54 was always true1ac
55 language = language[0] 1ac
56 if language in [None, '', 'English', 'En']: 56 ↛ 57line 56 didn't jump to line 57 because the condition on line 56 was never true1ac
57 localized = Language.from_name('en')
58 else:
59 localized = Language.from_name(language) 1ac
60 words.extend(set(list(localized.headers.values()) + list(localized.settings.values()) + 1ac
61 list(localized.bdd_prefixes) + localized.true_strings + localized.false_strings))
62 namespace = self._controller.get_local_namespace() 1ac
63 namespace.update_words_cache(words) 1ac
64 # print(f"DEBUG: suggesters.py SuggestionSource update_from_local words={words} namespace={namespace} "
65 # f"language={localized.name}")
67@total_ordering 1ab
68class _Suggester(object): 1ab
69 name = None 1ab
71 @staticmethod 1ab
72 def _suggestion(name): 1ab
73 s = lambda: 0 1qrsgdoef
74 s.name = name 1qrsgdoef
75 s.longname = name 1qrsgdoef
76 s.details = None 1qrsgdoef
77 return s 1qrsgdoef
79 def __eq__(self, other): 1ab
80 return self.name.lower() == other.name.lower()
82 def __hash__(self): 1ab
83 return hash(repr(self))
85 def __gt__(self, other): 1ab
86 return self.name.lower() > other.name.lower()
89class HistorySuggester(_Suggester): 1ab
91 def __init__(self): 1ab
92 self._suggestions = list() 1abq
94 def get_suggestions(self, name, *args): 1ab
95 _ = args 1qgdemf
96 return [s for s in self._suggestions if name is None or name.lower() in s.name.lower()] 1qgdemf
98 def store(self, name): 1ab
99 self._suggestions += [self._suggestion(name)] 1qd
100 # DEBUG For now remove sorting self._suggestions.sort()
103class _ImportSuggester(_Suggester): 1ab
105 def __init__(self, controller): 1ab
106 self._df_controller = controller.datafile_controller
108 def get_suggestions(self, name, *args): 1ab
109 _ = args 1gdoemf
110 already_imported = self.get_already_imported() 1gdoemf
111 all_resources = self.get_all_available() 1gdoemf
112 suggestion_names = all_resources - already_imported 1gdoemf
113 return [self._suggestion(n) for n in sorted(suggestion_names) if name in n] 1gdoemf
115 def get_already_imported(self): 1ab
116 return set(imp.name for imp in self._df_controller.imports) 1gdoemf
118 def get_all_available(self): 1ab
119 return NotImplemented
122class ResourceSuggester(_ImportSuggester): 1ab
124 def get_all_available(self): 1ab
125 return set(self._df_controller.relative_path_to(r) for r in self._df_controller._project.resources) 1oemf
128class CachedLibrarySuggester(_ImportSuggester): 1ab
130 def get_all_available(self): 1ab
131 return set(self._df_controller.get_all_cached_library_names()) 1gdoemf
134class BuiltInLibrariesSuggester(_Suggester): 1ab
136 def get_suggestions(self, name, *args): 1ab
137 _ = args 1rsgdemf
138 # print(f"DEBUG: suggesters.py BuiltInLibrariesSuggester get_suggestions ENTER name={name}")
139 return [self._suggestion(n) for n in sorted(robotapi.STDLIB_NAMES) 1rsgdemf
140 if name.lower() in n.lower() and n not in ['BuiltIn', 'Reserved', 'Easter']]
143class LibrariesSuggester(_Suggester): 1ab
145 def __init__(self, controller, history_suggester): 1ab
146 self._history_suggester = history_suggester
147 self._cached_suggester = CachedLibrarySuggester(controller)
148 self._builtin_suggester = BuiltInLibrariesSuggester()
150 def get_suggestions(self, name, *args): 1ab
151 history = set(h.name for h in self._history_suggester.get_suggestions(name, *args)) 1gdemf
152 cached = set(c.name for c in self._cached_suggester.get_suggestions(name, *args)) 1gdemf
153 builtin = set(b.name for b in self._builtin_suggester.get_suggestions(name, *args)) 1gdemf
154 already_imported = self._cached_suggester.get_already_imported() 1gdemf
155 return [self._suggestion(s) 1gdemf
156 for s in sorted((history | cached | builtin)-already_imported,
157 key=lambda s: s.lower())]