[apps/code] Editor asks VariableBox for autocompletion

This commit is contained in:
Léa Saviot
2020-03-18 13:43:04 +01:00
committed by Émilie Feral
parent 72aedaadd0
commit f883516716
7 changed files with 45 additions and 4 deletions

View File

@@ -251,9 +251,12 @@ void PythonTextArea::addAutocompletion() {
{
/* The previous code point is neither the beginning of the text, nor a
* space, nor a \n, and the next code point is the end of the word.
* Compute the text to insert */
// TODO LEA
textToInsert = "test";
* Compute the text to insert:
* Look first in the current script variables and functions, then in the
* builtins, then in the imported modules/scripts. */
VariableBoxController * varBox = m_contentView.pythonDelegate()->variableBoxController();
const char * beginningOfWord = UTF8Helper::BeginningOfWord(m_contentView.editedText(), autocompletionLocation);
textToInsert = varBox->autocompletionForText(beginningOfWord);
}
// Try to insert the text (this might fail if the buffer is full)

View File

@@ -27,6 +27,7 @@ protected:
m_autocomplete(false)
{
}
App * pythonDelegate() { return m_pythonDelegate; }
void setAutocompleting(bool autocomplete) { m_autocomplete = autocomplete; }
bool isAutocompleting() const { return m_autocomplete; }
void loadSyntaxHighlighter();

View File

@@ -187,6 +187,10 @@ void VariableBoxController::loadFunctionsAndVariables() {
#endif
}
const char * VariableBoxController::autocompletionForText(const char * text) const {
return nullptr;
}
HighlightCell * VariableBoxController::leafCellAtIndex(int index) {
assert(index >= 0 && index < k_maxNumberOfDisplayedRows);
return &m_leafCells[index];

View File

@@ -27,6 +27,7 @@ public:
/* VariableBoxController */
void loadFunctionsAndVariables();
const char * autocompletionForText(const char * text) const;
private:
constexpr static int k_maxScriptObjectNameSize = 100;
constexpr static int k_maxNumberOfDisplayedRows = 6; // 240/40

View File

@@ -84,7 +84,9 @@ size_t GlyphOffsetAtCodePoint(const char * buffer, const char * position);
* For instance, strlen("∑") = 3 but StringGlyphLength("∑") = 1 */
size_t StringGlyphLength(const char * s, int maxSize = -1);
// Returns the position of the first char between ' ', '\n' and 0.
// Returns the position of the first previous char ' ', '\n' or text
const char * BeginningOfWord(const char * text, const char * word);
// Returns the position of the first following char ' ', '\n' or 0
const char * EndOfWord(const char * word);
};

View File

@@ -378,6 +378,23 @@ size_t StringGlyphLength(const char * s, int maxSize) {
return glyphIndex;
}
const char * BeginningOfWord(const char * text, const char * word) {
if (text == word) {
return text;
}
UTF8Decoder decoder(text, word);
const char * codePointPointer = decoder.stringPosition();
CodePoint codePoint = decoder.previousCodePoint();
while (!CodePointIsEndOfWord(codePoint)) {
codePointPointer = decoder.stringPosition();
if (codePointPointer == text) {
break;
}
codePoint = decoder.previousCodePoint();
}
return codePointPointer;
}
const char * EndOfWord(const char * word) {
UTF8Decoder decoder(word);
CodePoint codePoint = decoder.nextCodePoint();

View File

@@ -275,6 +275,19 @@ QUIZ_CASE(ion_utf8_helper_string_glyph_length) {
}
void assert_beginning_of_word_is(const char * text, const char * word, const char * beginningOfWord) {
quiz_assert(UTF8Helper::BeginningOfWord(text, word) == beginningOfWord);
}
QUIZ_CASE(ion_utf8_helper_beginning_of_word) {
const char * test_sentence = "01 34+ \n89";
assert_beginning_of_word_is(test_sentence, test_sentence, test_sentence);
assert_beginning_of_word_is(test_sentence, test_sentence + 1, test_sentence);
assert_beginning_of_word_is(test_sentence, test_sentence + 2, test_sentence);
assert_beginning_of_word_is(test_sentence, test_sentence + 5, test_sentence + 3);
assert_beginning_of_word_is(test_sentence, test_sentence + 8, test_sentence + 8);
}
void assert_end_of_word_is(const char * word, const char * endOfWord) {
quiz_assert(UTF8Helper::EndOfWord(word) == endOfWord);
}