Skip to content

Commit

Permalink
ENH: Improve Python autocomplete list ordering
Browse files Browse the repository at this point in the history
Before this change, a strict alphabetical sorting was used, which means that order of attributes was:
- uppercase letters: constants + internal classes + VTK methods
- underscore: private attributes
- lowercase letters: Python and Qt attributes and methods

For example attributes of a Qt class:

```
AllowNestedDocks
AllowTabbedDocks
AnimatedDocks
DockOption()
...
PdmPhysicalDpiY
PdmWidth
PdmWidthMM
RenderFlag()
RenderFlags()
VerticalTabs
__bool__()
__class__()
__delattr__()
__dict__
__dir__()
__doc__
__eq__()
__format__()
...
__setattr__()
__sizeof__()
__str__()
__subclasshook__()
__weakref__
acceptDrops
accessibleDescription
accessibleName
actionEvent()
addToolBar()
...
windowRole()
windowState()
windowTitle
windowTitleChanged()
windowType()
x
y
```

This means that the user must scroll through a lot of irrelevant parts before gets to method names.

After this change, attributes are grouped into the following categories and displayed in this order:
- Python and Qt attributes and methods (starts with lowercase letter)
- VTK methods + static classes and types (starts with uppercase letter, ends with parenthesis)
- constants (starts with uppercase letter, does not end with parenthesis)
- private attributes (starts with underscore)

Case insensitive comparison is used within each group.

Example:

```
acceptDrops
accessibleDescription
accessibleName
actionEvent()
actions()
activateWindow()
addAction()
...
windowType()
winId()
x
y
DockOption()
DockOptions()
PaintDeviceMetric()
RenderFlag()
RenderFlags()
AllowNestedDocks
AllowTabbedDocks
AnimatedDocks
DrawChildren
...
PdmWidthMM
VerticalTabs
__bool__()
__class__()
__delattr__()
...
__str__()
__subclasshook__()
__weakref__
```
  • Loading branch information
lassoan committed Oct 18, 2021
1 parent 1d19b4b commit 08461b3
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Libs/Scripting/Python/Widgets/ctkPythonConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ class ctkPythonConsoleCompleter : public ctkConsoleCompleter
virtual void updateCompletionModel(const QString& completion);
static QString searchUsableCharForCompletion(const QString& completion);

/// Sort Python attributes in the following groups (with case insensitive sorting within each group):
/// - Python and Qt attributes and methods (starts with lowercase letter)
/// - VTK methods + static classes and types (starts with uppercase letter, ends with parenthesis)
/// - constants (starts with uppercase letter, does not end with parenthesis)
/// - private attributes (starts with underscore)
static bool PythonAttributeLessThan(const QString& s1, const QString& s2);

protected:
bool isInUserDefinedClass(const QString &pythonFunctionPath);
Expand Down Expand Up @@ -345,6 +351,48 @@ QString ctkPythonConsoleCompleter::searchUsableCharForCompletion(const QString&
return textToComplete;
}

//----------------------------------------------------------------------------
bool ctkPythonConsoleCompleter::PythonAttributeLessThan(const QString& s1, const QString& s2)
{
if (!s1.isEmpty() || !s2.isEmpty())
{
// Move Python private attributes to the back (start with underscore)
if (s1[0] == "_" && s2[0] != "_")
{
return false;
}
if (s1[0] != "_" && s2[0] == "_")
{
return true;
}
// Move Python and Qt attributes and methods to the front (start with lowercase)
if (s1[0].isLower() && !s2[0].isLower())
{
return true;
}
if (!s1[0].isLower() && s2[0].isLower())
{
return false;
}
// Move VTK methods and internal classes (start with uppercase and end with parentheses)
// above constants (start with uppercase and does not end with parentheses)
if (s1[0].isUpper() && s2[0].isUpper())
{
if (s1.endsWith("()") && !s2.endsWith("()"))
{
return true;
}
if (!s1.endsWith("()") && s2.endsWith("()"))
{
return false;
}
}
}

// use case insensitive sorting
return s1.toLower() < s2.toLower();
}

//----------------------------------------------------------------------------
void ctkPythonConsoleCompleter::updateCompletionModel(const QString& completion)
{
Expand Down Expand Up @@ -381,6 +429,7 @@ void ctkPythonConsoleCompleter::updateCompletionModel(const QString& completion)
attrs << this->PythonManager.pythonAttributes(lookup, module.toLatin1(),
appendParenthesis);
attrs.removeDuplicates();
std::sort(attrs.begin(), attrs.end(), ctkPythonConsoleCompleter::PythonAttributeLessThan);
}

// Initialize the completion model
Expand Down

0 comments on commit 08461b3

Please sign in to comment.