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

List:       kde-commits
Subject:    [kate] part: try to make the highlightingModeAt both faster and more safe
From:       Christoph Cullmann <cullmann () kde ! org>
Date:       2012-11-01 0:00:54
Message-ID: 20121101000054.E26CDA60A6 () git ! kde ! org
[Download RAW message or body]

Git commit 4f1f07cfc15e9411f3ba583110d78bff1bbceed0 by Christoph Cullmann.
Committed on 01/11/2012 at 01:00.
Pushed by cullmann into branch 'master'.

try to make the highlightingModeAt both faster and more safe
range checks + use QHash and no wrong loops where one used to compare wrong end \
                iterator :/
BUG: 308578

M  +19   -13   part/document/katedocument.cpp
M  +5    -25   part/syntax/katehighlight.cpp
M  +3    -2    part/syntax/katehighlight.h

http://commits.kde.org/kate/4f1f07cfc15e9411f3ba583110d78bff1bbceed0

diff --git a/part/document/katedocument.cpp b/part/document/katedocument.cpp
index 2929823..faf84d3 100644
--- a/part/document/katedocument.cpp
+++ b/part/document/katedocument.cpp
@@ -5390,7 +5390,11 @@ QStringList KateDocument::embeddedHighlightingModes() const
 
 QString KateDocument::highlightingModeAt(const KTextEditor::Cursor& position)
 {
-  Kate::TextLine kateLine = kateTextLine(position.line());
+  /**
+   * invalid line?
+   */
+  if (position.line() < 0 || position.line() >= lines())
+    return highlightingMode();
 
 //   const QVector< short >& attrs = kateLine->ctxArray();
 //   kDebug() << "----------------------------------------------------------------------";
 @@ -5400,28 +5404,30 @@ QString KateDocument::highlightingModeAt(const \
KTextEditor::Cursor& position)  //   kDebug() << \
"----------------------------------------------------------------------";  //   \
kDebug() << "col: " << position.column() << " lastchar:" << kateLine->lastChar() << " \
length:" << kateLine->length() << "global mode:" << highlightingMode();  
-  int len = kateLine->length();
-  int pos = position.column();
+  Kate::TextLine kateLine = kateTextLine(position.line());
+  const int len = kateLine->length();
+  const int pos = position.column();
   if ( pos >= len ) {
     const QVector< short >& ctxs = kateLine->ctxArray();
     int ctxcnt = ctxs.count();
     if ( ctxcnt == 0 ) {
       return highlightingMode();
     }
-    int ctx = ctxs.at(ctxcnt-1);
-    if ( ctx == 0 ) {
-      return highlightingMode();
-    }
-    return KateHlManager::self()->nameForIdentifier(highlight()->hlKeyForContext(ctx));
                
-  }
 
-  int attr = kateLine->attribute(pos);
-  if ( attr == 0 ) {
-    return mode();
+    // try ctx if != 0
+    if (int ctx = ctxs.at(ctxcnt-1))
+      return KateHlManager::self()->nameForIdentifier(highlight()->hlKeyForContext(ctx));
 +    
+    // hl mode fallback
+    return highlightingMode();
   }
 
-  return KateHlManager::self()->nameForIdentifier(highlight()->hlKeyForAttrib(attr));
 +  // try attr if != 0
+  if (int attr = kateLine->attribute(pos))
+    return KateHlManager::self()->nameForIdentifier(highlight()->hlKeyForAttrib(attr));
  
+  // hl mode fallback
+  return highlightingMode();
 }
 
 Kate::SwapFile* KateDocument::swapFile()
diff --git a/part/syntax/katehighlight.cpp b/part/syntax/katehighlight.cpp
index 6e0a390..770c2ad 100644
--- a/part/syntax/katehighlight.cpp
+++ b/part/syntax/katehighlight.cpp
@@ -87,7 +87,7 @@ KateHighlighting::KateHighlighting(const KateSyntaxModeListItem \
*def) : refCount  m_additionalData["none"]->deliminator = stdDeliminator;
     m_additionalData["none"]->wordWrapDeliminator = stdDeliminator;
     m_hlIndex[0] = "none";
-    m_ctxIndex[0]="";
+    m_ctxIndex[0]= "none";
   }
   else
   {
@@ -1006,34 +1006,14 @@ bool KateHighlighting::attributeRequiresSpellchecking( int \
attr )  return true;
 }
 
-QString KateHighlighting::hlKeyForContext(int i) const
+QString KateHighlighting::hlKeyForContext (int i) const
 {
-  int k = 0;
-  QMap<int,QString>::const_iterator it = m_ctxIndex.constEnd();
-  while ( it != m_hlIndex.constBegin() )
-  {
-    --it;
-    k = it.key();
-    if ( i >= k )
-      break;
-  }
-  return it.value();
+  return m_ctxIndex.value (i);
 }
 
-QString KateHighlighting::hlKeyForAttrib( int i ) const
+QString KateHighlighting::hlKeyForAttrib (int i) const
 {
-  // find entry. This is faster than QMap::find. m_hlIndex always has an entry
-  // for key '0' (it is "none"), so the result is always valid.
-  int k = 0;
-  QMap<int,QString>::const_iterator it = m_hlIndex.constEnd();
-  while ( it != m_hlIndex.constBegin() )
-  {
-    --it;
-    k = it.key();
-    if ( i >= k )
-      break;
-  }
-  return it.value();
+  return m_hlIndex.value (i);
 }
 
 bool KateHighlighting::isInWord( QChar c, int attrib ) const
diff --git a/part/syntax/katehighlight.h b/part/syntax/katehighlight.h
index 5907a3b..fddfad1 100644
--- a/part/syntax/katehighlight.h
+++ b/part/syntax/katehighlight.h
@@ -381,8 +381,9 @@ class KateHighlighting
      * The key is the starting index in the attribute array for each file.
      * @see hlKeyForAttrib
      */
-    QMap<int, QString> m_hlIndex;
-    QMap<int, QString> m_ctxIndex;
+    QHash<int, QString> m_hlIndex;
+    QHash<int, QString> m_ctxIndex;
+    
   public:
     inline bool foldingIndentationSensitive () { return \
m_foldingIndentationSensitive; }  inline bool allowsFolding(){return folding;}


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

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