Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ It will basically clone some java repositories and try to parse every java file.

### prettier-java-plugin

In this section, We suppose you are in the packages/prettier-plugin-java folder.
In this section, we suppose you are in the packages/prettier-plugin-java folder.

If you would like to check the impact of your changes on a sample code, edit the scripts/single-printer-run/\_input.java file and run

Expand All @@ -73,6 +73,12 @@ node scripts/update-test-ouput.js -repository relative/path/to/the/repository

It will then be output inside test-samples/repository-name.

To check the stability of the reformating, you can run several times Prettier with the `-times` flag (e.g. 5 times):

```bash
node scripts/update-test-output.js -single -times 5
```

If you run:

```bash
Expand Down
25 changes: 18 additions & 7 deletions packages/prettier-plugin-java/scripts/update-test-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ if (process.argv.indexOf("-single") > -1) {
samplesDir = path.resolve(__dirname, "./single-printer-run");
} else if (process.argv.indexOf("-repository") > -1) {
const testSamples = path.resolve(__dirname, "../test-samples");
originalSamplesDir = path.resolve(__dirname, process.argv[3]);
originalSamplesDir = path.resolve(
__dirname,
process.argv[process.argv.indexOf("-repository") + 1]
);
samplesDir = path.resolve(testSamples, path.basename(originalSamplesDir));
if (fs.existsSync(samplesDir)) {
fs.removeSync(samplesDir);
Expand All @@ -21,6 +24,11 @@ if (process.argv.indexOf("-single") > -1) {
console.log(`end copy ${originalSamplesDir} to ${samplesDir}`);
}

let numberOfTime = 1;
if (process.argv.indexOf("-times") > -1) {
Comment thread
clementdessoude marked this conversation as resolved.
numberOfTime = process.argv[process.argv.indexOf("-times") + 1];
}

const sampleFiles = klawSync(samplesDir, { nodir: true });
const javaSampleFiles = sampleFiles.filter(fileDesc => {
if (fileDesc.path.includes("node_modules")) {
Expand All @@ -38,12 +46,15 @@ javaSampleFiles.forEach(fileDesc => {

try {
console.log(`Reading <${fileDesc.path}>`);
const newExpectedText = prettier.format(javaFileText, {
parser: "java",
plugins: [path.resolve(__dirname, "../")],
tabWidth: 2,
endOfLine: "lf"
});
let newExpectedText = javaFileText;
for (let i = 0; i < numberOfTime; i++) {
newExpectedText = prettier.format(newExpectedText, {
parser: "java",
plugins: [path.resolve(__dirname, "../")],
tabWidth: 2,
endOfLine: "lf"
});
}
let outputFilePath = fileDesc.path.replace(/input.java$/, "output.java");
if (process.argv.indexOf("-repository") > -1) {
outputFilePath = fileDesc.path;
Expand Down
47 changes: 37 additions & 10 deletions packages/prettier-plugin-java/src/printers/prettier-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,46 @@ function getTrailingComments(token) {
return concat(arr);
}

function isJavaDoc(comment, lines) {
let isJavaDoc = true;
if (comment.tokenType.name === "TraditionalComment" && lines.length > 1) {
for (let i = 1; i < lines.length; i++) {
if (lines[i].trim().charAt(0) !== "*") {
isJavaDoc = false;
break;
}
}
} else {
isJavaDoc = false;
}

return isJavaDoc;
}

function formatJavaDoc(lines) {
const res = [lines[0].trim()];

for (let i = 1; i < lines.length; i++) {
res.push(prettier.hardline);
res.push(" " + lines[i].trim());
}

return res;
}

function formatComment(comment) {
const res = [];
comment.image.split("\n").forEach(l => {
if (l.match(/(\s+)(\*)(.*)/gm) && !l.match(/(\/)(\*)(.*)(\*)(\/)/gm)) {
res.push(" " + l.trim());
} else {
res.push(l);
}
res.push(hardLineWithoutBreakParent);
});
if (res[res.length - 1] === hardLineWithoutBreakParent) {
res.pop();
const lines = comment.image.split("\n");

if (isJavaDoc(comment, lines)) {
return formatJavaDoc(lines);
}

lines.forEach(line => {
res.push(line);
res.push(prettier.literalline);
});
res.pop();
return res;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Bug Fix: #279 - See also https://prettier.io/docs/en/rationale.html#comments
class T {
/*
* comment
*/
void t() {

}

/*
* comment
*/
void t() {

}

/*
* comment
*/
void t() {

}

/*
* comment
*/
void t() {

}

/*
* line 1
line 2
*/
void t() {

}

/*

*line 2
*/
void t() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Bug Fix: #279 - See also https://prettier.io/docs/en/rationale.html#comments
class T {

/*
* comment
*/
void t() {}

/*
* comment
*/
void t() {}

/*
* comment
*/
void t() {}

/*
* comment
*/
void t() {}

/*
* line 1
line 2
*/
void t() {}

/*

*line 2
*/
void t() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ private ArrayTable(
* elements but rowKeySet() will be empty and containsRow() won't
* acknolwedge them.
*/
rowKeyToIndex = Maps.indexMap(rowList);
rowKeyToIndex =
Maps.indexMap(rowList);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this what we want ?

columnKeyToIndex = Maps.indexMap(columnList);

@SuppressWarnings(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@

/*
* # Copyright 2013-2019 the original author or authors from the JHipster project.
* #
* # This file is part of the JHipster project, see https://www.jhipster.tech/
* # for more information.
* #
* # Licensed under the Apache License, Version 2.0 (the "License");
* # you may not use this file except in compliance with the License.
* # You may obtain a copy of the License at
* #
* # http://www.apache.org/licenses/LICENSE-2.0
* #
* # Unless required by applicable law or agreed to in writing, software
* # distributed under the License is distributed on an "AS IS" BASIS,
* # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* # See the License for the specific language governing permissions and
* # limitations under the License.
* #
*/
* # Copyright 2013-2019 the original author or authors from the JHipster project.
* #
* # This file is part of the JHipster project, see https://www.jhipster.tech/
* # for more information.
* #
* # Licensed under the Apache License, Version 2.0 (the "License");
* # you may not use this file except in compliance with the License.
* # You may obtain a copy of the License at
* #
* # http://www.apache.org/licenses/LICENSE-2.0
* #
* # Unless required by applicable law or agreed to in writing, software
* # distributed under the License is distributed on an "AS IS" BASIS,
* # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* # See the License for the specific language governing permissions and
* # limitations under the License.
* #
*/
/*
package io.github.jhipster.sample.repository;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ describe("prettier-java", () => {
testSample(path.resolve(__dirname, "./package"));
testSample(path.resolve(__dirname, "./comments-blocks-and-statements"));
testSample(path.resolve(__dirname, "./comments-only"));
testSample(path.resolve(__dirname, "./bug-fixes"));
});