GitHub PullRequestReviewCommentEvent at 2014-05-30 16:34:45
Repository: vigo
Actor: kisielk
Payload: {"comment":{"url":"https://api.github.com/repos/kisielk/vigo/pulls/comments/13237213","id":13237213,"diff_hunk":"@@ -0,0 +1,212 @@\n+package mode\n+\n+import (\n+\tcmd \"github.com/kisielk/vigo/commands\"\n+\t\"github.com/kisielk/vigo/editor\"\n+\t\"github.com/kisielk/vigo/buffer\"\n+\t\"github.com/kisielk/vigo/view\"\n+\t\"github.com/kisielk/vigo/utils\"\n+\t\"github.com/nsf/termbox-go\"\n+)\n+\n+type visualMode struct {\n+\teditor *editor.Editor\n+\tcount  string\n+\tlineMode bool\n+}\n+\n+func NewVisualMode(e *editor.Editor, lineMode bool) *visualMode {\n+\tm := visualMode{editor: e, lineMode: lineMode}\n+\tv := m.editor.ActiveView()\n+\tc := v.Cursor()\n+\n+\tstartPos, endPos := 0, 0\n+\n+\tif lineMode {\n+\t\tm.editor.SetStatus(\"Visual Line\")\n+\t} else {\n+\t\tstartPos = c.Boffset\n+\t\tendPos = c.Boffset\n+\n+\t\tm.editor.SetStatus(\"Visual\")\n+\t}\n+\n+\tviewTag := view.NewViewTag(\n+\t\tc.LineNum,\n+\t\tstartPos,\n+\t\tc.LineNum,\n+\t\tendPos,\n+\t\ttermbox.ColorDefault,\n+\t\ttermbox.ColorDefault|termbox.AttrReverse,\n+\t)\n+\n+\tv.VisualRange = &viewTag\n+\n+\treturn &m\n+}\n+\n+func (m *visualMode) OnKey(ev *termbox.Event) {\n+\n+\t// Consequtive non-zero digits specify action multiplier;\n+\t// accumulate and return. Accept zero only if it's\n+\t// a non-starting character.\n+\tif ('0' < ev.Ch && ev.Ch <= '9') || (ev.Ch == '0' && len(m.count) > 0) {\n+\t\tm.count = m.count + string(ev.Ch)\n+\t\tm.editor.SetStatus(m.count)\n+\t\treturn\n+\t}\n+\tcount := utils.ParseCount(m.count)\n+\tif count == 0 {\n+\t\tcount = 1\n+\t}\n+\tg := m.editor\n+\tv := g.ActiveView()\n+\tc := v.Cursor()\n+\tvRange := v.VisualRange\n+\n+\tstartLine, startPos := vRange.StartPos()\n+\tendLine, endPos := vRange.EndPos()\n+\n+\tswitch ev.Key {\n+\tcase termbox.KeyEsc:\n+\t\tm.editor.SetMode(NewNormalMode(m.editor))\n+\t}\n+\n+\tswitch ev.Ch {\n+\tcase 'h':\n+\t\tif !m.lineMode {\n+\t\t\tif c.Boffset >= startPos || c.LineNum != startLine {\n+\t\t\t\t// cursor is right of the start of the selection\n+\t\t\t\t// or anywhere on the line other that the start line\n+\t\t\t\t// move the end of the selection left\n+\t\t\t\tvRange.AdjustEndOffset(-count)\n+\t\t\t} else {\n+\t\t\t\t// cursor is anywhere on the start line\n+\t\t\t\t// move the start of the selection left\n+\t\t\t\tvRange.AdjustStartOffset(-count)\n+\t\t\t}\n+\n+\t\t\tv.VisualRange = vRange\n+\t\t}\n+\t\tg.Commands <- cmd.Repeat{cmd.MoveRune{Dir: cmd.Backward}, count}\n+\tcase 'j':\n+\t\tif c.LineNum >= endLine {\n+\t\t\t// cursor is below the end of the selection\n+\t\t\t// move the end of the selection down\n+\t\t\tvRange.AdjustEndLine(count)\n+\t\t} else {\n+\t\t\t// cursor is above the selection\n+\t\t\t// move the start of the selection down\n+\t\t\tvRange.AdjustStartLine(count)\n+\t\t}\n+\n+\t\t// cursor is one line above the selection and further\n+\t\t// along the line than the start of the selection\n+\t\t// flip the start and end offsets of the selection\n+\t\tif c.Boffset > endPos && c.LineNum == endLine -1 {\n+\t\t\tvRange.FlipStartAndEndOffsets()\n+\t\t}\n+\n+\t\t// cursor is on the same line as the entire selection, and left of the end of\n+\t\t// the selection.\n+\t\t// flip the offsets\n+\t\tif c.LineNum == endLine && c.LineNum == startLine && c.Boffset < endPos {\n+\t\t\tvRange.FlipStartAndEndOffsets()\n+\t\t}\n+\n+\t\tv.VisualRange = vRange\n+\t\tg.Commands <- cmd.Repeat{cmd.MoveLine{Dir: cmd.Forward}, count}\n+\tcase 'k':\n+\t\tif c.LineNum <= startLine {\n+\t\t\t// cursor is above the start of the selection\n+\t\t\t// move the start of the selection up\n+\t\t\tvRange.AdjustStartLine(-count)\n+\t\t} else {\n+\t\t\t// cursor is below the start of the selection\n+\t\t\t// move the end of the selection up\n+\t\t\tvRange.AdjustEndLine(-count)\n+\t\t}\n+\n+\t\t// cursor if right of the start of the selection and above\n+\t\t// flip the start and end offsets of the selection\n+\t\tif c.Boffset > startPos && c.LineNum <= startLine {\n+\t\t\tvRange.FlipStartAndEndOffsets()\n+\t\t}\n+\t\t\n+\t\t// cursor is left of the start of the selection\n+\t\t// and one line below.\n+\t\t// flip the offsets\n+\t\tif c.Boffset < startPos && c.LineNum == startLine +1 {\n+\t\t\tvRange.FlipStartAndEndOffsets()\n+\t\t}\n+\n+\t\tv.VisualRange = vRange\n+\t\tg.Commands <- cmd.Repeat{cmd.MoveLine{Dir: cmd.Backward}, count}\n+\tcase 'l':\n+\t\tif !m.lineMode {\n+\t\t\tif c.Boffset >= endPos && c.LineNum == endLine {\n+\t\t\t\t// cursor is right of the end of the selection and on the same line\n+\t\t\t\t// move the end of the selection right\n+\t\t\t\tvRange.AdjustEndOffset(count)\n+\t\t\t} else {\n+\t\t\t\t// cursor is left of the selection on any line\n+\t\t\t\t// move the start of the selection right\n+\t\t\t\tvRange.AdjustStartOffset(count)\n+\t\t\t}\n+\n+\t\t\tv.VisualRange = vRange\n+\t\t}\n+\t\tg.Commands <- cmd.Repeat{cmd.MoveRune{Dir: cmd.Forward}, count}\n+\tcase 'd':\n+\t\tstart, end := getVisualSelection(v)\n+\t\tv.Buffer().DeleteRange(start, end)\n+\t\tm.editor.SetMode(NewNormalMode(m.editor))\n+\tcase 'v':\n+\t\tm.editor.SetMode(NewNormalMode(m.editor))\n+\tcase 'V':\n+\t\tif m.lineMode {\n+\t\t\tm.editor.SetMode(NewNormalMode(m.editor))\n+\t\t} else {\n+\t\t\tv.VisualRange.SetStartOffset(0)\n+\t\t\tv.VisualRange.SetEndOffset(len(c.Line.Data))\n+\t\t}\n+\t}\n+\n+\t// FIXME: there must be a better way of doing this\n+\t// trigger a re-draw\n+\tg.Resize()","path":"mode/visual.go","position":177,"original_position":177,"commit_id":"983f7d43c5c14c730f961ea065b9ad1cee660b33","original_commit_id":"983f7d43c5c14c730f961ea065b9ad1cee660b33","user":{"login":"kisielk","id":58618,"avatar_url":"https://avatars.githubusercontent.com/u/58618?","gravatar_id":"55fd56dfef815d7aa543be09ad3ed3e9","url":"https://api.github.com/users/kisielk","html_url":"https://github.com/kisielk","followers_url":"https://api.github.com/users/kisielk/followers","following_url":"https://api.github.com/users/kisielk/following{/other_user}","gists_url":"https://api.github.com/users/kisielk/gists{/gist_id}","starred_url":"https://api.github.com/users/kisielk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kisielk/subscriptions","organizations_url":"https://api.github.com/users/kisielk/orgs","repos_url":"https://api.github.com/users/kisielk/repos","events_url":"https://api.github.com/users/kisielk/events{/privacy}","received_events_url":"https://api.github.com/users/kisielk/received_events","type":"User","site_admin":false},"body":"Okay, I figured it. `editor.Resize` sets the dirty flag on all the views, so they are redrawn the next time. `editor.Draw` will only redraw dirty flags, so I think the right thing to do would be to set the dirty flag on the view and then redraw just the view. There's no APIs exposed from the views for this yet though, so I think I'll get to it later. You can just leave this with the FIXME.","created_at":"2014-05-30T16:34:45Z","updated_at":"2014-05-30T16:34:45Z","html_url":"https://github.com/kisielk/vigo/pull/25#discussion_r13237213","pull_request_url":"https://api.github.com/repos/kisielk/vigo/pulls/25","_links":{"self":{"href":"https://api.github.com/repos/kisielk/vigo/pulls/comments/13237213"},"html":{"href":"https://github.com/kisielk/vigo/pull/25#discussion_r13237213"},"pull_request":{"href":"https://api.github.com/repos/kisielk/vigo/pulls/25"}}}}