-
Notifications
You must be signed in to change notification settings - Fork 236
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
Question: how to get real size of one span at position X? #1231
Comments
Would |
@Jugen Thank you for your answer. This is my code:
And this is output:
Firstly I couldn't understand how it is possible. But it can be possible if returned range is considered in specific paragraph. But, what if my span takes two paragraphs? Then how can I take the multiparagraph span at position X? Or I misunderstand something. |
Yeah, I'm surprised by that as well !? Looking at the code reveals that the returned IndexRange is relative to the paragraph as you guessed. I'd suggest using For style spans over multiple paragraphs you could try the following based on what is done in Paragraph: // Must use this method of getStyleSpans or the one with parameter IndexRange and not any other !
var spans = codeArea.getStyleSpans( 0, codeArea.getLength() );
var offset = spans.offsetToPosition( targetPos, Bias.Backward );
var styleStart = offset.getMinor();
var styleEnd = styleStart + spans.getStyleSpan( offset.getMajor() ).getLength(); |
@Jugen Thank you for your answer. I couldn't make it work: My code:
Usage:
Output:
|
I can provide test code, if it's required. I think it is necessary to add to RichTextFX API something like this. |
Please do provide test code, then I'll have go at it .... |
This is code:
|
Try updating public IndexRange getRealStyleRangeAt( int position )
{
var length = 0;
for ( var ss : codeArea.getStyleSpans( 0, codeArea.getLength() ) )
{
if ( (length += ss.getLength()) >= position )
{
var start = length - ss.getLength();
return new IndexRange( start, length );
}
}
return new IndexRange( 0,0 );
} |
@Jugen This is what I wanted to avoid. As I understand RichTextFX has its own model for style span ranges and I wanted to use it instead if iterating all style spans. Or there is no chance to get REAL range for concrete position? |
Yeah, I don't like the iteration either but I don't know of another way as I haven't been able to find it in the model. I can try and add a start field to var offset = spans.offsetToPosition( position, Bias.Backward );
var span = spans.getStyleSpan( offset.getMajor() );
var spanStart = span.getStart();
return new IndexRange( spanStart, spanStart + span.getLength() ); |
@Jugen The only thing I can suggest is to distinguish |
Please see the PR that I've submitted to try and address this issue for you. |
@Jugen I've checked. It seems to work. At least, all my tests finally passed. Only one question - when I do: |
Do the following three steps ....
private SuspendableYes suspendUndo = new SuspendableYes();
textArea.setUndoManager( UndoUtils.richTextSuspendableUndoManager( textArea, suspendUndo ) );
suspendUndo.suspendWhile( () -> textArea.setStyleSpans( position, ssb.create() ) ); |
@Jugen Yes, it helped. Thank you very much! |
When I do
I get:
So, I there are three spans. The length of the first is 6. Now I want to get the length of the span at position X. But when I do:
I get:
Can I get ONE span at position X and its full length?
The text was updated successfully, but these errors were encountered: