Class: TSJSON::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/language/source.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, name = 'TSJSON Source', locationOffset = { line: 1, column: 1 }) ⇒ Source

Returns a new instance of Source.



5
6
7
8
9
10
11
# File 'lib/language/source.rb', line 5

def initialize(
  body, name = 'TSJSON Source', locationOffset = { line: 1, column: 1 }
)
  self.body = body
  self.name = name
  self.locationOffset = locationOffset
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



3
4
5
# File 'lib/language/source.rb', line 3

def body
  @body
end

#locationOffsetObject

Returns the value of attribute locationOffset.



3
4
5
# File 'lib/language/source.rb', line 3

def locationOffset
  @locationOffset
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/language/source.rb', line 3

def name
  @name
end

Class Method Details

.leftPad(len, str) ⇒ Object



84
85
86
# File 'lib/language/source.rb', line 84

def self.leftPad(len, str)
  return whitespace(len - str.length) + str
end


13
14
15
16
17
18
19
20
# File 'lib/language/source.rb', line 13

def self.print_location(location)
  return(
    print_source_location(
      location.source,
      Location.get_location(location.source, location.start)
    )
  )
end


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/language/source.rb', line 88

def self.print_prefixed_lines(lines)
  existingLines =
    lines.filter do |line_arr|
      _, line = line_arr
      line != nil
    end

  padLen =
    existingLines.map do |prefix_arr|
      prefix = prefix_arr[0]
      prefix.length
    end.max

  return(
    existingLines.map do |line_arr|
      prefix, line = line_arr
      leftPad(padLen, prefix) + (line.empty? ? ' |' : ' | ' + line)
    end.join("\n")
  )
end


22
23
24
25
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/language/source.rb', line 22

def self.print_source_location(source, sourceLocation)
  firstLineColumnOffset = source.locationOffset[:column] - 1
  body = whitespace(firstLineColumnOffset) + source.body

  lineIndex = sourceLocation[:line] - 1
  lineOffset = source.locationOffset[:line] - 1
  lineNum = sourceLocation[:line] + lineOffset

  columnOffset = sourceLocation[:line] === 1 ? firstLineColumnOffset : 0
  columnNum = sourceLocation[:column] + columnOffset
  locationStr = "#{source.name}:#{lineNum}:#{columnNum}\n"

  lines = body.split("\n", -1)
  locationLine = lines[lineIndex]

  # Special case for minified documents
  if (locationLine.length > 120)
    subLineIndex = Math.floor(columnNum / 80)
    subLineColumnNum = columnNum % 80
    subLines = []

    i = 0
    loop do
      break unless i < locationLine.length
      subLines.push(locationLine.slice(i, 80))
      i += 80
    end

    return(
      locationStr +
        print_prefixed_lines(
          [["#{lineNum}", subLines[0]]].concat(
            subLines.slice(1, subLineIndex + 1).map do |subLine|
              ['', subLine]
            end
          ).concat(
            [
              [' ', whitespace(subLineColumnNum - 1) + '^'],
              ['', subLines[subLineIndex + 1]]
            ]
          )
        )
    )
  end

  return(
    locationStr +
      print_prefixed_lines(
        [
          ["#{lineNum - 1}", lineIndex > 1 ? lines[lineIndex - 1] : nil],
          ["#{lineNum}", locationLine],
          ['', whitespace(columnNum - 1) + '^'],
          ["#{lineNum + 1}", lines[lineIndex + 1]]
        ]
      )
  )
end

.whitespace(len) ⇒ Object



80
81
82
# File 'lib/language/source.rb', line 80

def self.whitespace(len)
  return Array.new(len + 1, '').join(' ')
end