branch: elpa/markdown-mode
commit e2e2bc753b4330f85b6ea7e62cd0552e8ea47b76
Merge: 080098ee73 80460e2818
Author: Shohei YOSHIDA <[email protected]>
Commit: GitHub <[email protected]>

    Merge pull request #958 from candera/perf/inline-code-scan-in-table-rows
    
    Search only the current row for inline code in table rows
---
 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 87f84b198c..88816fb243 100644
--- a/markdown-mode.el
+++ b/markdown-mode.el
@@ -2866,22 +2866,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 f0228ab5c2..a00a58db4b 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