Line selection with triple click

This commit is contained in:
Zachary Yedidia
2016-03-27 16:35:54 -04:00
parent 927dd9dba9
commit 72acdfb74a
2 changed files with 51 additions and 33 deletions

View File

@@ -42,8 +42,8 @@ type Cursor struct {
x int
y int
selectionStart int
selectionEnd int
curSelection [2]int
origSelection [2]int
}
// SetLoc sets the location of the cursor in terms of character number
@@ -62,50 +62,68 @@ func (c *Cursor) Loc() int {
// ResetSelection resets the user's selection
func (c *Cursor) ResetSelection() {
c.selectionStart = 0
c.selectionEnd = 0
c.curSelection[0] = 0
c.curSelection[1] = 0
}
// HasSelection returns whether or not the user has selected anything
func (c *Cursor) HasSelection() bool {
return c.selectionEnd != c.selectionStart
return c.curSelection[1] != c.curSelection[0]
}
// DeleteSelection deletes the currently selected text
func (c *Cursor) DeleteSelection() {
if c.selectionStart > c.selectionEnd {
c.v.eh.Remove(c.selectionEnd, c.selectionStart+1)
c.SetLoc(c.selectionEnd)
if c.curSelection[0] > c.curSelection[1] {
c.v.eh.Remove(c.curSelection[1], c.curSelection[0]+1)
c.SetLoc(c.curSelection[1])
} else {
c.v.eh.Remove(c.selectionStart, c.selectionEnd+1)
c.SetLoc(c.selectionStart)
c.v.eh.Remove(c.curSelection[0], c.curSelection[1]+1)
c.SetLoc(c.curSelection[0])
}
}
// GetSelection returns the cursor's selection
func (c *Cursor) GetSelection() string {
if c.selectionStart > c.selectionEnd {
return string([]rune(c.v.buf.text)[c.selectionEnd : c.selectionStart+1])
if c.curSelection[0] > c.curSelection[1] {
return string([]rune(c.v.buf.text)[c.curSelection[1] : c.curSelection[0]+1])
}
return string([]rune(c.v.buf.text)[c.selectionStart : c.selectionEnd+1])
return string([]rune(c.v.buf.text)[c.curSelection[0] : c.curSelection[1]+1])
}
// SelectLine selects the current line
func (c *Cursor) SelectLine() {
c.Start()
c.selectionStart = c.Loc()
c.curSelection[0] = c.Loc()
c.End()
c.selectionEnd = c.Loc()
c.curSelection[1] = c.Loc()
c.origSelection[0] = c.curSelection[0]
c.origSelection[1] = c.curSelection[1]
}
// AddLineToSelection adds the current line to the selection
func (c *Cursor) AddLineToSelection() {
if c.Loc() < c.selectionStart {
loc := c.Loc()
if loc > c.origSelection[0] && loc < c.origSelection[1] {
c.curSelection = c.origSelection
return
}
if loc < c.origSelection[0] {
c.Start()
c.selectionStart = c.Loc()
} else if c.Loc() > c.selectionEnd {
c.curSelection[0] = c.Loc()
} else if loc > c.origSelection[1] {
c.End()
c.selectionEnd = c.Loc()
c.curSelection[1] = c.Loc()
}
if loc < c.curSelection[0] {
c.Start()
c.curSelection[0] = c.Loc()
} else if loc > c.curSelection[1] {
c.End()
c.curSelection[1] = c.Loc()
}
}

View File

@@ -262,8 +262,8 @@ func (v *View) Paste() {
// SelectAll selects the entire buffer
func (v *View) SelectAll() {
v.cursor.selectionEnd = 0
v.cursor.selectionStart = v.buf.Len()
v.cursor.curSelection[1] = 0
v.cursor.curSelection[0] = v.buf.Len()
// Put the cursor at the beginning
v.cursor.x = 0
v.cursor.y = 0
@@ -456,24 +456,21 @@ func (v *View) HandleEvent(event tcell.Event) {
v.lastClickTime = time.Now()
v.tripleClick = true
v.doubleClick = false
messenger.Error("Triple click")
v.cursor.SelectLine()
} else {
// Double click
v.doubleClick = true
v.tripleClick = false
v.lastClickTime = time.Now()
messenger.Error("Double click")
}
} else {
messenger.Error("Single click")
v.doubleClick = false
v.tripleClick = false
v.lastClickTime = time.Now()
loc := v.cursor.Loc()
v.cursor.selectionStart = loc
v.cursor.selectionEnd = loc
v.cursor.curSelection[0] = loc
v.cursor.curSelection[1] = loc
}
} else {
if v.tripleClick {
@@ -481,7 +478,7 @@ func (v *View) HandleEvent(event tcell.Event) {
} else if v.doubleClick {
} else {
v.cursor.selectionEnd = v.cursor.Loc()
v.cursor.curSelection[1] = v.cursor.Loc()
}
}
v.mouseReleased = false
@@ -496,8 +493,11 @@ func (v *View) HandleEvent(event tcell.Event) {
// However, if we are running in a terminal that doesn't support mouse motion
// events, this still allows the user to make selections, except only after they
// release the mouse
v.MoveToMouseClick(x, y)
v.cursor.selectionEnd = v.cursor.Loc()
if !v.doubleClick && !v.tripleClick {
v.MoveToMouseClick(x, y)
v.cursor.curSelection[1] = v.cursor.Loc()
}
v.mouseReleased = true
}
// We don't want to relocate because otherwise the view will be relocated
@@ -575,8 +575,8 @@ func (v *View) DisplayView() {
highlightStyle = v.matches[lineN][colN]
if v.cursor.HasSelection() &&
(charNum >= v.cursor.selectionStart && charNum <= v.cursor.selectionEnd ||
charNum <= v.cursor.selectionStart && charNum >= v.cursor.selectionEnd) {
(charNum >= v.cursor.curSelection[0] && charNum <= v.cursor.curSelection[1] ||
charNum <= v.cursor.curSelection[0] && charNum >= v.cursor.curSelection[1]) {
lineStyle = tcell.StyleDefault.Reverse(true)
@@ -605,8 +605,8 @@ func (v *View) DisplayView() {
// The newline may be selected, in which case we should draw the selection style
// with a space to represent it
if v.cursor.HasSelection() &&
(charNum >= v.cursor.selectionStart && charNum <= v.cursor.selectionEnd ||
charNum <= v.cursor.selectionStart && charNum >= v.cursor.selectionEnd) {
(charNum >= v.cursor.curSelection[0] && charNum <= v.cursor.curSelection[1] ||
charNum <= v.cursor.curSelection[0] && charNum >= v.cursor.curSelection[1]) {
selectStyle := tcell.StyleDefault.Reverse(true)