[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [ktexteditor/syntax-highlighting] src/syntax: cleanup a bit
From:       Christoph Cullmann <null () kde ! org>
Date:       2018-07-31 19:47:28
Message-ID: E1fkacC-0001kB-DN () code ! kde ! org
[Download RAW message or body]

Git commit 45d4d003f4cccef28e869bada6d5484879e05f99 by Christoph Cullmann.
Committed on 31/07/2018 at 19:37.
Pushed by cullmann into branch 'syntax-highlighting'.

cleanup a bit

M  +9    -460  src/syntax/katehighlight.cpp
M  +0    -10   src/syntax/katehighlight.h

https://commits.kde.org/ktexteditor/45d4d003f4cccef28e869bada6d5484879e05f99

diff --git a/src/syntax/katehighlight.cpp b/src/syntax/katehighlight.cpp
index acf7b7db..5d281ed9 100644
--- a/src/syntax/katehighlight.cpp
+++ b/src/syntax/katehighlight.cpp
@@ -100,12 +100,15 @@ KateHighlighting::KateHighlighting(const \
                KSyntaxHighlighting::Definition &def)
      */
     setDefinition(def);
 
+    /**
+     * only create mapping and stuff if the definition is valid
+     */
     if (def.isValid()) {
         /**
          * create the format => attributes mapping
          */
-        for (const auto & def : definition().includedDefinitions()) {
-            for (const auto & format : def.formats()) {
+        for (const auto & includedDefinition : definition().includedDefinitions()) {
+            for (const auto & format : includedDefinition.formats()) {
                 if (m_formatsIdToIndex.insert(std::make_pair(format.id(), \
m_formats.size())).second) {  m_formats.push_back(format);
                 }
@@ -135,107 +138,6 @@ void KateHighlighting::cleanup()
     internalIDList.clear();
 }
 
-KateHlContext *KateHighlighting::generateContextStack(Kate::TextLineData::ContextStack \
                &contextStack,
-        KateHlContextModification modification,
-        int &indexLastContextPreviousLine)
-{
-    while (true) {
-        switch (modification.type) {
-        /**
-         * stay, do nothing, just return the last context
-         * in the stack or 0
-         */
-        case KateHlContextModification::doNothing:
-            return contextNum(contextStack.isEmpty() ? 0 : contextStack.last());
-
-        /**
-         * just add a new context to the stack
-         * and return this one
-         */
-        case KateHlContextModification::doPush:
-            contextStack.append(modification.newContext);
-            return contextNum(modification.newContext);
-
-        /**
-         * pop some contexts + add a new one afterwards, immediate....
-         */
-        case KateHlContextModification::doPopsAndPush:
-            // resize stack
-            contextStack.resize((modification.pops >= contextStack.size()) ? 0 : \
                (contextStack.size() - modification.pops));
-
-            // push imediate the new context....
-            // don't handle the previous line stuff at all....
-            // ### TODO ### think about this
-            contextStack.append(modification.newContext);
-            return contextNum(modification.newContext);
-
-        /**
-         * do only pops...
-         */
-        default: {
-            // resize stack
-            contextStack.resize((modification.pops >= contextStack.size()) ? 0 : \
                (contextStack.size() - modification.pops));
-
-            // handling of context of previous line....
-            if (indexLastContextPreviousLine >= (contextStack.size() - 1)) {
-                // set new index, if stack is empty, this is -1, done for \
                eternity...
-                indexLastContextPreviousLine = contextStack.size() - 1;
-
-                // stack already empty, nothing to do...
-                if (contextStack.isEmpty()) {
-                    return contextNum(0);
-                }
-
-                KateHlContext *c = contextNum(contextStack.last());
-
-                // this must be a valid context, or our context stack is borked....
-                Q_ASSERT(c);
-
-                // handle line end context as new modificationContext
-                modification = c->lineEndContext;
-                continue;
-            }
-
-            return contextNum(contextStack.isEmpty() ? 0 : contextStack.last());
-        }
-        }
-    }
-
-    // should never be reached
-    Q_ASSERT(false);
-
-    return contextNum(0);
-}
-
-/**
- * Creates a new dynamic context or reuse an old one if it has already been created.
- */
-int KateHighlighting::makeDynamicContext(KateHlContext *model, const QStringList \
                *args)
-{
-    QPair<KateHlContext *, QString> key(model, args->front());
-    short value;
-
-    if (dynamicCtxs.contains(key)) {
-        value = dynamicCtxs[key];
-    } else {
-#ifdef HIGHLIGHTING_DEBUG
-        qCDebug(LOG_KTE) << "new stuff: " << startctx;
-#endif
-
-        KateHlContext *newctx = model->clone(args);
-
-        m_contexts.push_back(newctx);
-
-        value = startctx++;
-        dynamicCtxs[key] = value;
-        KateHlManager::self()->incDynamicCtxs();
-    }
-
-    // qCDebug(LOG_KTE) << "Dynamic context: using context #" << value << " (for \
                model " << model << " with args " << *args << ")";
-
-    return value;
-}
-
 /**
  * Drop all dynamic contexts. Shall be called with extreme care, and shall be \
                immediately
  * followed by a full HL invalidation.
@@ -279,369 +181,15 @@ void KateHighlighting::doHighlight(const Kate::TextLineData \
*_prevLine,  return;
     }
 
-    const bool firstLine = (_prevLine == nullptr);
-    const Kate::TextLine dummy = Kate::TextLine(new Kate::TextLineData());
-    const Kate::TextLineData *prevLine = firstLine ? dummy.data() : _prevLine;
-
     /**
      * highlight the given line via the abstract highlighter
+     * a bit ugly: we set the line to highlight as member to be able to update its \
                stats in the applyFormat and applyFolding member functions
      */
     m_textLineToHighlight = textLine;
-    const KSyntaxHighlighting::State initialState (firstLine ? \
                KSyntaxHighlighting::State() : _prevLine->highlightingState());
-    KSyntaxHighlighting::State endOfLineState = highlightLine(textLine->string(), \
initialState); +    const KSyntaxHighlighting::State initialState (!_prevLine ? \
KSyntaxHighlighting::State() : _prevLine->highlightingState()); +    const \
KSyntaxHighlighting::State endOfLineState = highlightLine(textLine->string(), \
initialState);  textLine->setHighlightingState(endOfLineState);
     m_textLineToHighlight = nullptr;
-
-#if 0
-
-    int previousLine = -1;
-    KateHlContext *context;
-
-    // duplicate the ctx stack, only once !
-    Kate::TextLineData::ContextStack ctx(prevLine->contextStack());
-
-    if (ctx.isEmpty()) {
-        // If the stack is empty, we assume to be in Context 0 (Normal)
-        if (firstLine) {
-            context = contextNum(0);
-        } else {
-            context = generateContextStack(ctx, contextNum(0)->lineEndContext, \
                previousLine); //get stack ID to use
-        }
-    } else {
-        //qCDebug(LOG_KTE) << "\t\tctxNum = " << ctxNum << " contextList[ctxNum] = " \
                << contextList[ctxNum]; // ellis
-
-        //if (lineContinue)   qCDebug(LOG_KTE)<<QString("The old context should be \
                %1").arg((int)ctxNum);
-        context = contextNum(ctx.last());
-
-        //qCDebug(LOG_KTE)<<"test1-2-1-text2";
-
-        previousLine = ctx.size() - 1; //position of the last context ID of th \
                previous line within the stack
-
-        // hl continue set or not ???
-        if (prevLine->hlLineContinue()) {
-            previousLine--;
-        } else {
-            context = generateContextStack(ctx, context->lineEndContext, \
                previousLine);    //get stack ID to use
-        }
-
-        //qCDebug(LOG_KTE)<<"test1-2-1-text4";
-
-        //if (lineContinue)   qCDebug(LOG_KTE)<<QString("The new context is \
                %1").arg((int)ctxNum);
-    }
-
-    // text, for programming convenience :)
-    QChar lastChar = QLatin1Char(' ');
-    const QString &text = textLine->string();
-    const int len = textLine->length();
-
-    // calc at which char the first char occurs, set it to length of line if never
-    const int firstChar = textLine->firstChar();
-    const int startNonSpace = (firstChar == -1) ? len : firstChar;
-
-    // last found item
-    KateHlItem *item = nullptr;
-
-    // loop over the line, offset gives current offset
-    int offset = 0;
-
-    KateHighlighting::HighlightPropertyBag *additionalData = \
                m_additionalData[context->hlId];
-    KateHlContext *oldContext = context;
-
-    // optimization: list of highlighting items that need their cache reset
-    static QVarLengthArray<KateHlItem *> cachingItems;
-
-    // catch empty lines
-    if (len == 0) {
-        // regenerate context stack if needed
-        if (context->emptyLineContext) {
-            context = generateContextStack(ctx, \
                context->emptyLineContextModification, previousLine);
-        }
-    } else {
-        /**
-         * check if the folding begin/ends are balanced!
-         * constructed on demand!
-         */
-        QHash<short, int> *foldingStartToCount = nullptr;
-
-        /**
-         * loop over line content!
-         */
-        QChar lastDelimChar = 0;
-        int lastOffset = offset;
-        int infiniteLoopDetectionCounter = 0;
-        KateHlContext* previous = context;
-        while (offset < len) {
-            // If requested (happens from completion), return where context changes \
                occur.
-            if (contextChanges && ( offset == 0 || context != previous )) {
-                previous = context;
-                const ContextChange change = {context, offset};
-                contextChanges->append(change);
-            }
-
-            /**
-             * infinite loop check
-             */
-            if (lastOffset < offset) {
-                /**
-                 * we did advance a bit, reset counter
-                 */
-                lastOffset = offset;
-                infiniteLoopDetectionCounter = 0;
-            } else {
-                /**
-                 * we did not advance, inc counter
-                 */
-                ++infiniteLoopDetectionCounter;
-
-                /**
-                 * not more than four times as many rounds as contexts known
-                 * break out of this loop and issue message
-                 */
-                if (infiniteLoopDetectionCounter > (4 * m_contexts.size())) {
-                    qCDebug(LOG_KTE) << "potential infinite loop found during \
                highlighting, hl: " << iName;
-                    break;
-                }
-            }
-
-            bool anItemMatched = false;
-            bool customStartEnableDetermined = false;
-
-            foreach (item, context->items) {
-                // does we only match if we are firstNonSpace?
-                if (item->firstNonSpace && (offset > startNonSpace)) {
-                    continue;
-                }
-
-                // have we a column specified? if yes, only match at this column
-                if ((item->column != -1) && (item->column != offset)) {
-                    continue;
-                }
-
-                if (!item->alwaysStartEnable) {
-                    if (item->customStartEnable) {
-                        if (oldContext != context) {
-                            oldContext = context;
-                            additionalData = m_additionalData[oldContext->hlId];
-                        }
-                        if (customStartEnableDetermined || \
                additionalData->deliminator.contains(lastChar)) {
-                            customStartEnableDetermined = true;
-                        } else {
-                            continue;
-                        }
-                    } else {
-                        if (lastDelimChar == lastChar) {
-                        } else if (stdDeliminator().contains(lastChar)) {
-                            lastDelimChar = lastChar;
-                        } else {
-                            continue;
-                        }
-                    }
-                }
-
-                int offset2 = item->checkHgl(text, offset, len - offset);
-                if (item->haveCache && !item->cachingHandled) {
-                    cachingItems.append(item);
-                    item->cachingHandled = true;
-                }
-
-                if (offset2 <= offset) {
-                    continue;
-                }
-
-                // dominik: on lookAhead, do not preocess any data by fixing offset2
-                if (item->lookAhead) {
-                    offset2 = offset;
-                } else {
-                    // make sure the rule does not violate the text line length
-                    if (offset2 > len) {
-                        offset2 = len;
-                    }
-                }
-
-                // BUG 144599: Ignore a context change that would push the same \
                context
-                // without eating anything... this would be an infinite loop!
-                if (item->lookAhead && (item->ctx.pops < 2 && item->ctx.newContext \
                == (ctx.isEmpty() ? 0 : ctx.last()))) {
-                    continue;
-                }
-
-                // regenerate context stack if needed
-                context = generateContextStack(ctx, item->ctx, previousLine);
-
-                // dynamic context: substitute the model with an 'instance'
-                if (context->dynamic) {
-                    // try to retrieve captures from regexp
-                    QStringList captures;
-                    item->capturedTexts(captures);
-                    if (!captures.empty()) {
-                        // Replace the top of the stack and the current context
-                        int newctx = makeDynamicContext(context, &captures);
-                        if (ctx.size() > 0) {
-                            ctx[ctx.size() - 1] = newctx;
-                        }
-
-                        context = contextNum(newctx);
-                    }
-                }
-
-                // handle folding end or begin
-                if (item->region || item->region2) {
-                    /**
-                     * for each end region, decrement counter for that type, erase \
                if count reaches 0!
-                     */
-                    if (item->region2 && foldingStartToCount) {
-                        QHash<short, int>::iterator end = \
                foldingStartToCount->find(-item->region2);
-                        if (end != foldingStartToCount->end()) {
-                            if (end.value() > 1) {
-                                --(end.value());
-                            } else {
-                                foldingStartToCount->erase(end);
-                            }
-                        }
-                    }
-
-                    /**
-                     * increment counter for each begin region!
-                     */
-                    if (item->region) {
-                        // construct on demand!
-                        if (!foldingStartToCount) {
-                            foldingStartToCount = new QHash<short, int> ();
-                        }
-
-                        ++(*foldingStartToCount)[item->region];
-                    }
-                }
-
-                // even set attributes or end of region! ;)
-                int attribute = item->onlyConsume ? context->attr : item->attr;
-                if ((attribute > 0 && !item->lookAhead) || item->region2) {
-                    textLine->addAttribute(Kate::TextLineData::Attribute(offset, \
                offset2 - offset, attribute, item->region2));
-                }
-
-                // create 0 length attribute for begin of region, if any!
-                if (item->region) {
-                    textLine->addAttribute(Kate::TextLineData::Attribute(offset2, 0, \
                attribute, item->region));
-                }
-
-                // only process, if lookAhead is false
-                if (!item->lookAhead) {
-                    offset = offset2;
-                    lastChar = text[offset - 1];
-                }
-
-                anItemMatched = true;
-                break;
-            }
-
-            // something matched, continue loop
-            if (anItemMatched) {
-                continue;
-            }
-
-            item = nullptr;
-
-            // nothing found: set attribute of one char
-            // anders: unless this context does not want that!
-            if (context->fallthrough) {
-                // set context to context->ftctx.
-                context = generateContextStack(ctx, context->ftctx, previousLine); \
                //regenerate context stack
-
-                //qCDebug(LOG_KTE)<<"context num after fallthrough at col "<<z<<": \
                "<<ctxNum;
-                // the next is necessary, as otherwise keyword (or anything using \
                the std delimitor check)
-                // immediately after fallthrough fails. Is it bad?
-                // jowenn, can you come up with a nicer way to do this?
-                /*  if (offset)
-                    lastChar = text[offset - 1];
-                  else
-                    lastChar = '\\';*/
-                continue;
-            } else {
-                // set attribute if any
-                if (context->attr > 0) {
-                    textLine->addAttribute(Kate::TextLineData::Attribute(offset, 1, \
                context->attr, 0));
-                }
-
-                lastChar = text[offset];
-                offset++;
-            }
-        }
-
-        /**
-         * check if folding is not balanced and we have more starts then ends
-         * then this line is a possible folding start!
-         */
-        if (foldingStartToCount) {
-            /**
-             * possible folding start, if imbalanced, aka hash not empty!
-             */
-            if (!foldingStartToCount->isEmpty()) {
-                textLine->markAsFoldingStartAttribute();
-            }
-
-            /**
-             * kill hash
-             */
-            delete foldingStartToCount;
-            foldingStartToCount = nullptr;
-        }
-    }
-
-    /**
-     * has the context stack changed?
-     */
-    if ((ctxChanged = (ctx != textLine->contextStack()))) {
-        /**
-         * try to share the simple stack that contains only 0
-         */
-        static const Kate::TextLineData::ContextStack onlyDefaulContext(1, 0);
-        if (ctx == onlyDefaulContext) {
-            textLine->setContextStack(onlyDefaulContext);
-        }
-
-        /**
-         * next try: try to share data with last line
-         */
-        else if (ctx == prevLine->contextStack()) {
-            textLine->setContextStack(prevLine->contextStack());
-        }
-
-        /**
-         * ok, really use newly constructed stack!
-         */
-        else {
-            textLine->setContextStack(ctx);
-        }
-    }
-
-    // write hl continue flag
-    textLine->setHlLineContinue(item && item->lineContinue());
-
-    // check for indentation based folding
-    if (m_foldingIndentationSensitive && (tabWidth > 0) && \
                !textLine->markedAsFoldingStartAttribute()) {
-        bool skipIndentationBasedFolding = false;
-        for (int i = ctx.size() - 1; i >= 0; --i) {
-            if (contextNum(ctx[i])->noIndentationBasedFolding) {
-                skipIndentationBasedFolding = true;
-                break;
-            }
-        }
-
-        /**
-         * compute if we increase indentation in next line
-         */
-        if (!skipIndentationBasedFolding && !isEmptyLine(textLine) && \
                !isEmptyLine(nextLine)
-                && (textLine->indentDepth(tabWidth) < \
                nextLine->indentDepth(tabWidth))) {
-            textLine->markAsFoldingStartIndentation();
-        }
-    }
-
-    // invalidate caches
-    for (int i = 0; i < cachingItems.size(); ++i) {
-        cachingItems[i]->cachingHandled = false;
-        cachingItems[i]->haveCache = false;
-    }
-    cachingItems.clear();
-#endif
 }
 
 void KateHighlighting::applyFormat(int offset, int length, const \
KSyntaxHighlighting::Format &format) @@ -652,6 +200,7 @@ void \
KateHighlighting::applyFormat(int offset, int length, const KSyntaxHighligh  
 void KateHighlighting::applyFolding(int offset, int length, \
KSyntaxHighlighting::FoldingRegion region)  {
+    Q_ASSERT(m_textLineToHighlight);
 }
 
 void KateHighlighting::getKateExtendedAttributeList(const QString &schema, \
                QList<KTextEditor::Attribute::Ptr> &list, KConfig *cfg)
diff --git a/src/syntax/katehighlight.h b/src/syntax/katehighlight.h
index 2b63c8c6..8806b4d8 100644
--- a/src/syntax/katehighlight.h
+++ b/src/syntax/katehighlight.h
@@ -376,7 +376,6 @@ private:
 private:
     void init();
     void makeContextList();
-    int makeDynamicContext(KateHlContext *model, const QStringList *args);
     void handleKateHlIncludeRules();
     void handleKateHlIncludeRulesRecursive(int index, KateHlIncludeRules *list);
     int addToContextList(const QString &ident, int ctx0);
@@ -390,15 +389,6 @@ private:
     void readFoldingConfig();
     void readSpellCheckingConfig();
 
-    /**
-     * update given context stack
-     * @param contextStack context stack to manipulate
-     * @param modification description of the modification of the stack to execute
-     * @param indexLastContextPreviousLine index of the last context from the \
                previous line which still is in the stack
-     * @return current active context, last one of the stack or default context 0 \
                for empty stack
-     */
-    KateHlContext *generateContextStack(Kate::TextLineData::ContextStack \
&contextStack, KateHlContextModification modification, int \
                &indexLastContextPreviousLine);
-
     KateHlItem *createKateHlItem(KateSyntaxContextData *data, \
QList<KTextEditor::Attribute::Ptr> &iDl, QStringList *RegionList, QStringList \
                *ContextList);
     int lookupAttrName(const QString &name, QList<KTextEditor::Attribute::Ptr> \
&iDl);  


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic