aboutsummaryrefslogtreecommitdiffstats
path: root/tools/snippets_translate/main.py
diff options
context:
space:
mode:
authorJaime Resano <gemailpersonal02@gmail.com>2022-02-24 01:52:28 +0100
committerCristian Maureira-Fredes <cristian.maureira-fredes@qt.io>2022-06-29 20:34:31 +0000
commit168f0c941cefe2fcdaaa12498272f181fe246986 (patch)
treec2e3247285484f613a3be6ca030705276c408645 /tools/snippets_translate/main.py
parent0e8ab25c4ccbd46e62bbb81b169ec0d227cbfc33 (diff)
snippet translate: fix get_snippets
- Fixed the get_snippets function which did not work properly when more than one snippet id was on the same line. - Tests were added Pick-to: 6.2 6.3 Change-Id: Idffbb0aee258522d7855e2ad0e2b8df61a1872c8 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Diffstat (limited to 'tools/snippets_translate/main.py')
-rw-r--r--tools/snippets_translate/main.py49
1 files changed, 33 insertions, 16 deletions
diff --git a/tools/snippets_translate/main.py b/tools/snippets_translate/main.py
index 5e31f50ef..cf34bc6cd 100644
--- a/tools/snippets_translate/main.py
+++ b/tools/snippets_translate/main.py
@@ -9,6 +9,7 @@ import sys
from enum import Enum
from pathlib import Path
from textwrap import dedent
+from typing import List
from converter import snippet_translate
@@ -149,28 +150,44 @@ def is_valid_file(x):
return True
-def get_snippet_ids(line):
- """Extract the snippet ids for a line '//! [1] //! [2]'"""
+def get_snippet_ids(line: str) -> List[str]:
+ # Extract the snippet ids for a line '//! [1] //! [2]'
result = []
for m in SNIPPET_PATTERN.finditer(line):
result.append(m.group(1))
return result
-def get_snippets(data):
- """Extract (potentially overlapping) snippets from a C++ file indicated by //! [1]"""
- current_snippets = [] # Active ids
- snippets = []
- for line in data:
- new_ids = get_snippet_ids(line)
- for id in new_ids:
- if id in current_snippets: # id encountered 2nd time: Snippet ends
- current_snippets.remove(id)
- else:
- current_snippets.append(id)
-
- if new_ids or current_snippets:
- snippets.append(line)
+def get_snippets(lines: List[str]) -> List[List[str]]:
+ # Extract (potentially overlapping) snippets from a C++ file indicated by //! [1]
+ snippets: List[List[str]] = []
+ snippet: List[str]
+
+ i = 0
+ while i < len(lines):
+ line = lines[i]
+ i += 1
+
+ start_ids = get_snippet_ids(line)
+ while start_ids:
+ # Start of a snippet
+ start_id = start_ids.pop(0)
+ snippet = [line] # The snippet starts with his id
+
+ # Find the end of the snippet
+ j = i
+ while j < len(lines):
+ l = lines[j]
+ j += 1
+
+ # Add the line to the snippet
+ snippet.append(l)
+
+ # Check if the snippet is complete
+ if start_id in get_snippet_ids(l):
+ # End of snippet
+ snippets.append(snippet)
+ break
return snippets