Skip to content

Commit 3a5416c

Browse files
authored
Shows the qualifier for completions, if they've already been imported qualified (#99)
* Displays completions with their already imported qualifiers * alias -> qualifier * add changelog entry
1 parent 39452e4 commit 3a5416c

File tree

4 files changed

+73
-36
lines changed

4 files changed

+73
-36
lines changed

CHANGELOG.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# CHANGELOG
22

3+
## 2017-04-20
4+
- Displays completions with their already imported qualifiers. This can be toggled
5+
off with `(setq psc-ide-add-qualification-on-completion nil)`.
6+
7+
- Displays the result of the load command in the message buffer
8+
39
## 2017-04-18
4-
Switched to purescrpt-0.11 compatible `purs` executable by default. The 0.10
10+
- Switched to purescript-0.11 compatible `purs` executable by default. The 0.10
511
binaries can be regained with `(setq psc-ide-use-purs nil)`, but we'll remove
612
this switch when purescript-0.12 is released.

psc-ide-protocol.el

+8
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ Evaluates the CALLBACK in the context of the CURRENT buffer that initiated call
122122
(list :command "rebuild"
123123
:params (list
124124
:file (or filepath (buffer-file-name (current-buffer)))))))
125+
126+
(defun psc-ide-command-list-imports (&optional filepath)
127+
(json-encode
128+
(list :command "list"
129+
:params (list
130+
:type "import"
131+
:file (or filepath (buffer-file-name (current-buffer)))))))
132+
125133
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
126134
;;
127135
;; Protocol utilities.

psc-ide.el

+52-29
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@
114114
:group 'psc-ide
115115
:type 'boolean)
116116

117+
(defcustom psc-ide-add-qualification-on-completion "t"
118+
"Whether to automatically prepend the qualifier for completions
119+
that are imported qualified in the current module"
120+
:group 'psc-ide
121+
:type 'boolean)
122+
117123
(defcustom psc-ide-rebuild-on-save nil
118124
"Whether to rebuild files on save and display errors/warnings
119125
in a buffer"
@@ -196,7 +202,7 @@ in a buffer"
196202
;; Don't add an import when the option to do so is disabled
197203
(not psc-ide-add-import-on-completion)
198204
;; or when a qualified identifier was completed
199-
(s-contains-p "." (company-grab-symbol)))
205+
(or (get-text-property 0 :qualifier arg) (s-contains-p "." (company-grab-symbol))))
200206
(psc-ide-add-import-impl arg (vector
201207
(psc-ide-filter-modules
202208
(list (get-text-property 0 :module arg))))))))))
@@ -222,7 +228,8 @@ in a buffer"
222228
(defun psc-ide-load-all ()
223229
"Loads all the modules in the current project"
224230
(interactive)
225-
(psc-ide-send psc-ide-command-load-all 'psc-ide-unwrap-result))
231+
(psc-ide-send psc-ide-command-load-all
232+
(-compose 'message 'psc-ide-unwrap-result)))
226233

227234
(defun psc-ide-show-type (expand)
228235
"Show type of the symbol under cursor."
@@ -349,7 +356,7 @@ use when the search used was with `string-match'."
349356
(idx 3)
350357
result)
351358
(push `(module . ,(match-string-no-properties 1 string)) result)
352-
(push `(alias . ,(match-string-no-properties 3 string)) result)
359+
(push `(qualifier . ,(match-string-no-properties 3 string)) result)
353360
result))
354361

355362
(defun psc-ide-parse-imports-in-buffer (&optional buffer)
@@ -481,10 +488,10 @@ The cases we have to cover:
481488
3. fil| <- filter by prefix and imported modules"
482489
(let* ((components (s-split "\\." search))
483490
(prefix (car (last components)))
484-
(alias (s-join "." (butlast components))))
485-
(if (not (s-blank? alias))
491+
(qualifier (s-join "." (butlast components))))
492+
(if (not (s-blank? qualifier))
486493
;; 1. List.fil <- filter by prefix and List module
487-
(psc-ide-qualified-completion-command prefix alias)
494+
(psc-ide-qualified-completion-command prefix qualifier)
488495
(if manual
489496
;; 2. fil| + manual <- don't filter at all
490497
(psc-ide-command-complete
@@ -498,9 +505,9 @@ The cases we have to cover:
498505
nil
499506
(psc-ide-get-module-name))))))
500507

501-
(defun psc-ide-qualified-completion-command (prefix alias)
502-
"Builds a completion command for a PREFIX with ALIAS"
503-
(let ((modules (psc-ide-modules-for-alias alias)))
508+
(defun psc-ide-qualified-completion-command (prefix qualifier)
509+
"Builds a completion command for a PREFIX with QUALIFIERF"
510+
(let ((modules (psc-ide-modules-for-qualifier qualifier)))
504511
(psc-ide-command-complete
505512
(vector (psc-ide-filter-prefix prefix)
506513
(psc-ide-filter-modules (vconcat modules)))
@@ -512,34 +519,50 @@ The cases we have to cover:
512519
(-map (lambda (import) (cdr (assoc 'module import)))
513520
(psc-ide-parse-imports-in-buffer)))
514521

515-
(defun psc-ide-modules-for-alias (alias)
522+
(defun psc-ide-modules-for-qualifier (qualifier)
516523
"Searches the current module's imports for modules that are
517-
qualified as ALIAS"
524+
qualified as QUALIFIER"
518525
(let ((imports (psc-ide-parse-imports-in-buffer)))
519526
(-keep (lambda (import)
520-
(when (equal alias (cdr (assoc 'alias import)))
527+
(when (equal qualifier (cdr (assoc 'qualifier import)))
521528
(cdr (assoc 'module import)))) imports)))
522529

530+
(defun psc-ide-qualifier-for-module (module &optional parsed-imports)
531+
"Searches the current module's imports for MODULE and returns
532+
its qualifier. Returns nil if the module is not imported qualified"
533+
(let ((imports (or parsed-imports (psc-ide-parse-imports-in-buffer))))
534+
(-first-item
535+
(-keep (lambda (import)
536+
(when (equal module (cdr (assoc 'module import)))
537+
(cdr (assoc 'qualifier import)))) imports))))
538+
523539
(defun psc-ide-handle-completionresponse (callback response)
524540
"Accepts a callback and a completion response from psc-ide,
525541
processes the response into a format suitable for company and
526542
passes it into the callback"
527543
(let* ((result (psc-ide-unwrap-result response))
528-
(completions (-map 'psc-ide-annotate-completion result)))
544+
(completions (-map (-partial 'psc-ide-annotate-completion (psc-ide-parse-imports-in-buffer)) result)))
529545
(funcall callback completions)))
530546

531-
(defun psc-ide-annotate-completion (completion)
547+
(defun psc-ide-annotate-completion (parsed-imports completion)
532548
"Turns a completion from psc-ide into a string with
533549
text-properties, which carry additional information"
534-
(let ((identifier (cdr (assoc 'identifier completion)))
535-
(type (cdr (assoc 'type completion)))
536-
(module (cdr (assoc 'module completion))))
537-
;; :qualifier qualifier <- TODO: Add this back in
538-
(add-text-properties 0 1 (list :type type
539-
:module module) identifier)
540-
;; add-text-properties is sideeffecting and doesn't return the modified
541-
;; string, so we need to explicitly return the identifier from here
542-
identifier))
550+
(let-alist completion
551+
(let* ((qualifier (psc-ide-qualifier-for-module .module parsed-imports))
552+
(identifier (if (and psc-ide-add-qualification-on-completion
553+
qualifier
554+
;; Don't add a qualifier if we're already
555+
;; completing a qualified prefix
556+
(not (s-contains-p "." (company-grab-symbol))))
557+
(format "%s.%s" qualifier .identifier)
558+
.identifier)))
559+
560+
(add-text-properties 0 1 (list :type .type
561+
:module .module
562+
:qualifier qualifier) identifier)
563+
;; add-text-properties is sideeffecting and doesn't return the modified
564+
;; string, so we need to explicitly return the identifier from here
565+
identifier)))
543566

544567
(defun psc-ide-goto-definition-impl (search)
545568
"Asks for the definition location of SEARCH and jumps to it."
@@ -586,18 +609,18 @@ on whether WARN is true."
586609
"Builds a type command from SEARCH."
587610
(let* ((components (s-split "\\." search))
588611
(ident (car (last components)))
589-
(alias (s-join "." (butlast components))))
590-
(if (not (s-blank? alias))
591-
(psc-ide-qualified-type-command ident alias)
612+
(qualifier (s-join "." (butlast components))))
613+
(if (not (s-blank? qualifier))
614+
(psc-ide-qualified-type-command ident qualifier)
592615
(psc-ide-command-show-type
593616
(vector (psc-ide-filter-modules
594617
(psc-ide-all-imported-modules)))
595618
ident
596619
(psc-ide-get-module-name)))))
597620

598-
(defun psc-ide-qualified-type-command (ident alias)
599-
"Builds a type command for an IDENT with ALIAS"
600-
(let ((modules (psc-ide-modules-for-alias alias)))
621+
(defun psc-ide-qualified-type-command (ident qualifier)
622+
"Builds a type command for an IDENT with QUALIFIER"
623+
(let ((modules (psc-ide-modules-for-qualifier qualifier)))
601624
(psc-ide-command-show-type
602625
(vector (psc-ide-filter-modules (vconcat modules)))
603626
ident

test/psc-ide-test.el

+6-6
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import Halogen.HTML.Events.Indexed as P
4040
(string-match psc-ide-import-regex import)
4141
(let* ((import (psc-ide-extract-import-from-match-data import)))
4242
(should (equal (assoc 'module import) (cons 'module name)))
43-
(should (equal (assoc 'alias import) (cons 'alias as)))))
43+
(should (equal (assoc 'qualifier import) (cons 'qualifier as)))))
4444

4545
(ert-deftest test-get-import-from-match-data-full ()
4646
(test-import "import Mod.SubMod (foo, bar) as El"
@@ -50,7 +50,7 @@ import Halogen.HTML.Events.Indexed as P
5050
(ert-deftest test-match-import-single-module ()
5151
(test-import "import Foo" "Foo" nil))
5252

53-
(ert-deftest test-match-import-with-alias ()
53+
(ert-deftest test-match-import-with-qualifier ()
5454
(test-import "import Foo as F" "Foo" "F"))
5555

5656
(ert-deftest test-match-import-with-single-expose ()
@@ -62,10 +62,10 @@ import Halogen.HTML.Events.Indexed as P
6262
(ert-deftest test-match-import-with-multiple-exposings-loose ()
6363
(test-import "import Foo ( test1 , test2 )" "Foo" nil))
6464

65-
(ert-deftest test-match-import-with-alias+multiple-exposings-tight ()
65+
(ert-deftest test-match-import-with-qualifier+multiple-exposings-tight ()
6666
(test-import "import Foo (test1,test2) as F" "Foo" "F"))
6767

68-
(ert-deftest test-match-import-with-alias+multiple-exposings-loose ()
68+
(ert-deftest test-match-import-with-qualifier+multiple-exposings-loose ()
6969
(test-import
7070
"import Foo ( test1 , test2 ) as F"
7171
"Foo"
@@ -76,9 +76,9 @@ import Halogen.HTML.Events.Indexed as P
7676
(lambda () (psc-ide-all-imported-modules)))))
7777
(should (equal (length imports) 10))))
7878

79-
(ert-deftest test-moduels-for-alias ()
79+
(ert-deftest test-moduels-for-qualifier ()
8080
(let ((imports (psc-ide-test-example-with-buffer
81-
(lambda () (psc-ide-modules-for-alias "P")))))
81+
(lambda () (psc-ide-modules-for-qualifier "P")))))
8282
(should (equal (length imports) 2))))
8383

8484
(ert-deftest test-get-completion-settings ()

0 commit comments

Comments
 (0)