unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: JD Smith <jdtsmith@gmail.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: emacs-devel@gnu.org
Subject: Re: Recommendation for CAPF setup when you don't know completion string in advance
Date: Mon, 10 May 2021 23:33:44 -0400	[thread overview]
Message-ID: <DD714E9B-5392-4207-9EE3-816AA8D8E328@gmail.com> (raw)
In-Reply-To: <jwvh7knvss3.fsf-monnier+emacs@gnu.org>

> On Apr 3, 2021, at 7:49 PM, Stefan Monnier <monnier@iro.umontreal.ca> wrote:
> 
>> So it’s a quandary: I won’t yet know `beg’ and `end’ until _after_
>> interacting with iPython.  Is there any way to “revise” ‘beg’ and ‘end’ in
>> a collection function returned from a CAPF?
> 
> Indeed, that's a problem.
> You might be able to get away with the following:
> 
> Make your CAPF function return a beg..end that covers "the whole line"
> and which returns a completion table in the form of a function.
> That function will then defer to the iPython code for the grunt of its
> work and will return the "real" boundaries via the
> `completion-boundaries` method.  This will probably require some caching
> in the completion table so we don't call iPython too many times for
> a single completion (like once for `completion-boundaries`, once for
> `try-completion`, once for `all-completions`, etc...).


Thanks for these good suggestions.  I have been attempting a solution along these lines, and I think the performance is fine.  I notice that CAPF first asks for metadata (which are static here), then for the boundaries.  At that point, I don’t know the boundaries, so I talk to the iPython process, and ask it for all the completions for the entire line, given my position within it. It returns these, along with the portion of the full line it has decided to complete.  From this, I compute and return the boundaries.  Then, in subsequent calls to my table lambda, CAPF proceeds with its various ACTION's.  

Pseudo-code for the completion table function I’m building:

(lambda (string pred action)
  (when (no-result-yet-cached-or-cached-string-is-substring-of-string)
    (if (eq action 'metadata) `(metadata …)
      (unless last-prefix
	;; talk to ipython and save the completion info
	)
      (if (eq (car-safe action) 'boundaries) ; munge the boundaries
	  `(boundaries from . to) ; using saved data from ipython
	(complete-with-action action completion-alist string pred)))))

The problem I have encountered is in the final call to `complete-with-action’.  The original string may be an entire line, such as:

	for i in ran[Tab]

and iPython correctly figures out to complete just a portion of that string; say ran -> range.  I tell CAPF about these boundaries (in this case, 9 . 0).  If, however, I later call complete-with-action (which just calls try-completion, all-completions, etc.) with the entire line string (“for in ran”), together with a completion-alist that contains iPython’s sub-string completions (like “range”), it thinks there is no completion.  If instead I just peel off the part of the full line that needs completing (“ran”) and pass that to complete-with-action as STRING, it recognizes that there's a good completion now, but then _replaces the entire line_ with the result.

How do I get complete-with-action/try-completion/test-completion/all-completions etc. to respect my boundaries?





  parent reply	other threads:[~2021-05-11  3:33 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-03 22:23 Recommendation for CAPF setup when you don't know completion string in advance JD Smith
2021-04-03 23:49 ` Stefan Monnier
2021-04-04  2:52   ` JD Smith
2021-04-04  3:20     ` Stefan Monnier
2021-04-04 15:29       ` JD Smith
2021-05-11  3:33   ` JD Smith [this message]
2021-05-11  4:04     ` Stefan Monnier
2021-05-11 13:42       ` JD Smith
2021-05-11 15:52         ` Stefan Monnier
2021-05-11 14:37       ` Daniel Mendler
2021-05-11 15:57         ` Stefan Monnier
2021-05-11 16:10           ` Daniel Mendler
2021-05-11 16:54             ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DD714E9B-5392-4207-9EE3-816AA8D8E328@gmail.com \
    --to=jdtsmith@gmail.com \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).