Community
Participate
Working Groups
Requesting 2 new API on org.eclipse.jdt.core.dom.Compilation. /** * Returns the column number corresponding to the given source character * position in the original source string. Column number are zero-based. * Return zero if it is beyond the valid range. * * @param position a 0-based character position, possibly * negative or out of range * @return the 0-based coloumn number, or <code>0</code> if the character * position does not correspond to a source line in the original * source file or if column number information is not known for this * compilation unit * @see ASTParser */ public int columnNumber(final int position); /** * Given a line number and column number return the corresponding * position in the original source string. * Returns 0 if no line number information is available for this * compilation unit or the requested line number is less than one. * Return the total size of the source string if <code>line</code> * is greater than the actual number lines in the unit. * Assume 0 for the column number if <code>column</code> is less than 0 * or return the position of the last character of the line if <code>column</code> * is beyond the legal range. * * @param line the one-based line number * @param column the zero-based column number. * @return the 0-based character position in the source string. * Return <code>0</code> if line/column number information is not known * for this compilation unit or the input are not valid. */ public int getPosition(int line, int column); These 2 new APIs are required to correctly implement com.sun.mirror.util.SourcePosition.SourcePostion.column() and allow the posting of markers based on SourcePosition.
Post 3.1. Indeed makes sense, since we do not expose the line separator table. Olivier - can you confirm this ?
Yes, I can confirm that we don't expose the line end table. Therefore it is pretty expensive to compute it.
Would it be better to expose it, or to provide these APIs ?
Created attachment 22243 [details] CompilationUnit.java with implementation of proposed API.
My original thought was that it would be non-trivial to explain to dom clients the content/format of the line separation table and hence the proposed API. In the attachment was my implementation of the proposed API.
The proposed APIs are sound. I assume these changes are destined for the APT branch which will eventually be integrated into the 3.2 stream. Accordingly, please ensure that all API additions are tagged @since 3.2 so that we don't lose track of where they came in. Typo: "coloumn" Style: The putative audience of the specs is the caller of the method rather than the implementor. Hence "returns" rather than "return". Here's how I would reword the spec for the 2nd method: /** * Given a line number and column number, returns the corresponding * position in the original source string. * Returns 0 if no line number information is available for this * compilation unit or the requested line number is less than one. * Returns the total size of the source string if <code>line</code> * is greater than the actual number lines in the unit. * Returns 0 if <code>column</code> is less than 0, * or the position of the last character of the line if <code>column</code> * is beyond the legal range. * * @param line the one-based line number * @param column the zero-based column number * @return the 0-based character position in the source string; * returns <code>0</code> if line/column number information is not known * for this compilation unit or the inputs are not valid * @since 3.2 */ public int getPosition(int line, int column);
I will review them, add tests and release them in the APT branch.
I suggest returning -1 when line/column information are invalid. 0 is potentially the position of the first character when line = 1 and column = 0. Then it is not possible to make the distinction between an invalid call and a valid one. The proposed implementation doesn't return 0 when line/column are invalid. I will fix this and add tests.
What about positions in the middle of a line break? On windows a line break takes two characters. So it would be possible to give position in the middle. I don't think this position can be passed to an editor for example.
Fixed and released in APT_BRANCH. I kept 0 for the column number and for the positions if arguments are invalid. This is consistent with the way we fail for the line number. New tests have been added into org.eclipse.jdt.core.tests.dom.ASTPositionsTest.