Class: Kotoshu::Models::Context
- Inherits:
-
Object
- Object
- Kotoshu::Models::Context
- Defined in:
- lib/kotoshu/models/context.rb
Overview
Value object for text context around an error.
Provides the surrounding text before, current, and after an error location for context display and analysis.
Instance Attribute Summary collapse
-
#after ⇒ Object
readonly
Returns the value of attribute after.
-
#before ⇒ Object
readonly
Returns the value of attribute before.
-
#current ⇒ Object
readonly
Returns the value of attribute current.
-
#full_context ⇒ Object
readonly
Returns the value of attribute full_context.
-
#location ⇒ Object
readonly
Returns the value of attribute location.
-
#window ⇒ Object
readonly
Returns the value of attribute window.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Check if this context equals another.
-
#hash ⇒ Integer
Hash code for hash table usage.
-
#initialize(before:, current:, after:, location:, window: 5) ⇒ Context
constructor
Create a new context object.
-
#surrounding_words(n = 3) ⇒ Array<String>
Get surrounding words around the error location.
-
#to_s ⇒ String
(also: #inspect)
String representation.
-
#with_highlight(error_word) ⇒ String
Get context as a formatted string with error highlighting.
-
#word_at_location ⇒ String?
Get the word at the error location.
Constructor Details
#initialize(before:, current:, after:, location:, window: 5) ⇒ Context
Create a new context object.
28 29 30 31 32 33 34 35 36 |
# File 'lib/kotoshu/models/context.rb', line 28 def initialize(before:, current:, after:, location:, window: 5) @before = before @current = current @after = after @location = location @window = window @full_context = [before, current, after].compact.join("\n") freeze end |
Instance Attribute Details
#after ⇒ Object (readonly)
Returns the value of attribute after.
19 20 21 |
# File 'lib/kotoshu/models/context.rb', line 19 def after @after end |
#before ⇒ Object (readonly)
Returns the value of attribute before.
19 20 21 |
# File 'lib/kotoshu/models/context.rb', line 19 def before @before end |
#current ⇒ Object (readonly)
Returns the value of attribute current.
19 20 21 |
# File 'lib/kotoshu/models/context.rb', line 19 def current @current end |
#full_context ⇒ Object (readonly)
Returns the value of attribute full_context.
19 20 21 |
# File 'lib/kotoshu/models/context.rb', line 19 def full_context @full_context end |
#location ⇒ Object (readonly)
Returns the value of attribute location.
19 20 21 |
# File 'lib/kotoshu/models/context.rb', line 19 def location @location end |
#window ⇒ Object (readonly)
Returns the value of attribute window.
19 20 21 |
# File 'lib/kotoshu/models/context.rb', line 19 def window @window end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Check if this context equals another.
81 82 83 84 85 |
# File 'lib/kotoshu/models/context.rb', line 81 def ==(other) return false unless other.is_a?(Context) @location == other.location && @full_context == other.full_context end |
#hash ⇒ Integer
Hash code for hash table usage.
91 92 93 |
# File 'lib/kotoshu/models/context.rb', line 91 def hash [@location, @full_context].hash end |
#surrounding_words(n = 3) ⇒ Array<String>
Get surrounding words around the error location.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/kotoshu/models/context.rb', line 42 def surrounding_words(n = 3) return [] if @current.nil? || @current.empty? words = @current.split return [] if words.empty? # Try to find the word at the error location target_word = word_at_location return words unless target_word idx = words.index(target_word) return words unless idx # Get n words before and after start_idx = [0, idx - n].max end_idx = [words.size - 1, idx + n].min words[start_idx..end_idx].to_a end |
#to_s ⇒ String Also known as: inspect
String representation.
98 99 100 101 102 103 104 |
# File 'lib/kotoshu/models/context.rb', line 98 def to_s if @location.line "Line #{@location.line}: #{@full_context}" else @full_context end end |
#with_highlight(error_word) ⇒ String
Get context as a formatted string with error highlighting.
111 112 113 114 115 116 |
# File 'lib/kotoshu/models/context.rb', line 111 def with_highlight(error_word) return @full_context unless error_word # Find and highlight the error word @full_context.gsub(error_word) { |m| "\033[4m#{m}\033[0m" } end |
#word_at_location ⇒ String?
Get the word at the error location.
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/kotoshu/models/context.rb', line 65 def word_at_location return nil unless @location if @location.column # Get character at column return @current[@location.column] if @current && @location.column < @current.length end # For node-based locations, return the current text @current end |