Merge: doc: fixed some typos and other misc. corrections
[nit.git] / src / location.nit
index 108cf28..448d17d 100644 (file)
@@ -35,7 +35,8 @@ class SourceFile
        end
 
        # Create a new sourcefile using a dummy filename and a given content
-       init from_string(filename: String, string: String)
+       init from_string(filename: String, string: String) is
+               nosuper
        do
                self.filename = filename
                self.string = string
@@ -46,6 +47,20 @@ class SourceFile
        #
        # Used for fast access to each line when rendering parts of the `string`.
        var line_starts = new Array[Int]
+
+       # Extract a given line excluding the line-terminators characters.
+       #
+       # `line_number` starts at 1 for the first line.
+       fun get_line(line_number: Int): String do
+               if line_number > line_starts.length then return ""
+               var line_start = line_starts[line_number-1]
+               var line_end = line_start
+               var string = self.string
+               while line_end+1 < string.length and string.chars[line_end+1] != '\n' and string.chars[line_end+1] != '\r' do
+                       line_end += 1
+               end
+               return string.substring(line_start, line_end-line_start+1)
+       end
 end
 
 # A location inside a source file
@@ -99,7 +114,9 @@ class Location
        # loc = new Location.from_string("location.nit:82--105,8")
        # assert loc.to_s == "location.nit:82,0--105,8"
        # ~~~
-       init from_string(string: String) do
+       init from_string(string: String) is
+               nosuper
+       do
                self.line_start = 0
                self.line_end = 0
                self.column_start = 0
@@ -137,6 +154,15 @@ class Location
                end
        end
 
+       # Initialize a location corresponding to an opaque file.
+       #
+       # The path is used as is and is not open nor read.
+       init opaque_file(path: String)
+       do
+               var source = new SourceFile.from_string(path, "")
+               init(source, 0, 0, 0, 0)
+       end
+
        # The index in the start character in the source
        fun pstart: Int do return file.line_starts[line_start-1] + column_start-1
 
@@ -182,7 +208,7 @@ class Location
 
                if line_start == loc.line_start then
                        if column_start < loc.column_start then return false
-                       if column_start > loc.column_end then return false
+                       if line_start == loc.line_end and column_start > loc.column_end then return false
                end
 
                if line_end == loc.line_end and column_end > loc.column_end then return false
@@ -243,7 +269,7 @@ class Location
        # * `"0;32"` for green
        fun colored_line(color: String): String
        do
-               var esc = 27.ascii
+               var esc = 27.code_point
                var def = "{esc}[0m"
                var col = "{esc}[{color}m"