Skip to content

Commit

Permalink
Implement Join action
Browse files Browse the repository at this point in the history
  • Loading branch information
xanni committed Sep 26, 2024
1 parent 0432a17 commit 0701705
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 9 deletions.
55 changes: 55 additions & 0 deletions edits/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,61 @@ func Export(path string) {
}
}

func Join() {
csent := cache[cursor[Para]-1].csent
s := cursor[Sent]
if s == 0 {
s++
}

if len(csent) < 2 || s == len(csent) {
mergeNextPara()

return
}

var g string // Grapheme cluster
var sentence []string // Current sentence as grapheme clusters
source := after.String()
state := -1
for c := cursor[Char]; c < csent[s]; c++ {
g, source, _, state = uniseg.StepString(source, state)
sentence = append(sentence, g)
}

// Index backwards over punctuation at the end of the current sentence
i := len(sentence) - 1
for i >= 0 {
g := sentence[i]
r, _ := utf8.DecodeRuneInString(g)
if !(unicode.In(r, unicode.P, unicode.Z)) {
break
}
i--
}

// If every character from the cursor onwards is punctuation, keep going backwards
if i < 0 {
b := uniseg.ReverseString(before.String())
for _, r := range b {
if !unicode.IsPunct(r) {
break
}
i--
}
}

cursor[Char] += i + 1 // Move to the first punctuation character
r, _ := utf8.DecodeRuneInString(source)
if unicode.IsUpper(r) {
ps.ReplaceText(cursor[Para], cursor[Char], csent[s]+1, " "+string(unicode.ToLower(r)))
} else { // Keep existing space
ps.DeleteText(cursor[Para], cursor[Char], csent[s]-1)
}

ClearMarks()
}

func refresh() {
cache = nil
total = counts{0, 0, 0, 1}
Expand Down
44 changes: 42 additions & 2 deletions edits/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,11 @@ func TestCopy(t *testing.T) {
Copy()
assert.Equal("", ps.GetCut())

Mark()
drawWindow()
Copy()
assert.Equal("", ps.GetCut())

ps.AppendText(1, "Test")
drawWindow()

Expand Down Expand Up @@ -475,8 +480,7 @@ func TestDeleteMerge(t *testing.T) {
ps.SplitParagraph(1, 0)
ps.AppendText(1, test.p1)
ps.AppendText(2, test.p2)
cursor = counts{0, 0, 0, 2}
drawWindow()
drawPara(2)
cursor = counts{len(test.p1), 0, 0, 1}

drawWindow()
Expand Down Expand Up @@ -564,6 +568,42 @@ func TestExport(t *testing.T) {
assert.Contains(message, "failed export: ")
}

func TestJoin(t *testing.T) {
assert := assert.New(t)
setupTest()
ResizeScreen(margin+4, 3)
drawWindow()

tests := map[string]struct {
text string
cursor int
expect string
}{
"Empty": {"", 0, "Three"},
"First sentence": {"One. Two.", 0, "One two."},
"Before punctuation": {"One. Two.", 3, "One two."},
"Mid punctuation": {"One... Two.", 5, "One two."},
"Second sentence": {"One. Two.", 7, "One. Two. Three"},
"Already lowercase": {"One! two.", 0, "One two."},
}

for name, test := range tests {
t.Run(name, func(_ *testing.T) {
ps.Init()
ps.SplitParagraph(1, 0)
ps.AppendText(2, "Three")
drawPara(2)
ps.AppendText(1, test.text)
cursor[Char] = test.cursor

drawWindow()
Join()

assert.Equal(test.expect, ps.GetText(1))
})
}
}

func TestUndoRedo(t *testing.T) {
assert := assert.New(t)
setupTest()
Expand Down
5 changes: 3 additions & 2 deletions i18n/help.de
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Bei ausgewähltem Text tauscht die Leertaste die Auswahlen aus,
Mit den folgenden Tasten lassen sich spezielle Aktionen ausführen:
"↑" Bereich vergrößern, "↓" Bereich verkleinern, "←" nach links bewegen, "→" nach rechts bewegen,
"Rücktaste"/"Strg-H" vorhergehenden Text löschen, "Eingabetaste"/"Strg-M" neuer Absatz,
"Einfügen"/"Strg-V" ausgeschnittenen Text einfügen, "Entf"/"Strg-X" Text beim Ausschneiden löschen,
"Strg-C" Text kopieren, "Strg-J" benachbarte Sätze oder Absätze verbinden,
"Einfügen"/"Strg-V" ausgeschnittenen oder kopierten Text einfügen, "Entf"/"Strg-X" Text ausschneiden,
"Pos1"/"Strg-U" zum Anfang bewegen, "Ende"/"Strg-D" zum Ende bewegen,
"Tab"/"Strg-I" Markierung setzen, "Umschalt-Tab" alle Markierungen abbrechen, "Strg-C" Text kopieren,
"Tab"/"Strg-I" Markierung setzen, "Umschalt-Tab" alle Markierungen abbrechen,
"Strg-Q"/"Strg-W" beenden, "Strg-E" exportieren, "Strg-Z" rückgängig machen, "Strg-Y" wiederherstellen
5 changes: 3 additions & 2 deletions i18n/help.en
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ While text is selected "Space" will exchange the selections,
The following keys perform special actions:
"↑" increase scope, "↓" decrease scope, "←" move left, "→" move right,
"Backspace"/"Ctrl-H" erase preceding text, "Enter"/"Ctrl-M" new paragraph,
"Insert"/"Ctrl-V" insert cut text, "Delete"/"Ctrl-X" delete text as cut,
"Ctrl-C" copy text, "Ctrl-J" join adjacent sentences or paragraphs,
"Insert"/"Ctrl-V" insert cut or copied text, "Delete"/"Ctrl-X" cut text,
"Home"/"Ctrl-U" move to beginning, "End"/"Ctrl-D" move to end,
"Tab"/"Ctrl-I" set mark, "Shift-Tab" clear all marks, "Ctrl-C" copy text,
"Tab"/"Ctrl-I" set mark, "Shift-Tab" clear all marks,
"Ctrl-Q"/"Ctrl-W" quit, "Ctrl-E" export, "Ctrl-Z" undo, "Ctrl-Y" redo
8 changes: 5 additions & 3 deletions i18n/help.jp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
以下のキーは特別なアクションを実行します:
「↑」 範囲を拡大、「↓」 範囲を縮小、「←」 左に移動、「→」 右に移動、
「Backspace」/「Ctrl-H」 前のテキストを消去、「Enter」/「Ctrl-M」 新しい段落、
「Insert」/「Ctrl-V」 切り取ったテキストを挿入、「Delete」/「Ctrl-X」 切り取ったテキストの削除、
「Ctrl-C」 テキストをコピー、「Ctrl-J」 隣接する文または段落を結合、
「Insert」/「Ctrl-V」 切り取ったテキストまたはコピーしたテキストを挿入、
「Delete」/「Ctrl-X」 テキストを切り取り、「Ctrl-E」 エクスポート、
「Home」/「Ctrl-U」 先頭に移動、「End」/「Ctrl-D」 末尾に移動、
「Tab」/「Ctrl-I」 マークを設定、「Shift-Tab」 すべてのマークをクリア、「Ctrl-C」 テキストをコピー、
「Ctrl-Q」/「Ctrl-W」 終了、「Ctrl-E」 エクスポート、「Ctrl-Z」 元に戻す、「Ctrl-Y」 やり直し
「Tab」/「Ctrl-I」 マークを設定、「Shift-Tab」 すべてのマークをクリア、
「Ctrl-Q」/「Ctrl-W」 終了、「Ctrl-Z」 元に戻す、「Ctrl-Y」 やり直し
1 change: 1 addition & 0 deletions jotty.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var dispatch = map[tea.KeyType]func(){
tea.KeyCtrlE: export,
tea.KeyBackspace: edits.Backspace, tea.KeyCtrlH: edits.Backspace,
tea.KeyTab: edits.Mark, tea.KeyShiftTab: edits.ClearMarks,
tea.KeyCtrlJ: edits.Join,
tea.KeyEnter: edits.Enter, tea.KeySpace: edits.Space,
tea.KeyCtrlQ: confirmExit, tea.KeyCtrlW: confirmExit,
tea.KeyHome: edits.Home, tea.KeyCtrlU: edits.Home,
Expand Down

0 comments on commit 0701705

Please sign in to comment.