Bug 97766 - Requesting CompilationUnit.columnNumber() & CompilationUnit.getPosition(int, int)
Summary: Requesting CompilationUnit.columnNumber() & CompilationUnit.getPosition(int, ...
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P3 enhancement (vote)
Target Milestone: 3.2   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-05-31 20:09 EDT by Theodora Yeung CLA
Modified: 2005-06-17 13:03 EDT (History)
2 users (show)

See Also:


Attachments
CompilationUnit.java with implementation of proposed API. (34.40 KB, text/plain)
2005-06-02 12:52 EDT, Theodora Yeung CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Theodora Yeung CLA 2005-05-31 20:09:08 EDT
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.
Comment 1 Philipe Mulet CLA 2005-06-02 12:06:21 EDT
Post 3.1. 

Indeed makes sense, since we do not expose the line separator table.
Olivier - can you confirm this ?
Comment 2 Olivier Thomann CLA 2005-06-02 12:09:22 EDT
Yes, I can confirm that we don't expose the line end table.
Therefore it is pretty expensive to compute it.
Comment 3 Philipe Mulet CLA 2005-06-02 12:25:45 EDT
Would it be better to expose it, or to provide these APIs ?
Comment 4 Theodora Yeung CLA 2005-06-02 12:52:24 EDT
Created attachment 22243 [details]
CompilationUnit.java with implementation of proposed API.
Comment 5 Theodora Yeung CLA 2005-06-02 12:54:30 EDT
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. 
Comment 6 Jim des Rivieres CLA 2005-06-15 08:45:01 EDT
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);



Comment 7 Olivier Thomann CLA 2005-06-15 15:26:33 EDT
I will review them, add tests and release them in the APT branch.
Comment 8 Olivier Thomann CLA 2005-06-17 11:55:00 EDT
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.
Comment 9 Olivier Thomann CLA 2005-06-17 11:56:49 EDT
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.
Comment 10 Olivier Thomann CLA 2005-06-17 13:03:42 EDT
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.