1use std::path::Path;
2
3use crate::comment_parsers;
4use comment_parsers::{Go, JavaDoc, JsDoc, Lua, Solidity, Unit};
5use harper_core::Token;
6use harper_core::parsers::{self, MarkdownOptions, Parser};
7use harper_core::spell::MutableDictionary;
8use tree_sitter::Node;
9
10use crate::masker::CommentMasker;
11
12pub struct CommentParser {
13 inner: parsers::Mask<CommentMasker, Box<dyn Parser>>,
14}
15
16impl CommentParser {
17 pub fn create_ident_dict(&self, source: &[char]) -> Option<MutableDictionary> {
18 self.inner.masker.create_ident_dict(source)
19 }
20
21 pub fn new_from_language_id(
22 language_id: &str,
23 markdown_options: MarkdownOptions,
24 ) -> Option<Self> {
25 let language = match language_id {
26 "c" => tree_sitter_c::LANGUAGE,
27 "clojure" => tree_sitter_clojure::LANGUAGE,
28 "cmake" => tree_sitter_cmake::LANGUAGE,
29 "cpp" => tree_sitter_cpp::LANGUAGE,
30 "csharp" => tree_sitter_c_sharp::LANGUAGE,
31 "dart" => harper_tree_sitter_dart::LANGUAGE,
32 "gleam" => tree_sitter_gleam::LANGUAGE,
33 "elixir" => tree_sitter_elixir::LANGUAGE,
34 "go" => tree_sitter_go::LANGUAGE,
35 "groovy" => tree_sitter_groovy::LANGUAGE,
36 "haskell" => tree_sitter_haskell::LANGUAGE,
37 "daml" => tree_sitter_haskell::LANGUAGE,
38 "java" => tree_sitter_java::LANGUAGE,
39 "javascript" => tree_sitter_javascript::LANGUAGE,
40 "javascriptreact" => tree_sitter_typescript::LANGUAGE_TSX,
41 "kotlin" => tree_sitter_kotlin_ng::LANGUAGE,
42 "lua" => tree_sitter_lua::LANGUAGE,
43 "nix" => tree_sitter_nix::LANGUAGE,
44 "php" => tree_sitter_php::LANGUAGE_PHP,
45 "powershell" => tree_sitter_powershell::LANGUAGE,
46 "ruby" => tree_sitter_ruby::LANGUAGE,
47 "rust" => tree_sitter_rust::LANGUAGE,
48 "scala" => tree_sitter_scala::LANGUAGE,
49 "shellscript" => tree_sitter_bash::LANGUAGE,
50 "solidity" => tree_sitter_solidity::LANGUAGE,
51 "swift" => tree_sitter_swift::LANGUAGE,
52 "toml" => tree_sitter_toml_ng::LANGUAGE,
53 "typescript" => tree_sitter_typescript::LANGUAGE_TYPESCRIPT,
54 "typescriptreact" => tree_sitter_typescript::LANGUAGE_TSX,
55 "zig" => tree_sitter_zig::LANGUAGE,
56 _ => return None,
57 };
58
59 let comment_parser: Box<dyn Parser> = match language_id {
60 "go" => Box::new(Go::new_markdown(markdown_options)),
61 "java" => Box::new(JavaDoc::default()),
62 "javascript" | "javascriptreact" | "typescript" | "typescriptreact" => {
63 Box::new(JsDoc::new_markdown(markdown_options))
64 }
65 "lua" => Box::new(Lua::new_markdown(markdown_options)),
66 "solidity" => Box::new(Solidity::new_markdown(markdown_options)),
67 _ => Box::new(Unit::new_markdown(markdown_options)),
68 };
69
70 Some(Self {
71 inner: parsers::Mask::new(
72 CommentMasker::new(language.into(), Self::node_condition),
73 comment_parser,
74 ),
75 })
76 }
77
78 pub fn new_from_filename(filename: &Path, markdown_options: MarkdownOptions) -> Option<Self> {
80 Self::new_from_language_id(Self::filename_to_filetype(filename)?, markdown_options)
81 }
82
83 fn filename_to_filetype(path: &Path) -> Option<&'static str> {
89 Some(match path.extension()?.to_str()? {
90 "c" => "c",
91 "bb" | "cljc" | "cljd" | "clj" | "cljs" => "clojure",
92 "cmake" => "cmake",
93 "cpp" | "h" => "cpp",
94 "cs" => "csharp",
95 "dart" => "dart",
96 "gleam" => "gleam",
97 "ex" | "exs" => "elixir",
98 "go" => "go",
99 "groovy" | "gradle" => "groovy",
100 "hs" => "haskell",
101 "daml" => "daml",
102 "java" => "java",
103 "js" => "javascript",
104 "jsx" => "javascriptreact",
105 "kt" | "kts" => "kotlin",
106 "lua" => "lua",
107 "nix" => "nix",
108 "php" => "php",
109 "ps1" | "psd1" | "psm1" => "powershell",
110 "rb" => "ruby",
111 "rs" => "rust",
112 "sbt" | "sc" | "scala" | "mill" => "scala",
113 "bash" | "sh" => "shellscript",
114 "sol" => "solidity",
115 "swift" => "swift",
116 "toml" => "toml",
117 "ts" => "typescript",
118 "tsx" => "typescriptreact",
119 "zig" => "zig",
120 _ => return None,
121 })
122 }
123
124 fn node_condition(n: &Node) -> bool {
125 n.kind().contains("comment")
126 }
127}
128
129impl Parser for CommentParser {
130 fn parse(&self, source: &[char]) -> Vec<Token> {
131 self.inner.parse(source)
132 }
133}
134
135#[cfg(test)]
136mod tests {
137 use super::CommentParser;
138 use harper_core::parsers::{MarkdownOptions, StrParser};
139
140 #[test]
141 fn hang() {
142 use std::sync::mpsc::channel;
143 use std::thread;
144 use std::time::Duration;
145
146 let (tx, rx) = channel::<()>();
147
148 let handle = thread::spawn(move || {
149 let opts = MarkdownOptions::default();
150 let parser = CommentParser::new_from_language_id("java", opts).unwrap();
151 let _res = parser.parse_str("//{@j");
152 tx.send(()).expect("send failed");
153 });
154
155 rx.recv_timeout(Duration::from_secs(10)).expect("timed out");
156 handle.join().expect("failed to join");
157 }
158}