summaryrefslogtreecommitdiffstats
path: root/languages/kjssupport/jscodecompletion.cpp
blob: 436e0a95be6dee167eeffa73a1db10fab70ccac6 (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
//
// C++ Implementation: jscodecompletion
//
// Description:
//
//
// Author: ian reinhart geiser <geiseri@kde.org>, (C) 2004
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "jscodecompletion.h"
#include <tqwhatsthis.h>

#include <tqfileinfo.h>
#include <tqstringlist.h>
#include <tqtextstream.h>
#include <tqtimer.h>
#include <tdeapplication.h>
#include <tqregexp.h>

#include <kiconloader.h>
#include <tdelocale.h>
#include <kprocess.h>
#include <kdebug.h>
#include <tdeaction.h>
#include <tdeparts/part.h>
#include <kdialogbase.h>


#include <tdevelop/kdevcore.h>
#include <tdevelop/kdevmainwindow.h>
#include <tdevelop/kdevlanguagesupport.h>
#include <tdevelop/kdevpartcontroller.h>
#include <tdevelop/kdevproject.h>
#include <tdevelop/kdevappfrontend.h>
#include <tdevelop/domutil.h>
#include <tdevelop/codemodel.h>

JSCodeCompletion::JSCodeCompletion(TQObject *parent, const char *name)
		: TQObject(parent, name)
{
	m_argWidgetShow = false;
	m_completionBoxShow=false;
}


JSCodeCompletion::~JSCodeCompletion()
{}

void JSCodeCompletion::setActiveEditorPart( KParts::Part * part )
{
	if (!part || !part->widget())
		return;

	kdDebug() << "JSCodeCompletion::setActiveEditorPart"  << endl;

	// We need to think about this
	//      if(!(m_config->getCodeCompletion() || m_config->getCodeHinting())){
	//              return; // no help
	//      }

	m_editInterface = dynamic_cast<KTextEditor::EditInterface*>(part);
	if (!m_editInterface)
	{
		kdDebug() << "editor doesn't support the EditDocumentIface" << endl;
		return;
	}

	m_cursorInterface = dynamic_cast<KTextEditor::ViewCursorInterface*>(part->widget());
	if (!m_cursorInterface)
	{
		kdDebug() << "editor does not support the ViewCursorInterface" << endl;
		return;
	}

	m_codeInterface = dynamic_cast<KTextEditor::CodeCompletionInterface*>(part->widget());
	if (!m_codeInterface)
	{ // no CodeCompletionDocument available
		kdDebug() << "editor doesn't support the CodeCompletionDocumentIface" << endl;
		return;
	}

	disconnect(part->widget(), 0, this, 0 ); // to make sure that it is't connected twice
	connect(part->widget(), TQT_SIGNAL(cursorPositionChanged()),
	        this, TQT_SLOT(cursorPositionChanged()));
	connect(part->widget(), TQT_SIGNAL(argHintHidden()), this, TQT_SLOT(argHintHidden()));
	connect(part->widget(), TQT_SIGNAL(completionAborted()), this, TQT_SLOT(completionBoxAbort()));
	connect(part->widget(), TQT_SIGNAL(completionDone()), this, TQT_SLOT(completionBoxHidden()));
}

TQValueList< KTextEditor::CompletionEntry > JSCodeCompletion::getVars( const TQString & startText )
{
	kdDebug() << "getVars for " << startText << endl;
	TQValueList<KTextEditor::CompletionEntry> varList;
	/*
	TQValueList<TQString>::ConstIterator it;
	for (it = m_vars.begin(); it != m_vars.end(); ++it)
	{
		TQString var = "$" + (*it);
		kdDebug() << "Compair " << var << endl;
		if( var.startsWith( startText ))
		{
			KTextEditor::CompletionEntry e;
			e.text = var;
			//e.postfix ="";
			//e.prefix ="";
			kdDebug() << "getVar: " << var << endl;
			varList.append(e);
		}
	}
	*/
	return varList;
}

void JSCodeCompletion::cursorPositionChanged( )
{
	uint line, col;
	m_cursorInterface->cursorPositionReal(&line, &col);
	kdDebug() << "JSCodeCompletion::cursorPositionChanged:" << line << ":" << col  << endl;

	TQString lineStr = m_editInterface->textLine(line);
	if(lineStr.isNull() || lineStr.isEmpty())
	{
		kdDebug() << "No Text..." << endl;
		return; // nothing to do
	}
	//      if(m_config->getCodeCompletion())
	//      {
	TQString restLine = lineStr.mid(col);
	TQString prevText = lineStr.mid(0,col);

	if(restLine.left(1) != " " && restLine.left(1) != "\t" && !restLine.isNull())
	{
		kdDebug() << "no codecompletion because no empty character after cursor:" << restLine << ":" << endl;
		return;
	}

	TQRegExp prevReg("([\\d\\w]*)[.]$");

	if (prevReg.search( prevText ) != -1 )
	{
		// We are in completion mode
		TQString startMatch = prevReg.cap(0);
		kdDebug() << "Matching: " << startMatch << endl;
		m_completionBoxShow=true;
		m_codeInterface->showCompletionBox(getVars(startMatch),2);
	}
	else
	{
		kdDebug() << "no vars in: " << prevText << endl;
		return;
	}

	//      }


}

void JSCodeCompletion::completionBoxHidden( )
{
	kdDebug() << "Complete..." << endl;
	m_completionBoxShow=false;
}

void JSCodeCompletion::completionBoxAbort( )
{
	kdDebug() << "aborted..." << endl;
	m_completionBoxShow=false;

}


#include "jscodecompletion.moc"