CodeArea: how to convert offset to TextPos?
Andy Goryachev
andy.goryachev at oracle.com
Mon May 19 19:00:44 UTC 2025
A good question!
In the RichTextArea, it generally depends on the model. The default implementation, both in RichTextArea and CodeArea, the plaintext format handler uses "line.separator" system property (PlainTextFormatHandler:72). That is, both will emit \r\n line separators in Windows, \n elsewhere else.
The model may decide to change that behavior and register a different data format handler, as required by the application requirements.
-andy
From: openjfx-dev <openjfx-dev-retn at openjdk.org> on behalf of PavelTurk <pavelturk2000 at gmail.com>
Date: Saturday, May 17, 2025 at 05:19
To: openjfx-dev at openjdk.org <openjfx-dev at openjdk.org>
Subject: Re: CodeArea: how to convert offset to TextPos?
Andy, while implementing the conversion from TextPos to offset, I encountered the following issue.
Paragraph lines do not include line separators (\n or \r\n) at the end. However, in order to correctly
calculate the offset, I need to account for the size of the line separator, which can be either 1 or 2
characters long.
In RichTextFX, as far as I know, only \n is used (i.e., \r\n is replaced with \n). But what about the JavaFX CodeArea?
How can I determine the size of the line separator there? Or is it always 1 byte as well?
Best regards, Pavel
On 5/7/25 20:04, Andy Goryachev wrote:
There is no dedicated method to do that. The application can iterate over paragraphs to compute the text position, just keep in mind that the model can be quite large and it a) will take a lot of iterations and b) the result may exceed 2^31 (i.e. need to be 'long').
The same is true for the reverse operation, TextPos -> offset.
-andy
From: openjfx-dev <openjfx-dev-retn at openjdk.org><mailto:openjfx-dev-retn at openjdk.org> on behalf of PavelTurk <pavelturk2000 at gmail.com><mailto:pavelturk2000 at gmail.com>
Date: Wednesday, May 7, 2025 at 09:29
To: openjfx-dev at openjdk.org<mailto:openjfx-dev at openjdk.org> <openjfx-dev at openjdk.org><mailto:openjfx-dev at openjdk.org>
Subject: CodeArea: how to convert offset to TextPos?
Could anyone say how to convert offset to TextPos in CodeArea?
For example, in RTFX CodeArea we have:
var pos = codeArea.offsetToPosition(offset, Bias.Backward);
var paragraph = pos.getMajor();
Best regards, Pavel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/openjfx-dev/attachments/20250519/02e78967/attachment-0001.htm>
More information about the openjfx-dev
mailing list