summaryrefslogtreecommitdiffstats
path: root/languages/cpp/cppcodecompletion.h
blob: 4c2f4c8b36d3e76b797578057ca3c785f0c5500c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/***************************************************************************
                        cppcodecompletion.h  -  description
                           -------------------
  begin                : Sat Jul 21 2001
  copyright            : (C) 2001 by Victor R�er
  email                : [email protected]
  copyright            : (C) 2002,2003 by Roberto Raggi
  email                : [email protected]
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef __CPPCODECOMPLETION_H__
#define __CPPCODECOMPLETION_H__

#include "cppsupportpart.h"
#include "declarationinfo.h"

#include <ast.h>
#include <codemodel.h>
#include <set>

#include <tdetexteditor/viewcursorinterface.h>
#include <tdetexteditor/editinterface.h>
#include <tdetexteditor/codecompletioninterface.h>
#include <tdetexteditor/texthintinterface.h>
#include <tdetexteditor/cursorinterface.h>
#include <tdetexteditor/view.h>

#include <tqobject.h>
#include <tqmutex.h>
#include <tqstringlist.h>
#include <tqtimer.h>
#include <tqguardedptr.h>
#include <tqregexp.h>

#include "driver.h"
///A little debugging class
#include <tqpopupmenu.h>
class PopupTracker : public TQObject {
	Q_OBJECT
  
public:
	static PopupTracker* pt;
	
	static uint pendingPopups;
	
	static TQPopupMenu* createPopup( TQWidget* parent ) {
		if( !pt ) pt = new PopupTracker();
		TQPopupMenu* m = new TQPopupMenu( parent );
		++pendingPopups;
		connect( m, TQT_SIGNAL(destroyed()), pt, TQT_SLOT(destroyedPopup()) );
		return m;
	}
	
	static void print() {
		if( pendingPopups )
			kdDebug( 9007 ) << "PopupTracker: " << pendingPopups << " popups are still alive" << endl;
	}
	
public slots:
	void destroyedPopup() {
		--pendingPopups;
	}
};


class CodeCompletionEntry;
class CodeInformationRepository;
class SimpleContext;
class SimpleType;
class SimpleTypeNamespace;
class CppCodeCompletionData;
class SimpleTypeConfiguration;
class TypeDesc;
struct PopupFillerHelpStruct;
struct PopupClassViewFillerHelpStruct;
class SimpleTypeImpl;
class TranslationUnitAST;
namespace CppEvaluation
{
  class EvaluationResult;
}
struct ExpressionInfo;

typedef TDESharedPtr<SimpleTypeImpl> TypePointer;

class CppCodeCompletion : public TQObject
{
	Q_OBJECT
  
public:
    friend class SimpleType;
	enum CompletionMode
	{
	    NormalCompletion,
	    SignalCompletion,
	    SlotCompletion,
	    VirtualDeclCompletion
	};
	enum MemberAccessOp
	{
		NoOp,
		DotOp,
		ArrowOp
	};
    
public:
	CppCodeCompletion( CppSupportPart* part );
	virtual ~CppCodeCompletion();

	CodeInformationRepository* repository()
	{
		return m_repository;
	}
	CompletionMode completionMode() const
	{
		return m_completionMode;
	}

	TQString createTypeInfoString( int line, int column );
	
    TQString replaceCppComments( const TQString& contents );
    int expressionAt( const TQString& text, int index );
	TQStringList splitExpression( const TQString& text );
	
    CppEvaluation::EvaluationResult evaluateExpression( ExpressionInfo expr, SimpleContext* ctx );

    CppEvaluation::EvaluationResult evaluateExpressionAt( int line, int column, SimpleTypeConfiguration& conf, bool ifUnknownSetType = false );
    
    void contextEvaluationMenus ( TQPopupMenu *popup, const Context *context, int line, int col );

	CppSupportPart* cppSupport() const;

    HashedStringSet getIncludeFiles( const TQString& file = TQString() );

    static CppCodeCompletion* instance() {
      return m_instance;
    }

    ///Adds a string that will be ticked through the status-bar
    void addStatusText( TQString text, int timeout );
    void clearStatusText();

    TQString activeFileName() const {
      return m_activeFileName;
    }
  
public slots:
	/**
	 * @param invokedOnDemand if true and there is exactly one matching entry
	 *        complete the match immediately without showing the completion box.
	 *        This is only true, when the users invokes the completion himself
	 *        (eg presses the completion shortcut CTRL+space)
	 */
	void completeText( bool invokedOnDemand = false );
private slots:
    void emptyCache();
	void slotPartAdded( KParts::Part *part );
	void slotActivePartChanged( KParts::Part *part );
	void slotArgHintHidden();
	void slotCompletionBoxHidden();
	void slotTextChanged();
	void slotFileParsed( const TQString& fileName );
	void slotCodeModelUpdated( const TQString& fileName );
    void slotTimeout();
    void slotStatusTextTimeout();
    void computeFileEntryList();
    bool isTypeExpression( const TQString& expr );
	void slotTextHint( int line, int col, TQString &text );
    void popupAction( int number );
	void popupDefinitionAction( int number );
    void popupClassViewAction( int number );
	void synchronousParseReady( const TQString& file, ParsedFilePointer unit );
	void slotJumpToDefCursorContext();
	void slotJumpToDeclCursorContext();
	
private:
	enum FunctionType { Declaration, Definition };
	
    TypePointer createGlobalNamespace();
	bool functionContains( FunctionDom f , int line, int col );
	void getFunctionBody( FunctionDom f , int& line, int& col );
	void selectItem( ItemDom item );
    void addTypePopups( TQPopupMenu* parent, TypeDesc d, TQString depthAdd, TQString prefix = "" );
    void addTypeClassPopups( TQPopupMenu* parent, TypeDesc d, TQString depthAdd, TQString prefix = "" );
  TQValueList<TQStringList> computeSignatureList( CppEvaluation::EvaluationResult function );
	void integratePart( KParts::Part* part );
	void setupCodeInformationRepository();
	FunctionDefinitionAST* functionDefinition( AST* node );
	void computeRecoveryPoints( ParsedFilePointer unit );
	void computeRecoveryPointsLocked();
	void jumpCursorContext( FunctionType );
	bool getIncludeInfo( int line, TQString& includeFileName, TQString& includeFilePath, bool& usedProjectFiles );
	
    enum EvaluateExpressionOptions {
        IncludeStandardExpressions = 1,
        IncludeTypeExpression = 2,
        CompletionOption = 4, ///Cut off the last word because it is incomplete
	    SearchInFunctions = 8,
        SearchInClasses = 16,
        DefaultAsTypeExpression = 32, ///This makes the evaluation interpret any unidentified expression as a type-expression
        DefaultEvaluationOptions = 1 | 2 | 8 | 16,
        DefaultCompletionOptions = 1 | 4 | 8 | 16
    };
    
    bool mayBeTypeTail( int line, int column, TQString& append, bool inFunction = false );
    bool canBeTypePrefix( const TQString& prefix, bool inFunction = false );
    

    ExpressionInfo findExpressionAt( int line, int col, int startLine, int startCol, bool inFunction = false );
	SimpleContext* computeFunctionContext( FunctionDom f, int line, int col, SimpleTypeConfiguration& conf );
    
    CppEvaluation::EvaluationResult evaluateExpressionType( int line, int column, SimpleTypeConfiguration& conf, EvaluateExpressionOptions opt = DefaultCompletionOptions  );
    SimpleType unTypeDef( SimpleType scope , TQMap<TQString, TQString>& typedefs );
    
  //    TQString buildSignature( TypePointer currType );
    SimpleType typeOf( TQValueList<Tag>& tags, MemberAccessOp accessOp );
    
	/// @todo remove isInstance
	void computeCompletionEntryList( TQValueList<CodeCompletionEntry>& entryList, SimpleContext* ctx, bool isInstance, int depth = 0 );
	void computeCompletionEntryList( SimpleType type, TQValueList<CodeCompletionEntry>&
                entryList, const TQStringList& typeList, SimpleTypeNamespace* ns, std::set<HashedString>& ignore, bool isInstance, int depth = 0  );
	void computeCompletionEntryList( SimpleType type, TQValueList<CodeCompletionEntry>&
                entryList, const TQStringList& typeList, bool isInstance, int depth = 0  );
	void computeCompletionEntryList( SimpleType type, TQValueList<CodeCompletionEntry>& entryList, TQValueList<Tag>& tags, bool isInstance, int depth  );
	void computeCompletionEntryList( SimpleType type, TQValueList<CodeCompletionEntry>& entryList, ClassDom klass, bool isInstance, int depth   );
	void computeCompletionEntryList( SimpleType type, TQValueList<CodeCompletionEntry>& entryList, NamespaceDom scope, bool isInstance, int depth   );
	void computeCompletionEntryList( SimpleType type, TQValueList<CodeCompletionEntry>& entryList, const FunctionList& methods, bool isInstance, int depth );
	void computeCompletionEntryList( SimpleType type, TQValueList<CodeCompletionEntry>& entryList, const VariableList& attributes, bool isInstance, int depth );
	void computeCompletionEntryList( TQString parent, SimpleType type, TQValueList<CodeCompletionEntry>& entryList, const ClassList& lst, bool isInstance, int depth );
	void computeCompletionEntryList( TQString parent, SimpleType type, TQValueList<CodeCompletionEntry>& entryList, const TypeAliasList& lst, bool isInstance, int depth );
	void computeCompletionEntryList( SimpleType type, TQValueList<CodeCompletionEntry>& entryList, const NamespaceList& lst, bool isInstance, int depth );

	SimpleContext* computeContext( FunctionDefinitionAST* ast, int line, int col, int lineOffset, int colOffset );
	void computeContext( SimpleContext*& ctx, StatementAST* ast, int line, int col );
	void computeContext( SimpleContext*& ctx, StatementListAST* ast, int line, int col );
	void computeContext( SimpleContext*& ctx, IfStatementAST* ast, int line, int col );
	void computeContext( SimpleContext*& ctx, ForStatementAST* ast, int line, int col );
	void computeContext( SimpleContext*& ctx, DoStatementAST* ast, int line, int col );
	void computeContext( SimpleContext*& ctx, WhileStatementAST* ast, int line, int col );
	void computeContext( SimpleContext*& ctx, SwitchStatementAST* ast, int line, int col );
	void computeContext( SimpleContext*& ctx, TryBlockStatementAST* ast, int line, int col );
	void computeContext( SimpleContext*& ctx, CatchStatementListAST* ast, int line, int col );
	void computeContext( SimpleContext*& ctx, CatchStatementAST* ast, int line, int col );
	void computeContext( SimpleContext*& ctx, DeclarationStatementAST* ast, int line, int col );
	void computeContext( SimpleContext*& ctx, ConditionAST* ast, int line, int col );
	bool inContextScope( AST* ast, int line, int col, bool checkStart = true, bool checkEnd = true );

	TQString getText( int startLine, int startColumn, int endLine, int endColumn, int omitLine = -1 );
	
    
private:
    
    friend class SimpleTypeCatalog;
    friend class SimpleTypeCodeModel;
    friend class SimpleTypeImpl;
    friend class ExpressionEvaluation;
    friend class PopupFillerHelpStruct;
    friend class PopupClassViewFillerHelpStruct;
    TQGuardedPtr<CppSupportPart> m_pSupport;
	TQTimer* m_ccTimer;
    TQTimer* m_showStatusTextTimer;
    TQValueList<TQPair<int, TQString> > m_statusTextList;

    void fitContextItem( int nLine, int nColumn );
	void needRecoveryPoints();
    
	TQString m_activeFileName;
	KTextEditor::ViewCursorInterface* m_activeCursor;
	KTextEditor::EditInterface* m_activeEditor;
    KTextEditor::TextHintInterface* m_activeHintInterface;
	KTextEditor::CodeCompletionInterface* m_activeCompletion;
	KTextEditor::View* m_activeView;
	
	bool m_bArgHintShow;
	bool m_bCompletionBoxShow;
	bool m_blockForKeyword;
	bool m_demandCompletion;

	unsigned int m_ccLine;
	unsigned int m_ccColumn;
    static CppCodeCompletion* m_instance;

	CodeInformationRepository* m_repository;
	CppCodeCompletionData* d;
	CompletionMode m_completionMode;

	TQTime m_lastHintTime;

    //If more then the given count of comments were requested, all following ones will be blank.(Performance-reasons)
    void setMaxComments( int count );
    
    TQString commentFromItem( const SimpleType& parent, const ItemDom& item );
    TQString commentFromTag( const SimpleType& parent, Tag& tag );
        
    ItemDom m_cachedFromContext;  ///Can be a function or a class, representing the position from where the last completion was started. Necessary as long as all imports are put into the global namespace.
	
	TQRegExp m_includeRx;
	TQRegExp m_cppCodeCommentsRx;
	TQRegExp m_codeCompleteChRx;
	TQRegExp m_codeCompleteCh2Rx;
    TQValueList<KTextEditor::CompletionEntry> m_fileEntryList;

    int m_maxComments;
    
    typedef TQMap<int, DeclarationInfo> PopupActions;
    typedef TQMap<int, ItemDom> PopupClassViewActions;
    PopupActions m_popupActions;
    PopupActions m_popupDefinitionActions;
    PopupClassViewActions m_popupClassViewActions;
	
	// we need something to plug actions that are not in any menu 
	// into in order for their shortcuts to work
	TQWidget m_DummyActionWidget;
};

#endif 
// kate: indent-mode csands; tab-width 4;