equal
deleted
inserted
replaced
14 { |
14 { |
15 /* position */ |
15 /* position */ |
16 |
16 |
17 object Position |
17 object Position |
18 { |
18 { |
19 val zero: Position = Position(0, 0) |
19 val zero: Position = Position() |
20 } |
20 } |
21 |
21 |
22 sealed case class Position(line: Int, column: Int) |
22 sealed case class Position(line: Int = 0, column: Int = 0) |
23 { |
23 { |
24 def line1: Int = line + 1 |
24 def line1: Int = line + 1 |
25 def column1: Int = column + 1 |
25 def column1: Int = column + 1 |
26 def print: String = line1.toString + ":" + column1.toString |
26 def print: String = line1.toString + ":" + column1.toString |
27 |
27 |
61 |
61 |
62 |
62 |
63 /* positions within document node */ |
63 /* positions within document node */ |
64 |
64 |
65 sealed case class Position_Node(pos: Position, name: String) |
65 sealed case class Position_Node(pos: Position, name: String) |
|
66 { |
|
67 def line: Int = pos.line |
|
68 def column: Int = pos.column |
|
69 } |
|
70 |
66 sealed case class Range_Node(range: Range, name: String) |
71 sealed case class Range_Node(range: Range, name: String) |
|
72 { |
|
73 def start: Position = range.start |
|
74 def stop: Position = range.stop |
|
75 } |
67 |
76 |
68 |
77 |
69 /* document with newline as separator (not terminator) */ |
78 /* document with newline as separator (not terminator) */ |
70 |
79 |
71 object Document |
80 object Document |
97 def position(offset: Text.Offset, length: Length = Length): Position = |
106 def position(offset: Text.Offset, length: Length = Length): Position = |
98 { |
107 { |
99 @tailrec def move(i: Text.Offset, lines_count: Int, lines_rest: List[Line]): Position = |
108 @tailrec def move(i: Text.Offset, lines_count: Int, lines_rest: List[Line]): Position = |
100 { |
109 { |
101 lines_rest match { |
110 lines_rest match { |
102 case Nil => require(i == 0); Position(lines_count, 0) |
111 case Nil => require(i == 0); Position(lines_count) |
103 case line :: ls => |
112 case line :: ls => |
104 val n = line.text.length |
113 val n = line.text.length |
105 if (ls.isEmpty || i <= n) |
114 if (ls.isEmpty || i <= n) |
106 Position(lines_count, 0).advance(line.text.substring(n - i), length) |
115 Position(lines_count).advance(line.text.substring(n - i), length) |
107 else move(i - (n + 1), lines_count + 1, ls) |
116 else move(i - (n + 1), lines_count + 1, ls) |
108 } |
117 } |
109 } |
118 } |
110 move(offset, 0, lines) |
119 move(offset, 0, lines) |
111 } |
120 } |