Skip to content

Commit

Permalink
Write log file when "wildfire_debug" is turned on
Browse files Browse the repository at this point in the history
  • Loading branch information
Giacomo Comitti committed Feb 23, 2014
1 parent 5e17a41 commit 9d5cf67
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions plugin/wildfire.vim
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ if exists("g:loaded_wildfire")
endif
let g:loaded_wildfire = 1

let s:pathsep = has("win32") ? "\\" : "/"
let s:logfile = expand("<sfile>:p:h") . s:pathsep . "wildfire.log"
if get(g:, "wildfire_debug", 0)
cal writefile([], s:logfile)
endif

let s:save_cpo = &cpo
set cpo&vim

Expand Down Expand Up @@ -104,6 +110,9 @@ fu! s:Fuel(repeat)

let winview = winsaveview()

cal s:log(repeat("=", 100))
cal s:log("selections history: " . string(s:selections_history))

let candidates = {}
for object in keys(s:counts)

Expand All @@ -113,17 +122,21 @@ fu! s:Fuel(repeat)
let to = extend(to, {"startline": startline, "startcol": startcol,
\ "endline": endline, "endcol": endcol })

cal s:log("considering candidation for: " . string(to))

cal winrestview(winview)

" The selection failed with the candidate text object
if startline == endline && startcol == endcol
cal s:log(" ` failed: no selection can be performed")
continue
endif

" Sometimes Vim selects text objects even if the cursor is outside the
" them (e.g. `it`, `i"`, etc). We don't want this.
let cursor_col = s:origin[2]
if startline == endline && (cursor_col < startcol || cursor_col > endcol)
cal s:log(" ` failed: does not enclose the cursor")
let s:counts[object] += 1
continue
endif
Expand All @@ -133,6 +146,7 @@ fu! s:Fuel(repeat)
" This happens when the _count is incremented but the selection remains still
let _to = extend(copy(to), {"count": to.count-1})
if s:AlreadySelected(_to)
cal s:log(" ` failed: already selected")
continue
endif

Expand All @@ -158,10 +172,12 @@ fu! s:Fuel(repeat)
endif
endif

cal s:log(" ` success: text object size is " . size)
let candidates[size] = to

endfor

cal s:log("candidates: " . string(candidates))
cal s:SelectBestCandidate(candidates)

cal s:Fuel(a:repeat-1)
Expand All @@ -172,6 +188,7 @@ endfu
fu! s:SelectBestCandidate(candidates)
if len(a:candidates)
let to = a:candidates[min(keys(a:candidates))]
cal s:log("winner: " . string(to))
let s:selections_history = add(s:selections_history, to)
let s:counts[to.object] += 1
cal s:Select(to)
Expand Down Expand Up @@ -232,6 +249,14 @@ fu! s:odd_quotes(quote, s)
return n % 2 != 0
endfu

" Debug helpers
" =============================================================================

fu! s:log(msg)
if get(g:, "wildfire_debug", 0)
cal writefile(readfile(s:logfile) + split(a:msg, "\n"), s:logfile)
endif
endfu

" Commands and Mappings
" =============================================================================
Expand Down

0 comments on commit 9d5cf67

Please sign in to comment.