File indexing completed on 2024-04-28 15:30:25

0001 /*
0002     SPDX-FileCopyrightText: 2000 Waldo Bastian <bastian@kde.org>
0003     SPDX-FileCopyrightText: 2002-2004 Christoph Cullmann <cullmann@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KATE_BUFFER_H
0009 #define KATE_BUFFER_H
0010 
0011 #include "katetextbuffer.h"
0012 
0013 #include <ktexteditor_export.h>
0014 
0015 #include <QObject>
0016 
0017 class KateLineInfo;
0018 namespace KTextEditor
0019 {
0020 class DocumentPrivate;
0021 }
0022 class KateHighlighting;
0023 
0024 /**
0025  * The KateBuffer class maintains a collections of lines.
0026  *
0027  * @author Waldo Bastian <bastian@kde.org>
0028  * @author Christoph Cullmann <cullmann@kde.org>
0029  */
0030 class KTEXTEDITOR_EXPORT KateBuffer final : public Kate::TextBuffer
0031 {
0032     Q_OBJECT
0033 
0034 public:
0035     /**
0036      * Create an empty buffer.
0037      * @param doc parent document
0038      */
0039     explicit KateBuffer(KTextEditor::DocumentPrivate *doc);
0040 
0041     /**
0042      * Goodbye buffer
0043      */
0044     ~KateBuffer() override;
0045 
0046 public:
0047     /**
0048      * start some editing action
0049      */
0050     void editStart();
0051 
0052     /**
0053      * finish some editing action
0054      */
0055     void editEnd();
0056 
0057     /**
0058      * Update highlighting of the lines in
0059      * last edit transaction
0060      */
0061     void updateHighlighting();
0062 
0063     /**
0064      * were there changes in the current running
0065      * editing session?
0066      * @return changes done?
0067      */
0068     inline bool editChanged() const
0069     {
0070         return editingChangedBuffer();
0071     }
0072 
0073     /**
0074      * dirty lines start
0075      * @return start line
0076      */
0077     inline int editTagStart() const
0078     {
0079         return editingMinimalLineChanged();
0080     }
0081 
0082     /**
0083      * dirty lines end
0084      * @return end line
0085      */
0086     inline int editTagEnd() const
0087     {
0088         return editingMaximalLineChanged();
0089     }
0090 
0091     /**
0092      * line inserted/removed?
0093      * @return line inserted/removed?
0094      */
0095     inline bool editTagFrom() const
0096     {
0097         return editingChangedNumberOfLines() != 0;
0098     }
0099 
0100 public:
0101     /**
0102      * Clear the buffer.
0103      */
0104     void clear() override;
0105 
0106     /**
0107      * Open a file, use the given filename
0108      * @param m_file filename to open
0109      * @param enforceTextCodec enforce to use only the set text codec
0110      * @return success
0111      */
0112     bool openFile(const QString &m_file, bool enforceTextCodec);
0113 
0114     /**
0115      * Did encoding errors occur on load?
0116      * @return encoding errors occurred on load?
0117      */
0118     bool brokenEncoding() const
0119     {
0120         return m_brokenEncoding;
0121     }
0122 
0123     /**
0124      * Too long lines wrapped on load?
0125      * @return too long lines wrapped on load?
0126      */
0127     bool tooLongLinesWrapped() const
0128     {
0129         return m_tooLongLinesWrapped;
0130     }
0131 
0132     int longestLineLoaded() const
0133     {
0134         return m_longestLineLoaded;
0135     }
0136 
0137     /**
0138      * Can the current codec handle all chars
0139      * @return chars can be encoded
0140      */
0141     bool canEncode();
0142 
0143     /**
0144      * Save the buffer to a file, use the given filename + codec + end of line chars (internal use of qtextstream)
0145      * @param m_file filename to save to
0146      * @return success
0147      */
0148     bool saveFile(const QString &m_file);
0149 
0150 public:
0151     /**
0152      * Return line @p lineno.
0153      * Highlighting of returned line might be out-dated, which may be sufficient
0154      * for pure text manipulation functions, like search/replace.
0155      * If you require highlighting to be up to date, call @ref ensureHighlighted
0156      * prior to this method.
0157      */
0158     inline Kate::TextLine plainLine(int lineno)
0159     {
0160         if (lineno < 0 || lineno >= lines()) {
0161             return Kate::TextLine();
0162         }
0163 
0164         return line(lineno);
0165     }
0166 
0167     int lineLength(int lineno) const
0168     {
0169         if (lineno < 0 || lineno >= lines()) {
0170             return -1;
0171         }
0172 
0173         return Kate::TextBuffer::lineLength(lineno);
0174     }
0175 
0176     /**
0177      * Update highlighting of given line @p line, if needed.
0178      * If @p line is already highlighted, this function does nothing.
0179      * If @p line is not highlighted, all lines up to line + lookAhead
0180      * are highlighted.
0181      * @param lookAhead also highlight these following lines
0182      */
0183     void ensureHighlighted(int line, int lookAhead = 64);
0184 
0185     /**
0186      * Return the total number of lines in the buffer.
0187      */
0188     inline int count() const
0189     {
0190         return lines();
0191     }
0192 
0193     /**
0194      * Unwrap given line.
0195      * @param line line to unwrap
0196      */
0197     void unwrapLine(int line) override;
0198 
0199     /**
0200      * Wrap line at given cursor position.
0201      * @param position line/column as cursor where to wrap
0202      */
0203     void wrapLine(const KTextEditor::Cursor position) override;
0204 
0205 public:
0206     inline int tabWidth() const
0207     {
0208         return m_tabWidth;
0209     }
0210 
0211 public:
0212     void setTabWidth(int w);
0213 
0214     /**
0215      * Use @p highlight for highlighting
0216      *
0217      * @p highlight may be 0 in which case highlighting
0218      * will be disabled.
0219      */
0220     void setHighlight(int hlMode);
0221 
0222     KateHighlighting *highlight()
0223     {
0224         return m_highlight;
0225     }
0226 
0227     /**
0228      * Invalidate highlighting of whole buffer.
0229      */
0230     void invalidateHighlighting();
0231 
0232     /**
0233      * For a given line, compute if folding starts here.
0234      * @param startLine start line
0235      * @return does folding start here and is it indentation based?
0236      */
0237     std::pair<bool, bool> isFoldingStartingOnLine(int startLine);
0238 
0239     /**
0240      * For a given line, compute the folding range that starts there
0241      * to be used to fold e.g. from the icon border
0242      * @param startLine start line
0243      * @return folding range starting at the given line or invalid range when
0244      *         there is no folding start or @p startLine is not valid
0245      */
0246     KTextEditor::Range computeFoldingRangeForStartLine(int startLine);
0247 
0248 private:
0249     /**
0250      * Highlight information needs to be updated.
0251      *
0252      * @param from first line in range
0253      * @param to last line in range
0254      * @param invalidate should the rehighlighted lines be tagged?
0255      */
0256     KTEXTEDITOR_NO_EXPORT
0257     void doHighlight(int from, int to, bool invalidate);
0258 
0259 Q_SIGNALS:
0260     /**
0261      * Emitted when the highlighting of a certain range has
0262      * changed.
0263      */
0264     void tagLines(KTextEditor::LineRange lineRange);
0265     void respellCheckBlock(int start, int end);
0266 
0267 private:
0268     /**
0269      * document we belong to
0270      */
0271     KTextEditor::DocumentPrivate *const m_doc;
0272 
0273     /**
0274      * file loaded with encoding problems?
0275      */
0276     bool m_brokenEncoding;
0277 
0278     /**
0279      * too long lines wrapped on load?
0280      */
0281     bool m_tooLongLinesWrapped;
0282 
0283     /**
0284      * length of the longest line loaded
0285      */
0286     int m_longestLineLoaded;
0287 
0288     /**
0289      * current highlighting mode or 0
0290      */
0291     KateHighlighting *m_highlight;
0292 
0293     // for the scrapty indent sensitive langs
0294     int m_tabWidth;
0295 
0296     /**
0297      * last line with valid highlighting
0298      */
0299     int m_lineHighlighted;
0300 
0301     /**
0302      * number of dynamic contexts causing a full invalidation
0303      */
0304     int m_maxDynamicContexts;
0305 };
0306 
0307 #endif