summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyle Meyer <kyle@kyleam.com>2016-05-11 01:37:25 -0400
committerKyle Meyer <kyle@kyleam.com>2016-05-11 01:39:04 -0400
commit97b008e65f0402388cfacd32aee3b4dd4fbaf2ff (patch)
tree6f39a0512ffa6f658e51a8c32a8fcf173df83e0a
parentc4c05088e98179ab1ff5e90155b297fd2be9a087 (diff)
downloadsnakemake-mode-97b008e65f0402388cfacd32aee3b4dd4fbaf2ff.tar.gz
Add snakemake-block-bounds function
-rw-r--r--snakemake-mode.el45
-rw-r--r--snakemake-test.el70
-rw-r--r--snakemake.el2
3 files changed, 34 insertions, 83 deletions
diff --git a/snakemake-mode.el b/snakemake-mode.el
index cd8a421..853f356 100644
--- a/snakemake-mode.el
+++ b/snakemake-mode.el
@@ -132,6 +132,33 @@
"Regexp matching a call to a builtin Snakemake function.")
+;;; Info and navigation
+
+(defun snakemake-block-bounds ()
+ "Return bounds of rule or subworkflow block at point."
+ (save-excursion
+ (save-restriction
+ (prog-widen)
+ (let ((pos (point)))
+ (end-of-line)
+ (and (re-search-backward snakemake-rule-or-subworkflow-re nil t)
+ (let ((rule-start (or (match-beginning 1)
+ (match-beginning 3)))
+ (rule-indent (current-indentation))
+ rule-end)
+ (beginning-of-line)
+ (forward-line)
+ (while (and (or (< rule-indent (current-indentation))
+ (looking-at-p "^\\s-*$"))
+ (or (not (eobp))
+ (progn (setq rule-end (point-max))
+ nil)))
+ (setq rule-end (line-end-position))
+ (forward-line))
+ (when (<= rule-start pos rule-end)
+ (cons rule-start rule-end))))))))
+
+
;;; Indentation
(defun snakemake--calculate-indentation (&optional previous)
@@ -224,24 +251,6 @@ returned."
(and (memq this-command python-indent-trigger-commands)
(eq last-command this-command))))
-(defun snakemake-in-rule-or-subworkflow-block-p ()
- "Return non-nil if point is in block or on first blank line following one."
- (let ((blank-p (lambda nil
- (and (looking-at-p "^\\s-*$")
- ;; Ignore newlines in docstrings.
- (not (nth 3 (syntax-ppss)))))))
- (save-excursion
- (beginning-of-line)
- (when (funcall blank-p)
- (forward-line -1))
- (catch 'in-block
- (while (not (funcall blank-p))
- (cond ((looking-at-p snakemake-rule-or-subworkflow-re)
- (throw 'in-block t))
- ((bobp)
- (throw 'in-block nil)))
- (forward-line -1))))))
-
;;; Imenu
diff --git a/snakemake-test.el b/snakemake-test.el
index 034350c..d6f3d81 100644
--- a/snakemake-test.el
+++ b/snakemake-test.el
@@ -565,73 +565,15 @@ x = [1,
(indent-region (point) (point-max))
(buffer-string)))))
-;;;; Other
+;;;; Info and navigation
-(ert-deftest snakemake-test-in-rule-or-subworkflow-block-p ()
- ;; At top of block
- (snakemake-with-temp-text
- "
-<point>rule abc:
- output: 'file'"
- (should (snakemake-in-rule-or-subworkflow-block-p)))
-
- ;; Body of block
- (snakemake-with-temp-text
- "
-rule abc:
- output: <point>'file'"
- (should (snakemake-in-rule-or-subworkflow-block-p)))
-
- ;; First blank line after
- (snakemake-with-temp-text
- "
-rule abc:
- output: 'file'
-<point>"
- (should (snakemake-in-rule-or-subworkflow-block-p)))
-
- ;; Second blank line after
- (snakemake-with-temp-text
- "
-rule abc:
- output: 'file'
-
-<point>"
- (should-not (snakemake-in-rule-or-subworkflow-block-p)))
-
-
- ;; Blank line in docstring
- (snakemake-with-temp-text
- "
-rule abc:
- \"\"\"docstring header
-
- docstring line
- \"\"\"
- output: 'file'<point>"
- (should (snakemake-in-rule-or-subworkflow-block-p)))
-
- ;; Before
- (snakemake-with-temp-text
- "<point>
+(ert-deftest snakemake-test-block-bounds ()
+ (should-not
+ (snakemake-with-temp-text
+ "
rule abc:
output: 'file'"
- (should-not (snakemake-in-rule-or-subworkflow-block-p)))
-
- ;; At beginning of buffer
- (snakemake-with-temp-text
- "\
-rule abc:
- output: 'file'<point>"
- (should (snakemake-in-rule-or-subworkflow-block-p)))
-
- ;; Subworkflow
- (snakemake-with-temp-text
- "
-subworkflow otherworkflow:
-<point> workdir: '../path/to/otherworkflow'
- snakefile: '../path/to/otherworkflow/Snakefile'"
- (should (snakemake-in-rule-or-subworkflow-block-p))))
+ (snakemake-block-bounds))))
;;; snakemake.el
diff --git a/snakemake.el b/snakemake.el
index ce07712..47df68d 100644
--- a/snakemake.el
+++ b/snakemake.el
@@ -329,7 +329,7 @@ This function returns a list for consistency with other
target-returning functions, but any non-nil return value is
currently limited to a single-item list."
(when (and (derived-mode-p 'snakemake-mode)
- (snakemake-in-rule-or-subworkflow-block-p))
+ (snakemake-block-bounds))
(save-excursion
(end-of-line)
(re-search-backward snakemake-rule-or-subworkflow-re)