diff --git a/Makefile b/Makefile index 57fc426fa..e8cb10556 100644 --- a/Makefile +++ b/Makefile @@ -47,6 +47,7 @@ ELFILES = \ haskell-compat.el \ haskell-compile.el \ haskell-complete-module.el \ + haskell-completions.el \ haskell-customize.el \ haskell-debug.el \ haskell-decl-scan.el \ diff --git a/haskell-completions.el b/haskell-completions.el new file mode 100644 index 000000000..5709a9867 --- /dev/null +++ b/haskell-completions.el @@ -0,0 +1,46 @@ +;;; haskell-completions.el --- Haskell Completion package + +;; Copyright © 2015 Athur Fayzrakhmanov. All rights reserved. + +;; This file is part of haskell-mode package. +;; You can contact with authors using GitHub issue tracker: +;; https://github.com/haskell/haskell-mode/issues + +;; This file is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 3, or (at your option) +;; any later version. + +;; This file is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs; see the file COPYING. If not, write to +;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +;; Boston, MA 02110-1301, USA. + +;;; Commentary: + +;; This package provides completions related functionality for +;; Haskell Mode such grab completion prefix at point, and etc.. + +;;; Code: + +(defun haskell-completions-can-grab-prefix () + "Check if the case is appropriate for grabbing completion prefix. +Returns t if point is either at whitespace character, or at +punctuation, or at line end and preceeding character is not a +whitespace or new line, otherwise returns nil. + + Returns nil in presense of active region." + (when (not (region-active-p)) + (when (looking-at (rx (| space line-end punct))) + (when (not (bobp)) + (save-excursion + (backward-char) + (not (looking-at (rx (| space line-end))))))))) + +(provide 'haskell-completions) +;;; haskell-completions.el ends here diff --git a/tests/haskell-completions-tests.el b/tests/haskell-completions-tests.el new file mode 100644 index 000000000..b8c40b364 --- /dev/null +++ b/tests/haskell-completions-tests.el @@ -0,0 +1,74 @@ +;;; haskell-completions-tests.el --- Tests for Haskell Completion package + +;; Copyright © 2015 Athur Fayzrakhmanov. All rights reserved. + +;; This file is part of haskell-mode package. +;; You can contact with authors using GitHub issue tracker: +;; https://github.com/haskell/haskell-mode/issues + +;; This file is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 3, or (at your option) +;; any later version. + +;; This file is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs; see the file COPYING. If not, write to +;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +;; Boston, MA 02110-1301, USA. + +;;; Commentary: + +;; This package provides regression tests for haskell-completions package. + +;;; Code: + +(require 'ert) +(require 'haskell-mode) +(require 'haskell-completions) + + +(ert-deftest haskell-completions-can-grab-prefix-test () + "Tests the function `haskell-completions-can-grab-prefix'." + (with-temp-buffer + (haskell-mode) + (should (eql nil (haskell-completions-can-grab-prefix))) + (insert " ") + (should (eql nil (haskell-completions-can-grab-prefix))) + (insert "a") + (should (eql t (haskell-completions-can-grab-prefix))) + (save-excursion + (insert " ") + (should (eql nil (haskell-completions-can-grab-prefix))) + (insert "bc-") + (should (eql t (haskell-completions-can-grab-prefix))) + (insert "\n") + (should (eql nil (haskell-completions-can-grab-prefix))) + (insert "def:#!") + (should (eql t (haskell-completions-can-grab-prefix)))) + (should (eql t (haskell-completions-can-grab-prefix))) + ;; punctuation tests + (save-excursion (insert ")")) + (should (eql t (haskell-completions-can-grab-prefix))) + (save-excursion (insert ",")) + (should (eql t (haskell-completions-can-grab-prefix))) + (save-excursion (insert "'")) + (should (eql t (haskell-completions-can-grab-prefix))) + ;; should return nil in the middle of word + (save-excursion (insert "bcd")) + (should (eql nil (haskell-completions-can-grab-prefix))) + ;; region case + (let ((p (point))) + (goto-char (point-min)) + (push-mark) + (goto-char p) + (activate-mark) + (should (eql nil (haskell-completions-can-grab-prefix)))))) + + +(provide 'haskell-completions-tests) +;;; haskell-completions-tests.el ends here