Skip to content

Conversation

@joshbax189
Copy link
Contributor

Adds completion, supporting both company and completion-at-point completion styles out-of-the-box.

Call either completion-at-point or company-manual-begin to get completions.

Limitations

  • does not complete keywords, e.g. "for", "function" etc (although this could be enabled within company)
  • completion-at-point does not complete global values, i.e. when the prompt is empty

How it works

At a high level, it sends the current user input followed by TAB to the REPL, then turns the response back into a list of candidates.

The completion responses are dispatched to listening callbacks, which contain their own state in closures. The callbacks represent a specific completion, e.g. "complete 'Array.' with a list of properties", or "clear the input". This async design means we avoid blocking while waiting for responses, so completion does not feel laggy.

Specifically, js-comint--async-output-filter is the dispatcher: it diverts output to a hidden buffer js-comint--completion-buffer when there are callbacks in js-comint--completion-callbacks and when the input and prompt positions match. Each of js-comint--get-completion-async and js-comint--clear-input-async are listener "factories", which send some input and register a callback to handle the expected response, implicitly hiding the output until they are completed.

@redguardtoo
Copy link
Owner

You don't need require company explicitly. The standard practice is using completing-read api which supported by company-mode. Could you also squash your commits into one commit which is easier for my review?

Squashed commit of the following:

commit 479eb46
Author: Josh Bax <[email protected]>
Date:   Tue Dec 3 16:41:38 2024 -0800

    Fix: use emacs 28 compat completion fns in test

commit 5870d05
Author: Josh Bax <[email protected]>
Date:   Tue Dec 3 16:17:33 2024 -0800

    Fix: avoid Eask issue to unblock tests

commit 19446fd
Author: Josh Bax <[email protected]>
Date:   Tue Dec 3 10:51:17 2024 -0800

    Tidy comments and docstrings

commit af87be0
Author: Josh Bax <[email protected]>
Date:   Tue Dec 3 10:41:40 2024 -0800

    Fix do not pass arguments to callback

commit ee0824b
Author: Josh Bax <[email protected]>
Date:   Tue Dec 3 15:23:16 2024 -0800

    Add Capf tests

    * test/capf-tests.el: C

commit 9fcb825
Author: Josh Bax <[email protected]>
Date:   Mon Dec 2 17:50:10 2024 -0800

    Move tests into own folder and separate files

    This allows separate loading of company and CAPE

commit bd2616b
Author: Josh Bax <[email protected]>
Date:   Tue Dec 3 15:17:17 2024 -0800

    Native capf

commit 5d67b6b
Author: Josh Bax <[email protected]>
Date:   Wed Nov 27 11:22:20 2024 -0800

    Use string-match-p to support 28

commit 9f1241f
Author: Josh Bax <[email protected]>
Date:   Wed Nov 27 11:03:56 2024 -0800

    Cleanup hung callbacks when completing props of null object

commit 8937577
Author: Josh Bax <[email protected]>
Date:   Tue Nov 26 12:45:12 2024 -0800

    Quote string when used as regexp in callback

commit e276f26
Author: Josh Bax <[email protected]>
Date:   Tue Nov 26 11:46:38 2024 -0800

    Address fast-typing bug: extra input when callbacks overlap

commit 4353c89
Author: Josh Bax <[email protected]>
Date:   Tue Nov 26 10:28:00 2024 -0800

    Use ert-async for tests with checks in callbacks

    Update test-company-complete-long-line

commit 44f2eac
Author: Josh Bax <[email protected]>
Date:   Mon Nov 25 12:32:37 2024 -0800

    Fix matching bug in complete-substring

commit bb479f5
Author: Josh Bax <[email protected]>
Date:   Mon Nov 25 11:34:27 2024 -0800

    Remove unused completion string property

commit ea7a9fd
Author: Josh Bax <[email protected]>
Date:   Mon Nov 25 10:28:29 2024 -0800

    Fix: minor formatting change

commit 2ae491d
Author: Josh Bax <[email protected]>
Date:   Mon Oct 28 22:35:16 2024 -0700

    Cleanup completion buffers

commit e07de7b
Author: Josh Bax <[email protected]>
Date:   Mon Oct 28 22:22:11 2024 -0700

    Pass NODE_REPL_MODE as argument due to custom env

    Env var has no effect for some reason

commit b649559
Author: Josh Bax <[email protected]>
Date:   Mon Oct 28 22:19:17 2024 -0700

    Wiggle the cursor to produce a prompt and prevent hanging

    This makes most cases resolve with similar output too

commit 123c87d
Author: Josh Bax <[email protected]>
Date:   Mon Oct 28 21:23:52 2024 -0700

    Add Company integration tests

commit 83f8a31
Author: Josh Bax <[email protected]>
Date:   Mon Oct 28 19:37:40 2024 -0700

    Simplify process-completion-output

commit f80ca42
Author: Josh Bax <[email protected]>
Date:   Mon Oct 28 19:24:50 2024 -0700

    Proactively clear partial completions

commit 672c28a
Author: Josh Bax <[email protected]>
Date:   Mon Oct 28 16:08:57 2024 -0700

    Complete long statements by taking latest substring

commit 3e6863e
Author: Josh Bax <[email protected]>
Date:   Mon Oct 28 15:48:04 2024 -0700

    Don't complete at whitespace

commit 51ebba7
Author: Josh Bax <[email protected]>
Date:   Fri Oct 25 14:48:24 2024 -0700

    Add unit tests for listener callback code

commit 4157796
Author: Josh Bax <[email protected]>
Date:   Fri Oct 25 14:42:12 2024 -0700

    Recover from errors in company callbacks after completion

commit 0ed6d0b
Author: Josh Bax <[email protected]>
Date:   Fri Oct 25 14:12:50 2024 -0700

    Set TERM using env

    Remove with-environment-variables because it must be set within the
    comint call. Move other vars into the same location.

commit aa38210
Author: Josh Bax <[email protected]>
Date:   Fri Oct 25 10:22:44 2024 -0700

    Failed callbacks should be removed

commit 5b3efea
Author: Josh Bax <[email protected]>
Date:   Fri Oct 25 09:45:53 2024 -0700

    Factor out callback active test

commit 67208a6
Author: Josh Bax <[email protected]>
Date:   Thu Oct 24 23:11:50 2024 -0700

    Add retry counter for method completion

commit 9433d55
Author: Josh Bax <[email protected]>
Date:   Thu Oct 24 23:11:38 2024 -0700

    Factor out callback creation

commit dd0a5e5
Author: Josh Bax <[email protected]>
Date:   Thu Oct 24 13:08:07 2024 -0700

    Rewrite completions to use listener pattern callback

commit 83e3509
Author: Josh Bax <[email protected]>
Date:   Thu Oct 24 13:02:45 2024 -0700

    Rename company-js-comint-backend => company-js-comint

    Matches other backend names

commit 74592d5
Author: Josh Bax <[email protected]>
Date:   Tue Oct 22 23:56:41 2024 -0700

    Don't use comint-send-string for invisible input

commit 4816ab3
Author: Josh Bax <[email protected]>
Date:   Tue Oct 22 23:50:09 2024 -0700

    Inline company definition in should-complete

commit c6cbbda
Author: Josh Bax <[email protected]>
Date:   Tue Oct 22 12:48:22 2024 -0700

    Use 28.1 compatible kill-matching-buffers in test

commit bb64f53
Author: Josh Bax <[email protected]>
Date:   Tue Oct 22 12:36:52 2024 -0700

    Add should-complete unit tests

    Add Eask file

commit 0dc206a
Author: Josh Bax <[email protected]>
Date:   Tue Oct 22 11:23:47 2024 -0700

    Add test macro with-new-js-comint-buffer and simplify tests

    Kills comint buffer between runs for safety

commit 054c097
Author: Josh Bax <[email protected]>
Date:   Sun Oct 20 11:42:37 2024 -0700

    Add unload function to remove company backend

commit 96cf5a7
Author: Josh Bax <[email protected]>
Date:   Wed Oct 16 16:32:28 2024 -0700

    Unit tests for normal completion

commit 2721fd8
Author: Josh Bax <[email protected]>
Date:   Wed Oct 16 16:24:30 2024 -0700

    Use js-comint-get-process

commit f2f3d65
Author: Josh Bax <[email protected]>
Date:   Wed Oct 16 15:50:56 2024 -0700

    Do not take arguments for js-comint--discard-output when a callback

commit 7829fac
Author: Josh Bax <[email protected]>
Date:   Wed Oct 16 15:41:30 2024 -0700

    Discard should apply to all output until control char

commit 6e274c8
Author: Josh Bax <[email protected]>
Date:   Wed Oct 16 15:35:53 2024 -0700

    Add unit tests for js-comint--process-completion-output

commit 5b50ada
Author: Josh Bax <[email protected]>
Date:   Wed Oct 16 13:32:57 2024 -0700

    Properly complete method names

commit 7f57d76
Author: Josh Bax <[email protected]>
Date:   Wed Oct 16 11:12:29 2024 -0700

    Factor out completion processing logic

commit a98dc6e
Author: Josh Bax <[email protected]>
Date:   Wed Oct 16 09:15:20 2024 -0700

    Simplify handler logic

commit f182a52
Author: Josh Bax <[email protected]>
Date:   Tue Oct 15 15:30:25 2024 -0700

    Completion handler with debug messages

commit 18ff4f6
Author: Josh Bax <[email protected]>
Date:   Tue Oct 15 13:43:59 2024 -0700

    Filter completion output when a flag is set

commit 1cfedc9
Author: Josh Bax <[email protected]>
Date:   Tue Oct 15 10:13:52 2024 -0700

    Get input for completion

commit 1352dbf
Author: Josh Bax <[email protected]>
Date:   Tue Oct 15 09:01:49 2024 -0700

    Add stub for company-js-comint-backend and init code
@joshbax189
Copy link
Contributor Author

Could you also squash your commits into one commit which is easier for my review?

Done

The standard practice is using completing-read api which supported by company-mode

I think you mean completion-at-point not completing-read?

The reason I chose to add company-js-comint is because I designed the completion to use async functions. For example, see the signature js-comint--get-completion-async (input-string callback): the completion results are used as an argument to callback. This already matches the company API.

completion-at-point-function is a function that takes no arguments and (synchronously) returns (START END COLLECTION . PROPS). To convert js-comint--get-completion-async to a completion-at-point function I call the async function then poll for a result, see the implementation of js-comint--capf (note CAPF = completion-at-point function).

So, rather than having company-capf (async) call js-comint--capf (sync) call js-comint--get-completion-async (async), I just directly provide a company backend and enable it if company is loaded.

If company is not loaded, then js-comint--capf is used, so you can use completion with no extra packages installed.

@redguardtoo redguardtoo merged commit 68908b5 into redguardtoo:master Dec 5, 2024
3 checks passed
@redguardtoo
Copy link
Owner

Thanks.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants