Skip to content

skip used namespaces #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions cppclasshelper.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand All @@ -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))
self.view.sel().add(sublime.Region(pos))
4 changes: 2 additions & 2 deletions method_generator/klass/method.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
return method.strip()
2 changes: 1 addition & 1 deletion method_generator/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down