File indexing completed on 2024-04-28 11:45:20

0001 /*
0002     SPDX-FileCopyrightText: 2017 Allan Sandfeld Jensen <kde@carewolf.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KTEXTEDITOR_SCRIPTRANGE_H
0008 #define KTEXTEDITOR_SCRIPTRANGE_H
0009 
0010 #include <QJSEngine>
0011 #include <QJSValue>
0012 
0013 #include "ktexteditor/range.h"
0014 
0015 inline QJSValue rangeToScriptValue(QJSEngine *engine, KTextEditor::Range range)
0016 {
0017     QString code =
0018         QStringLiteral("new Range(%1, %2, %3, %4);").arg(range.start().line()).arg(range.start().column()).arg(range.end().line()).arg(range.end().column());
0019     QJSValue result = engine->evaluate(code);
0020     Q_ASSERT(!result.isError());
0021     return result;
0022 }
0023 
0024 inline KTextEditor::Range rangeFromScriptValue(const QJSValue &obj)
0025 {
0026     KTextEditor::Range range;
0027     range.setRange(KTextEditor::Cursor(obj.property(QStringLiteral("start")).property(QStringLiteral("line")).toInt(),
0028                                        obj.property(QStringLiteral("start")).property(QStringLiteral("column")).toInt()),
0029                    KTextEditor::Cursor(obj.property(QStringLiteral("end")).property(QStringLiteral("line")).toInt(),
0030                                        obj.property(QStringLiteral("end")).property(QStringLiteral("column")).toInt()));
0031     return range;
0032 }
0033 
0034 #endif