|
| 1 | +use emmylua_parser::{LuaDocDescription, LuaTokenKind}; |
| 2 | +use rowan::TextRange; |
| 3 | + |
| 4 | +mod md; |
| 5 | +mod ref_target; |
| 6 | +mod rst; |
| 7 | +mod util; |
| 8 | + |
| 9 | +pub use ref_target::*; |
| 10 | +use util::sort_result; |
| 11 | + |
| 12 | +#[cfg(test)] |
| 13 | +mod testlib; |
| 14 | + |
| 15 | +#[derive(Debug, Clone, Eq, PartialEq)] |
| 16 | +pub enum DescItemKind { |
| 17 | + /// Generic block of documentation. |
| 18 | + Scope, |
| 19 | + |
| 20 | + /// Cross-reference to a Lua object. |
| 21 | + Ref, |
| 22 | + |
| 23 | + /// Emphasis. |
| 24 | + Em, |
| 25 | + |
| 26 | + /// Strong emphasis. |
| 27 | + Strong, |
| 28 | + |
| 29 | + /// Code markup. |
| 30 | + Code, |
| 31 | + |
| 32 | + /// Hyperlink. |
| 33 | + Link, |
| 34 | + |
| 35 | + /// Inline markup, like stars around emphasized text. |
| 36 | + Markup, |
| 37 | + |
| 38 | + /// Directive name, code-block syntax name, role name, |
| 39 | + /// or some other form of argument. |
| 40 | + Arg, |
| 41 | + |
| 42 | + /// Line of code in a code block. |
| 43 | + CodeBlock, |
| 44 | + |
| 45 | + /// Line of code in a code block highlighted by Lua lexer. |
| 46 | + CodeBlockHl(LuaTokenKind), |
| 47 | +} |
| 48 | + |
| 49 | +#[derive(Debug, Clone)] |
| 50 | +pub struct DescItem { |
| 51 | + pub range: TextRange, |
| 52 | + pub kind: DescItemKind, |
| 53 | +} |
| 54 | + |
| 55 | +#[derive(Debug, Clone, Eq, PartialEq, Hash)] |
| 56 | +pub enum DescParserType { |
| 57 | + None, |
| 58 | + Md, |
| 59 | + MySt { |
| 60 | + primary_domain: Option<String>, |
| 61 | + }, |
| 62 | + Rst { |
| 63 | + primary_domain: Option<String>, |
| 64 | + default_role: Option<String>, |
| 65 | + }, |
| 66 | +} |
| 67 | + |
| 68 | +impl Default for DescParserType { |
| 69 | + fn default() -> Self { |
| 70 | + DescParserType::None |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/// Parses markup in comments. |
| 75 | +pub trait LuaDescParser { |
| 76 | + /// Process a description node and yield found documentation ranges. |
| 77 | + fn parse(&mut self, text: &str, desc: LuaDocDescription) -> Vec<DescItem>; |
| 78 | +} |
| 79 | + |
| 80 | +pub fn parse( |
| 81 | + kind: DescParserType, |
| 82 | + text: &str, |
| 83 | + desc: LuaDocDescription, |
| 84 | + cursor_position: Option<usize>, |
| 85 | +) -> Vec<DescItem> { |
| 86 | + let mut items = match kind { |
| 87 | + DescParserType::None => Vec::new(), |
| 88 | + DescParserType::Md => md::MdParser::new(cursor_position).parse(text, desc), |
| 89 | + DescParserType::MySt { primary_domain } => { |
| 90 | + md::MdParser::new_myst(primary_domain, cursor_position).parse(text, desc) |
| 91 | + } |
| 92 | + DescParserType::Rst { |
| 93 | + primary_domain, |
| 94 | + default_role, |
| 95 | + } => rst::RstParser::new(primary_domain, default_role, cursor_position).parse(text, desc), |
| 96 | + }; |
| 97 | + |
| 98 | + sort_result(&mut items); |
| 99 | + |
| 100 | + items |
| 101 | +} |
0 commit comments