ui.gadgets.editors: fix com-join-lines if there are more than two lines in the editor
parent
d585aa861c
commit
08b683de61
|
@ -1,8 +1,8 @@
|
||||||
USING: accessors ui.gadgets.editors tools.test kernel io
|
USING: accessors ui.gadgets.editors ui.gadgets.editors.private
|
||||||
io.streams.plain definitions namespaces ui.gadgets
|
tools.test kernel io io.streams.plain definitions namespaces
|
||||||
ui.gadgets.grids prettyprint documents ui.gestures ui.gadgets.debug
|
ui.gadgets ui.gadgets.grids prettyprint documents ui.gestures
|
||||||
models documents.elements ui.gadgets.scrollers ui.gadgets.line-support
|
ui.gadgets.debug models documents.elements ui.gadgets.scrollers
|
||||||
sequences ;
|
ui.gadgets.line-support sequences ;
|
||||||
IN: ui.gadgets.editors.tests
|
IN: ui.gadgets.editors.tests
|
||||||
|
|
||||||
[ "foo bar" ] [
|
[ "foo bar" ] [
|
||||||
|
@ -55,6 +55,9 @@ IN: ui.gadgets.editors.tests
|
||||||
[ ] [ <editor> com-join-lines ] unit-test
|
[ ] [ <editor> com-join-lines ] unit-test
|
||||||
[ ] [ <editor> "A" over set-editor-string com-join-lines ] unit-test
|
[ ] [ <editor> "A" over set-editor-string com-join-lines ] unit-test
|
||||||
[ "A B" ] [ <editor> "A\nB" over set-editor-string [ com-join-lines ] [ editor-string ] bi ] unit-test
|
[ "A B" ] [ <editor> "A\nB" over set-editor-string [ com-join-lines ] [ editor-string ] bi ] unit-test
|
||||||
|
[ "A B\nC\nD" ] [ <editor> "A\nB\nC\nD" over set-editor-string { 0 0 } over set-caret dup mark>caret [ com-join-lines ] [ editor-string ] bi ] unit-test
|
||||||
|
[ "A\nB C\nD" ] [ <editor> "A\nB\nC\nD" over set-editor-string { 1 0 } over set-caret dup mark>caret [ com-join-lines ] [ editor-string ] bi ] unit-test
|
||||||
|
[ "A\nB\nC D" ] [ <editor> "A\nB\nC\nD" over set-editor-string { 2 0 } over set-caret dup mark>caret [ com-join-lines ] [ editor-string ] bi ] unit-test
|
||||||
|
|
||||||
[ 2 ] [ <editor> 20 >>min-rows 20 >>min-cols pref-viewport-dim length ] unit-test
|
[ 2 ] [ <editor> 20 >>min-rows 20 >>min-cols pref-viewport-dim length ] unit-test
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,8 @@ caret-color
|
||||||
caret mark
|
caret mark
|
||||||
focused? blink blink-alarm ;
|
focused? blink blink-alarm ;
|
||||||
|
|
||||||
|
<PRIVATE
|
||||||
|
|
||||||
: <loc> ( -- loc ) { 0 0 } <model> ;
|
: <loc> ( -- loc ) { 0 0 } <model> ;
|
||||||
|
|
||||||
: init-editor-locs ( editor -- editor )
|
: init-editor-locs ( editor -- editor )
|
||||||
|
@ -27,6 +29,8 @@ focused? blink blink-alarm ;
|
||||||
COLOR: red >>caret-color
|
COLOR: red >>caret-color
|
||||||
monospace-font >>font ; inline
|
monospace-font >>font ; inline
|
||||||
|
|
||||||
|
PRIVATE>
|
||||||
|
|
||||||
: new-editor ( class -- editor )
|
: new-editor ( class -- editor )
|
||||||
new-line-gadget
|
new-line-gadget
|
||||||
<document> >>model
|
<document> >>model
|
||||||
|
@ -36,6 +40,8 @@ focused? blink blink-alarm ;
|
||||||
: <editor> ( -- editor )
|
: <editor> ( -- editor )
|
||||||
editor new-editor ;
|
editor new-editor ;
|
||||||
|
|
||||||
|
<PRIVATE
|
||||||
|
|
||||||
: activate-editor-model ( editor model -- )
|
: activate-editor-model ( editor model -- )
|
||||||
[ add-connection ]
|
[ add-connection ]
|
||||||
[ nip activate-model ]
|
[ nip activate-model ]
|
||||||
|
@ -70,6 +76,8 @@ SYMBOL: blink-interval
|
||||||
bi
|
bi
|
||||||
] [ drop ] if ;
|
] [ drop ] if ;
|
||||||
|
|
||||||
|
PRIVATE>
|
||||||
|
|
||||||
M: editor graft*
|
M: editor graft*
|
||||||
[ dup caret>> activate-editor-model ]
|
[ dup caret>> activate-editor-model ]
|
||||||
[ dup mark>> activate-editor-model ] bi ;
|
[ dup mark>> activate-editor-model ] bi ;
|
||||||
|
@ -142,6 +150,8 @@ M: editor ungraft*
|
||||||
] keep scroll>rect
|
] keep scroll>rect
|
||||||
] [ drop ] if ;
|
] [ drop ] if ;
|
||||||
|
|
||||||
|
<PRIVATE
|
||||||
|
|
||||||
: draw-caret? ( editor -- ? )
|
: draw-caret? ( editor -- ? )
|
||||||
{ [ focused?>> ] [ blink>> ] } 1&& ;
|
{ [ focused?>> ] [ blink>> ] } 1&& ;
|
||||||
|
|
||||||
|
@ -189,6 +199,8 @@ TUPLE: selected-line start end first? last? ;
|
||||||
] 3bi
|
] 3bi
|
||||||
] if ;
|
] if ;
|
||||||
|
|
||||||
|
PRIVATE>
|
||||||
|
|
||||||
M: editor draw-line ( line index editor -- )
|
M: editor draw-line ( line index editor -- )
|
||||||
[ selected-lines get at ] dip over
|
[ selected-lines get at ] dip over
|
||||||
[ draw-selected-line ] [ nip draw-unselected-line ] if ;
|
[ draw-selected-line ] [ nip draw-unselected-line ] if ;
|
||||||
|
@ -206,6 +218,8 @@ M: editor baseline font>> font-metrics ascent>> ;
|
||||||
|
|
||||||
M: editor cap-height font>> font-metrics cap-height>> ;
|
M: editor cap-height font>> font-metrics cap-height>> ;
|
||||||
|
|
||||||
|
<PRIVATE
|
||||||
|
|
||||||
: contents-changed ( model editor -- )
|
: contents-changed ( model editor -- )
|
||||||
[ [ nip caret>> ] [ drop ] 2bi '[ _ validate-loc ] (change-model) ]
|
[ [ nip caret>> ] [ drop ] 2bi '[ _ validate-loc ] (change-model) ]
|
||||||
[ [ nip mark>> ] [ drop ] 2bi '[ _ validate-loc ] (change-model) ]
|
[ [ nip mark>> ] [ drop ] 2bi '[ _ validate-loc ] (change-model) ]
|
||||||
|
@ -214,6 +228,8 @@ M: editor cap-height font>> font-metrics cap-height>> ;
|
||||||
: caret/mark-changed ( editor -- )
|
: caret/mark-changed ( editor -- )
|
||||||
[ restart-blinking ] keep scroll>caret ;
|
[ restart-blinking ] keep scroll>caret ;
|
||||||
|
|
||||||
|
PRIVATE>
|
||||||
|
|
||||||
M: editor model-changed
|
M: editor model-changed
|
||||||
{
|
{
|
||||||
{ [ 2dup model>> eq? ] [ contents-changed ] }
|
{ [ 2dup model>> eq? ] [ contents-changed ] }
|
||||||
|
@ -513,6 +529,8 @@ PRIVATE>
|
||||||
: change-selection ( editor quot -- )
|
: change-selection ( editor quot -- )
|
||||||
'[ gadget-selection @ ] [ user-input* drop ] bi ; inline
|
'[ gadget-selection @ ] [ user-input* drop ] bi ; inline
|
||||||
|
|
||||||
|
<PRIVATE
|
||||||
|
|
||||||
: join-lines ( string -- string' )
|
: join-lines ( string -- string' )
|
||||||
"\n" split
|
"\n" split
|
||||||
[ rest-slice [ [ blank? ] trim-head-slice ] change-each ]
|
[ rest-slice [ [ blank? ] trim-head-slice ] change-each ]
|
||||||
|
@ -520,22 +538,39 @@ PRIVATE>
|
||||||
[ " " join ]
|
[ " " join ]
|
||||||
tri ;
|
tri ;
|
||||||
|
|
||||||
: this-line-and-next ( document line -- start end )
|
|
||||||
[ nip 0 swap 2array ]
|
|
||||||
[ 1 + [ nip ] [ swap doc-line length ] 2bi 2array ]
|
|
||||||
2bi ;
|
|
||||||
|
|
||||||
: last-line? ( document line -- ? )
|
: last-line? ( document line -- ? )
|
||||||
[ last-line# ] dip = ;
|
[ last-line# ] dip = ;
|
||||||
|
|
||||||
|
: prev-line-and-this ( document line -- start end )
|
||||||
|
swap
|
||||||
|
[ drop 1 - 0 2array ]
|
||||||
|
[ [ drop ] [ doc-line length ] 2bi 2array ]
|
||||||
|
2bi ;
|
||||||
|
|
||||||
|
: join-with-prev ( document line -- )
|
||||||
|
[ prev-line-and-this ] [ drop ] 2bi
|
||||||
|
[ join-lines ] change-doc-range ;
|
||||||
|
|
||||||
|
: this-line-and-next ( document line -- start end )
|
||||||
|
swap
|
||||||
|
[ drop 0 2array ]
|
||||||
|
[ [ 1 + ] dip [ drop ] [ doc-line length ] 2bi 2array ]
|
||||||
|
2bi ;
|
||||||
|
|
||||||
|
: join-with-next ( document line -- )
|
||||||
|
[ this-line-and-next ] [ drop ] 2bi
|
||||||
|
[ join-lines ] change-doc-range ;
|
||||||
|
|
||||||
|
PRIVATE>
|
||||||
|
|
||||||
: com-join-lines ( editor -- )
|
: com-join-lines ( editor -- )
|
||||||
dup gadget-selection?
|
dup gadget-selection?
|
||||||
[ [ join-lines ] change-selection ] [
|
[ [ join-lines ] change-selection ] [
|
||||||
[ model>> ] [ editor-caret first ] bi
|
[ model>> ] [ editor-caret first ] bi {
|
||||||
2dup last-line? [ 2drop ] [
|
{ [ over last-line# 0 = ] [ 2drop ] }
|
||||||
[ this-line-and-next ] [ drop ] 2bi
|
{ [ 2dup last-line? ] [ join-with-prev ] }
|
||||||
[ join-lines ] change-doc-range
|
[ join-with-next ]
|
||||||
] if
|
} cond
|
||||||
] if ;
|
] if ;
|
||||||
|
|
||||||
multiline-editor "multiline" f {
|
multiline-editor "multiline" f {
|
||||||
|
@ -566,6 +601,8 @@ TUPLE: source-editor < multiline-editor ;
|
||||||
! Fields wrap an editor
|
! Fields wrap an editor
|
||||||
TUPLE: field < border editor min-cols max-cols ;
|
TUPLE: field < border editor min-cols max-cols ;
|
||||||
|
|
||||||
|
<PRIVATE
|
||||||
|
|
||||||
: field-theme ( gadget -- gadget )
|
: field-theme ( gadget -- gadget )
|
||||||
{ 2 2 } >>size
|
{ 2 2 } >>size
|
||||||
{ 1 0 } >>fill
|
{ 1 0 } >>fill
|
||||||
|
@ -576,6 +613,8 @@ TUPLE: field < border editor min-cols max-cols ;
|
||||||
{ 1 0 } >>fill
|
{ 1 0 } >>fill
|
||||||
field-theme ;
|
field-theme ;
|
||||||
|
|
||||||
|
PRIVATE>
|
||||||
|
|
||||||
: new-field ( class -- gadget )
|
: new-field ( class -- gadget )
|
||||||
[ <editor> ] dip new-border
|
[ <editor> ] dip new-border
|
||||||
dup gadget-child >>editor
|
dup gadget-child >>editor
|
||||||
|
|
Loading…
Reference in New Issue