File indexing completed on 2024-04-21 03:58:08

0001 /*
0002     SPDX-FileCopyrightText: 2008 Erlend Hamberg <ehamberg@gmail.com>
0003     SPDX-FileCopyrightText: 2011 Svyatoslav Kuzmich <svatoslav1@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include <ktexteditor/range.h>
0009 #include <vimode/range.h>
0010 
0011 using namespace KateVi;
0012 
0013 Range::Range()
0014     : Range(-1, -1, -1, -1, InclusiveMotion)
0015 {
0016 }
0017 
0018 Range::Range(int slin, int scol, int elin, int ecol, MotionType inc)
0019     : startLine(slin)
0020     , startColumn(scol)
0021     , endLine(elin)
0022     , endColumn(ecol)
0023     , motionType(inc)
0024     , valid(true)
0025     , jump(false)
0026 {
0027 }
0028 
0029 Range::Range(int elin, int ecol, MotionType inc)
0030     : Range(-1, -1, elin, ecol, inc)
0031 {
0032 }
0033 
0034 Range::Range(const KTextEditor::Cursor c, MotionType mt)
0035     : Range(-1, -1, c.line(), c.column(), mt)
0036 {
0037 }
0038 
0039 Range::Range(const KTextEditor::Cursor c1, const KTextEditor::Cursor c2, MotionType mt)
0040     : Range(c1.line(), c1.column(), c2.line(), c2.column(), mt)
0041 {
0042 }
0043 
0044 void Range::normalize()
0045 {
0046     int sl = startLine;
0047     int el = endLine;
0048     int sc = startColumn;
0049     int ec = endColumn;
0050 
0051     if (sl < el) {
0052         startLine = sl;
0053         startColumn = sc;
0054         endLine = el;
0055         endColumn = ec;
0056     } else {
0057         startLine = el;
0058         endLine = sl;
0059         if (sl != el) {
0060             startColumn = ec;
0061             endColumn = sc;
0062         } else {
0063             startColumn = qMin(sc, ec);
0064             endColumn = qMax(sc, ec);
0065         }
0066     }
0067 }
0068 
0069 KTextEditor::Range Range::toEditorRange() const
0070 {
0071     return KTextEditor::Range(startLine, startColumn, endLine, endColumn);
0072 }
0073 
0074 Range Range::invalid()
0075 {
0076     Range r;
0077     r.valid = false;
0078     return r;
0079 }
0080 
0081 QDebug operator<<(QDebug s, const Range &range)
0082 {
0083     s << "["
0084       << " (" << range.startLine << ", " << range.startColumn << ")"
0085       << " -> "
0086       << " (" << range.endLine << ", " << range.endColumn << ")"
0087       << "]"
0088       << " (" << (range.motionType == InclusiveMotion ? "Inclusive" : "Exclusive") << ") (jump: " << (range.jump ? "true" : "false") << ")";
0089     return s;
0090 }