From a0c359bf66b90783c7f6996ab46ea01e3d303e0d Mon Sep 17 00:00:00 2001 From: Michal Humpula Date: Sun, 18 Nov 2012 13:33:07 +0100 Subject: [PATCH 01/25] Use vim standard highlight --- README.md | 18 ++++--------- plugin/flog.vim | 68 ++++++++++--------------------------------------- 2 files changed, 19 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index 4d09bdc..90d6687 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ Vim Flog Plugin =============== -_(Forked from [Ruby Cyclomatic Complexity Plugin](https://github.com/skammer/vim-ruby-complexity) created by @skammer)_ +Personal version of fousa/vim-flog plugin. Indicates the Flog number in front of the action in your Ruby code to indicate the complexity. @@ -36,19 +36,11 @@ When this is done add `:silent exe "g:flog_enable"` to your .vimrc file. Configuration ------------- -You can set the colors for the complexity indication with the following commands in your .vimrc: +To change colors of sings, use the `higlight` command. The used groups are -* Set the color of for low complexity:
- `:silent exe "let g:flog_low_color=#a5c261"` - -* Set the color of for medium complexity:
- `:silent exe "let g:flog_medium_color=#ffc66d"` - -* Set the color of for high complexity:
- `:silent exe "let g:flog_high_color=#cc7833"` - -* Set the background color:
- `:silent exe "let g:flog_background_color=#323232"` +* SignLowComplexity +* SignMediumComplexity +* SignHighComplexity You can set the limits for the complexity indication with the following commands in your .vimrc: diff --git a/plugin/flog.vim b/plugin/flog.vim index b760909..3ea92b9 100644 --- a/plugin/flog.vim +++ b/plugin/flog.vim @@ -9,29 +9,9 @@ if !has('signs') || !has('ruby') finish endif -let s:low_color = "#a5c261" -let s:medium_color = "#ffc66d" -let s:high_color = "#cc7833" -let s:background_color = "#323232" let s:medium_limit = 10 let s:high_limit = 20 -if exists("g:flog_low_color") - let s:low_color = g:flog_low_color -endif - -if exists("g:flog_medium_color") - let s:medium_color = g:flog_medium_color -endif - -if exists("g:flog_high_color") - let s:high_color = g:flog_high_color -endif - -if exists("g:flog_background_color") - let s:background_color = g:flog_background_color -endif - if exists("g:flog_medium_limit") let s:medium_limit = g:flog_medium_limit endif @@ -128,53 +108,33 @@ def show_complexity(results = {}) medium_limit = VIM::evaluate('s:medium_limit') high_limit = VIM::evaluate('s:high_limit') complexity = case rest[0] - when 0..medium_limit then "low_complexity" - when medium_limit..high_limit then "medium_complexity" - else "high_complexity" + when 0..medium_limit then "LowComplexity" + when medium_limit..high_limit then "MediumComplexity" + else "HighComplexity" end value = rest[0].to_i value = "9+" if value >= 100 - VIM.command ":sign define #{value.to_s} text=#{value.to_s} texthl=#{complexity}" - VIM.command ":sign place #{line_number} line=#{line_number} name=#{value.to_s} file=#{VIM::Buffer.current.name}" + VIM.command ":sign define l#{value.to_s} text=#{value.to_s} texthl=Sign#{complexity}" + VIM.command ":sign place #{line_number} line=#{line_number} name=l#{value.to_s} file=#{VIM::Buffer.current.name}" end end EOF -function! s:UpdateHighlighting() - exe 'hi low_complexity guifg='.s:low_color - exe 'hi medium_complexity guifg='.s:medium_color - exe 'hi high_complexity guifg='.s:high_color - exe 'hi SignColumn guifg=#999999 guibg='.s:background_color.' gui=NONE' -endfunction - function! ShowComplexity() - ruby << EOF - -options = { - :quiet => true, - :continue => true, - :all => true - } - -flogger = Flog.new options -flogger.flog ::VIM::Buffer.current.name -show_complexity flogger.return_report - + options = { + :quiet => true, + :continue => true, + :all => true + } + + flogger = Flog.new options + flogger.flog ::VIM::Buffer.current.name + show_complexity flogger.return_report EOF - -call s:UpdateHighlighting() - endfunction -call s:UpdateHighlighting() - -sign define low_color text=XX texthl=low_complexity -sign define medium_color text=XX texthl=medium_complexity -sign define high_color text=XX texthl=high_complexity - - if !exists("g:flow_enable") || g:flog_enable autocmd! BufReadPost,BufWritePost,FileReadPost,FileWritePost *.rb call ShowComplexity() endif From fecfe4fe57cb20a45b1d6a41c67530f775ab4cf6 Mon Sep 17 00:00:00 2001 From: Michal Humpula Date: Sun, 18 Nov 2012 13:36:40 +0100 Subject: [PATCH 02/25] Make it work with Nerdtree --- plugin/flog.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/flog.vim b/plugin/flog.vim index 3ea92b9..4b20acf 100644 --- a/plugin/flog.vim +++ b/plugin/flog.vim @@ -136,5 +136,5 @@ EOF endfunction if !exists("g:flow_enable") || g:flog_enable - autocmd! BufReadPost,BufWritePost,FileReadPost,FileWritePost *.rb call ShowComplexity() + au bufnewfile,bufread *.rb call ShowComplexity() endif From caf872c5e764866dc4e7742c43540c2d298febd9 Mon Sep 17 00:00:00 2001 From: Michal Humpula Date: Sun, 18 Nov 2012 13:59:56 +0100 Subject: [PATCH 03/25] Update to new flog codebase, fixes minitest dsl --- plugin/flog.vim | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/plugin/flog.vim b/plugin/flog.vim index 4b20acf..4907081 100644 --- a/plugin/flog.vim +++ b/plugin/flog.vim @@ -26,6 +26,7 @@ require 'rubygems' require 'flog' class Flog + # MONKEY: endline addition def in_method(name, file, line, endline=nil) endline = line if endline.nil? method_name = Regexp === name ? name.inspect : name.to_s @@ -35,6 +36,7 @@ class Flog @method_stack.shift end + # MONKEY: exp.last.line addition def process_defn(exp) in_method exp.shift, exp.file, exp.line, exp.last.line do process_until_empty exp @@ -42,6 +44,7 @@ class Flog s() end + # MONKEY: exp.last.line addition def process_defs(exp) recv = process exp.shift in_method "::#{exp.shift}", exp.file, exp.line, exp.last.line do @@ -58,25 +61,31 @@ class Flog recv = exp.first # DSL w/ names. eg task :name do ... end - if (recv[0] == :call and recv[1] == nil and recv.arglist[1] and - [:lit, :str].include? recv.arglist[1][0]) then - msg = recv[2] - submsg = recv.arglist[1][1] - in_klass msg do - lastline = exp.last.respond_to?(:line) ? exp.last.line : nil # zomg teh hax! - # This is really weird. If a block has nothing in it, then for some - # strange reason exp.last becomes nil. I really don't care why this - # happens, just an annoying fact. - in_method submsg, exp.file, exp.line, lastline do - process_until_empty exp - end + # looks like s(:call, nil, :task, s(:lit, :name)) + # or s(:call, nil, :task, s(:str, "name")) + # or s(:call, nil, :task, s(:hash, s(:lit, :name) ...)) + + t, r, m, *a = recv + + if t == :call and r == nil and submsg = dsl_name?(a) then + m = "#{m}(#{submsg})" if m and [String, Symbol].include?(submsg.class) + in_klass m do # :task/namespace + # MONKEY: addition of lastline passing + lastline = exp.last.respond_to?(:line) ? exp.last.line : nil + in_method submsg, exp.file, exp.line, lastline do # :name + process_until_empty exp end - return s() + end + return s() end end + add_to_score :branch - exp.delete 0 - process exp.shift + + exp.delete 0 # { || ... } has 0 in arg slot + + process exp.shift # no penalty for LHS + penalize_by 0.1 do process_until_empty exp end @@ -104,16 +113,17 @@ end def show_complexity(results = {}) VIM.command ":silent sign unplace file=#{VIM::Buffer.current.name}" + medium_limit = VIM::evaluate('s:medium_limit') + high_limit = VIM::evaluate('s:high_limit') + results.each do |line_number, rest| - medium_limit = VIM::evaluate('s:medium_limit') - high_limit = VIM::evaluate('s:high_limit') complexity = case rest[0] when 0..medium_limit then "LowComplexity" when medium_limit..high_limit then "MediumComplexity" else "HighComplexity" end value = rest[0].to_i - value = "9+" if value >= 100 + value = "9+" if value >= 99 VIM.command ":sign define l#{value.to_s} text=#{value.to_s} texthl=Sign#{complexity}" VIM.command ":sign place #{line_number} line=#{line_number} name=l#{value.to_s} file=#{VIM::Buffer.current.name}" end From bb8fb5bda5380deb06d79d07fac536ae18b3c996 Mon Sep 17 00:00:00 2001 From: Michal Humpula Date: Sun, 18 Nov 2012 14:39:12 +0100 Subject: [PATCH 04/25] We realy don't need endofmethod anymore --- plugin/flog.vim | 79 ++++--------------------------------------------- 1 file changed, 5 insertions(+), 74 deletions(-) diff --git a/plugin/flog.vim b/plugin/flog.vim index 4907081..09e51b7 100644 --- a/plugin/flog.vim +++ b/plugin/flog.vim @@ -26,83 +26,14 @@ require 'rubygems' require 'flog' class Flog - # MONKEY: endline addition - def in_method(name, file, line, endline=nil) - endline = line if endline.nil? - method_name = Regexp === name ? name.inspect : name.to_s - @method_stack.unshift method_name - @method_locations[signature] = "#{file}:#{line}:#{endline}" - yield - @method_stack.shift - end - - # MONKEY: exp.last.line addition - def process_defn(exp) - in_method exp.shift, exp.file, exp.line, exp.last.line do - process_until_empty exp - end - s() - end - - # MONKEY: exp.last.line addition - def process_defs(exp) - recv = process exp.shift - in_method "::#{exp.shift}", exp.file, exp.line, exp.last.line do - process_until_empty exp - end - s() - end - - def process_iter(exp) - context = (self.context - [:class, :module, :scope]) - context = context.uniq.sort_by { |s| s.to_s } - - if context == [:block, :iter] or context == [:iter] then - recv = exp.first - - # DSL w/ names. eg task :name do ... end - # looks like s(:call, nil, :task, s(:lit, :name)) - # or s(:call, nil, :task, s(:str, "name")) - # or s(:call, nil, :task, s(:hash, s(:lit, :name) ...)) - - t, r, m, *a = recv - - if t == :call and r == nil and submsg = dsl_name?(a) then - m = "#{m}(#{submsg})" if m and [String, Symbol].include?(submsg.class) - in_klass m do # :task/namespace - # MONKEY: addition of lastline passing - lastline = exp.last.respond_to?(:line) ? exp.last.line : nil - in_method submsg, exp.file, exp.line, lastline do # :name - process_until_empty exp - end - end - return s() - end - end - - add_to_score :branch - - exp.delete 0 # { || ... } has 0 in arg slot - - process exp.shift # no penalty for LHS - - penalize_by 0.1 do - process_until_empty exp - end - s() - end - def return_report complexity_results = {} max = option[:all] ? nil : total * THRESHOLD each_by_score max do |class_method, score, call_list| location = @method_locations[class_method] if location then - line, endline = location.match(/.+:(\d+):(\d+)/).to_a[1..2].map{|l| l.to_i } - # This is a strange case of flog failing on blocks. - # http://blog.zenspider.com/2009/04/parsetree-eol.html - line, endline = endline-1, line if line >= endline - complexity_results[line] = [score, class_method, endline] + line = location.match(/.+:(\d+)/).to_a[1] + complexity_results[line] = [score, class_method] end end complexity_results @@ -116,13 +47,13 @@ def show_complexity(results = {}) medium_limit = VIM::evaluate('s:medium_limit') high_limit = VIM::evaluate('s:high_limit') - results.each do |line_number, rest| - complexity = case rest[0] + results.each do |line_number, (score, method)| + complexity = case score when 0..medium_limit then "LowComplexity" when medium_limit..high_limit then "MediumComplexity" else "HighComplexity" end - value = rest[0].to_i + value = score.to_i value = "9+" if value >= 99 VIM.command ":sign define l#{value.to_s} text=#{value.to_s} texthl=Sign#{complexity}" VIM.command ":sign place #{line_number} line=#{line_number} name=l#{value.to_s} file=#{VIM::Buffer.current.name}" From 0677eea04b851bb3650c7f3a6048db30397750e0 Mon Sep 17 00:00:00 2001 From: Michal Humpula Date: Sun, 18 Nov 2012 19:45:20 +0100 Subject: [PATCH 05/25] update complexity when leaving insert mode --- plugin/flog.vim | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/plugin/flog.vim b/plugin/flog.vim index 09e51b7..59889ce 100644 --- a/plugin/flog.vim +++ b/plugin/flog.vim @@ -26,6 +26,21 @@ require 'rubygems' require 'flog' class Flog + def flog_snippet(code, file = __FILE__) + @parser = RubyParser.new + + begin + ast = @parser.process(code, file) + rescue Timeout::Error + warn "TIMEOUT parsing #{file}. Skipping." + return + end + + mass[file] = ast.mass + process ast + rescue RubyParser::SyntaxError, Racc::ParseError => e + end + def return_report complexity_results = {} max = option[:all] ? nil : total * THRESHOLD @@ -53,10 +68,9 @@ def show_complexity(results = {}) when medium_limit..high_limit then "MediumComplexity" else "HighComplexity" end - value = score.to_i - value = "9+" if value >= 99 - VIM.command ":sign define l#{value.to_s} text=#{value.to_s} texthl=Sign#{complexity}" - VIM.command ":sign place #{line_number} line=#{line_number} name=l#{value.to_s} file=#{VIM::Buffer.current.name}" + value = score >= 100 ? "9+" : score.to_i + VIM.command ":sign define #{value} text=#{value} texthl=Sign#{complexity}" + VIM.command ":sign place #{line_number} line=#{line_number} name=#{value} file=#{VIM::Buffer.current.name}" end end @@ -66,16 +80,20 @@ function! ShowComplexity() ruby << EOF options = { :quiet => true, - :continue => true, :all => true } + buffer = ::VIM::Buffer.current + # nasty hack, but there is no read all... + code = (1..buffer.count).map{|i| buffer[i]}.join("\n") + flogger = Flog.new options - flogger.flog ::VIM::Buffer.current.name + flogger.flog_snippet code, buffer.name + #flogger.flog ::VIM::Buffer.current.name show_complexity flogger.return_report EOF endfunction if !exists("g:flow_enable") || g:flog_enable - au bufnewfile,bufread *.rb call ShowComplexity() + au bufnewfile,bufread,InsertLeave *.rb call ShowComplexity() endif From c3db326a4fe0491ce488b62f5e96ff71ab052043 Mon Sep 17 00:00:00 2001 From: Michal Humpula Date: Mon, 19 Nov 2012 10:09:10 +0100 Subject: [PATCH 06/25] silent error handling --- plugin/flog.vim | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/plugin/flog.vim b/plugin/flog.vim index 59889ce..00163c8 100644 --- a/plugin/flog.vim +++ b/plugin/flog.vim @@ -21,7 +21,7 @@ if exists("g:flog_high_limit") endif ruby << EOF - +$VERBOSE = nil # turn of those pesky warnings... require 'rubygems' require 'flog' @@ -31,14 +31,15 @@ class Flog begin ast = @parser.process(code, file) - rescue Timeout::Error - warn "TIMEOUT parsing #{file}. Skipping." - return + rescue + return false end mass[file] = ast.mass process ast + return true rescue RubyParser::SyntaxError, Racc::ParseError => e + return false end def return_report @@ -88,9 +89,10 @@ ruby << EOF code = (1..buffer.count).map{|i| buffer[i]}.join("\n") flogger = Flog.new options - flogger.flog_snippet code, buffer.name + if flogger.flog_snippet code, buffer.name #flogger.flog ::VIM::Buffer.current.name - show_complexity flogger.return_report + show_complexity flogger.return_report + end EOF endfunction From 784835d2a2d2ffbe248f3201542ee752eb986d12 Mon Sep 17 00:00:00 2001 From: Michal Humpula Date: Tue, 20 Nov 2012 18:21:04 +0100 Subject: [PATCH 07/25] fix signs unplacing --- plugin/flog.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/flog.vim b/plugin/flog.vim index 00163c8..5cebf31 100644 --- a/plugin/flog.vim +++ b/plugin/flog.vim @@ -59,7 +59,7 @@ class Flog end def show_complexity(results = {}) - VIM.command ":silent sign unplace file=#{VIM::Buffer.current.name}" + VIM.command ":silent sign unplace *" medium_limit = VIM::evaluate('s:medium_limit') high_limit = VIM::evaluate('s:high_limit') From 7d7007760f13ef85ada652ef9c79b41545270b26 Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Fri, 1 Mar 2013 12:55:44 -0800 Subject: [PATCH 08/25] Disable if flog isn't installed on the machine --- plugin/flog.vim | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/plugin/flog.vim b/plugin/flog.vim index 5cebf31..8272999 100644 --- a/plugin/flog.vim +++ b/plugin/flog.vim @@ -22,8 +22,13 @@ endif ruby << EOF $VERBOSE = nil # turn of those pesky warnings... -require 'rubygems' -require 'flog' +begin + require 'rubygems' + require 'flog' + FLOG_LOADED = true +rescue LoadError + FLOG_LOADED = false +end class Flog def flog_snippet(code, file = __FILE__) @@ -84,14 +89,16 @@ ruby << EOF :all => true } - buffer = ::VIM::Buffer.current - # nasty hack, but there is no read all... - code = (1..buffer.count).map{|i| buffer[i]}.join("\n") + if FLOG_LOADED + buffer = ::VIM::Buffer.current + # nasty hack, but there is no read all... + code = (1..buffer.count).map{|i| buffer[i]}.join("\n") - flogger = Flog.new options - if flogger.flog_snippet code, buffer.name - #flogger.flog ::VIM::Buffer.current.name - show_complexity flogger.return_report + flogger = Flog.new options + if flogger.flog_snippet code, buffer.name + #flogger.flog ::VIM::Buffer.current.name + show_complexity flogger.return_report + end end EOF endfunction From f12cee89972b7531f9f8b1fcdb0b8ba806ef9a57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20VANTOMME?= Date: Tue, 13 Aug 2013 13:00:19 +0200 Subject: [PATCH 09/25] fix typo --- plugin/flog.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/flog.vim b/plugin/flog.vim index 8272999..8da2e6b 100644 --- a/plugin/flog.vim +++ b/plugin/flog.vim @@ -103,6 +103,6 @@ ruby << EOF EOF endfunction -if !exists("g:flow_enable") || g:flog_enable +if !exists("g:flog_enable") || g:flog_enable au bufnewfile,bufread,InsertLeave *.rb call ShowComplexity() endif From e9c3cfaa15bc6f662ddc21678c285338aedeb221 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20VANTOMME?= Date: Tue, 13 Aug 2013 14:34:12 +0200 Subject: [PATCH 10/25] add commands FlogEnable, FlogDisable and FlogToggle --- plugin/flog.vim | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/plugin/flog.vim b/plugin/flog.vim index 8da2e6b..2202765 100644 --- a/plugin/flog.vim +++ b/plugin/flog.vim @@ -103,6 +103,42 @@ ruby << EOF EOF endfunction +function! HideComplexity() +ruby << EOF + if FLOG_LOADED + VIM.command ":redir @a" + VIM.command ":silent sign place file=#{VIM::Buffer.current.name}" + VIM.command ":redir END" + placed_signs = VIM.evaluate "@a" + placed_signs.lines.map(&:chomp).select{|s| s.include? 'id='}.each do |sign| + id = Hash[*sign.split(' ').map{|s| s.split('=')}.flatten]['id'] + VIM.command ":sign unplace #{id} file=#{VIM::Buffer.current.name}" + end + end +EOF +endfunction + +function! FlogDisable() + let g:flog_enable = 0 + call HideComplexity() +endfunction +command FlogDisable call FlogDisable() + +function! FlogEnable() + let g:flog_enable = 1 + call ShowComplexity() +endfunction +command FlogEnable call FlogEnable() + +function! FlogToggle() + if g:flog_enabled + call FlogDisable() + else + call FlogEnable() + endif +endfunction +command FlogToggle call FlogToggle() + if !exists("g:flog_enable") || g:flog_enable au bufnewfile,bufread,InsertLeave *.rb call ShowComplexity() endif From aa5c80174344382598b56d53d7a438aa4cbb71e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20VANTOMME?= Date: Tue, 13 Aug 2013 19:20:52 +0200 Subject: [PATCH 11/25] update complexity after saving the buffer --- plugin/flog.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/flog.vim b/plugin/flog.vim index 2202765..f3279ea 100644 --- a/plugin/flog.vim +++ b/plugin/flog.vim @@ -140,5 +140,5 @@ endfunction command FlogToggle call FlogToggle() if !exists("g:flog_enable") || g:flog_enable - au bufnewfile,bufread,InsertLeave *.rb call ShowComplexity() + au BufNewFile,BufRead,BufWritePost,InsertLeave *.rb call ShowComplexity() endif From 96e7e52d9889980777cf2655d3ecdd0114ca0d51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20VANTOMME?= Date: Thu, 22 Aug 2013 15:45:32 +0200 Subject: [PATCH 12/25] simplify return_report def --- plugin/flog.vim | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugin/flog.vim b/plugin/flog.vim index f3279ea..9565ecb 100644 --- a/plugin/flog.vim +++ b/plugin/flog.vim @@ -49,8 +49,7 @@ class Flog def return_report complexity_results = {} - max = option[:all] ? nil : total * THRESHOLD - each_by_score max do |class_method, score, call_list| + each_by_score threshold do |class_method, score, call_list| location = @method_locations[class_method] if location then line = location.match(/.+:(\d+)/).to_a[1] From 5588e78c116fa2a861301fad2ecb1f227db63107 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20VANTOMME?= Date: Thu, 22 Aug 2013 15:51:02 +0200 Subject: [PATCH 13/25] fix FlogToggle function --- plugin/flog.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/flog.vim b/plugin/flog.vim index 9565ecb..ee90350 100644 --- a/plugin/flog.vim +++ b/plugin/flog.vim @@ -130,7 +130,7 @@ endfunction command FlogEnable call FlogEnable() function! FlogToggle() - if g:flog_enabled + if exists("g:flog_enable") && g:flog_enable call FlogDisable() else call FlogEnable() From 2bb08d670aa3bf8aee21ae79de994e440fbe9f4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20VANTOMME?= Date: Thu, 22 Aug 2013 15:55:48 +0200 Subject: [PATCH 14/25] oops ! Why was this line commented out ? --- plugin/flog.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/flog.vim b/plugin/flog.vim index ee90350..d1da470 100644 --- a/plugin/flog.vim +++ b/plugin/flog.vim @@ -95,7 +95,7 @@ ruby << EOF flogger = Flog.new options if flogger.flog_snippet code, buffer.name - #flogger.flog ::VIM::Buffer.current.name + flogger.flog ::VIM::Buffer.current.name show_complexity flogger.return_report end end From ea378300d6c8f1ec4a32296b6998f32812c42a44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20VANTOMME?= Date: Thu, 22 Aug 2013 16:15:50 +0200 Subject: [PATCH 15/25] fix empty .rb file opening --- plugin/flog.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/flog.vim b/plugin/flog.vim index d1da470..6192cb7 100644 --- a/plugin/flog.vim +++ b/plugin/flog.vim @@ -35,7 +35,7 @@ class Flog @parser = RubyParser.new begin - ast = @parser.process(code, file) + return false unless ast = @parser.process(code, file) rescue return false end From 9d7b46e20c14abc6a9a57b53507adae0c151acd8 Mon Sep 17 00:00:00 2001 From: Plehanov Dmitriy Date: Sun, 8 Sep 2013 21:24:48 +0400 Subject: [PATCH 16/25] fix strange bug when vim said commands already here --- plugin/flog.vim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugin/flog.vim b/plugin/flog.vim index 6192cb7..5153a05 100644 --- a/plugin/flog.vim +++ b/plugin/flog.vim @@ -121,13 +121,13 @@ function! FlogDisable() let g:flog_enable = 0 call HideComplexity() endfunction -command FlogDisable call FlogDisable() +command! FlogDisable call FlogDisable() function! FlogEnable() let g:flog_enable = 1 call ShowComplexity() endfunction -command FlogEnable call FlogEnable() +command! FlogEnable call FlogEnable() function! FlogToggle() if exists("g:flog_enable") && g:flog_enable @@ -136,7 +136,7 @@ function! FlogToggle() call FlogEnable() endif endfunction -command FlogToggle call FlogToggle() +command! FlogToggle call FlogToggle() if !exists("g:flog_enable") || g:flog_enable au BufNewFile,BufRead,BufWritePost,InsertLeave *.rb call ShowComplexity() From b05ffd4ed51193137449fd709dfcc86658d0a61e Mon Sep 17 00:00:00 2001 From: Vladimir Rybas Date: Tue, 16 Sep 2014 12:11:44 +0700 Subject: [PATCH 17/25] Update complexity only on `BufRead, BufWritePost` That's a trade-off between speed & convenience --- plugin/flog.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/flog.vim b/plugin/flog.vim index 5153a05..e13a920 100644 --- a/plugin/flog.vim +++ b/plugin/flog.vim @@ -139,5 +139,5 @@ endfunction command! FlogToggle call FlogToggle() if !exists("g:flog_enable") || g:flog_enable - au BufNewFile,BufRead,BufWritePost,InsertLeave *.rb call ShowComplexity() + au BufRead,BufWritePost *.rb call ShowComplexity() endif From bed787aee45d56bbce454b9cdd585f102999eafa Mon Sep 17 00:00:00 2001 From: Vladimir Rybas Date: Wed, 17 Sep 2014 04:45:30 +0700 Subject: [PATCH 18/25] Unplace signs only in current buffer Commit 784835d introduced a bug when you got multiple buffers opened in multiple windows. If you unplace signs in all opened windows, they're going to be placed back by `show_complexity()` only in current window. But all other windows will jump for a few pixels to the left, which is ugly. The proper fix for original bug (when even current buffer signs weren't unplaced) would be to use `*` together with `file`: ``` :sign unplace * file={fname} Remove all placed signs in file {fname}. ``` --- plugin/flog.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/flog.vim b/plugin/flog.vim index e13a920..15e0a9c 100644 --- a/plugin/flog.vim +++ b/plugin/flog.vim @@ -63,7 +63,7 @@ class Flog end def show_complexity(results = {}) - VIM.command ":silent sign unplace *" + VIM.command ":silent sign unplace * file=#{VIM::Buffer.current.name}" medium_limit = VIM::evaluate('s:medium_limit') high_limit = VIM::evaluate('s:high_limit') From ec742af3bdff06029e6aa8cb379f0834f8d2969c Mon Sep 17 00:00:00 2001 From: Vladimir Rybas Date: Wed, 17 Sep 2014 05:13:14 +0700 Subject: [PATCH 19/25] Do not run `show_complexity()` on '_spec' files --- plugin/flog.vim | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugin/flog.vim b/plugin/flog.vim index 15e0a9c..61a26e8 100644 --- a/plugin/flog.vim +++ b/plugin/flog.vim @@ -83,12 +83,14 @@ EOF function! ShowComplexity() ruby << EOF + ignore_files = /_spec/ + options = { :quiet => true, :all => true } - if FLOG_LOADED + if !Vim::Buffer.current.name.match(ignore_files) && FLOG_LOADED buffer = ::VIM::Buffer.current # nasty hack, but there is no read all... code = (1..buffer.count).map{|i| buffer[i]}.join("\n") From ffc282f526f8600799a6007d51d029a1a7b0b092 Mon Sep 17 00:00:00 2001 From: alexbel Date: Tue, 16 Sep 2014 16:30:35 -0400 Subject: [PATCH 20/25] Add ability to hide low and middle coplexity levels (via @alexbel) + refactor Also set `FlogDummySign` to avoid sign column 'jumps' on files, where no signs have been set. --- README.md | 8 ++++++++ plugin/flog.vim | 26 ++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 90d6687..a36df8b 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,14 @@ You can set the limits for the complexity indication with the following commands * Set the limit to switch to a high complexity:
:silent exe "`let g:flog_high_limit=20"` +You can hide some levels of complexity: + +* Hide low complexity:
+ `:silent exe "let g:flog_hide_low=1"` + +* Hide medium complexity:
+ `:silent exe "let g:flog_hide_medium=1"` + Credits ------- diff --git a/plugin/flog.vim b/plugin/flog.vim index 61a26e8..0b5a065 100644 --- a/plugin/flog.vim +++ b/plugin/flog.vim @@ -11,6 +11,16 @@ endif let s:medium_limit = 10 let s:high_limit = 20 +let s:hide_low = 0 +let s:hide_medium = 0 + +if exists("g:flog_hide_low") + let s:hide_low = g:flog_hide_low +endif + +if exists("g:flog_hide_medium") + let s:hide_medium = g:flog_hide_medium +endif if exists("g:flog_medium_limit") let s:medium_limit = g:flog_medium_limit @@ -63,9 +73,14 @@ class Flog end def show_complexity(results = {}) - VIM.command ":silent sign unplace * file=#{VIM::Buffer.current.name}" medium_limit = VIM::evaluate('s:medium_limit') - high_limit = VIM::evaluate('s:high_limit') + high_limit = VIM::evaluate('s:high_limit') + hide_medium = VIM::evaluate('s:hide_medium') + hide_low = VIM::evaluate('s:hide_low') + + VIM.command ":silent sign unplace * file=#{VIM::Buffer.current.name}" + VIM.command ":sign define FlogDummySign" + VIM.command ":sign place 9999 line=1 name=FlogDummySign file=#{VIM::Buffer.current.name}" results.each do |line_number, (score, method)| complexity = case score @@ -74,8 +89,11 @@ def show_complexity(results = {}) else "HighComplexity" end value = score >= 100 ? "9+" : score.to_i - VIM.command ":sign define #{value} text=#{value} texthl=Sign#{complexity}" - VIM.command ":sign place #{line_number} line=#{line_number} name=#{value} file=#{VIM::Buffer.current.name}" + value = nil if (hide_low == 1 && value < medium_limit) || (hide_medium == 1 && value < high_limit) + if value + VIM.command ":sign define #{value} text=#{value} texthl=Sign#{complexity}" if value + VIM.command ":sign place #{line_number} line=#{line_number} name=#{value} file=#{VIM::Buffer.current.name}" + end end end From f9af84dfe7dc7d1c0ec2480d7517cbc895436b4b Mon Sep 17 00:00:00 2001 From: Vladimir Rybas Date: Wed, 17 Sep 2014 06:30:46 +0700 Subject: [PATCH 21/25] Add README about Enable/Disable/Toggle commands --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index a36df8b..bf5f1ea 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,20 @@ You can hide some levels of complexity: * Hide medium complexity:
`:silent exe "let g:flog_hide_medium=1"` +You can also turn flog off and on: + +* Turn on flog + `:call FlogEnable()` + +* Turn off flog + `:call FlogDisable()` + +* Toggle flog + `:call FlogToggle()` + +Additionally, you can map this in your .vimrc: + `:map ,f :call FlogToggle()` + Credits ------- From 292679934c43ec8da65b9cbfd5f896e98784b102 Mon Sep 17 00:00:00 2001 From: Vladimir Rybas Date: Wed, 17 Sep 2014 09:04:49 +0700 Subject: [PATCH 22/25] Remove duplicated `if` statement --- plugin/flog.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/flog.vim b/plugin/flog.vim index 0b5a065..2ea453d 100644 --- a/plugin/flog.vim +++ b/plugin/flog.vim @@ -91,7 +91,7 @@ def show_complexity(results = {}) value = score >= 100 ? "9+" : score.to_i value = nil if (hide_low == 1 && value < medium_limit) || (hide_medium == 1 && value < high_limit) if value - VIM.command ":sign define #{value} text=#{value} texthl=Sign#{complexity}" if value + VIM.command ":sign define #{value} text=#{value} texthl=Sign#{complexity}" VIM.command ":sign place #{line_number} line=#{line_number} name=#{value} file=#{VIM::Buffer.current.name}" end end From 165fcd364a2a220734403eed16cf81810832f0d5 Mon Sep 17 00:00:00 2001 From: Vladimir Rybas Date: Wed, 17 Sep 2014 22:42:39 +0700 Subject: [PATCH 23/25] Return @skammer to README as original author of `vim-ruby-complexity` --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 87e319a..fe64af6 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ Vim Flog Plugin =============== -Personal version of fousa/vim-flog plugin. +_(Forked from [Ruby Cyclomatic Complexity Plugin](https://github.com/skammer/vim-ruby-complexity) created by @skammer)_ Indicates the Flog number in front of the action in your Ruby code to indicate the complexity. From 3ca6890d738b42b59ce9674b10e5b6e924f78233 Mon Sep 17 00:00:00 2001 From: Vladimir Rybas Date: Tue, 6 Oct 2015 13:16:35 +0700 Subject: [PATCH 24/25] Fix bug when method complexity > 100 Looks like Vim allows to put maximum of 2 symbols in Sign Column. The solution of previous author was to show "9+" as complexity score. But it blows up on very next line with exception: Error detected while processing function ShowComplexity: line 20: ArgumentError: comparison of String with 15 failed The solution is to show maximum of 99 complexity value, even if complexity exceeds 100. --- plugin/flog.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/flog.vim b/plugin/flog.vim index 0718e58..7eef75d 100644 --- a/plugin/flog.vim +++ b/plugin/flog.vim @@ -87,7 +87,7 @@ def show_complexity(results = {}) when medium_limit..high_limit then "MediumComplexity" else "HighComplexity" end - value = score >= 100 ? "9+" : score.to_i + value = score >= 100 ? 99 : score.to_i value = nil if (hide_low == 1 && value < medium_limit) || (hide_medium == 1 && value < high_limit) if value VIM.command ":sign define #{value} text=#{value} texthl=Sign#{complexity}" From 557db126bc2e24709b0771b5f4cf2a7d8e3225ce Mon Sep 17 00:00:00 2001 From: Vladimir Rybas Date: Mon, 22 Feb 2016 21:43:26 +0700 Subject: [PATCH 25/25] Do not use RubyParser. Rely on Flog's internal parsing mechanism. Complexity scores shown in Vim and with flog CLI were radically different. I suspect it's because, instead of analyzing actual Ruby source, RubyParser was analyzing AST instead. --- plugin/flog.vim | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/plugin/flog.vim b/plugin/flog.vim index 7eef75d..9bf478b 100644 --- a/plugin/flog.vim +++ b/plugin/flog.vim @@ -40,25 +40,9 @@ rescue LoadError end class Flog - def flog_snippet(code, file = __FILE__) - @parser = RubyParser.new - - begin - return false unless ast = @parser.process(code, file) - rescue - return false - end - - mass[file] = ast.mass - process ast - return true - rescue RubyParser::SyntaxError, Racc::ParseError => e - return false - end - def return_report complexity_results = {} - each_by_score threshold do |class_method, score, call_list| + each_by_score(threshold) do |class_method, score, call_list| location = @method_locations[class_method] if location then line = location.match(/.+:(\d+)/).to_a[1] @@ -87,7 +71,7 @@ def show_complexity(results = {}) when medium_limit..high_limit then "MediumComplexity" else "HighComplexity" end - value = score >= 100 ? 99 : score.to_i + value = score >= 100 ? 99 : score.round value = nil if (hide_low == 1 && value < medium_limit) || (hide_medium == 1 && value < high_limit) if value VIM.command ":sign define #{value} text=#{value} texthl=Sign#{complexity}" @@ -112,10 +96,9 @@ ruby << EOF code = (1..buffer.count).map{|i| buffer[i]}.join("\n") flogger = Flog.new options - if flogger.flog_snippet code, buffer.name - flogger.flog ::VIM::Buffer.current.name - show_complexity flogger.return_report - end + flogger.flog_ruby `cat #{::VIM::Buffer.current.name}` + flogger.calculate_total_scores + show_complexity flogger.return_report end EOF endfunction