diff --git a/apps/code/editor_controller.cpp b/apps/code/editor_controller.cpp index 762887c26..1c5299794 100644 --- a/apps/code/editor_controller.cpp +++ b/apps/code/editor_controller.cpp @@ -53,6 +53,7 @@ void EditorController::didBecomeFirstResponder() { } void EditorController::viewWillAppear() { + m_editorView.loadSyntaxHighlighter(); m_editorView.setCursorLocation(strlen(m_editorView.text())); } @@ -60,6 +61,7 @@ void EditorController::viewDidDisappear() { m_menuController->scriptContentEditionDidFinish(); delete[] m_areaBuffer; m_areaBuffer = nullptr; + m_editorView.unloadSyntaxHighlighter(); } bool EditorController::textAreaDidReceiveEvent(TextArea * textArea, Ion::Events::Event event) { diff --git a/apps/code/editor_view.h b/apps/code/editor_view.h index 299d3e0ab..be4bcd907 100644 --- a/apps/code/editor_view.h +++ b/apps/code/editor_view.h @@ -12,15 +12,15 @@ public: void setTextAreaDelegate(TextAreaDelegate * delegate) { m_textArea.setDelegate(delegate); } - const char * text() const { - return m_textArea.text(); - } + const char * text() const { return m_textArea.text(); } void setText(char * textBuffer, size_t textBufferSize) { m_textArea.setText(textBuffer, textBufferSize); } bool setCursorLocation(int location) { return m_textArea.setCursorLocation(location); } + void loadSyntaxHighlighter() { m_textArea.loadSyntaxHighlighter(); }; + void unloadSyntaxHighlighter() { m_textArea.unloadSyntaxHighlighter(); }; void scrollViewDidChangeOffset(ScrollViewDataSource * scrollViewDataSource) override; void didBecomeFirstResponder() override; private: diff --git a/apps/code/python_text_area.cpp b/apps/code/python_text_area.cpp index e600f1570..b07e0a529 100644 --- a/apps/code/python_text_area.cpp +++ b/apps/code/python_text_area.cpp @@ -58,15 +58,19 @@ static inline int TokenLength(mp_lexer_t * lex) { } } -PythonTextArea::ContentView::ContentView(KDText::FontSize fontSize) : - TextArea::ContentView(fontSize) -{ +void PythonTextArea::ContentView::loadSyntaxHighlighter() { + assert(m_pythonHeap == nullptr); m_pythonHeap = static_cast(malloc(k_pythonHeapSize)); + if (m_pythonHeap != nullptr) { + MicroPython::init(m_pythonHeap, m_pythonHeap + k_pythonHeapSize); + } } -PythonTextArea::ContentView::~ContentView() { - if (m_pythonHeap) { +void PythonTextArea::ContentView::unloadSyntaxHighlighter() { + if (m_pythonHeap != nullptr) { + MicroPython::deinit(); free(m_pythonHeap); + m_pythonHeap = nullptr; } } @@ -85,7 +89,7 @@ void PythonTextArea::ContentView::clearRect(KDContext * ctx, KDRect rect) const void PythonTextArea::ContentView::drawLine(KDContext * ctx, int line, const char * text, size_t length, int fromColumn, int toColumn) const { LOG_DRAW("Drawing \"%.*s\"\n", length, text); - if (!m_pythonHeap) { + if (m_pythonHeap == nullptr) { drawStringAt( ctx, line, @@ -98,8 +102,6 @@ void PythonTextArea::ContentView::drawLine(KDContext * ctx, int line, const char return; } - MicroPython::init(m_pythonHeap, m_pythonHeap + k_pythonHeapSize); - nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { /* We're using the MicroPython lexer to do syntax highlighting on a per-line @@ -151,8 +153,6 @@ void PythonTextArea::ContentView::drawLine(KDContext * ctx, int line, const char mp_lexer_free(lex); nlr_pop(); } - - MicroPython::deinit(); } KDRect PythonTextArea::ContentView::dirtyRectFromCursorPosition(size_t index, bool lineBreak) const { diff --git a/apps/code/python_text_area.h b/apps/code/python_text_area.h index 160d5e214..6d988e617 100644 --- a/apps/code/python_text_area.h +++ b/apps/code/python_text_area.h @@ -12,11 +12,18 @@ public: m_contentView(fontSize) { } + void loadSyntaxHighlighter() { m_contentView.loadSyntaxHighlighter(); } + void unloadSyntaxHighlighter() { m_contentView.unloadSyntaxHighlighter(); } protected: class ContentView : public TextArea::ContentView { public: - ContentView(KDText::FontSize fontSize); - ~ContentView(); + ContentView(KDText::FontSize fontSize) : + TextArea::ContentView(fontSize), + m_pythonHeap(nullptr) + { + } + void loadSyntaxHighlighter(); + void unloadSyntaxHighlighter(); void clearRect(KDContext * ctx, KDRect rect) const override; void drawLine(KDContext * ctx, int line, const char * text, size_t length, int fromColumn, int toColumn) const override; KDRect dirtyRectFromCursorPosition(size_t index, bool lineBreak) const override; @@ -27,7 +34,6 @@ protected: private: const ContentView * nonEditableContentView() const override { return &m_contentView; } ContentView m_contentView; - char * m_pythonHeap; }; }