1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use std::path::PathBuf;

/// A representation of a single high-level grammar token of vim syntax,
/// such as a comment or function.
#[derive(Debug, PartialEq)]
pub enum VimNode {
    StandaloneDocComment {
        doc: String,
    },
    Function {
        name: String,
        args: Vec<String>,
        modifiers: Vec<String>,
        doc: Option<String>,
    },
    Command {
        name: String,
        modifiers: Vec<String>,
        doc: Option<String>,
    },
    Variable {
        name: String,
        init_value_token: String,
        doc: Option<String>,
    },
    /// A defined "Flag" like the mechanism used in google/vim-maktaba.
    Flag {
        name: String,
        default_value_token: Option<String>,
        doc: Option<String>,
    },
}

impl VimNode {
    pub fn get_doc(&self) -> Option<&str> {
        match self {
            VimNode::StandaloneDocComment { doc } => Some(doc.as_str()),
            VimNode::Function { doc, .. }
            | VimNode::Command { doc, .. }
            | VimNode::Variable { doc, .. }
            | VimNode::Flag { doc, .. } => doc.as_deref(),
        }
    }
}

/// An individual module (a.k.a. file) of vimscript code.
#[derive(Debug, PartialEq)]
pub struct VimModule {
    pub path: Option<PathBuf>,
    pub doc: Option<String>,
    pub nodes: Vec<VimNode>,
}

/// An entire vim plugin with all the metadata parsed from its files.
#[derive(Debug, PartialEq)]
pub struct VimPlugin {
    pub content: Vec<VimModule>,
}