Skip to content

Commit

Permalink
Merge pull request #83 from chalme/ctags
Browse files Browse the repository at this point in the history
Ctags TypeScript init
  • Loading branch information
phodal authored Mar 4, 2021
2 parents a9b3b9c + c29714a commit 4575270
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
31 changes: 31 additions & 0 deletions _fixtures/ctags/source/animal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// https://github.com/microsoft/TypeScriptSamples/blob/master/simple/animals.ts

class Animal {
name : string
constructor(name : string) {
this.name = name;
}
move(meters) {
console.log(this.name + " moved " + meters + "m.");
}
}

class Snake extends Animal {
move() {
console.log("Slithering...");
super.move(5);
}
}

class Horse extends Animal {
move() {
console.log("Galloping...");
super.move(45);
}
}

var sam = new Snake("Sammy the Python")
var tom: Animal = new Horse("Tommy the Palomino")

sam.move()
tom.move(34)
21 changes: 21 additions & 0 deletions _fixtures/ctags/ts_tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/
!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/
!_TAG_OUTPUT_EXCMD mixed /number, pattern, mixed, or combineV2/
!_TAG_OUTPUT_FILESEP slash /slash or backslash/
!_TAG_OUTPUT_MODE u-ctags /u-ctags or e-ctags/
!_TAG_PATTERN_LENGTH_LIMIT 96 /0 for no limit/
!_TAG_PROC_CWD /Users/chalme/CLionProjects/coco/_fixtures/ctags/source/ //
!_TAG_PROGRAM_AUTHOR Universal Ctags Team //
!_TAG_PROGRAM_NAME Universal Ctags /Derived from Exuberant Ctags/
!_TAG_PROGRAM_URL https://ctags.io/ /official site/
!_TAG_PROGRAM_VERSION 5.9.0 /98ff77d/
Animal animal.ts /^class Animal {$/;" class line:3 language:TypeScript
Horse animal.ts /^class Horse extends Animal {$/;" class line:20 language:TypeScript inherits:Animal
Snake animal.ts /^class Snake extends Animal {$/;" class line:13 language:TypeScript inherits:Animal
constructor animal.ts /^ constructor(name : string) {$/;" method line:5 language:TypeScript class:Animal access:public
move animal.ts /^ move() {$/;" method line:14 language:TypeScript class:Snake access:public
move animal.ts /^ move() {$/;" method line:21 language:TypeScript class:Horse access:public
move animal.ts /^ move(meters) {$/;" method line:8 language:TypeScript class:Animal access:public
name animal.ts /^ name : string$/;" property line:4 language:TypeScript class:Animal access:public
sam animal.ts /^var sam = new Snake("Sammy the Python")$/;" variable line:27 language:TypeScript
tom animal.ts /^var tom: Animal = new Horse("Tommy the Palomino")$/;" variable line:28 language:TypeScript
29 changes: 28 additions & 1 deletion plugins/coco_struct/src/ctags/ctags_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ lazy_static! {
([A-Za-z0-9_.]+)
(,(\s|\t)*([A-Za-z0-9_.]+))*(\s|\t)* # for `name, access, returntype string`
(?P<datatype>[A-Za-z0-9_.<>\[\]]+)").unwrap();
static ref TYPE_SCRIPT_TYPE: Regex = Regex::new(
r"/\^([ |\t]*).*:([ |\t]*)(?P<datatype>[A-Za-z0-9_.<>\[\]]+).*\$/"
).unwrap();

static ref TYPE_KEYWORDS: [&'static str; 18] = [
"private",
Expand Down Expand Up @@ -208,10 +211,15 @@ impl CtagsParser {
data_type = (&capts["datatype"]).to_string();
}
}
"TypeScript" => {
if let Some(capts) = TYPE_SCRIPT_TYPE.captures(line) {
data_type = (&capts["datatype"]).to_string();
}
}
_ => {}
}

if tag_type.eq("member") || tag_type.eq("field") {
if tag_type.eq("member") || tag_type.eq("field") || tag_type.eq("property") {
let mut member = MemberInfo::new(name, access, data_type);
if !pure_data_type.is_empty() {
member.pure_data_type = pure_data_type;
Expand Down Expand Up @@ -418,4 +426,23 @@ name src/coco_struct.rs /^ pub name: String,$/;\" field line:22 language:Rust
assert_eq!("migrate", string_field.methods[1].name);
assert_eq!("save", string_field.methods[2].name);
}

#[test]
pub fn should_parse_ts_file() {
let dir = tags_dir().join("ts_tags");
let parser = CtagsParser::parse(dir);
let classes = parser.classes();

assert_eq!(3, classes.len());
assert_eq!("TypeScript", classes[0].lang);

assert_eq!(1, classes[0].members.len());
assert_eq!("name", classes[0].members[0].name);
assert_eq!("string", classes[0].members[0].data_type);

let methods = classes[0].methods.clone();
assert_eq!(2, methods.len());
assert_eq!("constructor", methods[0].name);
assert_eq!("move", methods[1].name)
}
}

0 comments on commit 4575270

Please sign in to comment.