Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

formatWithCursor and --cursor-offset seem not work #660

Open
nonsense-j opened this issue Apr 5, 2024 · 0 comments
Open

formatWithCursor and --cursor-offset seem not work #660

nonsense-j opened this issue Apr 5, 2024 · 0 comments

Comments

@nonsense-j
Copy link

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

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:

{
  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:

{
  formatted: 'public class HelloWorldExample {\n' +
    '\n' +
    '  public static void main(String args[]) {\n' +
    '    System.out.println("Hello World !");\n' +
    '  }\n' +
    '}\n',
  cursorOffset: 2,
  comments: []
}
@nonsense-j nonsense-j changed the title formatWithCursor and --cursor-offset seems not work formatWithCursor and --cursor-offset seem not work Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant