File indexing completed on 2024-04-21 03:57:38

0001 /*
0002     SPDX-FileCopyrightText: 2008 Paul Giannaros <paul@giannaros.org>
0003     SPDX-FileCopyrightText: 2009-2018 Dominik Haumann <dhaumann@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KATE_SCRIPT_DOCUMENT_H
0009 #define KATE_SCRIPT_DOCUMENT_H
0010 
0011 #include <QObject>
0012 #include <QStringList>
0013 
0014 #include <ktexteditor_export.h>
0015 
0016 #include <QJSValue>
0017 
0018 #include <ktexteditor/cursor.h>
0019 #include <ktexteditor/range.h>
0020 
0021 namespace KTextEditor
0022 {
0023 class DocumentPrivate;
0024 }
0025 
0026 /**
0027  * Thinish wrapping around KTextEditor::DocumentPrivate, exposing the methods we want exposed
0028  * and adding some helper methods.
0029  *
0030  * setDocument _must_ be called before using any other method. This is not checked
0031  * for the sake of speed.
0032  */
0033 class KTEXTEDITOR_EXPORT KateScriptDocument : public QObject
0034 {
0035     Q_OBJECT
0036     // Note: we have no Q_PROPERTIES due to consistency: everything is a function.
0037 
0038 public:
0039     explicit KateScriptDocument(QJSEngine *, QObject *parent = nullptr);
0040     void setDocument(KTextEditor::DocumentPrivate *document);
0041     KTextEditor::DocumentPrivate *document();
0042 
0043     // BEGIN
0044     Q_INVOKABLE QString fileName();
0045     Q_INVOKABLE QString url();
0046     Q_INVOKABLE QString mimeType();
0047     Q_INVOKABLE QString encoding();
0048     Q_INVOKABLE QString highlightingMode();
0049     Q_INVOKABLE QStringList embeddedHighlightingModes();
0050     Q_INVOKABLE QString highlightingModeAt(const QJSValue &pos);
0051     Q_INVOKABLE bool isModified();
0052     Q_INVOKABLE QString text();
0053     Q_INVOKABLE QString text(int fromLine, int fromColumn, int toLine, int toColumn);
0054     Q_INVOKABLE QString text(const QJSValue &jsfrom, const QJSValue &jsto);
0055     Q_INVOKABLE QString text(const QJSValue &jsrange);
0056     Q_INVOKABLE QString line(int line);
0057     Q_INVOKABLE QString wordAt(int line, int column);
0058     Q_INVOKABLE QString wordAt(const QJSValue &jscursor);
0059     Q_INVOKABLE QJSValue wordRangeAt(int line, int column);
0060     Q_INVOKABLE QJSValue wordRangeAt(const QJSValue &jscursor);
0061     Q_INVOKABLE QString charAt(int line, int column);
0062     Q_INVOKABLE QString charAt(const QJSValue &jscursor);
0063     Q_INVOKABLE QString firstChar(int line);
0064     Q_INVOKABLE QString lastChar(int line);
0065     Q_INVOKABLE bool isSpace(int line, int column);
0066     Q_INVOKABLE bool isSpace(const QJSValue &jscursor);
0067     Q_INVOKABLE bool matchesAt(int line, int column, const QString &s);
0068     Q_INVOKABLE bool matchesAt(const QJSValue &cursor, const QString &s);
0069     Q_INVOKABLE bool setText(const QString &s);
0070     Q_INVOKABLE bool clear();
0071     Q_INVOKABLE bool truncate(int line, int column);
0072     Q_INVOKABLE bool truncate(const QJSValue &jscursor);
0073     Q_INVOKABLE bool insertText(int line, int column, const QString &s);
0074     Q_INVOKABLE bool insertText(const QJSValue &jscursor, const QString &s);
0075     Q_INVOKABLE bool removeText(int fromLine, int fromColumn, int toLine, int toColumn);
0076     Q_INVOKABLE bool removeText(const QJSValue &range);
0077     Q_INVOKABLE bool removeText(const QJSValue &jsfrom, const QJSValue &jsto);
0078     Q_INVOKABLE bool insertLine(int line, const QString &s);
0079     Q_INVOKABLE bool removeLine(int line);
0080     Q_INVOKABLE bool wrapLine(int line, int column);
0081     Q_INVOKABLE bool wrapLine(const QJSValue &cursor);
0082     Q_INVOKABLE void joinLines(int startLine, int endLine);
0083     Q_INVOKABLE int lines();
0084     Q_INVOKABLE bool isLineModified(int line);
0085     Q_INVOKABLE bool isLineSaved(int line);
0086     Q_INVOKABLE bool isLineTouched(int line);
0087     Q_INVOKABLE int findTouchedLine(int startLine, bool down);
0088     Q_INVOKABLE int length();
0089     Q_INVOKABLE int lineLength(int line);
0090     Q_INVOKABLE void editBegin();
0091     Q_INVOKABLE void editEnd();
0092     Q_INVOKABLE int firstColumn(int line);
0093     Q_INVOKABLE int lastColumn(int line);
0094     Q_INVOKABLE int prevNonSpaceColumn(int line, int column);
0095     Q_INVOKABLE int prevNonSpaceColumn(const QJSValue &jscursor);
0096     Q_INVOKABLE int nextNonSpaceColumn(int line, int column);
0097     Q_INVOKABLE int nextNonSpaceColumn(const QJSValue &jscursor);
0098     Q_INVOKABLE int prevNonEmptyLine(int line);
0099     Q_INVOKABLE int nextNonEmptyLine(int line);
0100     Q_INVOKABLE bool isInWord(const QString &character, int attribute);
0101     Q_INVOKABLE bool canBreakAt(const QString &character, int attribute);
0102     Q_INVOKABLE bool canComment(int startAttribute, int endAttribute);
0103     Q_INVOKABLE QString commentMarker(int attribute);
0104     Q_INVOKABLE QString commentStart(int attribute);
0105     Q_INVOKABLE QString commentEnd(int attribute);
0106 
0107     Q_INVOKABLE QJSValue documentRange();
0108     Q_INVOKABLE QJSValue documentEnd();
0109     Q_INVOKABLE bool isValidTextPosition(int line, int column);
0110     Q_INVOKABLE bool isValidTextPosition(const QJSValue &cursor);
0111 
0112     /**
0113      * Get the syntax highlighting attribute at a given position in the document.
0114      */
0115     Q_INVOKABLE int attribute(int line, int column);
0116     Q_INVOKABLE int attribute(const QJSValue &jscursor);
0117 
0118     /**
0119      * Return true if the highlight attribute equals @p attr.
0120      */
0121     Q_INVOKABLE bool isAttribute(int line, int column, int attr);
0122     Q_INVOKABLE bool isAttribute(const QJSValue &jscursor, int attr);
0123 
0124     /**
0125      * Get the name of the syntax highlighting attribute at the given position.
0126      */
0127     Q_INVOKABLE QString attributeName(int line, int column);
0128     Q_INVOKABLE QString attributeName(const QJSValue &jscursor);
0129 
0130     /**
0131      * Return true is the name of the syntax attribute equals @p name.
0132      */
0133     Q_INVOKABLE bool isAttributeName(int line, int column, const QString &name);
0134     Q_INVOKABLE bool isAttributeName(const QJSValue &cursor, const QString &name);
0135 
0136     Q_INVOKABLE QString variable(const QString &s);
0137     Q_INVOKABLE void setVariable(const QString &s, const QString &v);
0138     // END
0139 
0140     Q_INVOKABLE int firstVirtualColumn(int line);
0141     Q_INVOKABLE int lastVirtualColumn(int line);
0142     Q_INVOKABLE int toVirtualColumn(int line, int column);
0143     Q_INVOKABLE int toVirtualColumn(const QJSValue &cursor);
0144     Q_INVOKABLE QJSValue toVirtualCursor(int line, int column);
0145     Q_INVOKABLE QJSValue toVirtualCursor(const QJSValue &jscursor);
0146     Q_INVOKABLE int fromVirtualColumn(int line, int virtualColumn);
0147     Q_INVOKABLE int fromVirtualColumn(const QJSValue &jscursor);
0148     Q_INVOKABLE QJSValue fromVirtualCursor(int line, int column);
0149     Q_INVOKABLE QJSValue fromVirtualCursor(const QJSValue &jscursor);
0150 
0151     KTextEditor::Cursor anchorInternal(int line, int column, QChar character);
0152     KTextEditor::Cursor anchor(KTextEditor::Cursor cursor, QChar character);
0153     Q_INVOKABLE QJSValue anchor(int line, int column, QChar character);
0154     Q_INVOKABLE QJSValue anchor(const QJSValue &cursor, QChar character);
0155     KTextEditor::Cursor rfindInternal(int line, int column, const QString &text, int attribute = -1);
0156     KTextEditor::Cursor rfind(KTextEditor::Cursor cursor, const QString &text, int attribute = -1);
0157     Q_INVOKABLE QJSValue rfind(int line, int column, const QString &text, int attribute = -1);
0158     Q_INVOKABLE QJSValue rfind(const QJSValue &cursor, const QString &text, int attribute = -1);
0159 
0160     Q_INVOKABLE int defStyleNum(int line, int column);
0161     Q_INVOKABLE int defStyleNum(const QJSValue &cursor);
0162     Q_INVOKABLE bool isCode(int line, int column);
0163     Q_INVOKABLE bool isCode(const QJSValue &cursor);
0164     Q_INVOKABLE bool isComment(int line, int column);
0165     Q_INVOKABLE bool isComment(const QJSValue &cursor);
0166     Q_INVOKABLE bool isString(int line, int column);
0167     Q_INVOKABLE bool isString(const QJSValue &cursor);
0168     Q_INVOKABLE bool isRegionMarker(int line, int column);
0169     Q_INVOKABLE bool isRegionMarker(const QJSValue &cursor);
0170     Q_INVOKABLE bool isChar(int line, int column);
0171     Q_INVOKABLE bool isChar(const QJSValue &cursor);
0172     Q_INVOKABLE bool isOthers(int line, int column);
0173     Q_INVOKABLE bool isOthers(const QJSValue &cursor);
0174 
0175     Q_INVOKABLE bool startsWith(int line, const QString &pattern, bool skipWhiteSpaces);
0176     Q_INVOKABLE bool endsWith(int line, const QString &pattern, bool skipWhiteSpaces);
0177 
0178     Q_INVOKABLE void indent(const QJSValue &jsrange, int change);
0179 
0180 private:
0181     KTEXTEDITOR_NO_EXPORT
0182     static bool _isCode(int defaultStyle);
0183 
0184     KTextEditor::DocumentPrivate *m_document;
0185     QJSEngine *m_engine;
0186 };
0187 
0188 #endif