Skip to content

Commit 16f1d11

Browse files
committed
feat: support prettier-ignore-start/-end in addition to @formatter:off/:on
1 parent 92a2223 commit 16f1d11

File tree

5 files changed

+27
-27
lines changed

5 files changed

+27
-27
lines changed

packages/prettier-plugin-java/src/comments.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,44 @@ import {
1010
type JavaParserOptions
1111
} from "./printers/helpers.js";
1212

13-
const formatterOffOnRangesByCst = new WeakMap<
13+
const prettierIgnoreRangesByCst = new WeakMap<
1414
JavaNode,
15-
FormatterOffOnRange[]
15+
PrettierIgnoreRange[]
1616
>();
1717

18-
export function determineFormatterOffOnRanges(cst: JavaNonTerminal) {
18+
export function determinePrettierIgnoreRanges(cst: JavaNonTerminal) {
1919
const { comments } = cst;
2020
if (!comments) {
2121
return;
2222
}
2323
const ranges = comments
2424
.filter(({ image }) =>
25-
/^(\/\/\s*@formatter:(off|on)\s*|\/\*\s*@formatter:(off|on)\s*\*\/)$/.test(
25+
/^\/(?:\/\s*(?:prettier-ignore-(?:start|end)|@formatter:(?:off|on))\s*|\*\s*(?:prettier-ignore-(?:start|end)|@formatter:(?:off|on))\s*\*\/)$/.test(
2626
image
2727
)
2828
)
2929
.reduce((ranges, { image, startOffset }) => {
3030
const previous = ranges.at(-1);
31-
if (image.endsWith("off")) {
32-
if (previous?.on !== Infinity) {
33-
ranges.push({ off: startOffset, on: Infinity });
31+
if (image.includes("start") || image.includes("off")) {
32+
if (previous?.end !== Infinity) {
33+
ranges.push({ start: startOffset, end: Infinity });
3434
}
35-
} else if (previous?.on === Infinity) {
36-
previous.on = startOffset;
35+
} else if (previous?.end === Infinity) {
36+
previous.end = startOffset;
3737
}
3838
return ranges;
39-
}, new Array<FormatterOffOnRange>());
40-
formatterOffOnRangesByCst.set(cst, ranges);
39+
}, new Array<PrettierIgnoreRange>());
40+
prettierIgnoreRangesByCst.set(cst, ranges);
4141
}
4242

43-
export function isFullyBetweenFormatterOffOn(path: AstPath<JavaNode>) {
43+
export function isFullyBetweenPrettierIgnore(path: AstPath<JavaNode>) {
4444
const { node, root } = path;
4545
const start = parser.locStart(node);
4646
const end = parser.locEnd(node);
4747
return (
48-
formatterOffOnRangesByCst
48+
prettierIgnoreRangesByCst
4949
.get(root)
50-
?.some(range => range.off < start && end < range.on) === true
50+
?.some(range => range.start < start && end < range.end) === true
5151
);
5252
}
5353

@@ -300,7 +300,7 @@ export type JavaComment = IToken & {
300300
followingNode?: JavaNode;
301301
};
302302

303-
type FormatterOffOnRange = {
304-
off: number;
305-
on: number;
303+
type PrettierIgnoreRange = {
304+
start: number;
305+
end: number;
306306
};

packages/prettier-plugin-java/src/parser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { parse } from "java-parser";
22
import type { Parser } from "prettier";
3-
import { determineFormatterOffOnRanges } from "./comments.js";
3+
import { determinePrettierIgnoreRanges } from "./comments.js";
44
import {
55
isTerminal,
66
type JavaNode,
@@ -14,7 +14,7 @@ export default {
1414
cst.comments?.forEach(comment => {
1515
comment.value = comment.image;
1616
});
17-
determineFormatterOffOnRanges(cst);
17+
determinePrettierIgnoreRanges(cst);
1818
return cst;
1919
},
2020
astFormat: "java",

packages/prettier-plugin-java/src/printer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
canAttachComment,
44
handleLineComment,
55
handleRemainingComment,
6-
isFullyBetweenFormatterOffOn
6+
isFullyBetweenPrettierIgnore
77
} from "./comments.js";
88
import {
99
isNonTerminal,
@@ -26,7 +26,7 @@ export default {
2626
node.comments?.some(({ image }) =>
2727
/^(\/\/\s*prettier-ignore|\/\*\s*prettier-ignore\s*\*\/)$/.test(image)
2828
) === true ||
29-
(canAttachComment(node) && isFullyBetweenFormatterOffOn(path))
29+
(canAttachComment(node) && isFullyBetweenPrettierIgnore(path))
3030
);
3131
},
3232
canAttachComment,

packages/prettier-plugin-java/test/unit-test/formatter-on-off/multiple/_input.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
// @formatter:off
1+
// prettier-ignore-start
22
public class PrettierIgnoreClass {
33
public void myMethod(int param1, int param2, int param3, int param4, int param5, int param6, int param7, int param8, int param9, int param10) {
44

55
}
66
}
7-
// @formatter:on
7+
/* prettier-ignore-end */
88
public class PrettierIgnoreClass {
99
public void myMethod(int param1, int param2, int param3, int param4, int param5, int param6, int param7, int param8, int param9, int param10) {
1010

1111
}
1212
}
13-
// @formatter:off
13+
/* @formatter:off */
1414
public class PrettierIgnoreClass {
1515
public void myMethod(int param1, int param2, int param3, int param4, int param5, int param6, int param7, int param8, int param9, int param10) {
1616

packages/prettier-plugin-java/test/unit-test/formatter-on-off/multiple/_output.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// @formatter:off
1+
// prettier-ignore-start
22
public class PrettierIgnoreClass {
33
public void myMethod(int param1, int param2, int param3, int param4, int param5, int param6, int param7, int param8, int param9, int param10) {
44

55
}
66
}
77

8-
// @formatter:on
8+
/* prettier-ignore-end */
99
public class PrettierIgnoreClass {
1010

1111
public void myMethod(
@@ -22,7 +22,7 @@ public void myMethod(
2222
) {}
2323
}
2424

25-
// @formatter:off
25+
/* @formatter:off */
2626
public class PrettierIgnoreClass {
2727
public void myMethod(int param1, int param2, int param3, int param4, int param5, int param6, int param7, int param8, int param9, int param10) {
2828

0 commit comments

Comments
 (0)