File indexing completed on 2024-05-05 04:36:50

0001 /* This file is part of KDevelop
0002  *
0003  * Copyright 2010 Niko Sams <niko.sams@gmail.com>
0004  * Copyright 2010 Alexander Dymo <adymo@kdevelop.org>
0005  * Copyright (C) 2011-2015 Miquel Sabaté Solà <mikisabate@gmail.com>
0006  *
0007  * This program is free software; you can redistribute it and/or modify
0008  * it under the terms of the GNU Library General Public License as
0009  * published by the Free Software Foundation; either version 2 of the
0010  * License, or (at your option) any later version.
0011  *
0012  * This program is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015  * GNU General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU General Public
0018  * License along with this program; if not, write to the
0019  * Free Software Foundation, Inc.,
0020  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0021  */
0022 
0023 #ifndef RUBY_CONTEXT_BUILDER_H
0024 #define RUBY_CONTEXT_BUILDER_H
0025 
0026 #include <duchain/duchainexport.h>
0027 #include <language/duchain/builders/abstractcontextbuilder.h>
0028 #include <language/duchain/problem.h>
0029 #include <parser/astvisitor.h>
0030 #include <util/path.h>
0031 
0032 namespace ruby {
0033 
0034 class EditorIntegrator;
0035 using ContextBuilderBase = KDevelop::AbstractContextBuilder<Ast, NameAst>;
0036 
0037 /**
0038  * @class ContextBuilder
0039  *
0040  * The ContextBuilder is a convenient class to handle contexts on the AST.
0041  * The other builders have this class as a base class.
0042  */
0043 class KDEVRUBYDUCHAIN_EXPORT ContextBuilder
0044     : public ContextBuilderBase
0045     , public AstVisitor
0046 {
0047 public:
0048     ContextBuilder();
0049     ~ContextBuilder() override;
0050 
0051     /// Re-implemented from KDevelop::AbstractContextBuilder.
0052     KDevelop::ReferencedTopDUContext build(const KDevelop::IndexedString &url,
0053                                            Ast *node,
0054                                            const KDevelop::ReferencedTopDUContext& updateContext = {}) override;
0055 
0056     /// @returns a list of unresolved imports.
0057     inline const QVector<KDevelop::IndexedString> unresolvedImports() const
0058     {
0059         return m_unresolvedImports;
0060     }
0061 
0062     /// Set the priority of the build to the given @p priority.
0063     inline void setPriority(int priority)
0064     {
0065         m_priority = priority;
0066     }
0067 
0068 protected:
0069     /// Set the given @p editor as the EditorIntegrator for this class.
0070     void setEditor(EditorIntegrator *editor);
0071 
0072     /**
0073      * @returns the editor used by the builder.
0074      * @note Used by KDevelop::AbstractContextBuilder.
0075      */
0076     EditorIntegrator * editor() const;
0077 
0078     /**
0079      * The following are methods re-implemented from
0080      * KDevelop::AbstractContextBuilder that deal with contexts and nodes.
0081      */
0082 
0083     void setContextOnNode(Ast *node, KDevelop::DUContext *ctx) override;
0084     KDevelop::DUContext * contextFromNode(Ast *node) override;
0085     KDevelop::DUContext * newContext(const KDevelop::RangeInRevision &range) override;
0086     KDevelop::TopDUContext * newTopContext(const KDevelop::RangeInRevision &range,
0087                                            KDevelop::ParsingEnvironmentFile *file = nullptr) override;
0088 
0089     /// And now methods that deal with nodes, documents and ranges.
0090 
0091     /// @returns KDevelop::CursorInRevision at the start of @param node.
0092     const KDevelop::CursorInRevision startPos(const Ast *node) const;
0093 
0094     /// Re-implemented from KDevelop::AbstractContextBuilder.
0095     KDevelop::RangeInRevision editorFindRange(Ast *fromRange, Ast *toRange) override;
0096 
0097     /// Given a @param node, it @returns a KDevelop::DocumentRange.
0098     const KDevelop::DocumentRange getDocumentRange(const Node *node) const;
0099 
0100     /// Given a @param range, it @returns a KDevelop::DocumentRange.
0101     const KDevelop::DocumentRange getDocumentRange(const KDevelop::RangeInRevision &range) const;
0102 
0103     /// Re-implemented from KDevelop::AbstractContextBuilder.
0104     KDevelop::QualifiedIdentifier identifierForNode(NameAst *name) override;
0105 
0106     /// Methods re-implemented from AstVisitor or mere helpers.
0107 
0108     void startVisiting(Ast *node) override;
0109     void visitModuleStatement(Ast *node) override;
0110     void visitClassStatement(Ast *node) override;
0111     void visitMethodStatement(Ast *node) override;
0112     void visitBlock(Ast *node) override;
0113     void visitRequire(Ast *node, bool relative = false) override;
0114 
0115     /**
0116      * Issue a require. The required file will be scheduled for parsing
0117      * if it's the first time that is loaded.
0118      *
0119      * @param path The path where the required file is located.
0120      */
0121     void require(const KDevelop::Path &path);
0122 
0123     /**
0124      * Append a new problem that appeared at the given @p node with @p msg
0125      * as its description. The @p sev is the severity of the problem, which
0126      * is IProblem::Error by default.
0127      * @note that you should call i18n() first.
0128      */
0129     void appendProblem(const Node *node, const QString &msg,
0130                        KDevelop::IProblem::Severity sev = KDevelop::IProblem::Error);
0131 
0132     /**
0133      * Append a new problem that appeared at the given @p range with @p msg
0134      * as its description. The @p sev is the severity of the problem, which
0135      * is IProblem::Error by default.
0136      * @note that you should call i18n() first.
0137      */
0138     void appendProblem(const KDevelop::RangeInRevision &range,
0139                        const QString &msg,
0140                        KDevelop::IProblem::Severity sev = KDevelop::IProblem::Error);
0141 
0142 protected:
0143     bool m_mapAst; // make KDevelop::AbstractContextBuilder happy.
0144     EditorIntegrator *m_editor;
0145     QVector<KDevelop::IndexedString> m_unresolvedImports;
0146 
0147 private:
0148     /**
0149      * @returns the range that contains the method arguments
0150      * contained in @param node.
0151      */
0152     const KDevelop::RangeInRevision rangeForMethodArguments(Ast *node);
0153 
0154 private:
0155     int m_priority;
0156     KDevelop::TopDUContextPointer m_builtinsContext;
0157 };
0158 
0159 }
0160 
0161 #endif // RUBY_CONTEXT_BUILDER_H