Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

Add LuaDoc Support (#1820)

Lukas Werner3526dcf35c98
diff --git a/harper-comments/src/comment_parser.rs b/harper-comments/src/comment_parser.rs
index 3550afc27..d716114e1 100644
--- a/harper-comments/src/comment_parser.rs
+++ b/harper-comments/src/comment_parser.rs
@@ -1,7 +1,7 @@
use std::path::Path;
use crate::comment_parsers;
-use comment_parsers::{Go, JavaDoc, JsDoc, Solidity, Unit};
+use comment_parsers::{Go, JavaDoc, JsDoc, Lua, Solidity, Unit};
use harper_core::Token;
use harper_core::parsers::{self, MarkdownOptions, Parser};
use harper_core::spell::MutableDictionary;
@@ -53,6 +53,7 @@ impl CommentParser {
let comment_parser: Box<dyn Parser> = match language_id {
"go" => Box::new(Go::new_markdown(markdown_options)),
+ "lua" => Box::new(Lua::new_markdown(markdown_options)),
"java" => Box::new(JavaDoc::default()),
"javascriptreact" | "typescript" | "typescriptreact" | "javascript" => {
Box::new(JsDoc::new_markdown(markdown_options))
diff --git a/harper-comments/src/comment_parsers/lua.rs b/harper-comments/src/comment_parsers/lua.rs
new file mode 100644
index 000000000..5426ba428
--- /dev/null
+++ b/harper-comments/src/comment_parsers/lua.rs
@@ -0,0 +1,83 @@
+use harper_core::Lrc;
+use harper_core::Span;
+use harper_core::Token;
+use harper_core::parsers::{Markdown, MarkdownOptions, Parser};
+
+use super::without_initiators;
+
+#[derive(Clone)]
+pub struct Lua {
+ inner: Lrc<dyn Parser>,
+}
+
+impl Lua {
+ pub fn new(parser: Lrc<dyn Parser>) -> Self {
+ Self { inner: parser }
+ }
+
+ pub fn new_markdown(markdown_options: MarkdownOptions) -> Self {
+ Self::new(Lrc::new(Markdown::new(markdown_options)))
+ }
+}
+
+impl Parser for Lua {
+ fn parse(&self, source: &[char]) -> Vec<Token> {
+ let mut tokens = Vec::new();
+
+ let mut chars_traversed = 0;
+
+ for line in source.split(|c| *c == '\n') {
+ if starts_with_prefix(line) {
+ tokens.push(Token::new(
+ Span::new_with_len(chars_traversed, 0),
+ harper_core::TokenKind::Newline(2),
+ ));
+ chars_traversed += line.len() + 1;
+ continue;
+ }
+
+ let mut new_tokens = parse_line(line, self.inner.clone());
+
+ if chars_traversed + line.len() < source.len() {
+ new_tokens.push(Token::new(
+ Span::new_with_len(line.len(), 1),
+ harper_core::TokenKind::Newline(1),
+ ));
+ }
+
+ new_tokens
+ .iter_mut()
+ .for_each(|t| t.span.push_by(chars_traversed));
+
+ chars_traversed += line.len() + 1;
+ tokens.append(&mut new_tokens);
+ }
+
+ tokens
+ }
+}
+
+fn starts_with_prefix(source: &[char]) -> bool {
+ let actual = without_initiators(source);
+ let actual_chars = actual.get_content(source);
+
+ matches!(actual_chars, ['@', ..])
+}
+
+fn parse_line(source: &[char], parser: Lrc<dyn Parser>) -> Vec<Token> {
+ let actual = without_initiators(source);
+
+ if actual.is_empty() {
+ return Vec::new();
+ }
+
+ let source = actual.get_content(source);
+
+ let mut new_tokens = parser.parse(source);
+
+ new_tokens
+ .iter_mut()
+ .for_each(|t| t.span.push_by(actual.start));
+
+ new_tokens
+}
diff --git a/harper-comments/src/comment_parsers/mod.rs b/harper-comments/src/comment_parsers/mod.rs
index f8e53670b..b45723904 100644
--- a/harper-comments/src/comment_parsers/mod.rs
+++ b/harper-comments/src/comment_parsers/mod.rs
@@ -1,6 +1,7 @@
mod go;
mod javadoc;
mod jsdoc;
+mod lua;
mod solidity;
mod unit;
@@ -8,6 +9,7 @@ pub use go::Go;
use harper_core::Span;
pub use javadoc::JavaDoc;
pub use jsdoc::JsDoc;
+pub use lua::Lua;
pub use solidity::Solidity;
pub use unit::Unit;
diff --git a/harper-comments/tests/language_support.rs b/harper-comments/tests/language_support.rs
index e6838b81e..f5388e381 100644
--- a/harper-comments/tests/language_support.rs
+++ b/harper-comments/tests/language_support.rs
@@ -44,6 +44,8 @@ macro_rules! create_test {
create_test!(multiline_comments.cpp, 3);
create_test!(multiline_comments.ts, 3);
create_test!(multiline_comments.sol, 3);
+create_test!(clean.lua, 0);
+create_test!(dirty.lua, 1);
create_test!(clean.rs, 0);
create_test!(clean.sol, 0);
create_test!(jsdoc.ts, 4);
diff --git a/harper-comments/tests/language_support_sources/clean.lua b/harper-comments/tests/language_support_sources/clean.lua
new file mode 100644
index 000000000..7cbb77336
--- /dev/null
+++ b/harper-comments/tests/language_support_sources/clean.lua
@@ -0,0 +1,7 @@
+---@meta
+
+---@alias MyCustomType integer
+
+---Calculate a value using [my custom type](lua://MyCustomType)
+---@param x MyCustomType
+function calculate(x) end
diff --git a/harper-comments/tests/language_support_sources/dirty.lua b/harper-comments/tests/language_support_sources/dirty.lua
new file mode 100644
index 000000000..ceb938272
--- /dev/null
+++ b/harper-comments/tests/language_support_sources/dirty.lua
@@ -0,0 +1,9 @@
+---@meta
+
+---@alias MyCustomType integer
+
+---Calculate a value using [my custom type](lua://MyCustomType)
+---
+--- This calcumalates stuff
+---@param x MyCustomType
+function calculate(x) end