diff options
Diffstat (limited to 'indra/newview/llscripteditor.cpp')
| -rw-r--r-- | indra/newview/llscripteditor.cpp | 435 |
1 files changed, 410 insertions, 25 deletions
diff --git a/indra/newview/llscripteditor.cpp b/indra/newview/llscripteditor.cpp index 59cf3ac02b..ac6b81f677 100644 --- a/indra/newview/llscripteditor.cpp +++ b/indra/newview/llscripteditor.cpp @@ -31,6 +31,179 @@ #include "llsyntaxid.h" #include "lllocalcliprect.h" #include "llviewercontrol.h" +#include "llworkerthread.h" +#include <algorithm> +#include <utility> + +static LLWorkerThread& getSyntaxWorkerThread() +{ + static LLWorkerThread* sThread = new LLWorkerThread("SyntaxParse", true); + return *sThread; +} + +class LLScriptEditorSyntaxWorker final : public LLWorkerClass +{ +public: + struct Request + { + LLWString text; + S32 text_generation = 0; + U32 keywords_generation = 0; + bool disable_highlight = false; + LLKeywords* keywords = nullptr; + }; + + struct Result + { + LLWString text; + LLKeywords::segment_ops_t ops; + S32 text_generation = 0; + U32 keywords_generation = 0; + bool disable_highlight = false; + LLKeywords* keywords = nullptr; + }; + + LLScriptEditorSyntaxWorker(LLWorkerThread* thread, LLScriptEditor* editor) + : LLWorkerClass(thread, "ScriptEditorSyntax"), + mEditor(editor), + mHasPending(false), + mDropResults(false) + { + } + + void queueRequest(const Request& request) + { + bool should_add = false; + { + LLMutexLock lock(&mRequestMutex); + mPendingRequest = request; + mHasPending = true; + should_add = (!haveWork() && !isWorking()); + } + if (should_add) + { + addWork(0); + } + } + + void pump() + { + checkWork(); + bool should_add = false; + { + LLMutexLock lock(&mRequestMutex); + should_add = (!haveWork() && !isWorking() && mHasPending); + } + if (should_add) + { + addWork(0); + } + } + + void waitForIdle(bool drop_results) + { + mDropResults = drop_results; + { + LLMutexLock lock(&mRequestMutex); + mHasPending = false; + } + if (mRequestHandle != LLWorkerThread::nullHandle()) + { + mWorkerThread->waitForResult(mRequestHandle, false); + checkWork(); + } + mDropResults = false; + } + + void shutdown() + { + mEditor = nullptr; + waitForIdle(true); + scheduleDelete(); + } + +private: + void startWork(S32 param) override + { + LLMutexLock lock(&mRequestMutex); + if (!mHasPending) + { + return; + } + mActiveRequest = mPendingRequest; + mHasPending = false; + } + + bool doWork(S32 param) override + { + Request request; + { + LLMutexLock lock(&mRequestMutex); + request = mActiveRequest; + } + + Result result; + result.text = request.text; + result.text_generation = request.text_generation; + result.keywords_generation = request.keywords_generation; + result.disable_highlight = request.disable_highlight; + result.keywords = request.keywords; + + if (request.keywords) + { + request.keywords->collectSegmentOps(result.ops, request.text, request.disable_highlight); + } + + { + LLMutexLock lock(&mResultMutex); + mResult = std::move(result); + } + return true; + } + + void endWork(S32 param, bool aborted) override + { + if (aborted || mDropResults || !mEditor) + { + return; + } + + Result result; + { + LLMutexLock lock(&mResultMutex); + result = std::move(mResult); + } + + if (mEditor->getTextGeneration() != result.text_generation) + { + return; + } + if (mEditor->getKeywordsGeneration() != result.keywords_generation) + { + return; + } + if (&mEditor->getKeywords() != result.keywords) + { + return; + } + + mEditor->queueSyntaxApply(std::move(result.text), + std::move(result.ops), + result.text_generation, + result.keywords_generation, + result.disable_highlight, + result.keywords); + } + + LLScriptEditor* mEditor; + LLMutex mRequestMutex; + LLMutex mResultMutex; + Request mPendingRequest; + Request mActiveRequest; + Result mResult; + bool mHasPending; + bool mDropResults; +}; const S32 UI_TEXTEDITOR_LINE_NUMBER_MARGIN = 32; @@ -45,7 +218,21 @@ LLScriptEditor::Params::Params() LLScriptEditor::LLScriptEditor(const Params& p) : LLTextEditor(p) , mShowLineNumbers(p.show_line_numbers), - mUseDefaultFontSize(p.default_font_size) + mUseDefaultFontSize(p.default_font_size), + mLuauLanguage(false), + mKeywordsGeneration(0), + mLastQueuedTextGeneration(-1), + mLastQueuedKeywordsGeneration(0), + mLastQueuedDisableHighlight(false), + mLastQueuedKeywords(nullptr), + mSyntaxWorker(nullptr), + mSyntaxApplyState(SyntaxApplyState::Idle), + mPendingApplyOpIndex(0), + mPendingApplySegmentIndex(0), + mPendingApplyTextGeneration(-1), + mPendingApplyKeywordsGeneration(0), + mPendingApplyDisableHighlight(false), + mPendingApplyKeywords(nullptr) { if (mShowLineNumbers) { @@ -54,10 +241,22 @@ LLScriptEditor::LLScriptEditor(const Params& p) } } +LLScriptEditor::~LLScriptEditor() +{ + if (mSyntaxWorker) + { + mSyntaxWorker->shutdown(); + getSyntaxWorkerThread().update(0.f); + mSyntaxWorker = nullptr; + } +} + bool LLScriptEditor::postBuild() { gSavedSettings.getControl("LSLFontSizeName")->getCommitSignal()->connect(boost::bind(&LLScriptEditor::onFontSizeChange, this)); - return LLTextEditor::postBuild(); + bool result = LLTextEditor::postBuild(); + setFont(getScriptFont()); + return result; } void LLScriptEditor::draw() @@ -119,7 +318,7 @@ void LLScriptEditor::drawLineNumbers() // draw the line numbers if(line.mLineNum != last_line_num && line.mRect.mTop <= scrolled_view_rect.mTop) { - const LLWString ltext = utf8str_to_wstring(llformat("%d", line.mLineNum )); + const LLWString ltext = utf8str_to_wstring(llformat("%d", mLuauLanguage ? line.mLineNum + 1 : line.mLineNum)); bool is_cur_line = cursor_line == line.mLineNum; const U8 style = is_cur_line ? LLFontGL::BOLD : LLFontGL::NORMAL; const LLColor4& fg_color = is_cur_line ? mCursorColor : mReadOnlyFgColor; @@ -141,49 +340,220 @@ void LLScriptEditor::drawLineNumbers() } } -void LLScriptEditor::initKeywords() +void LLScriptEditor::initKeywords(bool luau_language) { - mKeywords.initialize(LLSyntaxIdLSL::getInstance()->getKeywordsXML()); + mKeywordsLua.initialize(LLSyntaxDefCache::getInstance()->getLuaKeywords(), true); + mKeywordsLSL.initialize(LLSyntaxDefCache::getInstance()->getLSLKeywords(), false); + + mLuauLanguage = luau_language; + } void LLScriptEditor::loadKeywords() { LL_PROFILE_ZONE_SCOPED; - mKeywords.processTokens(); - - LLStyleConstSP style = new LLStyle(LLStyle::Params().font(getScriptFont()).color(mDefaultColor.get())); + ensureSyntaxWorker(); + mSyntaxWorker->waitForIdle(true); + resetPendingSyntaxApply(); + getKeywords().processTokens(); + ++mKeywordsGeneration; - segment_vec_t segment_list; - mKeywords.findSegments(&segment_list, getWText(), *this, style); - - mSegments.clear(); - segment_set_t::iterator insert_it = mSegments.begin(); - for (segment_vec_t::iterator list_it = segment_list.begin(); list_it != segment_list.end(); ++list_it) + LLKeywords::segment_ops_t ops; + const LLWString& text = getWText(); + const llwchar* base = text.c_str(); + for (const llwchar* cur = base; *cur; ++cur) { - insert_it = mSegments.insert(insert_it, *list_it); + if (*cur == '\n') + { + ops.push_back({LLKeywords::SegmentOp::OP_LINE_BREAK, (S32)(cur - base), 0, nullptr}); + } } + applySyntaxSegments(text, ops); + queueSyntaxParse(); } void LLScriptEditor::updateSegments() { - if (mReflowIndex < S32_MAX && mKeywords.isLoaded() && mParseOnTheFly) + if (getKeywords().isLoaded() && mParseOnTheFly) { LL_PROFILE_ZONE_SCOPED; + ensureSyntaxWorker(); + mSyntaxWorker->pump(); + if (mReflowIndex < S32_MAX) + { + queueSyntaxParse(); + } + processPendingSyntaxApply(); + } + + LLTextBase::updateSegments(); +} + +void LLScriptEditor::ensureSyntaxWorker() +{ + if (!mSyntaxWorker) + { + mSyntaxWorker = new LLScriptEditorSyntaxWorker(&getSyntaxWorkerThread(), this); + } +} + +void LLScriptEditor::queueSyntaxParse() +{ + static LLCachedControl<bool> sDisableSyntaxHighlighting(gSavedSettings, "ScriptEditorDisableSyntaxHighlight", false); + const bool disable_syntax_highlighting = sDisableSyntaxHighlighting; + + LLKeywords* keywords = &getKeywords(); + const S32 text_generation = getTextGeneration(); + + if (mLastQueuedTextGeneration == text_generation + && mLastQueuedKeywordsGeneration == mKeywordsGeneration + && mLastQueuedDisableHighlight == disable_syntax_highlighting + && mLastQueuedKeywords == keywords) + { + return; + } + + mLastQueuedTextGeneration = text_generation; + mLastQueuedKeywordsGeneration = mKeywordsGeneration; + mLastQueuedDisableHighlight = disable_syntax_highlighting; + mLastQueuedKeywords = keywords; + + LLScriptEditorSyntaxWorker::Request request; + request.text = getWText(); + request.text_generation = text_generation; + request.keywords_generation = mKeywordsGeneration; + request.disable_highlight = disable_syntax_highlighting; + request.keywords = keywords; + mSyntaxWorker->queueRequest(request); +} + +void LLScriptEditor::queueSyntaxApply(LLWString text, + LLKeywords::segment_ops_t ops, + S32 text_generation, + U32 keywords_generation, + bool disable_highlight, + LLKeywords* keywords) +{ + if (text_generation != getTextGeneration() + || keywords_generation != mKeywordsGeneration + || keywords != &getKeywords()) + { + return; + } + + resetPendingSyntaxApply(); + + // Small updates can apply synchronously; larger ones use background slices. + constexpr size_t kImmediateOpsThreshold = 500; + if (ops.size() <= kImmediateOpsThreshold) + { + applySyntaxSegments(text, ops); + return; + } + + mPendingApplyText = std::move(text); + mPendingApplyOps = std::move(ops); + mPendingApplyTextGeneration = text_generation; + mPendingApplyKeywordsGeneration = keywords_generation; + mPendingApplyDisableHighlight = disable_highlight; + mPendingApplyKeywords = keywords; + mPendingApplyOpIndex = 0; + mPendingApplySegmentIndex = 0; + mPendingApplySegments.clear(); + mPendingApplySegmentSet.clear(); + mPendingApplyStyle = new LLStyle(LLStyle::Params().font(getScriptFont()).color(mDefaultColor.get())); + mSyntaxApplyState = SyntaxApplyState::Building; +} + +void LLScriptEditor::processPendingSyntaxApply() +{ + if (mSyntaxApplyState == SyntaxApplyState::Idle) + { + return; + } - LLStyleConstSP style = new LLStyle(LLStyle::Params().font(getScriptFont()).color(mDefaultColor.get())); + static LLCachedControl<bool> sDisableSyntaxHighlighting(gSavedSettings, "ScriptEditorDisableSyntaxHighlight", false); + if (mPendingApplyTextGeneration != getTextGeneration() + || mPendingApplyKeywordsGeneration != mKeywordsGeneration + || mPendingApplyKeywords != &getKeywords() + || mPendingApplyDisableHighlight != sDisableSyntaxHighlighting) + { + resetPendingSyntaxApply(); + return; + } - // HACK: No non-ascii keywords for now - segment_vec_t segment_list; - mKeywords.findSegments(&segment_list, getWText(), *this, style); + // Slice sizes control UI budget while applying large highlight updates. + constexpr size_t kOpsPerSlice = 250; + constexpr size_t kSegmentsPerSlice = 250; - clearSegments(); - for (segment_vec_t::iterator list_it = segment_list.begin(); list_it != segment_list.end(); ++list_it) + if (mSyntaxApplyState == SyntaxApplyState::Building) + { + if (mPendingApplyStyle.isNull()) + { + mPendingApplyStyle = new LLStyle(LLStyle::Params().font(getScriptFont()).color(mDefaultColor.get())); + } + bool done = getKeywords().applySegmentOpsRange(&mPendingApplySegments, + mPendingApplyText, + mPendingApplyOps, + mPendingApplyOpIndex, + kOpsPerSlice, + *this, + mPendingApplyStyle); + if (done) { - insertSegment(*list_it); + mSyntaxApplyState = SyntaxApplyState::Inserting; } + return; } - LLTextBase::updateSegments(); + size_t end_index = std::min(mPendingApplySegmentIndex + kSegmentsPerSlice, mPendingApplySegments.size()); + for (; mPendingApplySegmentIndex < end_index; ++mPendingApplySegmentIndex) + { + LLTextSegmentPtr segment = mPendingApplySegments[mPendingApplySegmentIndex]; + segment->linkToDocument(this); + mPendingApplySegmentSet.insert(segment); + } + + if (mPendingApplySegmentIndex >= mPendingApplySegments.size()) + { + S32 saved_scroll_index = mScrollIndex; + segment_set_t old_segments; + old_segments.swap(mSegments); + mSegments.swap(mPendingApplySegmentSet); + mScrollIndex = saved_scroll_index; + needsReflow(0); + resetPendingSyntaxApply(); + } +} + +void LLScriptEditor::resetPendingSyntaxApply() +{ + mSyntaxApplyState = SyntaxApplyState::Idle; + mPendingApplyText.clear(); + mPendingApplyOps.clear(); + mPendingApplySegments.clear(); + mPendingApplySegmentSet.clear(); + mPendingApplyOpIndex = 0; + mPendingApplySegmentIndex = 0; + mPendingApplyStyle = LLStyleConstSP(); + mPendingApplyTextGeneration = -1; + mPendingApplyKeywordsGeneration = 0; + mPendingApplyDisableHighlight = false; + mPendingApplyKeywords = nullptr; +} + +void LLScriptEditor::applySyntaxSegments(const LLWString& text, const LLKeywords::segment_ops_t& ops) +{ + LLStyleConstSP style = new LLStyle(LLStyle::Params().font(getScriptFont()).color(mDefaultColor.get())); + + segment_vec_t segment_list; + getKeywords().applySegmentOps(&segment_list, text, ops, *this, style); + + clearSegments(); + for (segment_vec_t::iterator list_it = segment_list.begin(); list_it != segment_list.end(); ++list_it) + { + insertSegment(*list_it); + } } void LLScriptEditor::clearSegments() @@ -194,6 +564,21 @@ void LLScriptEditor::clearSegments() } } +LLKeywords::keyword_iterator_t LLScriptEditor::keywordsBegin() +{ + return getKeywords().begin(); +} + +LLKeywords::keyword_iterator_t LLScriptEditor::keywordsEnd() +{ + return getKeywords().end(); +} + +LLKeywords& LLScriptEditor::getKeywords() +{ + return mLuauLanguage ? mKeywordsLua : mKeywordsLSL; +} + // Most of this is shamelessly copied from LLTextBase void LLScriptEditor::drawSelectionBackground() { @@ -218,7 +603,6 @@ void LLScriptEditor::drawSelectionBackground() ++rect_it) { LLRect selection_rect = *rect_it; - selection_rect = *rect_it; selection_rect.translate(mVisibleTextRect.mLeft - content_display_rect.mLeft, mVisibleTextRect.mBottom - content_display_rect.mBottom); gl_rect_2d(selection_rect, selection_color); } @@ -241,6 +625,7 @@ void LLScriptEditor::onFontSizeChange() { if (!mUseDefaultFontSize) { + setFont(getScriptFont()); needsReflow(); } } |
