File indexing completed on 2024-04-21 11:14:05

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2009 Alexander Rieder <alexanderrieder@gmail.com>
0004 */
0005 
0006 #ifndef _COMPLETIONOBJECT_H
0007 #define _COMPLETIONOBJECT_H
0008 
0009 #include <KCompletion>
0010 
0011 #include "cantor_export.h"
0012 
0013 namespace Cantor
0014 {
0015 class CompletionObjectPrivate;
0016 class Session;
0017 
0018 /**
0019  * This Object is used to provide a Tab Completion, in an asynchronous way.
0020  * Each backend, supporting tab completion, needs to provide their own
0021  * CompletionObject, that reimplements the fetching of the completions
0022  * and emits done() as soon as the completions are available
0023  *
0024  * @author Alexander Rieder
0025  */
0026 class CANTOR_EXPORT CompletionObject : public KCompletion
0027 {
0028   Q_OBJECT
0029   public:
0030     /**
0031      * Constructor
0032      * @param parent the session, this object belongs to
0033      */
0034     explicit CompletionObject(Session* parent);
0035     ///Destructor
0036     ~CompletionObject() override;
0037 
0038     enum LineCompletionMode {
0039     PreliminaryCompletion, ///< Only insert the completion
0040     FinalCompletion        ///< also add () for functions, etc
0041     };
0042 
0043    /**
0044      * Returns a list of completions
0045      * @return a list of completions
0046      */
0047     QStringList completions() const;
0048    /**
0049      * Returns the last completion
0050      * @return the last completion
0051      */
0052     QString completion() const;
0053     /**
0054      * returns the command, this completion is for
0055      * @return the command, this completion is for
0056      */
0057     QString command() const;
0058     /**
0059      * returns the session, this object belongs to
0060      * @return the session, this object belongs to
0061      */
0062     Session* session() const;
0063 
0064     /**
0065      * Sets the line and cursor index at which a completion should be found
0066      * This triggers an asynchronous fetching of completions,
0067      * which emits done() when done.
0068      * @param line the line that is to be completed
0069      * @param index the cursor position in line
0070      */
0071     void setLine(const QString& line, int index);
0072     /**
0073      * Takes the changed line and updates the command accordingly.
0074      * This triggers an asynchronous fetching of completions,
0075      * which emits done() when done.
0076      * @param line the line that is to be completed
0077      * @param index the cursor position in line
0078      */
0079     void updateLine(const QString& line, int index);
0080     /**
0081      * Takes a completion and a completion mode and triggers and calculates
0082      * the new line with this completion. If the completion mode is
0083      * FinalCompletion some postprocessing is done asynchronously.
0084      * Emits lineDone when finished.
0085      * @param comp the completion that's to be processed
0086      * @param mode the mode of completion. Can be PreliminaryCompletion
0087      * (insert only) or FinalCompletion (also add () for functions, etc.)
0088      */
0089     void completeLine(const QString& comp, LineCompletionMode mode);
0090 
0091   protected:
0092 
0093     enum IdentifierType {
0094     VariableType,   ///< a variable
0095     FunctionWithArguments,   ///< a function that takes arguments
0096     FunctionType = FunctionWithArguments, ///< an alias for function with arguments
0097     FunctionWithoutArguments, ///< a function that takes no arguments
0098     KeywordType,  ///< a keyword
0099     UnknownType   ///< no identifier type was found
0100     };
0101     /**
0102      * returns the identifier for fetchIdentifierType
0103      * @return the identifier for fetchIdentifierType
0104      */
0105     QString identifier() const;
0106 
0107     /**
0108      * Sets the completions
0109      * @param completions list of possible completions
0110      */
0111     void setCompletions(const QStringList& completions);
0112     /**
0113      * sets the command/command-part
0114      * @param cmd the command/command-part
0115      */
0116     void setCommand(const QString& cmd);
0117     /**
0118      * Find an identifier in cmd that ends at index
0119      * @param cmd the command
0120      * @param index the index to look at
0121      */
0122     virtual int locateIdentifier(const QString& cmd, int index) const;
0123     /**
0124      * return true if c may be used in identifier names
0125      * @param c the character
0126      */
0127     virtual bool mayIdentifierContain(QChar c) const;
0128     /**
0129      * return true if identifier names can begin with c
0130      * @param c the character
0131      */
0132     virtual bool mayIdentifierBeginWith(QChar c) const;
0133     /**
0134      * Completes line with function identifier and emits lineDone with the
0135      * completed line. Helper function for completeLine.
0136      * @param type whether the function takes arguments, default: FunctionWithArguments
0137      */
0138     void completeFunctionLine(IdentifierType type = FunctionWithArguments);
0139     /**
0140      * Completes line with keyword identifier and emits lineDone with the
0141      * completed line. Helper function for completeLine.
0142      */
0143     void completeKeywordLine();
0144     /**
0145      * Completes line with variable identifier and emits lineDone with the
0146      * completed line. Helper function for completeLine.
0147      */
0148     void completeVariableLine();
0149     /**
0150      * Completes line with identifier of unknown type and emits lineDone with
0151      * the completed line. Helper function for completeLine.
0152      */
0153     void completeUnknownLine();
0154   protected Q_SLOTS:
0155     /**
0156      * This function should be reimplemented to start the actual fetching
0157      * of the completions. It can be asynchronous.
0158      * Remember to emit fetchingDone, if the fetching is complete
0159      */
0160     virtual void fetchCompletions() = 0;
0161     /**
0162      * Fetch the identifier type of d->identifier; reimplemented in
0163      * the backends. Emit fetchingTypeDone when done.
0164      */
0165     virtual void fetchIdentifierType();
0166     /**
0167      * Find the completion. To be called when fetching is done.
0168      * Emits done() when done.
0169      */
0170     void findCompletion();
0171     /**
0172      * Calls the appropriate complete*Line based on type
0173      * @param type the identifier type found in line()
0174      */
0175     void completeLineWithType(Cantor::CompletionObject::IdentifierType type);
0176     /**
0177      * Handle a completion request after a opening parenthesis.
0178      * @param type the type of the identifier before the parenthesis
0179      */
0180     void handleParenCompletionWithType(Cantor::CompletionObject::IdentifierType type);
0181   Q_SIGNALS:
0182     /**
0183      * indicates that the fetching of completions is done
0184      */
0185     void fetchingDone();
0186     /**
0187      * indicates that the type of identifier() was found and passes the
0188      * type as an argument
0189      * @param type the identifier type
0190      */
0191     void fetchingTypeDone(Cantor::CompletionObject::IdentifierType type);
0192     /**
0193      * indicates that the possible completions and a common completion string
0194      * have been found
0195      */
0196     void done();
0197     /**
0198      * emitted when the line completion is done, passes the new line and
0199      * the cursor index
0200      * @param line the new line
0201      * @param index the new cursor index
0202      */
0203     void lineDone(const QString &line, int index);
0204   private:
0205     CompletionObjectPrivate* d;
0206 };
0207 
0208 }
0209 
0210 #endif /* _COMPLETIONOBJECT_H */