Class: Kotoshu::Models::Context

Inherits:
Object
  • Object
show all
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.

Examples:

Creating context

context = Context.new(
  before: "The quick brown",
  current: "fox",
  after: "jumps over",
  location: Location.new(line: 5, column: 16)
)
context.full_context  # => "The quick brown fox jumps over"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(before:, current:, after:, location:, window: 5) ⇒ Context

Create a new context object.

Parameters:

  • before (String)

    Text before the error

  • current (String)

    The current line/text containing the error

  • after (String)

    Text after the error

  • location (Documents::Location)

    The error location

  • window (Integer) (defaults to: 5)

    Window size used for context (default: 5)



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

#afterObject (readonly)

Returns the value of attribute after.



19
20
21
# File 'lib/kotoshu/models/context.rb', line 19

def after
  @after
end

#beforeObject (readonly)

Returns the value of attribute before.



19
20
21
# File 'lib/kotoshu/models/context.rb', line 19

def before
  @before
end

#currentObject (readonly)

Returns the value of attribute current.



19
20
21
# File 'lib/kotoshu/models/context.rb', line 19

def current
  @current
end

#full_contextObject (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

#locationObject (readonly)

Returns the value of attribute location.



19
20
21
# File 'lib/kotoshu/models/context.rb', line 19

def location
  @location
end

#windowObject (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.

Parameters:

  • other (Object)

    Another object

Returns:

  • (Boolean)

    True if contexts match



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

#hashInteger

Hash code for hash table usage.

Returns:

  • (Integer)

    Hash code



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.

Parameters:

  • n (Integer) (defaults to: 3)

    Number of words on each side (default: 3)

Returns:

  • (Array<String>)

    Surrounding words



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_sString Also known as: inspect

String representation.

Returns:

  • (String)

    Human-readable 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.

Parameters:

  • error_word (String)

    The error word to highlight

Returns:

  • (String)

    Formatted context with ANSI codes



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_locationString?

Get the word at the error location.

Returns:

  • (String, nil)

    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