Mercurial Hosting > luan
changeset 1952:970e0d9d706d
fix line wrap
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 25 May 2025 23:08:36 -0600 |
parents | c4a926820433 |
children | 1caa7ddfc461 |
files | src/luan/modules/swing/TextAreaLuan.java |
diffstat | 1 files changed, 56 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/modules/swing/TextAreaLuan.java Sat May 24 21:49:42 2025 -0600 +++ b/src/luan/modules/swing/TextAreaLuan.java Sun May 25 23:08:36 2025 -0600 @@ -14,6 +14,10 @@ import javax.swing.Timer; import javax.swing.text.DefaultCaret; import javax.swing.text.BadLocationException; +import javax.swing.text.View; +import javax.swing.text.Element; +import javax.swing.text.WrappedPlainView; +import javax.swing.text.Segment; import javax.swing.event.DocumentEvent; import goodjava.logging.Logger; import goodjava.logging.LoggerFactory; @@ -51,6 +55,44 @@ } } + static class CustomWrappedPlainView extends WrappedPlainView { + public CustomWrappedPlainView(Element elem) { + super(elem,false); + } + + @Override protected int calculateBreakPosition(int p0, int p1) { + try { + int candidate = super.calculateBreakPosition(p0, p1); + if (candidate == p1) + return p1; // Everything fits — don't wrap early + Segment segment = new Segment(); + getDocument().getText(p0, candidate - p0, segment); + char[] a = segment.array; + int start = segment.offset; + int i = start + segment.count; + if( !isWordChar(a[i]) ) + return candidate; + do { + if( --i < start ) + return candidate; + } while( isWordChar(a[i]) ); + int breakPos = i + 1 - start + p0; + do { + if( --i < start ) + return candidate; + } while( !isWordChar(a[i]) ); + return breakPos; + } catch (BadLocationException e) { + throw new RuntimeException(e); + } + } + + private boolean isWordChar(char c) { + return Character.isJavaIdentifierPart(c); + } + } + + private boolean showWhitespace = false; private List<Range> highlights = Collections.emptyList(); private static final Color highlightColor = new Color(0x2dada3); @@ -58,13 +100,26 @@ public TextAreaLuan() { super(); - //logger.info(UIManager.getLookAndFeel().getName()); + //logger.info(getUI().toString()); if( UIManager.getLookAndFeel().getName().startsWith("FlatLaf") ) { setCaret(flatLafCaret); } getDocument().addDocumentListener(new WeakDocumentListener(ldl)); } + @Override public void updateUI() { + super.updateUI(); + if (UIManager.getLookAndFeel().getName().startsWith("FlatLaf")) { + setUI(new com.formdev.flatlaf.ui.FlatTextAreaUI() { + @Override public View create(Element elem) { + if (getLineWrap() && getWrapStyleWord()) + return new CustomWrappedPlainView(elem); + return super.create(elem); + } + }); + } + } + @Override public int getRowHeight() { return super.getRowHeight(); }