From 97b512d8f8789fd4aef238d86150cb592adab84b Mon Sep 17 00:00:00 2001 From: Vasiliy Polikarpov Date: Tue, 25 Feb 2020 16:46:25 +0300 Subject: [PATCH] skip used namespaces --- cppclasshelper.py | 22 ++++++++++++++++++---- method_generator/klass/method.py | 4 ++-- method_generator/tokenizer.py | 2 +- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/cppclasshelper.py b/cppclasshelper.py index 9393aac..9ab874e 100644 --- a/cppclasshelper.py +++ b/cppclasshelper.py @@ -1,4 +1,4 @@ -import sublime, sublime_plugin, os +import sublime, sublime_plugin, os, re from sublime_lib import ResourcePath from .method_generator.exceptions import ClassValidationException @@ -147,13 +147,13 @@ class GenerateMethodDefinitionCommand(sublime_plugin.WindowCommand): method_definitions = None method_to_insert = None settings = None + using_namespace_list = None NEWLINE_AFTER_TEMPLATE = False NEWLINE_AFTER_METHOD = False PLACE_CURSOR_BETWEEN_BRACKETS = False def run(self): - # get filename of active view and get class name which to search the header vars = self.window.extract_variables() class_name = vars["file_base_name"] @@ -189,10 +189,11 @@ def on_class_select(self, class_index): def _generate_method(self, class_file): + self.using_namespace_list = self._find_namespaces(self.window.extract_variables()["file"]) + with open(class_file, 'r') as file: source_code = file.read() - try: generator = Generator(source_code) @@ -221,6 +222,11 @@ def insert_method(self, method): self.PLACE_CURSOR_BETWEEN_BRACKETS ) + if method._class.namespace is None or any(method._class.namespace in s for s in self.using_namespace_list): + method.add_option("skip_namespace", True) + else: + method.add_option("skip_namespace", False) + self.window.run_command('insert_method', { 'method': str(method), 'cursor_between_brackets': self.PLACE_CURSOR_BETWEEN_BRACKETS @@ -252,6 +258,14 @@ def _find_class(self, directory, class_name): return class_files + def _find_namespaces(self, file_path): + with open(file_path, 'r') as file: + source_code = file.read() + namespaces = [] + for (ns) in re.findall(r"(using namespace)\s+([\w_:]+)", source_code): + namespaces.append(ns) + return namespaces + class InsertMethodCommand(sublime_plugin.TextCommand): """ @@ -267,4 +281,4 @@ def run(self, edit, **kwargs): if kwargs["cursor_between_brackets"]: pos = self.view.sel()[0].begin() - 2 self.view.sel().clear() - self.view.sel().add(sublime.Region(pos)) \ No newline at end of file + self.view.sel().add(sublime.Region(pos)) diff --git a/method_generator/klass/method.py b/method_generator/klass/method.py index 964db55..d63e37b 100644 --- a/method_generator/klass/method.py +++ b/method_generator/klass/method.py @@ -126,7 +126,7 @@ def __str__(self): method += " " # insert 2 colons if class has namespace - if self._class.namespace is not None: + if not self._options["skip_namespace"]: method += self._class.namespace + "::" method += self._class.name @@ -169,4 +169,4 @@ def __str__(self): method += "{}" # strip to only make sure, there are no spaces at begin and end of definition - return method.strip() \ No newline at end of file + return method.strip() diff --git a/method_generator/tokenizer.py b/method_generator/tokenizer.py index 7963297..a63284d 100644 --- a/method_generator/tokenizer.py +++ b/method_generator/tokenizer.py @@ -74,7 +74,7 @@ def tokenize(cls, input_str): :return: """ - match = re.search(r"(namespace)\s+([\w:]+)", input_str) + match = re.search(r"(namespace)\s+([\w_:]+)", input_str) if match and match.group(2) is None and match.group(1) is None: raise ClassValidationException("Could not parse class namespace")