From 50bbdb2030062157686f654875d349cb9c7e3a02 Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Tue, 13 Mar 2018 00:48:40 -0700 Subject: [PATCH] Support Rust dependencies --- tests/samples/codefiles/rust.rs | 21 +++++++++++++ tests/test_dependencies.py | 13 ++++++++ wakatime/dependencies/rust.py | 54 +++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 tests/samples/codefiles/rust.rs create mode 100644 wakatime/dependencies/rust.py diff --git a/tests/samples/codefiles/rust.rs b/tests/samples/codefiles/rust.rs new file mode 100644 index 0000000..78ef7c7 --- /dev/null +++ b/tests/samples/codefiles/rust.rs @@ -0,0 +1,21 @@ +extern crate proc_macro; +extern crate phrases as sayings; +extern crate syn; +#[macro_use] +extern crate quote; + +use sayings::japanese::greetings as ja_greetings; +use sayings::japanese::farewells::*; +use sayings::english::{self, greetings as en_greetings, farewells as en_farewells}; + +extern "C" { + fn c_callback(n: c_int); +} + +fn main() { + println!("Hello in English; {}", en_greetings::hello()); + println!("And in Japanese: {}", ja_greetings::hello()); + println!("Goodbye in English: {}", english::farewells::goodbye()); + println!("Again: {}", en_farewells::goodbye()); + println!("And in Japanese: {}", goodbye()); +} diff --git a/tests/test_dependencies.py b/tests/test_dependencies.py index e58961f..54686f8 100644 --- a/tests/test_dependencies.py +++ b/tests/test_dependencies.py @@ -435,3 +435,16 @@ class DependenciesTestCase(TestCase): expected_lines=14, entity='scala.scala', ) + + def test_rust_dependencies_detected(self): + self.shared( + expected_dependencies=[ + 'proc_macro', + 'phrases', + 'syn', + 'quote', + ], + expected_language='Rust', + expected_lines=21, + entity='rust.rs', + ) diff --git a/wakatime/dependencies/rust.py b/wakatime/dependencies/rust.py new file mode 100644 index 0000000..77c9505 --- /dev/null +++ b/wakatime/dependencies/rust.py @@ -0,0 +1,54 @@ +# -*- coding: utf-8 -*- +""" + wakatime.dependencies.rust + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + + Parse dependencies from Rust code. + + :copyright: (c) 2018 Alan Hamlett. + :license: BSD, see LICENSE for more details. +""" + +from . import TokenParser + + +class RustParser(TokenParser): + state = None + + def parse(self): + for index, token, content in self.tokens: + self._process_token(token, content) + return self.dependencies + + def _process_token(self, token, content): + if self.partial(token) == 'Keyword': + self._process_keyword(token, content) + elif self.partial(token) == 'Whitespace': + self._process_whitespace(token, content) + elif self.partial(token) == 'Name': + self._process_name(token, content) + else: + self._process_other(token, content) + + def _process_keyword(self, token, content): + if self.state == 'extern' and content == 'crate': + self.state = 'extern crate' + else: + self.state = content + + def _process_whitespace(self, token, content): + pass + + def _process_name(self, token, content): + if self.state == 'extern crate': + self.append(self._format(content)) + self.state = None + + def _process_other(self, token, content): + self.state = None + + def _format(self, content): + content = content.strip() + if content.startswith('"') or content.startswith("'"): + return None + return content