Support Rust dependencies

This commit is contained in:
Alan Hamlett 2018-03-13 00:48:40 -07:00
parent 165b6c0bb9
commit 50bbdb2030
3 changed files with 88 additions and 0 deletions

View file

@ -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());
}

View file

@ -435,3 +435,16 @@ class DependenciesTestCase(TestCase):
expected_lines=14, expected_lines=14,
entity='scala.scala', 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',
)

View file

@ -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