Skip to content

Commit cc1f3de

Browse files
committed
feat: support java doc
1 parent 55ef546 commit cc1f3de

23 files changed

+218
-43
lines changed

src/context.rs

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,78 @@ impl<'a> DocBuild<'a> for Comment {
143143
result.push(b.nl());
144144
}
145145
CommentType::Block => {
146-
let lines: &Vec<&str> = &self.value.split('\n').collect();
147-
for (i, line) in lines.iter().enumerate() {
148-
result.push(b.txt(line.trim()));
146+
let mut lines: Vec<&str> = self.value.split('\n').collect();
147+
148+
// JavaDoc formatting
149+
if self.value.starts_with("/**") {
150+
// Handle the first line that might contain both /** and content
151+
if !lines.is_empty() {
152+
let first_line = lines[0].trim();
153+
if first_line.len() > 3 {
154+
// Has content after /**
155+
// Extract the content after /**
156+
let content = first_line[3..].trim();
157+
if !content.is_empty() {
158+
// Replace first line with just /**
159+
lines[0] = "/**";
160+
// Insert the content as a new second line
161+
lines.insert(1, content);
162+
}
163+
}
164+
}
165+
166+
for (i, line) in lines.iter().enumerate() {
167+
let trimmed = line.trim();
168+
169+
if i == 0 {
170+
// First line (/**) remains unchanged
171+
result.push(b.txt(trimmed));
172+
} else if i == lines.len() - 1 {
173+
if let Some(before_end) = trimmed.strip_suffix("*/") {
174+
let content = before_end.trim();
175+
if content.is_empty() {
176+
result.push(b.txt(" */"));
177+
} else {
178+
result.push(b.txt(format!(" * {}", content))); // First line: Preserve content
179+
result.push(b.nl()); // Newline before closing */
180+
result.push(b.txt(" */")); // Second line: Properly close the comment
181+
}
182+
} else {
183+
result.push(b.txt(" */")); // Fallback (shouldn't happen in valid JavaDoc)
184+
}
185+
} else if trimmed.is_empty() {
186+
// Handle empty lines
187+
result.push(b.txt(" *"));
188+
} else {
189+
// Handle content lines
190+
if let Some(after_star) = trimmed.strip_prefix('*') {
191+
// Line has a star - normalize to exactly one space
192+
let content = after_star.trim_start(); // Remove all leading spaces
193+
if content.is_empty() {
194+
// Just a star with no content
195+
result.push(b.txt(" *"));
196+
} else {
197+
// Star with content - add exactly one space
198+
result.push(b.txt(format!(" * {}", content)));
199+
}
200+
} else {
201+
// Line doesn't have a star, add standard formatting
202+
result.push(b.txt(format!(" * {}", trimmed)));
203+
}
204+
}
205+
206+
if i < lines.len() - 1 {
207+
result.push(b.nl());
208+
}
209+
}
210+
} else {
211+
// Regular block comment (non-JavaDoc)
212+
for (i, line) in lines.iter().enumerate() {
213+
result.push(b.txt(line.trim()));
149214

150-
if i < lines.len() - 1 {
151-
result.push(b.nl());
215+
if i < lines.len() - 1 {
216+
result.push(b.nl());
217+
}
152218
}
153219
}
154220
}

tests/battle_test/hello.cls

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,42 @@
1-
// format twice will change the format
1+
/*
2+
* This is a
3+
* multiline block comment
4+
* that is not JavaDoc.
5+
*/
6+
7+
/** This is a JavaDoc
8+
with multiple lines
9+
and some indentation. */
10+
11+
/**
12+
This is a JavaDoc
13+
* with inconsistent
14+
* indentation.
15+
*/
16+
17+
/**
18+
*** This is a JavaDoc
19+
*** with excessive asterisks.
20+
*/
21+
22+
/**
23+
* This is a JavaDoc.
24+
*
25+
* It has an empty line.
26+
*/
27+
28+
/** This method does something.
29+
*
30+
* @param x The first parameter.
31+
* @param y The second parameter.
32+
* @return The result.
33+
*/
34+
35+
/**
36+
* This has extra spaces.
37+
*
38+
* @author Someone
39+
*/
40+
241
class A {
3-
{
4-
LogEntryEventBuilder logEntryEventBuilder =
5-
new LogEntryEventBuilder(
6-
getUserSettings(),
7-
loggingLevel,
8-
shouldSave
9-
).// Don't include the logging system's classes in the stack trace
10-
parseStackTrace(
11-
new System.DmlException()
12-
.getStackTraceString()
13-
.replaceAll(
14-
'(' + String.join(CLASSES_TO_IGNORE, '|') + ')\\..+?column 1',
15-
''
16-
)
17-
.trim()
18-
);
19-
}
2042
}

tests/comments/CommentsBeforeInterfaceDeclaration.cls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// First Test Comment
22

3-
/**
3+
/*
44
* First ApexDoc
55
*/
66
interface CommentsBeforeInterfaceDeclaration {

tests/comments/CommentsBeforeInterfaceDeclaration.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// First Test Comment
22

3-
/**
3+
/*
44
* First ApexDoc
55
*/
66
interface CommentsBeforeInterfaceDeclaration {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// First Test Comment
22

33
/**
4-
* First ApexDoc
5-
*/
4+
* First ApexDoc
5+
*/
66
trigger Case_trg on Case(before update) {
77
System.debug('SOMETHING');
88
}

tests/comments/dangling_comments1.cls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class A {
22
{
3-
/**
3+
/*
44
block comment
55
*/
66

tests/comments/dangling_comments1.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Class A{
22
{
33

4-
/**
4+
/*
55
block comment
66
*/
77

tests/comments/post_comments1.cls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ class A {
55

66
// line comment
77

8-
/**
8+
/*
99
block comment
1010
*/

tests/comments/post_comments1.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Class A{
55

66
// line comment
77

8-
/**
8+
/*
99
block comment
1010
*/
1111

tests/comments/post_comments2.cls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class A {
44

55
// line comment
66

7-
/**
7+
/*
88
block comment
99
*/
1010
}

0 commit comments

Comments
 (0)