jhipster / jhipster/prettier-java
formatWithCursor and --cursor-offset seem not work
- Vorherrschende Sprache
- Java
- Sterne
- 1.2k
- Forks
- 120
- Ø Merge
- 1 T. 14 Std.
- Gemergte PRs (30 T.)
- 27
Beschreibung
**Prettier-Java 2.6.0**
### Detail
I want to get the index change of given cursor when formatting Java code. I noticed that it can be done with `formatWithCursor`(API) or `--cursor-offset`(CLI). However, whatever valid offset I try, the result will always be 0. Besides, I have also tried with formatting babel code (builtin in Prettier), which behaved correctly. I wonder how I can fix this. Is this a feature unfinished?
I will show you the example with API (CLI also fails extractly the same)
### API Usage with `formatWithCursor`
```js
import * as prettier from "prettier";
import * as prettierPluginJava from "prettier-plugin-java";
const javaText = `
public class HelloWorldExample{public static void main(String args[]){
System.out.println("Hello World !");
}
}
`;
const formattedText = await prettier.formatWithCursor(javaText, {
cursorOffset: 5,
parser: "java",
plugins: [prettierPluginJava.default],
});
```
**Output:**
```java
{
formatted: 'public class HelloWorldExample {\n' +
'\n' +
' public static void main(String args[]) {\n' +
' System.out.println("Hello World !");\n' +
' }\n' +
'}\n',
cursorOffset: 0,
comments: []
}
```
**Expected output:**
```java
{
formatted: 'public class HelloWorldExample {\n' +
'\n' +
' public static void main(String args[]) {\n' +
' System.out.println("Hello World !");\n' +
' }\n' +
'}\n',
cursorOffset: 2,
comments: []
}
Beitragsleitfaden
Rechercherichtung
Reproduce the Java example using the formatWithCursor API and the --cursor-offset CLI option, then compare their results with a parser that handles cursor offsets correctly. Trace the plugin's cursor-offset handling from these entry points. Done means a valid input offset produces the expected adjusted cursorOffset instead of always returning 0.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- java, javascript
- Bereich
- tooling
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100