ni_nitdoc: added fast copy past utility to signatures.
[nit.git] / src / location.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # This module is used to model Nit source-file and locations in source-file
18 package location
19
20 # A raw text Nit source file
21 class SourceFile
22 # The path of the source
23 var filename: String
24
25 # The content of the source
26 var string: String
27
28 # Create a new sourcefile using a filename and a stream
29 init(filename: String, stream: IStream)
30 do
31 self.filename = filename
32 string = stream.read_all
33 line_starts[0] = 0
34 end
35
36 # Create a new sourcefile using a dummy filename and a given content
37 init from_string(filename: String, string: String)
38 do
39 self.filename = filename
40 self.string = string
41 line_starts[0] = 0
42 end
43
44 # Position of each line start
45 var line_starts: Array[Int] = new Array[Int]
46 end
47
48 # A location inside a source file
49 class Location
50 super Comparable
51 redef type OTHER: Location
52
53 readable var _file: nullable SourceFile
54 readable var _line_start: Int
55 readable var _line_end: Int
56 readable var _column_start: Int
57 readable var _column_end: Int
58
59 init(f: nullable SourceFile, line_s: Int, line_e: Int, column_s: Int, column_e: Int) do
60 _file = f
61 _line_start = line_s
62 _line_end = line_e
63 _column_start = column_s
64 _column_end = column_e
65 end
66
67 # The verbatim associated text in the source-file
68 fun text: String
69 do
70 var res = self.text_cache
71 if res != null then return res
72 var l = self
73 var pstart = l.file.line_starts[l.line_start-1] + l.column_start-1
74 var pend = l.file.line_starts[l.line_end-1] + l.column_end-1
75 res = l.file.string.substring(pstart, pend-pstart+1)
76 self.text_cache = res
77 return res
78 end
79
80 private var text_cache: nullable String
81
82 init with_file(f: SourceFile) do init(f,0,0,0,0)
83
84 redef fun ==(other: nullable Object): Bool do
85 if other == null then return false
86 if not other isa Location then return false
87
88 if other.file != file then return false
89 if other.line_start != line_start then return false
90 if other.line_end != line_end then return false
91 if other.column_start != column_start then return false
92 if other.column_end != column_end then return false
93
94 return true
95 end
96
97 fun located_in(loc: nullable Location): Bool do
98 if loc == null then return false
99
100 if line_start < loc.line_start then return false
101 if line_start > loc.line_end then return false
102
103 if line_end > loc.line_end then return false
104
105 if line_start == loc.line_start then
106 if column_start < loc.column_start then return false
107 if column_start > loc.column_end then return false
108 end
109
110 if line_end == loc.line_end and column_end > loc.column_end then return false
111
112 return true
113 end
114
115 redef fun to_s: String do
116 var file_part = ""
117 if file != null then
118 file_part = file.filename
119 if file.filename.length > 0 then file_part += ":"
120 end
121
122 if line_start == line_end then
123 if column_start == column_end then
124 return "{file_part}{line_start},{column_start}"
125 else
126 return "{file_part}{line_start},{column_start}--{column_end}"
127 end
128 else
129 return "{file_part}{line_start},{column_start}--{line_end},{column_end}"
130 end
131 end
132
133 fun relative_to(loc: nullable Location): String do
134 var relative: Location
135 if loc != null and loc.file == self.file then
136 relative = new Location(null, self.line_start, self.line_end, self.column_start, self.column_end)
137 else
138 relative = new Location(self.file, self.line_start, self.line_end, self.column_start, self.column_end)
139 end
140 return relative.to_s
141 end
142
143 redef fun <(other: OTHER): Bool do
144 if self == other then return false
145 if self.located_in(other) then return true
146 if other.located_in(self) then return false
147
148 if line_start != other.line_start then return line_start < other.line_start
149 if column_start != other.column_start then return column_start < other.column_start
150 if line_end != other.line_end then return line_end < other.line_end
151
152 return column_end < other.column_end
153 end
154
155 # Return the associated line with the location highlihted with color and a carret under the starting position
156 # `color' must be and terminal escape sequence used as "{escape}[{color}m;"
157 # "0;31" for red
158 # "1;31" for bright red
159 # "0;32" for green
160 fun colored_line(color: String): String
161 do
162 var esc = 27.ascii
163 var def = "{esc}[0m"
164 var col = "{esc}[{color}m"
165
166 var l = self
167 var i = l.line_start
168 var line_start = l.file.line_starts[i-1]
169 var line_end = line_start
170 var string = l.file.string
171 while line_end+1 < string.length and string[line_end+1] != '\n' and string[line_end+1] != '\r' do
172 line_end += 1
173 end
174 var lstart = string.substring(line_start, l.column_start - 1)
175 var cend
176 if i != l.line_end then
177 cend = line_end - line_start + 1
178 else
179 cend = l.column_end
180 end
181 var lmid
182 var lend
183 if line_start + cend <= string.length then
184 lmid = string.substring(line_start + l.column_start - 1, cend - l.column_start + 1)
185 lend = string.substring(line_start + cend, line_end - line_start - cend + 1)
186 else
187 lmid = ""
188 lend = ""
189 end
190 var indent = new Buffer
191 for j in [line_start..line_start+l.column_start-1[ do
192 if string[j] == '\t' then
193 indent.add '\t'
194 else
195 indent.add ' '
196 end
197 end
198 return "\t{lstart}{col}{lmid}{def}{lend}\n\t{indent}^"
199 end
200 end
201