Skip to content

Strange behaviour of shm/comment in some cases #117

Open
@d12frosted

Description

@d12frosted

Hey,

Consider following code:

{-# LANGUAGE SomeExtension #-}

module App where

-- one line comment
main :: IO ()
main = undefined

When you call shm/comment while on line -- one line comment - instead of uncommenting this one-line comment, it will 'uncomment' {-# LANGUAGE SomeExtension -#} into # LANGUAGE SomeExtension #.

What expected - uncomment one-liner via comment-dwim or do nothing. But anyway, don't uncomment pragma.


Some additional information.

Looks like shm/comment uses shm-in-comment to decide either it's in comment or not. And the latter function handles pragmas properly. But shm/comment uses (search-backward-regexp "{-" nil nil 1), so in situation with one liner it finds {- from the pragma - and uncomments it. I think instead of searching for {- it should search for {-[^#]. But it would just throw error in case of one-line comment. Probably it's better to handle them.

What I am using right now as a workaround is following function

(defun my-shm/comment ()
        "Comment the current node, or if there is none, or some error,
  fall back to `comment-dwim'. If the region is active, uses
  `comment-dwim'."
        (interactive)
        (if (region-active-p)
            (call-interactively 'comment-dwim)
          (let ((is-multiline-comment nil)
                (current (shm-current-node)))
            (cond
             ((shm-in-comment)
              (save-excursion
                (unless (looking-at "[-{]-[^#]")
                  (search-backward-regexp "[-{]-[^#]" nil nil 1))
                (when (looking-at "{-[^#]")
                  (setq is-multiline-comment t))
                (delete-region (point) (+ 2 (point)))
                (if is-multiline-comment
                    (search-forward-regexp "-}" nil nil 1)
                  (search-forward-regexp "\n" nil nil 1))
                (if (string-equal (match-string 0) "-}")
                    (delete-region (- (point) 2) (point))
                  (delete-region (- (point) 0) (point)))))
             (current
              (save-excursion
                (goto-char (shm-node-start current))
                (insert "{-")
                (goto-char (shm-node-end current))
                (insert "-}")
                (font-lock-fontify-region (shm-node-start current)
                                          (shm-node-end current))))
             (t (call-interactively 'comment-dwim))))))

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions