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

Format code #105

Open
HanzoDev1375 opened this issue Jan 17, 2025 · 10 comments
Open

Format code #105

HanzoDev1375 opened this issue Jan 17, 2025 · 10 comments
Assignees
Labels

Comments

@HanzoDev1375
Copy link

hello how to format code by in library??

@phax phax self-assigned this Jan 17, 2025
@phax phax added the question label Jan 17, 2025
@phax
Copy link
Owner

phax commented Jan 17, 2025

When you are using the CSSWriter to emit CSS code, you can provide CSSWriterSettings.
Via CSSWriterSettings.setOptimizedOutput (false) it will create formatted CSS code.

hth

@HanzoDev1375
Copy link
Author

@phax can you send demo java code?

@HanzoDev1375
Copy link
Author

HanzoDev1375 commented Jan 17, 2025

See e.g. https://github.com/phax/ph-css/blob/master/ph-css/src/test/java/com/helger/css/writer/CSSWriterTest.java#L148

Great, there is only one problem, when I try to sort the properties, it does not respond, sample code

h1{

color:red; 
} 

What I have in mind

h1{

color:  red; 
} 

@phax
Copy link
Owner

phax commented Jan 17, 2025

There is currently no way to customize the output with additional blanks or so - sorry

@HanzoDev1375
Copy link
Author

@phax I have a question that has been bothering me. What should I do to parse CSS3 code, for example to get lines and columns?

@phax
Copy link
Owner

phax commented Jan 26, 2025

To read with line and column, you need to provide a CSSReaderSettings object to your CSSReader.
Inside the CSSReaderSettings object you have to call .setUseSourceLocation(true)

@HanzoDev1375
Copy link
Author

HanzoDev1375 commented Jan 26, 2025 via email

phax added a commit that referenced this issue Jan 27, 2025
@phax
Copy link
Owner

phax commented Jan 27, 2025

@HanzoDev1375 I created a small sample for you

@HanzoDev1375
Copy link
Author

@phax good code

my code

void cssrun() {
    String cssCode = binding.editor.getText().toString();
    CascadingStyleSheet sheet =
        CSSReader.readFromStringReader(cssCode, new CSSReaderSettings().setUseSourceLocation(true));
    CSSStyleRule rule = (CSSStyleRule) sheet.getAllStyleRules();
    List<CSSStyleRule> rl = new ArrayList<>();
    rl.add(rule);
    rl.forEach(
        i -> {
          var is = i.getSourceLocation().getLastTokenBeginLineNumber();
          var is2 = i.getSourceLocation().getLastTokenEndLineNumber();
          var builder = new StringBuilder();
          builder.append("Line: ").append(is).append('\n');
          builder.append("Col: ").append(is2).append("\n");
          Toast.makeText(getApplicationContext(),builder.toString(),2).show();
        });
  }

not work me I am getting a runtime error, which is a class casting error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants