puppet-debugger 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,98 +1,98 @@
1
1
 
2
+ # frozen_string_literal: true
2
3
  class CodeFile
3
- class SourceNotFound < Exception
4
+ class SourceNotFound < RuntimeError
4
5
  end
5
6
 
6
- DEFAULT_EXT = '.pp'
7
-
8
- # List of all supported languages.
9
- # @return [Hash]
10
- EXTENSIONS = {
11
- %w(.py) => :python,
12
- %w(.js) => :javascript,
13
- %w(.pp) => :puppet,
14
- %w(.css) => :css,
15
- %w(.xml) => :xml,
16
- %w(.php) => :php,
17
- %w(.html) => :html,
18
- %w(.diff) => :diff,
19
- %w(.java) => :java,
20
- %w(.json) => :json,
21
- %w(.c .h) => :c,
22
- %w(.rhtml) => :rhtml,
23
- %w(.yaml .yml) => :yaml,
24
- %w(.cpp .hpp .cc .h cxx) => :cpp,
25
- %w(.rb .ru .irbrc .gemspec .pryrc) => :ruby,
26
- }
27
-
28
- # @return [Symbol] The type of code stored in this wrapper.
29
- attr_reader :code_type
30
-
31
- # @param [String] filename The name of a file with code to be detected
32
- # @param [Symbol] code_type The type of code the `filename` contains
33
- def initialize(filename, code_type = type_from_filename(filename))
34
- @filename = filename
35
- @code_type = code_type
36
- end
37
-
38
- # @return [String] The code contained in the current `@filename`.
39
- def code
40
- path = abs_path
41
- @code_type = type_from_filename(path)
42
- File.read(path)
43
- end
7
+ DEFAULT_EXT = '.pp'
8
+
9
+ # List of all supported languages.
10
+ # @return [Hash]
11
+ EXTENSIONS = {
12
+ %w(.py) => :python,
13
+ %w(.js) => :javascript,
14
+ %w(.pp) => :puppet,
15
+ %w(.css) => :css,
16
+ %w(.xml) => :xml,
17
+ %w(.php) => :php,
18
+ %w(.html) => :html,
19
+ %w(.diff) => :diff,
20
+ %w(.java) => :java,
21
+ %w(.json) => :json,
22
+ %w(.c .h) => :c,
23
+ %w(.rhtml) => :rhtml,
24
+ %w(.yaml .yml) => :yaml,
25
+ %w(.cpp .hpp .cc .h cxx) => :cpp,
26
+ %w(.rb .ru .irbrc .gemspec .pryrc) => :ruby
27
+ }.freeze
28
+
29
+ # @return [Symbol] The type of code stored in this wrapper.
30
+ attr_reader :code_type
31
+
32
+ # @param [String] filename The name of a file with code to be detected
33
+ # @param [Symbol] code_type The type of code the `filename` contains
34
+ def initialize(filename, code_type = type_from_filename(filename))
35
+ @filename = filename
36
+ @code_type = code_type
37
+ end
44
38
 
45
- private
39
+ # @return [String] The code contained in the current `@filename`.
40
+ def code
41
+ path = abs_path
42
+ @code_type = type_from_filename(path)
43
+ File.read(path)
44
+ end
46
45
 
47
- # @raise [MethodSource::SourceNotFoundError] if the `filename` is not
48
- # readable for some reason.
49
- # @return [String] absolute path for the given `filename`.
50
- def abs_path
51
- code_path.detect { |path| readable?(path) } or
52
- raise SourceNotFound,
53
- "Cannot open #{ @filename.inspect } for reading."
54
- end
46
+ private
55
47
 
56
- # @param [String] path
57
- # @return [Boolean] if the path, with or without the default ext,
58
- # is a readable file then `true`, otherwise `false`.
59
- def readable?(path)
60
- File.readable?(path) && !File.directory?(path) or
61
- File.readable?(path << DEFAULT_EXT)
62
- end
48
+ # @raise [MethodSource::SourceNotFoundError] if the `filename` is not
49
+ # readable for some reason.
50
+ # @return [String] absolute path for the given `filename`.
51
+ def abs_path
52
+ code_path.detect { |path| readable?(path) } ||
53
+ raise(SourceNotFound,
54
+ "Cannot open #{@filename.inspect} for reading.")
55
+ end
63
56
 
64
- # @return [Array] All the paths that contain code that Pry can use for its
65
- # API's. Skips directories.
66
- def code_path
67
- [from_pwd, from_pry_init_pwd, *from_load_path]
68
- end
57
+ # @param [String] path
58
+ # @return [Boolean] if the path, with or without the default ext,
59
+ # is a readable file then `true`, otherwise `false`.
60
+ def readable?(path)
61
+ File.readable?(path) && !File.directory?(path) ||
62
+ File.readable?(path << DEFAULT_EXT)
63
+ end
69
64
 
70
- # @param [String] filename
71
- # @param [Symbol] default (:unknown) the file type to assume if none could be
72
- # detected.
73
- # @return [Symbol, nil] The CodeRay type of a file from its extension, or
74
- # `nil` if `:unknown`.
75
- def type_from_filename(filename, default = :unknown)
76
- _, @code_type = EXTENSIONS.find do |k, _|
77
- k.any? { |ext| ext == File.extname(filename) }
78
- end
65
+ # @return [Array] All the paths that contain code that Pry can use for its
66
+ # API's. Skips directories.
67
+ def code_path
68
+ [from_pwd, from_pry_init_pwd, *from_load_path]
69
+ end
79
70
 
80
- code_type || default
71
+ # @param [String] filename
72
+ # @param [Symbol] default (:unknown) the file type to assume if none could be
73
+ # detected.
74
+ # @return [Symbol, nil] The CodeRay type of a file from its extension, or
75
+ # `nil` if `:unknown`.
76
+ def type_from_filename(filename, default = :unknown)
77
+ _, @code_type = EXTENSIONS.find do |k, _|
78
+ k.any? { |ext| ext == File.extname(filename) }
81
79
  end
82
80
 
83
- # @return [String]
84
- def from_pwd
85
- File.expand_path(@filename, Dir.pwd)
86
- end
81
+ code_type || default
82
+ end
87
83
 
88
- # @return [String]
89
- def from_pry_init_pwd
90
- File.expand_path(@filename, Dir.pwd)
91
- end
84
+ # @return [String]
85
+ def from_pwd
86
+ File.expand_path(@filename, Dir.pwd)
87
+ end
92
88
 
93
- # @return [String]
94
- def from_load_path
95
- $LOAD_PATH.map { |path| File.expand_path(@filename, path) }
96
- end
89
+ # @return [String]
90
+ def from_pry_init_pwd
91
+ File.expand_path(@filename, Dir.pwd)
92
+ end
97
93
 
94
+ # @return [String]
95
+ def from_load_path
96
+ $LOAD_PATH.map { |path| File.expand_path(@filename, path) }
97
+ end
98
98
  end
@@ -1,69 +1,68 @@
1
- class DebuggerCode
2
-
3
- # Represents a range of lines in a code listing.
4
- #
5
- # @api private
6
- class CodeRange
7
-
8
- # @param [Integer] start_line
9
- # @param [Integer?] end_line
10
- def initialize(start_line, end_line = nil)
11
- @start_line = start_line
12
- @end_line = end_line
13
- force_set_end_line
14
- end
1
+ # frozen_string_literal: true
2
+ class DebuggerCode
3
+ # Represents a range of lines in a code listing.
4
+ #
5
+ # @api private
6
+ class CodeRange
7
+ # @param [Integer] start_line
8
+ # @param [Integer?] end_line
9
+ def initialize(start_line, end_line = nil)
10
+ @start_line = start_line
11
+ @end_line = end_line
12
+ force_set_end_line
13
+ end
15
14
 
16
- # @param [Array<LOC>] lines
17
- # @return [Range]
18
- def indices_range(lines)
19
- Range.new(*indices(lines))
20
- end
15
+ # @param [Array<LOC>] lines
16
+ # @return [Range]
17
+ def indices_range(lines)
18
+ Range.new(*indices(lines))
19
+ end
21
20
 
22
- private
21
+ private
23
22
 
24
- def start_line; @start_line; end
25
- def end_line; @end_line; end
23
+ attr_reader :start_line
26
24
 
27
- # If `end_line` is equal to `nil`, then calculate it from the first
28
- # parameter, `start_line`. Otherwise, leave it as it is.
29
- # @return [void]
30
- def force_set_end_line
31
- if start_line.is_a?(Range)
32
- set_end_line_from_range
33
- else
34
- @end_line ||= start_line
35
- end
36
- end
25
+ attr_reader :end_line
37
26
 
38
- # Finds indices of `start_line` and `end_line` in the given Array of
39
- # +lines+.
40
- #
41
- # @param [Array<LOC>] lines
42
- # @return [Array<Integer>]
43
- def indices(lines)
44
- [find_start_index(lines), find_end_index(lines)]
27
+ # If `end_line` is equal to `nil`, then calculate it from the first
28
+ # parameter, `start_line`. Otherwise, leave it as it is.
29
+ # @return [void]
30
+ def force_set_end_line
31
+ if start_line.is_a?(Range)
32
+ set_end_line_from_range
33
+ else
34
+ @end_line ||= start_line
45
35
  end
36
+ end
46
37
 
47
- # @return [Integer]
48
- def find_start_index(lines)
49
- return start_line if start_line < 0
50
- lines.index { |loc| loc.lineno >= start_line } || lines.length
51
- end
38
+ # Finds indices of `start_line` and `end_line` in the given Array of
39
+ # +lines+.
40
+ #
41
+ # @param [Array<LOC>] lines
42
+ # @return [Array<Integer>]
43
+ def indices(lines)
44
+ [find_start_index(lines), find_end_index(lines)]
45
+ end
52
46
 
53
- # @return [Integer]
54
- def find_end_index(lines)
55
- return end_line if end_line < 0
56
- (lines.index { |loc| loc.lineno > end_line } || 0) - 1
57
- end
47
+ # @return [Integer]
48
+ def find_start_index(lines)
49
+ return start_line if start_line < 0
50
+ lines.index { |loc| loc.lineno >= start_line } || lines.length
51
+ end
58
52
 
59
- # For example, if the range is 4..10, then `start_line` would be equal to
60
- # 4 and `end_line` to 10.
61
- # @return [void]
62
- def set_end_line_from_range
63
- @end_line = start_line.last
64
- @end_line -= 1 if start_line.exclude_end?
65
- @start_line = start_line.first
66
- end
53
+ # @return [Integer]
54
+ def find_end_index(lines)
55
+ return end_line if end_line < 0
56
+ (lines.index { |loc| loc.lineno > end_line } || 0) - 1
67
57
  end
68
58
 
59
+ # For example, if the range is 4..10, then `start_line` would be equal to
60
+ # 4 and `end_line` to 10.
61
+ # @return [void]
62
+ def set_end_line_from_range
63
+ @end_line = start_line.last
64
+ @end_line -= 1 if start_line.exclude_end?
65
+ @start_line = start_line.first
66
+ end
67
+ end
69
68
  end
@@ -1,80 +1,78 @@
1
- class DebuggerCode
1
+ # frozen_string_literal: true
2
+ class DebuggerCode
3
+ # Represents a line of code. A line of code is a tuple, which consists of a
4
+ # line and a line number. A `LOC` object's state (namely, the line
5
+ # parameter) can be changed via instance methods. `Pry::Code` heavily uses
6
+ # this class.
7
+ #
8
+ # @api private
9
+ # @example
10
+ # loc = LOC.new("def example\n :example\nend", 1)
11
+ # puts loc.line
12
+ # def example
13
+ # :example
14
+ # end
15
+ # #=> nil
16
+ #
17
+ # loc.indent(3)
18
+ # loc.line #=> " def example\n :example\nend"
19
+ class LOC
20
+ # @return [Array<String, Integer>]
21
+ attr_reader :tuple
2
22
 
3
- # Represents a line of code. A line of code is a tuple, which consists of a
4
- # line and a line number. A `LOC` object's state (namely, the line
5
- # parameter) can be changed via instance methods. `Pry::Code` heavily uses
6
- # this class.
7
- #
8
- # @api private
9
- # @example
10
- # loc = LOC.new("def example\n :example\nend", 1)
11
- # puts loc.line
12
- # def example
13
- # :example
14
- # end
15
- # #=> nil
16
- #
17
- # loc.indent(3)
18
- # loc.line #=> " def example\n :example\nend"
19
- class LOC
20
-
21
- # @return [Array<String, Integer>]
22
- attr_reader :tuple
23
-
24
- # @param [String] line The line of code.
25
- # @param [Integer] lineno The position of the +line+.
26
- def initialize(line, lineno)
27
- @tuple = [line.chomp, lineno.to_i]
28
- end
29
-
30
- # @return [Boolean]
31
- def ==(other)
32
- other.tuple == tuple
33
- end
23
+ # @param [String] line The line of code.
24
+ # @param [Integer] lineno The position of the +line+.
25
+ def initialize(line, lineno)
26
+ @tuple = [line.chomp, lineno.to_i]
27
+ end
34
28
 
35
- def dup
36
- self.class.new(line, lineno)
37
- end
29
+ # @return [Boolean]
30
+ def ==(other)
31
+ other.tuple == tuple
32
+ end
38
33
 
39
- # @return [String]
40
- def line
41
- tuple.first
42
- end
34
+ def dup
35
+ self.class.new(line, lineno)
36
+ end
43
37
 
44
- # @return [Integer]
45
- def lineno
46
- tuple.last
47
- end
38
+ # @return [String]
39
+ def line
40
+ tuple.first
41
+ end
48
42
 
49
- #Prepends the line number `lineno` to the `line`.
50
- # @param [Integer] max_width
51
- # @return [void]
52
- def add_line_number(max_width = 0)
53
- padded = lineno.to_s.rjust(max_width)
54
- tuple[0] = "#{ padded }: #{ line }"
55
- end
43
+ # @return [Integer]
44
+ def lineno
45
+ tuple.last
46
+ end
56
47
 
57
- # Prepends a marker "=>" or an empty marker to the +line+.
58
- #
59
- # @param [Integer] marker_lineno If it is equal to the `lineno`, then
60
- # prepend a hashrocket. Otherwise, an empty marker.
61
- # @return [void]
62
- def add_marker(marker_lineno)
63
- tuple[0] =
64
- if lineno == marker_lineno
65
- " => #{ line }".cyan
66
- else
67
- " #{ line }"
68
- end
69
- end
48
+ # Prepends the line number `lineno` to the `line`.
49
+ # @param [Integer] max_width
50
+ # @return [void]
51
+ def add_line_number(max_width = 0)
52
+ padded = lineno.to_s.rjust(max_width)
53
+ tuple[0] = "#{padded}: #{line}"
54
+ end
70
55
 
71
- # Indents the `line` with +distance+ spaces.
72
- #
73
- # @param [Integer] distance
74
- # @return [void]
75
- def indent(distance)
76
- tuple[0] = "#{ ' ' * distance }#{ line }"
77
- end
56
+ # Prepends a marker "=>" or an empty marker to the +line+.
57
+ #
58
+ # @param [Integer] marker_lineno If it is equal to the `lineno`, then
59
+ # prepend a hashrocket. Otherwise, an empty marker.
60
+ # @return [void]
61
+ def add_marker(marker_lineno)
62
+ tuple[0] =
63
+ if lineno == marker_lineno
64
+ " => #{line}".cyan
65
+ else
66
+ " #{line}"
67
+ end
78
68
  end
79
69
 
70
+ # Indents the `line` with +distance+ spaces.
71
+ #
72
+ # @param [Integer] distance
73
+ # @return [void]
74
+ def indent(distance)
75
+ tuple[0] = "#{' ' * distance}#{line}"
76
+ end
77
+ end
80
78
  end