Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jdt-core-dev] Re: Questions about the new Code Formatter

I must have missed the announcement, so sorry if I'm a bit late. I just
thought it would be good if you know how we use the code formatter so we can
find a good solution.

This is where we use the code formatter:
- In the Java Editor, the user can select a selection of text and format it
- Code Generation, e.g. wizard, fomat the newly generated code: Fit new code
into existing code
- Code manipulation: Insert new code in existing, nest existing in new code
or make modification in a code.

We are using the following principles:
- Never use the code formatters options defined in JavaCore: Only use the
API in ICodeFormatter (bare formatting of a string). This is important for
that we can allow contributed code formatters
- Never touch existing code: Only generated code is formatter. User
formatting should not be touched.

The last point is the trickiest issue: What we do is that we format a code
snippet with placeholder and then replace the placeholders with the existing
code that should not be touched. When inserting, indentation of the existing
code has to be adapted to the new location.
e.g. the 'surround with' refactoring
when surrounding a statement
int x = y ++;
we wont change any of the spacings, they might be wanted.
formatting a snippet try { x; } catch(Exception e) { }
then replace 'x;' with the selected block of statements, correnctly
indented.
try {
int x = y ++;
} catch (Exception e) {
}

All this logic of how to add new code in existing code is in the
ASTRewriter.
I think the new limitations that only CU can be formatted is not a problem.
What we need is that we can specify ranges of code that has to stay the
same. We tried to do this using the positions you can pass to the formatter.
However, it would be better if we have it both: positions that are tracked,
and ranges of code that has to be unchanged.
Martin


Hi,

Ok, Adam, we move the discussion to JDT/Core.

Yes, the new formatter will require a full compilation unit, because it is
working on an AST. You can still format a code snippet using the positions
before and after the formatting. If you want any kind of code snippets
without using positions, then we might want to discuss about it.
The new formatter will implement ICodeFormatter. This is the entry point we
have for the extension point.
My point for JDT/UI is to allow the user to choose what code formatter is
the default one in case there are multiple code formatters available.
Let us know what your problems or concerns can be. Why you didn't talk
about that when we sent messages about the new implementation of the code
formatter? You never asked to keep the possibility to format any kind of
code snippet.
Olivier

_______________________________________________
jdt-core-dev mailing list
jdt-core-dev@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/jdt-core-dev



Back to the top