Any way to get Column position for Row/Col Panel

Questions on using RSyntaxTextArea should go here.

Moderator: robert

Any way to get Column position for Row/Col Panel

Postby ulmetrs » Wed Apr 02, 2014 3:04 pm

I am making a status panel that displays the caret's row/col position.

The getCaretLineNumber() works fine but getCaretOffsetFromLineStart() counts all tabs as a single character which means:

aTAB aa| --offset = 4
aaaaaaa| --offset = 7

columns don't match up from 1 line to the next. Is there any possible way to get a true column number as in eclipse?
ulmetrs
 
Posts: 7
Joined: Fri Mar 28, 2014 5:50 pm

Re: Any way to get Column position for Row/Col Panel

Postby robert » Sat Apr 05, 2014 1:21 am

I don't think there's a built-in way, but you could write a utility method to compute this. (and such a utility method could end up in e.g. RSyntaxUtilities):

Pseudo-code (untested):

java code:

public static int getRealOffsetInLine(RTextArea textArea) {
int tabSize = textArea.getTabSize();
int tabbedOffset = textArea.getCaretOffsetFromLineStart();
int realOffs = 0;
try {
int lineIndex = textArea.getLineOffsetOfCurrentLine():
int start = textArea.getLineStartOffset(lineIndex);
int end = textArea.getLineEndOffset(lineIndex);
String text = textArea.getText(start, end);
for (int i=0; i<tabbedOffset; i++) {
char ch = text.charAt(i);
if (ch=='\t') {
realOffs += (realOffs%tabSize==0) ? tabSize : (tabSize - (realOffs%tabSize));
}
else {
realOffs++;
}
}
return realOffs;
} catch (BadLocationException ble) {
ble.printStackTrace(); // Never happens
}
}
User avatar
robert
 
Posts: 797
Joined: Sat May 10, 2008 5:16 pm

Re: Any way to get Column position for Row/Col Panel

Postby ulmetrs » Wed Apr 09, 2014 5:26 pm

Hey thanks for the reply! Did you test this out before posting though I am getting all kinds of crazy errors with getText(start, end), and for starters getLineOffsetOfCurrentLine() isn't even in my RTextArea version. (I switched to getLineStartOffsetOfCurrentLine() and getLineEndOffsetOfCurrentLine()).

I am using getText() with offsets that are logical but its just not retrieving the text that is specified by the offsets.

1 AAAA
2 BBBB
3 CCCC

When my cursor is on line 1 its fine, start and end are 0, 4 and getText returns
AAAA
When my cursor is on line 2 however, start and end are 5,9 andgetText returns
BBBB
CCCC
When my cursor is on line 3, start and end are 10,14 and getText returns nothing

Just all kinds of wierdness, I need to call this method on caret update to show the true column number in a status panel. I am going to follow your example
and try to make one that works.
ulmetrs
 
Posts: 7
Joined: Fri Mar 28, 2014 5:50 pm

Re: Any way to get Column position for Row/Col Panel

Postby ulmetrs » Wed Apr 09, 2014 5:33 pm

Ok what I did was:

String text[] = textArea.getText().split("\n");
String line = text[textArea.getCaretLineNumber()];
int tabSize = textArea.getTabSize();
int tabbedOffset = textArea.getCaretOffsetFromLineStart();
int realOffs = 0;
for(int i=0; i<tabbedOffset; i++) {
char ch = text.charAt(i);
if (ch=='\t')
realOffs += (realOffs%tabSize==0) ? tabSize : (tabSize - (realOffs%tabSize));
else
realOffs++;
}
return realOffs;
ulmetrs
 
Posts: 7
Joined: Fri Mar 28, 2014 5:50 pm

Re: Any way to get Column position for Row/Col Panel

Postby robert » Fri Apr 11, 2014 9:12 pm

No, I didn't test it, which is why I prefaced the code snippet with "untested" :)

The way you are attempting to do things is pretty heavyweight; it will work fine for small documents, but degrade not-so-gracefully with larger documents (you're essentially making a copy of the entire document with each call to your method). I think your issue with my example was that I erroneously thought that there was a method with signature:

Code: Select all
JTextComponent.getText(int start, int end)


When in reality the signature is different:

Code: Select all
JTextComponent.getText(int start, int length)


Javadoc: JTextComponent.getText(int, int)

So if you change the second argument in the call to textArea.getText() to be the line's length instead of its ending offset, you should be good to go.

Let me know if you still have trouble.
User avatar
robert
 
Posts: 797
Joined: Sat May 10, 2008 5:16 pm

Re: Any way to get Column position for Row/Col Panel

Postby Guest » Mon Apr 14, 2014 6:36 pm

Yes that works perfect I feel pretty silly not realizing what the issue with getText() was =| Thanks!
Guest
 


Return to Help

Who is online

Users browsing this forum: No registered users and 2 guests

cron