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

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 "katehighlight.h"
0012 #include "katetextbuffer.h"
0013 
0014 #include <ktexteditor_export.h>
0015 
0016 #include <QObject>
0017 
0018 class KateLineInfo;
0019 namespace KTextEditor
0020 {
0021 class DocumentPrivate;
0022 }
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      * Unwrap given line.
0187      * @param line line to unwrap
0188      */
0189     void unwrapLine(int line) override;
0190 
0191     /**
0192      * Wrap line at given cursor position.
0193      * @param position line/column as cursor where to wrap
0194      */
0195     void wrapLine(const KTextEditor::Cursor position) override;
0196 
0197 public:
0198     inline int tabWidth() const
0199     {
0200         return m_tabWidth;
0201     }
0202 
0203 public:
0204     void setTabWidth(int w);
0205 
0206     /**
0207      * Use @p highlight for highlighting
0208      *
0209      * @p highlight may be 0 in which case highlighting
0210      * will be disabled.
0211      */
0212     void setHighlight(int hlMode);
0213 
0214     KateHighlighting *highlight()
0215     {
0216         return m_highlight;
0217     }
0218 
0219     /**
0220      * Invalidate highlighting of whole buffer.
0221      */
0222     void invalidateHighlighting();
0223 
0224     /**
0225      * Compute folding vector for the given line, will internally do a re-highlighting.
0226      * @param line line to get folding vector for
0227      */
0228     KateHighlighting::Foldings computeFoldings(int line);
0229 
0230     /**
0231      * For a given line, compute if folding starts here.
0232      * @param startLine start line
0233      * @return does folding start here and is it indentation based?
0234      */
0235     std::pair<bool, bool> isFoldingStartingOnLine(int startLine);
0236 
0237     /**
0238      * For a given line, compute the folding range that starts there
0239      * to be used to fold e.g. from the icon border
0240      * @param startLine start line
0241      * @return folding range starting at the given line or invalid range when
0242      *         there is no folding start or @p startLine is not valid
0243      */
0244     KTextEditor::Range computeFoldingRangeForStartLine(int startLine);
0245 
0246 private:
0247     /**
0248      * Highlight information needs to be updated.
0249      *
0250      * @param from first line in range
0251      * @param to last line in range
0252      * @param invalidate should the rehighlighted lines be tagged?
0253      */
0254     KTEXTEDITOR_NO_EXPORT
0255     void doHighlight(int from, int to, bool invalidate);
0256 
0257 Q_SIGNALS:
0258     /**
0259      * Emitted when the highlighting of a certain range has
0260      * changed.
0261      */
0262     void tagLines(KTextEditor::LineRange lineRange);
0263     void respellCheckBlock(int start, int end);
0264 
0265 private:
0266     /**
0267      * document we belong to
0268      */
0269     KTextEditor::DocumentPrivate *const m_doc;
0270 
0271     /**
0272      * file loaded with encoding problems?
0273      */
0274     bool m_brokenEncoding;
0275 
0276     /**
0277      * too long lines wrapped on load?
0278      */
0279     bool m_tooLongLinesWrapped;
0280 
0281     /**
0282      * length of the longest line loaded
0283      */
0284     int m_longestLineLoaded;
0285 
0286     /**
0287      * current highlighting mode or 0
0288      */
0289     KateHighlighting *m_highlight;
0290 
0291     // for the scrapty indent sensitive langs
0292     int m_tabWidth;
0293 
0294     /**
0295      * last line with valid highlighting
0296      */
0297     int m_lineHighlighted;
0298 };
0299 
0300 #endif