ソースを参照

Correctly functioning pasting

Johan Rong 1 ヶ月 前
コミット
458ad3c7b8
5 ファイル変更60 行追加50 行削除
  1. 2 8
      internal/buffer/buffer.go
  2. 14 22
      internal/editor/action.go
  3. 3 7
      internal/editor/keyinput.go
  4. 3 3
      internal/editor/render.go
  5. 38 10
      main.go

+ 2 - 8
internal/buffer/buffer.go

@@ -44,12 +44,12 @@ func (buf *Buffer) ensureRope() {
 	}
 }
 
-func (buf *Buffer) Insert(content rune) {
+func (buf *Buffer) Insert(content string) {
 	buf.ensureRope()
 	buf.CM.DeduplicateAndSort()
 
 	delta := 0
-	data := []byte(string(content))
+	data := []byte(content)
 	shift := len(data)
 
 	for i := range buf.CM.Cursors {
@@ -75,12 +75,6 @@ func (buf *Buffer) Insert(content rune) {
 	}
 }
 
-func (buf *Buffer) Paste(content string) {
-	for _, r := range content {
-		buf.Insert(r)
-	}
-}
-
 func (buf *Buffer) Delete() {
 	buf.ensureRope()
 

+ 14 - 22
internal/editor/action.go

@@ -66,14 +66,6 @@ func DefaultActionManager() ActionManager {
 					}
 				},
 			},
-			Action{
-				Binding: "shift+ctrl+Rune[v]",
-				Command: "paste",
-				AskArgs: false,
-				Callback: func(m *Model, args []string) {
-					m.Screen.GetClipboard()
-				},
-			},
 			Action{
 				Binding: "ctrl+q",
 				Command: "q",
@@ -92,7 +84,7 @@ func DefaultActionManager() ActionManager {
 						if m.BM.Current().Path == "" {
 							m.Mode = ModeNormal
 							m.BM.PaletteBuffer.Clear()
-							m.BM.PaletteBuffer.Paste("moose.error:Missing filename argument for save")
+							m.BM.PaletteBuffer.Insert("moose.error:Missing filename argument for save")
 							return
 						}
 					}
@@ -108,12 +100,12 @@ func DefaultActionManager() ActionManager {
 					if err != nil {
 						m.Mode = ModeNormal
 						m.BM.PaletteBuffer.Clear()
-						m.BM.PaletteBuffer.Paste("moose.error:Could not save to file \"" + path + "\": " + err.Error())
+						m.BM.PaletteBuffer.Insert("moose.error:Could not save to file \"" + path + "\": " + err.Error())
 						return
 					}
 
 					m.BM.PaletteBuffer.Clear()
-					m.BM.PaletteBuffer.Paste("moose.info:Wrote " + strconv.Itoa(len(data)) + " bytes to \"" + path + "\"")
+					m.BM.PaletteBuffer.Insert("moose.info:Wrote " + strconv.Itoa(len(data)) + " bytes to \"" + path + "\"")
 					return
 				},
 			},
@@ -123,14 +115,14 @@ func DefaultActionManager() ActionManager {
 				AskArgs: true,
 			},
 			Action{
-				Binding: "ctrl+Rune[e]",
+				Binding: "ctrl+e",
 				Command: "e",
 				AskArgs: true,
 				Callback: func(m *Model, args []string) {
 					if len(args) < 1 {
 						m.Mode = ModeNormal
 						m.BM.PaletteBuffer.Clear()
-						m.BM.PaletteBuffer.Paste("moose.error:Did not specify path for edit")
+						m.BM.PaletteBuffer.Insert("moose.error:Did not specify path for edit")
 						return
 					}
 
@@ -138,7 +130,7 @@ func DefaultActionManager() ActionManager {
 					if err != nil {
 						m.Mode = ModeNormal
 						m.BM.PaletteBuffer.Clear()
-						m.BM.PaletteBuffer.Paste("moose.error:Could not edit file \"" + args[0] + "\": " + err.Error())
+						m.BM.PaletteBuffer.Insert("moose.error:Could not edit file \"" + args[0] + "\": " + err.Error())
 						return
 					}
 
@@ -161,7 +153,7 @@ func DefaultActionManager() ActionManager {
 				Callback: func(m *Model, args []string) {
 					m.Mode = ModePalette
 					m.BM.PaletteBuffer.Clear()
-					m.BM.PaletteBuffer.Insert('/')
+					m.BM.PaletteBuffer.Insert("/")
 				},
 			},
 			Action{
@@ -170,7 +162,7 @@ func DefaultActionManager() ActionManager {
 				Callback: func(m *Model, args []string) {
 					m.Mode = ModePalette
 					m.BM.PaletteBuffer.Clear()
-					m.BM.PaletteBuffer.Insert('?')
+					m.BM.PaletteBuffer.Insert("?")
 				},
 			},
 		},
@@ -196,7 +188,7 @@ func DefaultActionManager() ActionManager {
 				Command: "TODO:",
 				AskArgs: false,
 				Callback: func(m *Model, args []string) {
-					m.BM.Current().ClearCursors()
+					m.BM.Current().Insert("test")
 				},
 			},
 			Action{
@@ -205,7 +197,7 @@ func DefaultActionManager() ActionManager {
 				AskArgs: false,
 				Callback: func(m *Model, args []string) {
 					for _ = range 4 {
-						m.BM.Current().Insert(' ')
+						m.BM.Current().Insert(" ")
 					}
 				},
 			},
@@ -227,7 +219,7 @@ func DefaultActionManager() ActionManager {
 			Action{
 				Binding: "enter",
 				Callback: func(m *Model, args []string) {
-					m.BM.Current().Insert('\n')
+					m.BM.Current().Insert("\n")
 				},
 			},
 		},
@@ -263,15 +255,15 @@ func DefaultActionManager() ActionManager {
 
 						m.Mode = ModeNormal
 						m.BM.PaletteBuffer.Clear()
-						m.BM.PaletteBuffer.Paste("moose.error:Unknown command \"" + cmd + "\"")
+						m.BM.PaletteBuffer.Insert("moose.error:Unknown command \"" + cmd + "\"")
 					} else if strings.HasPrefix(args[0], "?") {
 						m.Mode = ModeFind
 						m.BM.PaletteBuffer.Clear()
-						m.BM.PaletteBuffer.Paste("moose.error:Find mode unimplemented \"" + cmd + "\"")
+						m.BM.PaletteBuffer.Insert("moose.error:Find mode unimplemented \"" + cmd + "\"")
 					} else {
 						m.Mode = ModeNormal
 						m.BM.PaletteBuffer.Clear()
-						m.BM.PaletteBuffer.Paste("moose.error:Unknown palette input \"" + cmd + "\"")
+						m.BM.PaletteBuffer.Insert("moose.error:Unknown palette input \"" + cmd + "\"")
 					}
 				},
 			},

+ 3 - 7
internal/editor/keyinput.go

@@ -11,7 +11,7 @@ func (m *Model) HandleKeyInput(ev *tcell.EventKey) {
 			if action.Command != "" && action.AskArgs == true {
 				m.Mode = ModePalette
 				m.BM.PaletteBuffer.Clear()
-				m.BM.PaletteBuffer.Paste("/" + action.Command)
+				m.BM.PaletteBuffer.Insert("/" + action.Command + " ")
 				return
 			} else if action.Command == "" && action.AskArgs == true {
 				m.Mode = ModePalette
@@ -24,16 +24,12 @@ func (m *Model) HandleKeyInput(ev *tcell.EventKey) {
 
 	if m.Mode == ModeWrite {
 		if ev.Key() == tcell.KeyRune {
-			for _, r := range ev.Str() {
-				m.BM.Current().Insert(r)
-			}
+			m.BM.Current().Insert(ev.Str())
 		}
 	}
 	if m.Mode == ModePalette {
 		if ev.Key() == tcell.KeyRune {
-			for _, r := range ev.Str() {
-				m.BM.PaletteBuffer.Insert(r)
-			}
+			m.BM.PaletteBuffer.Insert(ev.Str())
 		}
 	}
 }

+ 3 - 3
internal/editor/render.go

@@ -104,11 +104,11 @@ func (m Model) Render() {
 	modeStr := m.Mode.String()
 	if m.Mode == ModePalette {
 		if strings.HasPrefix(m.BM.PaletteBuffer.String(), "/") {
-			modeStr = "command"
+			modeStr += " (command)"
 		} else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "?") {
-			modeStr = "find"
+			modeStr += " (find)"
 		} else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "=") {
-			modeStr = "replace"
+			modeStr += " (replace)"
 		}
 	}
 

+ 38 - 10
main.go

@@ -19,13 +19,14 @@ func main() {
 		os.Exit(1)
 	}
 
+
 	style := tcell.StyleDefault.Background(color.Reset).Foreground(color.Reset)
 	s.SetStyle(style)
 
 	m := editor.NewModel(s, style)
 
 	s.EnableMouse()
-	s.DisablePaste()
+	s.EnablePaste()
 	s.Clear()
 
 	quit := func() {
@@ -37,9 +38,14 @@ func main() {
 	}
 	defer quit()
 
-	for {
-		s.Clear()
+	isPasting := false
+	pasteBuffer := ""
 
+	s.Clear()
+	m.Render()
+	s.Show()
+
+	for {
 		if m.ShouldQuit {
 			return
 		}
@@ -49,14 +55,36 @@ func main() {
 		switch ev := ev.(type) {
 		case *tcell.EventResize:
 			s.Sync()
-		case *tcell.EventClipboard:
-			m.BM.Current().Paste(string(ev.Data()))
+		case *tcell.EventPaste:
+			if ev.Start() {
+				isPasting = true
+				pasteBuffer = "" 
+			} else if ev.End() {
+				isPasting = false
+				m.BM.Current().Insert(pasteBuffer)
+				
+				s.Clear()
+				m.Render()
+				s.Show()
+			}
+
 		case *tcell.EventKey:
-			m.HandleKeyInput(ev)
+			if isPasting {
+				if ev.Key() == tcell.KeyEnter || ev.Key() == tcell.KeyCtrlJ {
+					pasteBuffer += "\n"
+				} else if ev.Key() == tcell.KeyTab {
+					pasteBuffer += "\t"
+				} else if ev.Key() == tcell.KeyRune {
+					pasteBuffer += ev.Str()
+				}
+			} else {
+				m.HandleKeyInput(ev)
+				
+				s.Clear()
+				m.Render()
+				s.Show()
+			}
 		}
-
-		m.Render()
-
-		s.Show()
 	}
+
 }