[apps/code] Autocompletion steps

This commit is contained in:
Léa Saviot
2020-03-13 16:00:52 +01:00
committed by Émilie Feral
parent 343d2328b8
commit 938e0dd840
4 changed files with 45 additions and 1 deletions

View File

@@ -196,4 +196,38 @@ KDRect PythonTextArea::ContentView::dirtyRectFromPosition(const char * position,
);
}
bool PythonTextArea::handleEventWithText(const char * text, bool indentation, bool forceCursorRightOfText) {
if (*text == 0) {
return false;
}
if (m_contentView.isAutocompleting()) {
removeAutocompletion();
}
bool result = TextArea::handleEventWithText(text, indentation, forceCursorRightOfText);
addAutocompletion();
return result;
}
void PythonTextArea::removeAutocompletion() {
assert(m_contentView.isAutocompleting());
const char * autocompleteStart = m_contentView.cursorLocation();
const char * autocompleteEnd = UTF8Helper::EndOfWord(autocompleteStart);
assert(autocompleteEnd > autocompleteStart);
m_contentView.removeText(autocompleteStart, autocompleteEnd);
m_contentView.setAutocompleting(false);
}
void PythonTextArea::addAutocompletion() {
assert(!m_contentView.isAutocompleting());
// Compute the text to insert
// TODO LEA
const char * textToInsert = "test";
// Try to insert the text (this might fail if the buffer is full)
if (textToInsert && m_contentView.insertTextAtLocation(textToInsert, const_cast<char *>(cursorLocation()))) {
m_contentView.setAutocompleting(true);
}
}
}

View File

@@ -16,6 +16,7 @@ public:
}
void loadSyntaxHighlighter() { m_contentView.loadSyntaxHighlighter(); }
void unloadSyntaxHighlighter() { m_contentView.unloadSyntaxHighlighter(); }
bool handleEventWithText(const char * text, bool indentation = false, bool forceCursorRightOfText = false) override;
protected:
class ContentView : public TextArea::ContentView {
public:
@@ -25,6 +26,8 @@ protected:
m_autocomplete(false)
{
}
void setAutocompleting(bool autocomplete) { m_autocomplete = autocomplete; }
bool isAutocompleting() const { return m_autocomplete; }
void loadSyntaxHighlighter();
void unloadSyntaxHighlighter();
void clearRect(KDContext * ctx, KDRect rect) const override;
@@ -35,6 +38,8 @@ protected:
bool m_autocomplete;
};
private:
void removeAutocompletion();
void addAutocompletion();
const ContentView * nonEditableContentView() const override { return &m_contentView; }
ContentView m_contentView;
};

View File

@@ -122,6 +122,7 @@ protected:
bool removePreviousGlyph() override;
bool removeEndOfLine() override;
bool removeStartOfLine();
size_t removeText(const char * start, const char * end);
size_t deleteSelection() override;
protected:
KDRect glyphFrameAtPosition(const char * text, const char * position) const override;

View File

@@ -521,9 +521,13 @@ bool TextArea::ContentView::removeStartOfLine() {
return false;
}
size_t TextArea::ContentView::removeText(const char * start, const char * end) {
return m_text.removeText(start, end);
}
size_t TextArea::ContentView::deleteSelection() {
assert(!selectionIsEmpty());
size_t removedLength = m_text.removeText(m_selectionStart, m_selectionEnd);
size_t removedLength = removeText(m_selectionStart, m_selectionEnd);
/* We cannot call resetSelection() because m_selectionStart and m_selectionEnd
* are invalid */
m_selectionStart = nullptr;