Skip to content

Commit ad91f4b

Browse files
committed
refactor(ast): improve object lookup efficiency
1 parent 7fb13c2 commit ad91f4b

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

fortls/parsers/internal/ast.py

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -216,34 +216,40 @@ def get_inner_scope(self, line_number: int):
216216
return curr_scope
217217

218218
def get_object(self, FQSN: str):
219-
FQSN_split = FQSN.split("::")
220-
curr_obj = self.global_dict.get(FQSN_split[0])
221-
if curr_obj is None:
222-
# Look for non-exportable scopes
223-
for scope in self.scope_list:
224-
if FQSN_split[0] == scope.FQSN:
225-
curr_obj = scope
226-
break
227-
if curr_obj is None:
219+
def find_child_by_name(parent, name):
220+
for child in parent.children:
221+
if child.name == name:
222+
return child
223+
if child.name.startswith("#GEN_INT"):
224+
found = next(
225+
(
226+
int_child
227+
for int_child in child.get_children()
228+
if int_child.name == name
229+
),
230+
None,
231+
)
232+
if found:
233+
return found
228234
return None
229-
if len(FQSN_split) > 1:
230-
for name in FQSN_split[1:]:
231-
next_obj = None
232-
for child in curr_obj.children:
233-
if child.name.startswith("#GEN_INT"):
234-
for int_child in child.get_children():
235-
if int_child.name == name:
236-
next_obj = int_child
237-
break
238-
if next_obj is not None:
239-
break
240-
if child.name == name:
241-
next_obj = child
242-
break
243-
if next_obj is None:
244-
return None
245-
curr_obj = next_obj
246-
return curr_obj
235+
236+
parts = FQSN.split("::")
237+
current = self.global_dict.get(parts[0])
238+
239+
# Look for non-exportable scopes
240+
if current is None:
241+
current = next(
242+
(scope for scope in self.scope_list if scope.FQSN == parts[0]), None
243+
)
244+
if current is None:
245+
return None
246+
247+
for part in parts[1:]:
248+
current = find_child_by_name(current, part)
249+
if current is None:
250+
return None
251+
252+
return current
247253

248254
def resolve_includes(self, workspace, path: str = None):
249255
file_dir = os.path.dirname(self.path)

0 commit comments

Comments
 (0)