Support Rust dependencies
This commit is contained in:
parent
165b6c0bb9
commit
50bbdb2030
3 changed files with 88 additions and 0 deletions
21
tests/samples/codefiles/rust.rs
Normal file
21
tests/samples/codefiles/rust.rs
Normal 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());
|
||||
}
|
|
@ -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',
|
||||
)
|
||||
|
|
54
wakatime/dependencies/rust.py
Normal file
54
wakatime/dependencies/rust.py
Normal 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
|
Loading…
Reference in a new issue