~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to languages/kjssupport/jscodecompletion.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2006-05-23 18:39:42 UTC
  • Revision ID: james.westby@ubuntu.com-20060523183942-hucifbvh68k2bwz7
Tags: upstream-3.3.2
Import upstream version 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// C++ Implementation: jscodecompletion
 
3
//
 
4
// Description:
 
5
//
 
6
//
 
7
// Author: ian reinhart geiser <geiseri@kde.org>, (C) 2004
 
8
//
 
9
// Copyright: See COPYING file that comes with this distribution
 
10
//
 
11
//
 
12
#include "jscodecompletion.h"
 
13
#include <qwhatsthis.h>
 
14
 
 
15
#include <qfileinfo.h>
 
16
#include <qstringlist.h>
 
17
#include <qtextstream.h>
 
18
#include <qtimer.h>
 
19
#include <kapplication.h>
 
20
#include <qregexp.h>
 
21
 
 
22
#include <kiconloader.h>
 
23
#include <klocale.h>
 
24
#include <kprocess.h>
 
25
#include <kdebug.h>
 
26
#include <kaction.h>
 
27
#include <kparts/part.h>
 
28
#include <kdialogbase.h>
 
29
 
 
30
 
 
31
#include <kdevelop/kdevcore.h>
 
32
#include <kdevelop/kdevmainwindow.h>
 
33
#include <kdevelop/kdevlanguagesupport.h>
 
34
#include <kdevelop/kdevpartcontroller.h>
 
35
#include <kdevelop/kdevproject.h>
 
36
#include <kdevelop/kdevappfrontend.h>
 
37
#include <kdevelop/domutil.h>
 
38
#include <kdevelop/codemodel.h>
 
39
 
 
40
JSCodeCompletion::JSCodeCompletion(QObject *parent, const char *name)
 
41
                : QObject(parent, name)
 
42
{
 
43
        m_argWidgetShow = false;
 
44
        m_completionBoxShow=false;
 
45
}
 
46
 
 
47
 
 
48
JSCodeCompletion::~JSCodeCompletion()
 
49
{}
 
50
 
 
51
void JSCodeCompletion::setActiveEditorPart( KParts::Part * part )
 
52
{
 
53
        if (!part || !part->widget())
 
54
                return;
 
55
 
 
56
        kdDebug() << "JSCodeCompletion::setActiveEditorPart"  << endl;
 
57
 
 
58
        // We need to think about this
 
59
        //      if(!(m_config->getCodeCompletion() || m_config->getCodeHinting())){
 
60
        //              return; // no help
 
61
        //      }
 
62
 
 
63
        m_editInterface = dynamic_cast<KTextEditor::EditInterface*>(part);
 
64
        if (!m_editInterface)
 
65
        {
 
66
                kdDebug() << "editor doesn't support the EditDocumentIface" << endl;
 
67
                return;
 
68
        }
 
69
 
 
70
        m_cursorInterface = dynamic_cast<KTextEditor::ViewCursorInterface*>(part->widget());
 
71
        if (!m_cursorInterface)
 
72
        {
 
73
                kdDebug() << "editor does not support the ViewCursorInterface" << endl;
 
74
                return;
 
75
        }
 
76
 
 
77
        m_codeInterface = dynamic_cast<KTextEditor::CodeCompletionInterface*>(part->widget());
 
78
        if (!m_codeInterface)
 
79
        { // no CodeCompletionDocument available
 
80
                kdDebug() << "editor doesn't support the CodeCompletionDocumentIface" << endl;
 
81
                return;
 
82
        }
 
83
 
 
84
        disconnect(part->widget(), 0, this, 0 ); // to make sure that it is't connected twice
 
85
        connect(part->widget(), SIGNAL(cursorPositionChanged()),
 
86
                this, SLOT(cursorPositionChanged()));
 
87
        connect(part->widget(), SIGNAL(argHintHidden()), this, SLOT(argHintHidden()));
 
88
        connect(part->widget(), SIGNAL(completionAborted()), this, SLOT(completionBoxAbort()));
 
89
        connect(part->widget(), SIGNAL(completionDone()), this, SLOT(completionBoxHidden()));
 
90
}
 
91
 
 
92
QValueList< KTextEditor::CompletionEntry > JSCodeCompletion::getVars( const QString & startText )
 
93
{
 
94
        kdDebug() << "getVars for " << startText << endl;
 
95
        QValueList<KTextEditor::CompletionEntry> varList;
 
96
        /*
 
97
        QValueList<QString>::ConstIterator it;
 
98
        for (it = m_vars.begin(); it != m_vars.end(); ++it)
 
99
        {
 
100
                QString var = "$" + (*it);
 
101
                kdDebug() << "Compair " << var << endl;
 
102
                if( var.startsWith( startText ))
 
103
                {
 
104
                        KTextEditor::CompletionEntry e;
 
105
                        e.text = var;
 
106
                        //e.postfix ="";
 
107
                        //e.prefix ="";
 
108
                        kdDebug() << "getVar: " << var << endl;
 
109
                        varList.append(e);
 
110
                }
 
111
        }
 
112
        */
 
113
        return varList;
 
114
}
 
115
 
 
116
void JSCodeCompletion::cursorPositionChanged( )
 
117
{
 
118
        uint line, col;
 
119
        m_cursorInterface->cursorPositionReal(&line, &col);
 
120
        kdDebug() << "JSCodeCompletion::cursorPositionChanged:" << line << ":" << col  << endl;
 
121
 
 
122
        QString lineStr = m_editInterface->textLine(line);
 
123
        if(lineStr.isNull() || lineStr.isEmpty())
 
124
        {
 
125
                kdDebug() << "No Text..." << endl;
 
126
                return; // nothing to do
 
127
        }
 
128
        //      if(m_config->getCodeCompletion())
 
129
        //      {
 
130
        QString restLine = lineStr.mid(col);
 
131
        QString prevText = lineStr.mid(0,col);
 
132
 
 
133
        if(restLine.left(1) != " " && restLine.left(1) != "\t" && !restLine.isNull())
 
134
        {
 
135
                kdDebug() << "no codecompletion because no empty character after cursor:" << restLine << ":" << endl;
 
136
                return;
 
137
        }
 
138
 
 
139
        QRegExp prevReg("([\\d\\w]*)[.]$");
 
140
 
 
141
        if (prevReg.search( prevText ) != -1 )
 
142
        {
 
143
                // We are in completion mode
 
144
                QString startMatch = prevReg.cap(0);
 
145
                kdDebug() << "Matching: " << startMatch << endl;
 
146
                m_completionBoxShow=true;
 
147
                m_codeInterface->showCompletionBox(getVars(startMatch),2);
 
148
        }
 
149
        else
 
150
        {
 
151
                kdDebug() << "no vars in: " << prevText << endl;
 
152
                return;
 
153
        }
 
154
 
 
155
        //      }
 
156
 
 
157
 
 
158
}
 
159
 
 
160
void JSCodeCompletion::completionBoxHidden( )
 
161
{
 
162
        kdDebug() << "Complete..." << endl;
 
163
        m_completionBoxShow=false;
 
164
}
 
165
 
 
166
void JSCodeCompletion::completionBoxAbort( )
 
167
{
 
168
        kdDebug() << "aborted..." << endl;
 
169
        m_completionBoxShow=false;
 
170
 
 
171
}
 
172
 
 
173
 
 
174
#include "jscodecompletion.moc"