diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp
index 48de83d5ef2fb77b3552e6021a0bc2fbd9dc8766..ea4f665a7e9576bfce30f26c6d38dd2a1772f051 100644
--- a/src/plugins/texteditor/basetexteditor.cpp
+++ b/src/plugins/texteditor/basetexteditor.cpp
@@ -1603,7 +1603,6 @@ namespace TextEditor {
             int firstColumn;
             int lastColumn;
         };
-
     }
 }
 
diff --git a/src/plugins/texteditor/completionwidget.cpp b/src/plugins/texteditor/completionwidget.cpp
index 661cda0c3220c219f014ec236a88465a6588efa1..f9057d390781aadd2c20c4cc2811007db9a92160 100644
--- a/src/plugins/texteditor/completionwidget.cpp
+++ b/src/plugins/texteditor/completionwidget.cpp
@@ -39,8 +39,9 @@
 #include <utils/qtcassert.h>
 
 #include <QtCore/QEvent>
-#include <QtGui/QKeyEvent>
 #include <QtGui/QApplication>
+#include <QtGui/QDesktopWidget>
+#include <QtGui/QKeyEvent>
 #include <QtGui/QVBoxLayout>
 
 #include <limits.h>
@@ -130,6 +131,8 @@ CompletionWidget::CompletionWidget(CompletionSupport *support, ITextEditable *ed
     layout->addWidget(this);
 
     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+    m_popupFrame->setMinimumSize(1, 1);
+    setMinimumSize(1, 1);
 }
 
 bool CompletionWidget::event(QEvent *e)
@@ -227,20 +230,15 @@ void CompletionWidget::setCompletionItems(const QList<TextEditor::CompletionItem
 
 void CompletionWidget::showCompletions(int startPos)
 {
-    const QPoint &pos = m_editor->cursorRect(startPos).bottomLeft();
-    m_popupFrame->move(pos.x() - 16, pos.y());
-    m_popupFrame->setMinimumSize(1, 1);
-    setMinimumSize(1, 1);
-
-    updateSize();
-
+    updatePositionAndSize(startPos);
     m_popupFrame->show();
     show();
     setFocus();
 }
 
-void CompletionWidget::updateSize()
+void CompletionWidget::updatePositionAndSize(int startPos)
 {
+    // Determine size by calculating the space of the visible items
     int visibleItems = m_model->rowCount();
     if (visibleItems > NUMBER_OF_VISIBLE_ITEMS)
         visibleItems = NUMBER_OF_VISIBLE_ITEMS;
@@ -254,10 +252,25 @@ void CompletionWidget::updateSize()
             shint = tmp;
     }
 
-    const int width = (shint.width() + (m_popupFrame->frameWidth() * 2) + 30);
-    const int height = (shint.height() * visibleItems) + m_popupFrame->frameWidth() * 2;
+    const int frameWidth = m_popupFrame->frameWidth();
+    const int width = shint.width() + frameWidth * 2 + 30;
+    const int height = shint.height() * visibleItems + frameWidth * 2;
+
+    // Determine the position, keeping the popup on the screen
+    const QRect cursorRect = m_editor->cursorRect(startPos);
+    const QDesktopWidget *desktop = QApplication::desktop();
+    const QRect screen = desktop->availableGeometry(desktop->screenNumber(this));
+
+    QPoint pos = cursorRect.bottomLeft();
+    pos.rx() -= 16 + frameWidth;    // Space for the icons
+
+    if (pos.y() + height > screen.bottom())
+        pos.setY(cursorRect.top() - height);
+
+    if (pos.x() + width > screen.right())
+        pos.setX(screen.right() - width);
 
-    m_popupFrame->resize(width, height);
+    m_popupFrame->setGeometry(pos.x(), pos.y(), width, height);
 }
 
 void CompletionWidget::completionActivated(const QModelIndex &index)
diff --git a/src/plugins/texteditor/completionwidget.h b/src/plugins/texteditor/completionwidget.h
index c1fb28fc1cf17f7df58f7c287393f13417e45c38..b124d2e257acaee4aca879370247b08e7a9b3b8e 100644
--- a/src/plugins/texteditor/completionwidget.h
+++ b/src/plugins/texteditor/completionwidget.h
@@ -74,7 +74,7 @@ private slots:
     void completionActivated(const QModelIndex &index);
 
 private:
-    void updateSize();
+    void updatePositionAndSize(int startPos);
 
     QPointer<QFrame> m_popupFrame;
     bool m_blockFocusOut;