How to stop RSyntaxTextArea from changing line breaks

General Discussion on RSyntaxTextArea.

Moderator: robert

How to stop RSyntaxTextArea from changing line breaks

Postby narupley » Fri Jan 31, 2014 5:03 pm

Say I have a string containing carriage returns (0x0D). I paste it into RSyntaxTextArea, then copy it from the text area and back into somewhere else. All my original carriage returns have been replaced with LFs (0x0A). This is the same thing Sublime Text does by default, and it's horrible.

I saw that the TextEditorPane has a setLineSeparator method, but it appears to do nothing. I set it to "\r" to test it out, but the incorrect behavior stayed exactly the same. In any case, that setLineSeparator method isn't what I need. The text area needs to be able to accept multiple types of line breaks (CR/LF/CRLF) and preserve them when copy/pasting between the pane and something else.

Is there a way for me to tell RSyntaxTextArea "No, contrary to popular belief you actually don't know what's best for me, leave my strings alone"?


Part 2: Is there a simple way to show "meta" characters in the text area? Like whenever a literal line feed is in the string, in the text pane it should show "\n" instead. Obviously when a user copies the string out from the text area it should not copy out a literal backslash and "n", but instead copy out the actual LF. We do this in our current application using JEdit. Here's what it looks like:

Image
Image
Image

Obviously JEdit doesn't do all the fancy things that RSyntaxTextArea does like code folding, but at least it got these basic things right.
narupley
 
Posts: 1
Joined: Fri Jan 31, 2014 4:35 pm

Re: How to stop RSyntaxTextArea from changing line breaks

Postby robert » Sat Feb 01, 2014 12:35 am

Unfortunately all Swing JTextComponents (including RSyntaxTextArea) do not provide support for remembering individual, differing line terminators for different lines in a single document. They always normalize line endings to a single separator.

By default, I *think* JTextComponents choose the OS-specific default line terminator, but it might always just choose LF, I'm not sure about that. When reading a file via textArea.read(), it uses some heuristic to determine what the line terminator should be for that particular content when/if you write it back out. In any case, you can override this and tell the Document (not the text component) to use a specific line ending when writing its content:

java code:

Document doc = textArea.getDocument();
doc.putProperty(DefaultEditorKit.EndOfLineStringProperty, "\r\n"); // For example


This property is definitely used when writing file contents via textArea.write(Writer), but unfortunately I'm not 100% sure whether it is also used when copy/pasting text. And again, it won't preserve line endings read when the file was loaded either (if they are homogeneous in a file).

Preserving individual line endings would require some legwork on RSTA's part. Please enter a feature request on GitHub so this can be tracked.
User avatar
robert
 
Posts: 797
Joined: Sat May 10, 2008 5:16 pm


Return to Open Discussion

Who is online

Users browsing this forum: No registered users and 2 guests

cron