Parcourir la source

Implement completion on Ctrl+Space (or Ctrl+M).

git-svn-id: file:///var/svn/tigccpp/trunk@839 9552661e-59e3-4036-b4f2-dbe53926924f
kevinkofler il y a 18 ans
Parent
commit
ac541b7eb2
6 fichiers modifiés avec 111 ajouts et 1 suppressions
  1. 46 0
      ktigcc/completion.cpp
  2. 12 0
      ktigcc/completion.h
  3. 7 0
      ktigcc/mainform.ui
  4. 27 1
      ktigcc/mainform.ui.h
  5. 1 0
      ktigcc/srcfilewin.ui
  6. 18 0
      ktigcc/srcfilewin.ui.h

+ 46 - 0
ktigcc/completion.cpp

@@ -195,6 +195,22 @@ bool completionEntriesForFile(const QString &fileText,
   return completionEntriesForFileRecursive(fileText,fileName,mainForm,result);
 }
 
+static QValueList<KTextEditor::CompletionEntry> sortCompletionEntries(
+  const QValueList<KTextEditor::CompletionEntry> &entries)
+{
+  QMap<QString,QValueList<KTextEditor::CompletionEntry> > map;
+  for (QValueList<KTextEditor::CompletionEntry>::ConstIterator it=entries.begin();
+       it!=entries.end(); ++it) {
+    const KTextEditor::CompletionEntry &entry=*it;
+    map[entry.text].append(entry);
+  }
+  QValueList<KTextEditor::CompletionEntry> result;
+  for (QMap<QString,QValueList<KTextEditor::CompletionEntry> >::ConstIterator
+       it=map.begin(); it!=map.end(); ++it)
+    mergeCompletionEntries(result,*it);
+  return result;
+}
+
 bool parseHelpSources(QWidget *parent, const QString &directory,
                       QMap<QString,CompletionInfo> &sysHdrCompletion)
 {
@@ -321,3 +337,33 @@ void TemplatePopup::QPopupMenu_activated(int id)
     view->setCursorPositionReal(row,col);
   } else view->insertText(code);
 }
+
+CompletionPopup::CompletionPopup(Kate::View *parent, const QString &fileName,
+                                 MainForm *mainForm, QObject *receiver)
+  : QObject(parent)
+{
+  connect(this,SIGNAL(closed()),receiver,SLOT(completionPopup_closed()));
+  QValueList<KTextEditor::CompletionEntry> entries;
+  if (!completionEntriesForFile(parent->getDoc()->text(),fileName,mainForm,
+                                entries)) {
+    emit closed();
+    deleteLater();
+    return;
+  }
+  entries=sortCompletionEntries(entries);
+  unsigned column=parent->cursorColumnReal();
+  int offset=0;
+  if (column) {
+    QString textLine=parent->currentTextLine();
+    if (column<=textLine.length()) {
+      while (column && (textLine[--column].isLetterOrNumber()
+                        || textLine[column]=='_' || textLine[column]=='$'))
+        offset++;
+    }
+  }
+  connect(parent,SIGNAL(completionAborted()),this,SLOT(deleteLater()));
+  connect(parent,SIGNAL(completionAborted()),this,SIGNAL(closed()));
+  connect(parent,SIGNAL(completionDone()),this,SLOT(deleteLater()));
+  connect(parent,SIGNAL(completionDone()),this,SIGNAL(closed()));
+  parent->showCompletionBox(entries,offset);
+}

+ 12 - 0
ktigcc/completion.h

@@ -74,3 +74,15 @@ class TemplatePopup : public QPopupMenu {
   private:
     Kate::View *view;
 };
+
+class CompletionPopup : public QObject {
+  Q_OBJECT
+
+  public:
+    CompletionPopup(Kate::View *parent, const QString &fileName,
+                    MainForm *mainForm, QObject *receiver);
+    virtual ~CompletionPopup() {}
+
+  signals:
+    void closed();
+};

+ 7 - 0
ktigcc/mainform.ui

@@ -23,6 +23,12 @@
             <property name="name">
                 <cstring>splitter</cstring>
             </property>
+            <property name="frameShape">
+                <enum>NoFrame</enum>
+            </property>
+            <property name="frameShadow">
+                <enum>Plain</enum>
+            </property>
             <property name="orientation">
                 <enum>Horizontal</enum>
             </property>
@@ -1687,6 +1693,7 @@
     <slot>clipboard_dataChanged()</slot>
     <slot>fileTreeItemRenamed( QListViewItem * item, const QString &amp; newName, int col )</slot>
     <slot>KDirWatch_dirty( const QString &amp; fileName )</slot>
+    <slot>completionPopup_closed()</slot>
 </slots>
 <functions>
     <function specifier="static">deleteErrorsForLVFile( QListViewItem * item )</function>

+ 27 - 1
ktigcc/mainform.ui.h

@@ -1305,7 +1305,13 @@ void MainForm::accel_activated(int index)
         break;
       case 9:
       case 10:
-        // TODO: completion
+        if (IS_FILE(currentListItem)
+            && CURRENT_VIEW==static_cast<ListViewFile *>(currentListItem)->kateView) {
+          // Disable newLineHook.
+          accel->setItemEnabled(6,FALSE);
+          accel->setItemEnabled(7,FALSE);
+          new CompletionPopup(CURRENT_VIEW,pathInProject(currentListItem),this,this);
+        }
         break;
       case 11: // next file
       case 12:
@@ -1351,6 +1357,18 @@ void MainForm::accel_activated(int index)
   }
 }
 
+void MainForm::completionPopup_closed()
+{
+  if (IS_FILE(currentListItem)) {
+    CATEGORY_OF(category,currentListItem->parent());
+    if (IS_EDITABLE_CATEGORY(category)) {
+      // Restore newLineHook.
+      accel->setItemEnabled(6,TRUE);
+      accel->setItemEnabled(7,TRUE);
+    }
+  }
+}
+
 void MainForm::fileTreeAccel_activated(int index)
 {
   QListViewItem *item=fileTree->currentItem();
@@ -5221,6 +5239,8 @@ void MainForm::fileTreeClicked(QListViewItem *item)
     accel->setItemEnabled(6,FALSE);
     accel->setItemEnabled(7,FALSE);
     accel->setItemEnabled(8,FALSE);
+    accel->setItemEnabled(9,FALSE);
+    accel->setItemEnabled(10,FALSE);
   } else if (IS_FILE(item)) {
     fileNewFolderAction->setEnabled(TRUE);
     CATEGORY_OF(category,item->parent());
@@ -5258,6 +5278,8 @@ void MainForm::fileTreeClicked(QListViewItem *item)
       accel->setItemEnabled(6,TRUE);
       accel->setItemEnabled(7,TRUE);
       accel->setItemEnabled(8,TRUE);
+      accel->setItemEnabled(9,TRUE);
+      accel->setItemEnabled(10,TRUE);
     } else {
       filePrintAction->setEnabled(FALSE);
       filePrintQuicklyAction->setEnabled(FALSE);
@@ -5282,6 +5304,8 @@ void MainForm::fileTreeClicked(QListViewItem *item)
       accel->setItemEnabled(6,FALSE);
       accel->setItemEnabled(7,FALSE);
       accel->setItemEnabled(8,FALSE);
+      accel->setItemEnabled(9,FALSE);
+      accel->setItemEnabled(10,FALSE);
     }
   } else {
     fileNewFolderAction->setEnabled(FALSE);
@@ -5308,6 +5332,8 @@ void MainForm::fileTreeClicked(QListViewItem *item)
     accel->setItemEnabled(6,FALSE);
     accel->setItemEnabled(7,FALSE);
     accel->setItemEnabled(8,FALSE);
+    accel->setItemEnabled(9,FALSE);
+    accel->setItemEnabled(10,FALSE);
   }
   currentListItem=item;
   updateLeftStatusLabel();

+ 1 - 0
ktigcc/srcfilewin.ui

@@ -663,6 +663,7 @@
     <slot>current_view_charactersInteractivelyInserted( int line, int col, const QString &amp; characters )</slot>
     <slot>clipboard_dataChanged()</slot>
     <slot>KDirWatch_dirty( const QString &amp; fileName )</slot>
+    <slot>completionPopup_closed()</slot>
 </slots>
 <functions>
     <function>initBase()</function>

+ 18 - 0
ktigcc/srcfilewin.ui.h

@@ -224,6 +224,8 @@ void SourceFileWindow::initBase()
   THIS->accel->insertItem(Key_Enter,6);
   THIS->accel->insertItem(Key_Return,7);
   THIS->accel->insertItem(CTRL+Key_J,8);
+  THIS->accel->insertItem(CTRL+Key_Space,9);
+  THIS->accel->insertItem(CTRL+Key_M,10);
   THIS->accel->setItemEnabled(0,!!(CURRENT_VIEW->getDoc()->undoCount()));
   THIS->accel->setItemEnabled(1,!!(CURRENT_VIEW->getDoc()->redoCount()));
   THIS->accel->setItemEnabled(2,CURRENT_VIEW->getDoc()->hasSelection());
@@ -233,6 +235,8 @@ void SourceFileWindow::initBase()
   THIS->accel->setItemEnabled(6,TRUE);
   THIS->accel->setItemEnabled(7,TRUE);
   THIS->accel->setItemEnabled(8,TRUE);
+  THIS->accel->setItemEnabled(9,TRUE);
+  THIS->accel->setItemEnabled(10,TRUE);
   connect(THIS->accel,SIGNAL(activated(int)),this,SLOT(accel_activated(int)));
   if (preferences.useSystemIcons) {
     setUsesBigPixmaps(TRUE);
@@ -357,6 +361,13 @@ void SourceFileWindow::accel_activated(int index)
       case 8:
         new TemplatePopup(CURRENT_VIEW);
         break;
+      case 9:
+      case 10:
+        // Disable newLineHook.
+        THIS->accel->setItemEnabled(6,FALSE);
+        THIS->accel->setItemEnabled(7,FALSE);
+        new CompletionPopup(CURRENT_VIEW,THIS->fileName,THIS->mainForm,this);
+        break;
       default: break;
     }
   } else if (index == 6 || index == 7) {
@@ -365,6 +376,13 @@ void SourceFileWindow::accel_activated(int index)
   }
 }
 
+void SourceFileWindow::completionPopup_closed()
+{
+  // Restore newLineHook.
+  THIS->accel->setItemEnabled(6,TRUE);
+  THIS->accel->setItemEnabled(7,TRUE);
+}
+
 void *SourceFileWindow::createView(const QString &fileName, const QString &fileText, const QString &hlModeName, unsigned tabWidth)
 {
   // Create Document object.