diff --git a/apps/code/python_text_area.cpp b/apps/code/python_text_area.cpp index 821895b2b..0ce5009ef 100644 --- a/apps/code/python_text_area.cpp +++ b/apps/code/python_text_area.cpp @@ -375,24 +375,30 @@ bool PythonTextArea::addAutocompletionTextAtIndex(int nextIndex, int * currentIn bool addParentheses = false; const char * textToInsert = varBox->autocompletionAlternativeAtIndex(autocompletionLocation - autocompletionTokenBeginning, &textToInsertLength, &addParentheses, nextIndex, currentIndexToUpdate); - // Try to insert the text (this might fail if the buffer is full) - if (textToInsert != nullptr - && textToInsertLength > 0 - && m_contentView.insertTextAtLocation(textToInsert, const_cast(autocompletionLocation), textToInsertLength)) - { + if (textToInsert == nullptr) { + return false; + } + + if (textToInsertLength > 0) { + // Try to insert the text (this might fail if the buffer is full) + if (!m_contentView.insertTextAtLocation(textToInsert, const_cast(autocompletionLocation), textToInsertLength)) { + return false; + } autocompletionLocation += textToInsertLength; m_contentView.setAutocompletionEnd(autocompletionLocation); - // Try to insert the parentheses if needed text - const char * parentheses = ScriptNodeCell::k_parentheses; - constexpr int parenthesesLength = 2; - assert(strlen(parentheses) == parenthesesLength); - if (addParentheses && m_contentView.insertTextAtLocation(parentheses, const_cast(autocompletionLocation), parenthesesLength)) { - m_contentView.setAutocompleting(true); - m_contentView.setAutocompletionEnd(autocompletionLocation + parenthesesLength); - } - return true; } - return false; + + // Try to insert the parentheses if needed + const char * parentheses = ScriptNodeCell::k_parentheses; + constexpr int parenthesesLength = 2; + assert(strlen(parentheses) == parenthesesLength); + /* If couldInsertText is false, we should not try to add the parentheses as + * there was already not enough space to add the autocompletion. */ + if (addParentheses && m_contentView.insertTextAtLocation(parentheses, const_cast(autocompletionLocation), parenthesesLength)) { + m_contentView.setAutocompleting(true); + m_contentView.setAutocompletionEnd(autocompletionLocation + parenthesesLength); + } + return true; } void PythonTextArea::cycleAutocompletion(bool downwards) { diff --git a/apps/code/variable_box_controller.cpp b/apps/code/variable_box_controller.cpp index 68b53e5e9..5f24c98a5 100644 --- a/apps/code/variable_box_controller.cpp +++ b/apps/code/variable_box_controller.cpp @@ -522,7 +522,7 @@ void VariableBoxController::loadBuiltinNodes(const char * textToAutocomplete, in bool strictlyStartsWith = false; startsWith = NodeNameCompare(&node, textToAutocomplete, textToAutocompleteLength, &strictlyStartsWith); if (startsWith == 0) { // The node name and name are equal - startsWith = builtinNames[i].type == ScriptNode::Type::WithParentheses ? 0 : -1; // We accept the node only if it has parentheses + startsWith = node.type() == ScriptNode::Type::WithParentheses ? 0 : -1; // We accept the node only if it has parentheses } else if (strictlyStartsWith) { startsWith = 0; } else if (startsWith > 0) {