From 9fd813c489958f98f5e8b215ab8b91b47f86fb5a Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Thu, 1 Oct 2015 12:45:15 -0700 Subject: [PATCH] improve C# and php dependency detection --- .../packages/wakatime/dependencies/dotnet.py | 37 +++++++++++++++++-- plugin/packages/wakatime/dependencies/php.py | 4 +- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/plugin/packages/wakatime/dependencies/dotnet.py b/plugin/packages/wakatime/dependencies/dotnet.py index c6456fb..7d67341 100644 --- a/plugin/packages/wakatime/dependencies/dotnet.py +++ b/plugin/packages/wakatime/dependencies/dotnet.py @@ -10,9 +10,16 @@ """ from . import TokenParser +from ..compat import u class CSharpParser(TokenParser): + exclude = [ + r'^system$', + r'^microsoft$', + ] + state = None + buffer = u('') def parse(self): for index, token, content in self.tokens: @@ -20,14 +27,38 @@ class CSharpParser(TokenParser): return self.dependencies def _process_token(self, token, content): - if self.partial(token) == 'Namespace': + if self.partial(token) == 'Keyword': + self._process_keyword(token, content) + if self.partial(token) == 'Namespace' or self.partial(token) == 'Name': self._process_namespace(token, content) + elif self.partial(token) == 'Punctuation': + self._process_punctuation(token, content) else: self._process_other(token, content) + def _process_keyword(self, token, content): + if content == 'using': + self.state = 'import' + self.buffer = u('') + def _process_namespace(self, token, content): - if content != 'import' and content != 'package' and content != 'namespace': - self.append(content, truncate=True) + if self.state == 'import': + if u(content) != u('import') and u(content) != u('package') and u(content) != u('namespace') and u(content) != u('static'): + if u(content) == u(';'): # pragma: nocover + self._process_punctuation(token, content) + else: + self.buffer += u(content) + + def _process_punctuation(self, token, content): + if self.state == 'import': + if u(content) == u(';'): + self.append(self.buffer, truncate=True) + self.buffer = u('') + self.state = None + elif u(content) == u('='): + self.buffer = u('') + else: + self.buffer += u(content) def _process_other(self, token, content): pass diff --git a/plugin/packages/wakatime/dependencies/php.py b/plugin/packages/wakatime/dependencies/php.py index 728c75d..e4aa0a2 100644 --- a/plugin/packages/wakatime/dependencies/php.py +++ b/plugin/packages/wakatime/dependencies/php.py @@ -61,10 +61,10 @@ class PhpParser(TokenParser): def _process_literal_string(self, token, content): if self.state == 'include': - if content != '"': + if content != '"' and content != "'": content = content.strip() if u(token) == 'Token.Literal.String.Double': - content = u('"{0}"').format(content) + content = u("'{0}'").format(content) self.append(content) self.state = None