branch: elpa/markdown-mode
commit 80460e2818c3b1a420a96c26df42dc821e93f865
Author: Craig Andera <[email protected]>
Commit: Craig Andera <[email protected]>

    Search only the current row for inline code in table rows
    
    markdown-inline-code-at-pos looks for code fragments from the start of
    the text block to its end.  A pipe table has no blank lines, so it is a
    single text block, and the bold, italic and link matchers call this
    function for every candidate.  Fontifying a table is therefore
    quadratic in its size.
    
    A table row cannot contain a code fragment that spans lines, so narrow
    to the row when POS is on one.
    
    On a 116-row table of 450 KB (rows up to 12 KB, many **bold** spans),
    fontifying it takes 70s before and 1.5s after, with identical faces.
    
    Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
---
 CHANGES.md             |  2 ++
 markdown-mode.el       | 37 +++++++++++++++++++++++--------------
 tests/markdown-test.el | 30 ++++++++++++++++++++++++++++++
 3 files changed, 55 insertions(+), 14 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index ecb27213e4..9f8dab145a 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -21,6 +21,8 @@
     - skip export tests if export command is not installed
     - reduce memory allocations in property checking functions
     - improve performance to check properties in range by using c functions
+    - Fontifying a large table is no longer quadratic in its size: looking
+      for inline code on a table row now only searches that row
 
   [gh-937]: https://github.com/jrblevin/markdown-mode/pull/937
   [gh-953]: https://github.com/jrblevin/markdown-mode/issues/953
diff --git a/markdown-mode.el b/markdown-mode.el
index dc97f8de7d..76d8320386 100644
--- a/markdown-mode.el
+++ b/markdown-mode.el
@@ -2865,22 +2865,31 @@ because `thing-at-point-looking-at' does not work 
reliably with
 The match data is set as follows:
 Group 1 matches the opening backquotes.
 Group 2 matches the code fragment itself, without backquotes.
-Group 3 matches the closing backquotes."
+Group 3 matches the closing backquotes.
+
+A table row cannot contain a code fragment that spans lines, so
+when POS is on a table row only that row is searched.  Otherwise
+every candidate would rescan the whole table, which is a single
+text block, making fontification quadratic in the table size."
   (save-excursion
     (goto-char pos)
-    (let ((old-point (point))
-          (end-of-block (progn (markdown-end-of-text-block) (point)))
-          found)
-      (if from
-          (goto-char from)
-        (markdown-beginning-of-text-block))
-      (while (and (markdown-match-code end-of-block)
-                  (setq found t)
-                  (< (match-end 0) old-point)))
-      (let ((match-group (if (eq (char-after (match-beginning 0)) ?`) 0 1)))
-        (and found                                        ; matched something
-             (<= (match-beginning match-group) old-point) ; match contains 
old-point
-             (> (match-end 0) old-point))))))
+    (save-restriction
+      (when (progn (beginning-of-line) (looking-at-p "[ \t]*|"))
+        (narrow-to-region (line-beginning-position) (line-end-position)))
+      (goto-char pos)
+      (let ((old-point (point))
+            (end-of-block (progn (markdown-end-of-text-block) (point)))
+            found)
+        (if from
+            (goto-char from)
+          (markdown-beginning-of-text-block))
+        (while (and (markdown-match-code end-of-block)
+                    (setq found t)
+                    (< (match-end 0) old-point)))
+        (let ((match-group (if (eq (char-after (match-beginning 0)) ?`) 0 1)))
+          (and found                                        ; matched something
+               (<= (match-beginning match-group) old-point) ; match contains 
old-point
+               (> (match-end 0) old-point)))))))
 
 (defun markdown-inline-code-at-pos-p (pos)
   "Return non-nil if there is an inline code fragment at POS.
diff --git a/tests/markdown-test.el b/tests/markdown-test.el
index d4059574c3..738fffac70 100644
--- a/tests/markdown-test.el
+++ b/tests/markdown-test.el
@@ -2560,6 +2560,36 @@ See GH-275."
     (markdown-test-range-has-face 12 12 'markdown-markup-face)
     (markdown-test-range-has-face 18 18 'markdown-markup-face)))
 
+(ert-deftest test-markdown-font-lock/inline-code-in-table-rows ()
+  "Inline code and bold in table rows are fontified; a `|' in code is not a 
cell edge."
+  (markdown-test-string "| a | b |\n|---|---|\n| **x** | `y` |\n| `**z**` | w 
|\n"
+    (font-lock-ensure)
+    (markdown-test-range-has-face 25 25 'markdown-bold-face)
+    (markdown-test-range-has-face 32 32 'markdown-inline-code-face)
+    (markdown-test-range-has-face 40 44 'markdown-inline-code-face)
+    (should-not (memq 'markdown-bold-face (get-text-property 42 'face)))))
+
+(ert-deftest test-markdown-inline-code-at-pos/table-row-scan-is-bounded ()
+  "Looking for inline code on a table row must not scan the rest of the table.
+A table is one text block, so scanning it for every bold or italic
+candidate made fontifying a large table quadratic."
+  (markdown-test-string "| a | b |\n|---|---|\n| **x** | y |\n| z | `w` |\n"
+    (goto-char (point-min))
+    (forward-line 2)
+    (let ((eol (line-end-position))
+          (max-last 0))
+      (cl-letf* ((orig (symbol-function 'markdown-match-code))
+                 ((symbol-function 'markdown-match-code)
+                  (lambda (last)
+                    (setq max-last (max max-last last))
+                    (funcall orig last))))
+        (should-not (markdown-inline-code-at-pos (+ (point) 3))))
+      (should (<= max-last eol)))
+    ;; Code on the row is still found.
+    (goto-char (point-max))
+    (forward-line -1)
+    (should (markdown-inline-code-at-pos (+ (point) 7)))))
+
 (ert-deftest test-markdown-font-lock/ignore-uri-in-inline-code ()
   "Avoid rendering URIs inside inline code."
   (markdown-test-string

Reply via email to