Bug 60987 - auto complete directly if only one available completion
Summary: auto complete directly if only one available completion
Status: RESOLVED FIXED
Alias: None
Product: kdevelop
Classification: Applications
Component: Code completion (show other bugs)
Version: git master
Platform: Compiled Sources Linux
: NOR wishlist
Target Milestone: ---
Assignee: kdevelop-bugs-null
URL:
Keywords:
: 63324 (view as bug list)
Depends on:
Blocks:
 
Reported: 2003-07-09 14:49 UTC by Robert Jonsson
Modified: 2005-03-11 18:00 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Robert Jonsson 2003-07-09 14:49:23 UTC
Version:           3.0.0a5 (using KDE 3.1.2)
Installed from:    compiled sources
Compiler:          gcc version 3.3 (Mandrake Linux 9.2 3.3-2mdk)
OS:          Linux (i686) release 2.4.21-0.13mdk

If there is only one known completion when pressing alt-space, kdevelop should skip the selection box and complete the word directly.
Comment 1 Amilcar do Carmo Lucas 2003-08-28 12:15:46 UTC
*** Bug 63324 has been marked as a duplicate of this bug. ***
Comment 2 Adam Treat 2005-03-11 05:47:08 UTC
I don't think this is such a good idea.  We are completing on several different levels now, and autocompleting when you didn't intend would be bad.  The only even partially sane way this would work is if the completion had been specifically narrowed by keying, but even then it is a bad idea.
Comment 3 Dominik Haumann 2005-03-11 12:41:35 UTC
> I don't think this is such a good idea.  We are completing on several
> different levels now, and autocompleting when you didn't intend would
> be bad.
Only do this if you press CTRL+space, eg. invoke the autocompletion by hand (To use your words: "The user *does intend* to autocomplete"). There should be no problem.

Example: class foo { void fooBarBaz();};
foo f; f.fooBar|  <- now CTRL+space and the Baz appears immediately without an extra box, because there is only one entry and the developer very probably knows what he is doing/wants.

In other words: I think it is a good idea :)
Comment 4 Adam Treat 2005-03-11 13:17:13 UTC
Well, when you go being reasonable like that...  I'll reopen, but this isn't going to happen for awhile because KTE doesn't make it easy to implement this.
Comment 5 Jay 2005-03-11 13:46:20 UTC
Thanks Dominik. An additional (and very good idea IMHO :) ) would be to hint that there is only one available completion. I've seen it done with tooltips in a windows IDE. E.g. you type your
foo.doVeryIn
and a tooltip appears above the cursor showing foo.doVeryInterestingStuff. When I hit the completion key, the text in the tooltip is used.

This feature was also used when writing method implementations. The header with the method signature was already there, then in a .cpp file write:
void Foo::doSomething(
and a tooltip appears with the complete parameter list. Pressing the completion key fills the whole parameter list in and closes the brace.
Comment 6 Dominik Haumann 2005-03-11 14:39:27 UTC
@Adam Trea: hm, what about

if ( entryList.size() == 1 && invokedOnDemand ) { /* complete immediately*/
  m_activeEditor->insertText(m_ccLine, m_ccColumn, entryList[0].text);
} else if (entryList.size()) { /* as it was before */ }

so you do not have to go the CompletionBox-way at all. Still missing is the bool "invokedOnDemand" :) This could be a parameter of void completeText(); maybe.


@Jörg Rüppel: only one wish/bug per bug report. This has in general nothing to do with the original bug report, so please open another one :) I even believe that showing tooltips is not possible right now (that right?).
Comment 7 Adam Treat 2005-03-11 16:11:30 UTC
Doh!  Of course, the completion box doesn't need to be shown at all in this case. I'll let you complete the feature since you've obviously got the idea...
Comment 8 Dominik Haumann 2005-03-11 17:54:05 UTC
CVS commit by dhaumann: 

fix bug 60987: auto complete directly if only one available completion
Reviewed by Adam Treat.

BUG: 60987


  M +27 -2     cppcodecompletion.cpp   1.163
  M +7 -1      cppcodecompletion.h   1.55
  M +1 -1      cppsupportpart.cpp   1.290


--- kdevelop/languages/cpp/cppcodecompletion.cpp  #1.162:1.163
@@ -860,5 +860,5 @@ QStringList CppCodeCompletion::evaluateE
 }
 
-void CppCodeCompletion::completeText( )
+void CppCodeCompletion::completeText( bool invokedOnDemand /*= false*/ )
 {
         kdDebug( 9007 ) << "CppCodeCompletion::completeText()" << endl;
@@ -1230,5 +1230,30 @@ void CppCodeCompletion::completeText( )
                 }
 
-                if ( entryList.size() )
+                QStringList trueMatches;
+
+                if ( invokedOnDemand )
+                {
+                        // find matching words
+                        QValueList<KTextEditor::CompletionEntry>::Iterator it;
+                        for ( it = entryList.begin(); it != entryList.end(); ++it )
+                        {
+                                if ( (*it).text.startsWith( word :) )
+                                {
+                                        trueMatches << (*it).text;
+
+                                        // if more than one entry matches, abort immediately
+                                        if ( trueMatches.size() > 1 )
+                                                break;
+                                }
+                        }
+                }
+
+                if ( invokedOnDemand && trueMatches.size() == 1 )
+                {
+                        // there is only one entry -> complete immediately
+                        m_activeEditor->insertText( m_ccLine, m_ccColumn,
+                                trueMatches[0].right( trueMatches[0].length() - word.length() ) );
+                }
+                else if ( entryList.size() )
                 {
                         entryList = unique( entryList );

--- kdevelop/languages/cpp/cppcodecompletion.h  #1.54:1.55
@@ -74,5 +74,11 @@ public:
 
 public slots:
-        void completeText();
+        /**
+         * @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:

--- kdevelop/languages/cpp/cppsupportpart.cpp  #1.289:1.290
@@ -1014,5 +1014,5 @@ void CppSupportPart::slotCompleteText()
         if ( !m_pCompletion )
                 return ;
-        m_pCompletion->completeText();
+        m_pCompletion->completeText( true );
 }
 
Comment 9 Jay 2005-03-11 18:00:36 UTC
yay, thanks!