Top Level Namespace

Defined Under Namespace

Modules: WhatToDo

Instance Method Summary collapse

Instance Method Details

#code_sample(file, line) ⇒ Object

Builds a code sample from the given file and linenumber. Will display the 4 lines above and below the relevant line and will highlight the relevant line.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/whatToDo/check/todo.rb', line 26

def code_sample(file, line)
  line = line.to_i

  from = line - 4
  from = 0 if from < 0

  result = "\n\n"

  snippet(file, from, line + 4).split("\n").each do |code_line|
    active = (line == from)
    spaces = " " * (8 - from.to_s.length - (active ? 3 : 0))
    result << spaces + (active ? '>> '.bold.red : '')
    result << from.to_s.bold.yellow + ": ".bold.yellow

    first = true

    if code_line.length > 0
      scan = code_line.scan(/^ +/)[0]
      ident = scan ? scan.length + 12 : 12
      code_line.scan(/.{1,80}/m).each do |l|
        result << (first ? '' : ' ' * ident) + (active ? l.bold.red : l) + "\n"
        first = false
      end
    else
      result << "\n"
    end

    from += 1
  end

  result
end

#get_linesObject

Executes a recursive grep in the current directory to find all todos, and the respecitve filename and linenumber. If there’s a .gitignore that function will ignore all directories and files from .gitignore



7
8
9
10
11
12
13
14
15
16
# File 'lib/whatToDo/check/todo.rb', line 7

def get_lines
  exclude = ''

  if File.file?(".gitignore")
    exclude = `cat .gitignore`.split("\n").map { |l| " | grep -v '^#{l}'" }
    exclude = exclude.join
  end

  `egrep -rnH 'TODO|FIXME|XXX' * | grep -v '^Binary file' #{exclude}`.split("\n")
end

#indent(str, count, char = ' ') ⇒ Object

Helper method to ident a string



2
3
4
5
6
7
8
9
10
11
# File 'lib/whatToDo/util.rb', line 2

def indent(str, count, char = ' ')
  str.gsub(/([^\n]*)(\n|$)/) do |match|
    last_iteration = ($1 == '' && $2 == '')
    line = ''
    line << (char * count) unless last_iteration
    line << $1
    line << $2
    line
  end
end

#snippet(file, from, to) ⇒ Object

Extracs a code snippet from a file and reduced the identation to the minimum



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/whatToDo/check/todo.rb', line 65

def snippet(file, from, to)
  code = ""
  current_line = 0

  File.open(file, 'r') do |f|
    while read_line = f.gets
      current_line += 1
      code << read_line.gsub(/\t/m, '  ') if current_line >= from && current_line <= to
    end
  end

  code.gsub(/^#{code.scan(/^ +/).min}/, '')
end

#unindent(code) ⇒ Object

Reduces the identation of a given string to the minimum



85
86
87
# File 'lib/whatToDo/check/todo.rb', line 85

def unindent(code)
  code.gsub(/^#{scan(/^\s+/).min}/, "")
end