Skip to content

Commit

Permalink
Improve guess module name
Browse files Browse the repository at this point in the history
  • Loading branch information
gracjan committed Jul 14, 2016
1 parent 27a6e6f commit 2e2ce54
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
30 changes: 22 additions & 8 deletions haskell-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -1113,14 +1113,28 @@ successful, nil otherwise."
(if (not (haskell-mode-try-insert-scc-at-point))
(error "Could not insert or remove SCC"))))

(defun haskell-guess-module-name ()
"Guess the current module name of the buffer."
(interactive)
(let ((components (cl-loop for part
in (reverse (split-string (buffer-file-name) "/"))
(defun haskell-guess-module-name-from-file-name (file-name)
"Guess the module name from FILE-NAME.
Based on given FILE-NAME this function tries to find path
components that look like module identifiers and composes full
module path using this information. For example:
/Abc/Def/Xyz.lhs => Abc.Def.Xyz
/Ab-c/Def/Xyz.lhs => Def.Xyz
src/Abc/Def/Xyz.hs => Abc.Def.Xyz
c:\\src\\Abc\\Def\\Xyz.hs => Abc.Def.Xyz
This function usually will be used with `buffer-file-name':
(haskell-guess-module-name-from-file-name (buffer-file-name))"

(let* ((file-name-sans-ext (file-name-sans-extension file-name))
(components (cl-loop for part
in (reverse (split-string file-name-sans-ext "/"))
while (let ((case-fold-search nil))
(string-match "^[A-Z]+" part))
collect (replace-regexp-in-string "\\.l?hs$" "" part))))
(string-match (concat "^" haskell-lexeme-modid "$") part))
collect part)))
(mapconcat 'identity (reverse components) ".")))

(defvar haskell-auto-insert-module-format-string
Expand All @@ -1133,7 +1147,7 @@ successful, nil otherwise."
(when (and (= (point-min)
(point-max))
(buffer-file-name))
(insert (format haskell-auto-insert-module-format-string (haskell-guess-module-name)))
(insert (format haskell-auto-insert-module-format-string (haskell-guess-module-name-from-file-name (buffer-file-name))))
(goto-char (point-min))
(end-of-line)))

Expand Down
2 changes: 1 addition & 1 deletion haskell.el
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ for various things, but is optional."
(insert-file-contents-literally
(concat (haskell-session-current-dir (haskell-session))
"/"
(haskell-guess-module-name)
(haskell-guess-module-name-from-file-name (buffer-file-name))
".imports")))

(defun haskell-interactive-jump-to-error-line ()
Expand Down

0 comments on commit 2e2ce54

Please sign in to comment.