Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

How does emacs display a dialog where you can select all the methods to select along with some additional UI elements for extra options?


Emacs can be described as an interactive, lisp-based environment for building textual UIs (TUIs). It's very easy to extend it with arbitrary, dynamic behaviours, including ones that need to collect information from the user.

As a trivial example, let's say for some reason I keep needing to generate the sha256 hash of a password and add it to the current file. I could add this to my .emacs:

    (defun my-insert-password-hash (password)
      (interactive "MPassword: ")
      (insert (secure-hash 'sha256 password)))

    (global-set-key (kbd "C-c p h") 'my-insert-password-hash)
Now if I hit Ctrl-C followed by p then h I will be prompted for a password in the minibuffer and the hash of the string I provide will be added where my cursor is. I didn't need to write any GUI code.

This kind of user interaction can equally easily allow the user to select from lists of dynamically determined options, including very large ones (with nice fuzzy matching menus if you use ivy, helm or similar). It's also trivial to write functions that prompt for several pieces of information, ask for different information depending on context, etc.

In the case of LSP, the server only has to provide information about what options are available and what possible responses are permitted. It's easy for emacs to dynamically provide the corresponding UI.


Ok, so you're not pointing to an implementation of the feature that I asked about. Instead, you're asking me to implement it as a special case. That's backwards.

You're effectively confirming that there needs to be feature-specific integration code for each and every navigation/refactoring/... feature in the editor. Once you have that, you again have tight coupling.


Not at all. The server allows for discovery of code actions and their parameters and the UI is displayed in response, by code that knows nothing about particular actions. It goes something like this (won't be accurate in the details, check the lsp spec [1] under Code Action Request if you want chapter and verse):

User to emacs: I want to perform a code action.

Emacs to server: The selection is this. What code actions can you perform?

Server to emacs: I can perform actions called "Extract Method", "Extract Base Class", etc.

Emacs to user: Choose an action from this list

User to emacs: I'll do "Extract method"

Emacs to server: We're going with "Extract Method", what info do you need.

Server to emacs: I need an item called "method name" which is a string and an item called "visibility", which is one of the following: "public", "private", "protected".

Emacs to user: Enter method name... Select visibility...

Emacs to server: Here are the parameter values.

Server: Here are the edits necessary to perform the code action

Emacs: Updates code.

No special per-action code is required. If you want to see an implementation have a look at lsp-mode[2].

I hope that makes it clear. I've spent more of my day explaining this than I should have now, so I'll leave it there.

[1] https://microsoft.github.io/language-server-protocol/specifi...

[2] https://emacs-lsp.github.io/lsp-mode/


Please forget "Extract Method" as an example. It's simply far too trivial. Imaging extracting a new superclass or interface from a class with 100 methods and you need to pick the set of methods to extract. You can't do that with 100 y/n choices.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: