Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange behaviour of shm/comment in some cases #117

Open
d12frosted opened this issue Oct 13, 2015 · 1 comment
Open

Strange behaviour of shm/comment in some cases #117

d12frosted opened this issue Oct 13, 2015 · 1 comment

Comments

@d12frosted
Copy link
Contributor

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))))))
@chrisdone
Copy link
Member

This should be an easy fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants