1 | /* |
2 | SPDX-FileCopyrightText: 2008 Erlend Hamberg <ehamberg@gmail.com> |
3 | SPDX-FileCopyrightText: 2011 Svyatoslav Kuzmich <svatoslav1@gmail.com> |
4 | |
5 | SPDX-License-Identifier: LGPL-2.0-or-later |
6 | */ |
7 | |
8 | #include <ktexteditor/range.h> |
9 | #include <vimode/range.h> |
10 | |
11 | using namespace KateVi; |
12 | |
13 | Range::Range() |
14 | : Range(-1, -1, -1, -1, InclusiveMotion) |
15 | { |
16 | } |
17 | |
18 | Range::Range(int slin, int scol, int elin, int ecol, MotionType inc) |
19 | : startLine(slin) |
20 | , startColumn(scol) |
21 | , endLine(elin) |
22 | , endColumn(ecol) |
23 | , motionType(inc) |
24 | , valid(true) |
25 | , jump(false) |
26 | { |
27 | } |
28 | |
29 | Range::Range(int elin, int ecol, MotionType inc) |
30 | : Range(-1, -1, elin, ecol, inc) |
31 | { |
32 | } |
33 | |
34 | Range::Range(const KTextEditor::Cursor c, MotionType mt) |
35 | : Range(-1, -1, c.line(), c.column(), mt) |
36 | { |
37 | } |
38 | |
39 | Range::Range(const KTextEditor::Cursor c1, const KTextEditor::Cursor c2, MotionType mt) |
40 | : Range(c1.line(), c1.column(), c2.line(), c2.column(), mt) |
41 | { |
42 | } |
43 | |
44 | void Range::normalize() |
45 | { |
46 | int sl = startLine; |
47 | int el = endLine; |
48 | int sc = startColumn; |
49 | int ec = endColumn; |
50 | |
51 | if (sl < el) { |
52 | startLine = sl; |
53 | startColumn = sc; |
54 | endLine = el; |
55 | endColumn = ec; |
56 | } else { |
57 | startLine = el; |
58 | endLine = sl; |
59 | if (sl != el) { |
60 | startColumn = ec; |
61 | endColumn = sc; |
62 | } else { |
63 | startColumn = qMin(a: sc, b: ec); |
64 | endColumn = qMax(a: sc, b: ec); |
65 | } |
66 | } |
67 | } |
68 | |
69 | KTextEditor::Range Range::toEditorRange() const |
70 | { |
71 | return KTextEditor::Range(startLine, startColumn, endLine, endColumn); |
72 | } |
73 | |
74 | Range Range::invalid() |
75 | { |
76 | Range r; |
77 | r.valid = false; |
78 | return r; |
79 | } |
80 | |
81 | QDebug operator<<(QDebug s, const Range &range) |
82 | { |
83 | s << "[" |
84 | << " (" << range.startLine << ", " << range.startColumn << ")" |
85 | << " -> " |
86 | << " (" << range.endLine << ", " << range.endColumn << ")" |
87 | << "]" |
88 | << " (" << (range.motionType == InclusiveMotion ? "Inclusive" : "Exclusive" ) << ") (jump: " << (range.jump ? "true" : "false" ) << ")" ; |
89 | return s; |
90 | } |
91 | |