smallcloudai / refact-lsp

LSP server for Refact, suitable for Sublime Text, and other editors
BSD 3-Clause "New" or "Revised" License
18 stars 10 forks source link

[bounty] In Sublime Text, add support for LSP 3.18 InlineCompletion #1

Closed olegklimov closed 7 months ago

olegklimov commented 10 months ago

In this project there's an LSP server for Refact. It almost works well, but it adds text to a popup in Sublime, doesn't show the gray text.

sublime-multiline-insert

The mini_auto_complete option in Sublime replaces popup with a single line gray text, but it's not the right thing either.

The right thing would be multiline gray text, and it so happens the upcoming LSP standard version 3.18 has the official support for it, called InlineCompletion.

This issue/bounty is for implementing it correctly in Sublime.

Some tricky parts:

A discussion at Sublime forum, they also have Discord.

olegklimov commented 10 months ago

/bounty $3000

algora-pbc[bot] commented 10 months ago

💎 $3,000 bounty created by olegklimov 🙋 If you'd like to work on this issue, comment below to get assigned 👉 To claim this bounty, submit a pull request that includes the text /claim #1 somewhere in its body 📝 Before proceeding, please make sure you can receive payouts in your country 💵 Payment arrives in your account 2-5 days after the bounty is rewarded 💯 You keep 100% of the bounty award 🙏 Thank you for contributing to smallcloudai/refact-lsp!

aazam-gh commented 10 months ago

/attempt #1

Options
mnmt7 commented 10 months ago

I would like to take a look at this.

mnmt7 commented 10 months ago

@olegklimov Just to be clear, I need to make a PR to sublimelsp/LSP adding support for LSP 3.18 InlineCompletion.

olegklimov commented 10 months ago

Just to be clear, I need to make a PR to sublimelsp/LSP adding support for LSP 3.18 InlineCompletion

mnmt7 commented 10 months ago

@olegklimov Just to be clear, I need to make a PR to sublimelsp/LSP adding support for LSP 3.18 InlineCompletion.

@jwortmann Would you be open to such a PR?

jwortmann commented 10 months ago

Hello, so based on this issue am I right assuming that you are planning or working on adding inline completions to the Refact LSP server?

With the inline completions added to the 3.18 LSP specs, I was/am considering to experiment with it and try to implement it myself, however, at the moment and as far as I know there isn't any server yet with support for inline completions. So the problem would be that there is currently no way to test it, which seems like a blocking factor for this feature, because there are likely some limitations and edge cases to be expected. GitHub Copilot for example uses its own custom requests for the completions (which I think are not really compatible with the inline completions from the LSP specs), and there are significant bugs/limitations for the phantom (inline) completions implemented in the LSP-copilot helper package: https://github.com/TerminalFi/LSP-copilot/issues?q=is%3Aissue+is%3Aopen+label%3Aphantom Most of them could probably only be fixed or solved by SublimeHQ in Sublime Text core.

But perhaps the implementation from the LSP-copilot package can be used as a starting point.

@mnmt7 Of course contributions for the LSP package are welcome. Feel free to open a PR if/when you have implemented something. Or maybe we can even work together.

olegklimov commented 10 months ago

isn't any server yet with support for inline completions

This can be easily made into such server. It's still proof of concept, very small and simple at this stage.

LSP-copilot

It seems they got stuck in 2022, arrived at something that worked good enough for them (a popup?).

solved by SublimeHQ in Sublime Text core

Maybe we can persuade them to solve a couple we need.

nexuspy commented 10 months ago

/attempt https://github.com/smallcloudai/refact-lsp/issues/1

Options
digital-phoenix commented 9 months ago

@nexuspy Are you still working on this issue? I'd love to take a shot at this if you're busy with other stuff.

ogil7190 commented 9 months ago

@olegklimov do we need to PR into your repo or sublime ?

olegklimov commented 9 months ago

Hi @ogil7190 It's much more clear now!

LSP is a protocol, inline completions from 3.18 are not currently supported by any IDE, including VS Code Insiders.

But, it is possible to send any custom call to the LSP server, and wait for results, in VS Code typescript it can be done like this:

completions = await client.sendRequest("my-lsp/getCompletions", params);

where client is an LSP client, from vscode-languageclient package. Most likely it is just as easy in a Sublime extension.

In this issue, the goal is to make Sublime Text support multi line completions. That might require some changes to Sublime itself, or maybe just changes to the open LSP module for Sublime.

It's completed if there's an easy way for an extension to produce inline completions (gray text) that can be accepted by pressing Tab. And that doesn't interfere too much with a regular completion popup that already exists in Sublime. The grey text itself should be easy to deliver to the Sublime extension, because LSP is easy to work with, we can make a custom function without following the new standard.

digital-phoenix commented 9 months ago

/attempt #1

I looked into the sublime plugin api and it seems like this shouldn't be too difficult to complete.

Options
jwortmann commented 9 months ago

This task is still blocked by this server not supporting the inline completions from the language server protocol, no? Or is it part of the task to add that as well, i.e. to provide both an implementation in sublimelsp/LSP, and also a PR to this repository here?

I can guarantee that there will be no PR merged into the Sublime LSP package until there exists a server which implements the inline completions.

inline completions from 3.18 are not currently supported by any IDE, including VS Code Insiders

Usually it is a hard prerequisite that a client implementation exists (usually it's VS Code), for any new feature to be included into the LSP specifications. So this actually surprises me quite a bit, but I must say that I haven't checked the VS Code source code, so I don't want to doubt what you wrote. But it means that this is basically a chicken or egg problem, because probably no client would want to add something that is not supported by any server, and vice versa...

However, if custom requests are on the table, that would certainly lower the entry barrier to get this done. Could you confirm that a custom request like refact-lsp/inlineCompletion is an allowed option?

Another alternative would be to just create a custom "Refact AI" package for Sublime Text, without any LSP at all, if I understand your last paragraph correctly?

Anyways, I expect all of the alternatives to still have significant limitations in usability due to Sublime Text API limitations and bugs with the inline (phantom) text.

solved by SublimeHQ in Sublime Text core

Maybe we can persuade them to solve a couple we need.

I'm not sure how, though. The corresponding issues are well known and were reported in the issue tracker quite some time ago, see for example https://github.com/sublimehq/sublime_text/issues/5536 and various other issues. Still, there seems to be no reaction from the SublimeHQ dev(s), and in general it seems unclear to me how they prioritize such bugs and feature requests.


It was/is unclear to me whether you plan to add inline completion support to this server, but if the requirements are more loose (like a custom request), then I'd also like to give this a try if possible

/attempt #1

Options
algora-pbc[bot] commented 9 months ago

Note: The user @digital-phoenix is already attempting to complete issue #1 and claim the bounty. If you attempt to complete the same issue, there is a chance that @digital-phoenix will complete the issue first, and be awarded the bounty. We recommend discussing with @digital-phoenix and potentially collaborating on the same solution versus creating an alternate solution.

digital-phoenix commented 9 months ago

@olegklimov Am I correct in assuming you plan to support the multi line lsp spec with the refact server?

ogil7190 commented 9 months ago

@olegklimov I think i have solved the issue as you like, I can provide my solution if you want, please look at the SS and tell me if its correct or not.

Screenshot 2023-09-27 at 1 46 15 AM
olegklimov commented 9 months ago

@ogil7190

correct or not

Looks fantastic! But it also needs behavior to be correct (or at least not super annoying). The inline completion might conflict with a regular popup that already exists in Sublime. They might trigger at different moments in time, appear to fight for the screen space, etc.

olegklimov commented 9 months ago

@jwortmann

Another alternative would be to just create a custom "Refact AI" package for Sublime Text, without any LSP at all, if I understand your last paragraph correctly?

It still has to be an LSP server, because it needs some other events LSP servers typically receive, like file opened, text changed, here is where the workspace (project folder) is.

ogil7190 commented 9 months ago

@olegklimov We need to have a plugin like VScode. This needs a custom tailored solution as nothing direct from API is accessible. This will also not interfere with existing popups for real estate in my opinion.

image
olegklimov commented 9 months ago

interfere with existing popups for real estate in my opinion

What happens when you hit Tab? To provide visual clues to the user, the one visible (popup or the grey text) should react to Tab. So only one should be visible. Or I'm open to other solution that people at least can get used to. Or I can use it for a day and honestly say it works natural for me. It's the tricky part I think here.

olegklimov commented 9 months ago

We need to have a plugin like VScode

Right, let me create the repo:

https://github.com/smallcloudai/refact-sublime

To get a sense of what kind of behavior is needed, @ogil7190 you can try our VS Code plugin -- we have free tier cloud inference.

ogil7190 commented 9 months ago

@olegklimov i am already a user of your free tier 😂

  1. On new lines, we show text suggestions, since there is no popup.
  2. as we start typing something, we can get popup, in that case if popup is visible, text suggestions are hidden and we can use tab on popup for the first suggestion or we can close the popup using escape.
  3. As we use escape, we can show inline suggestions, and if we press tab there we can accept the suggestion or cancel it using escape.
  4. We need to have follow-as-user-type suggestions, it removes the part of suggestions as it is becoming a real text, in this case we do not change the suggestion text, but as user start typing it can show popup, so in this case both suggestion and popup are visible together.
  5. If there are more than 1 line suggestion, popup appears at top of the row not bottom.

Lets start working on it, i am sure i can implement this complex UX and make it feel like the same in both cases. But we need to regularly connect, PR comments is too slow.

Screenshot 2023-09-27 at 3 02 13 AM
jwortmann commented 9 months ago

It still has to be an LSP server, because it needs some other events LSP servers typically receive, like file opened, text changed, here is where the workspace (project folder) is.

Those events can easily be communicated via the regular Sublime Text plugin API. There are other plugins like for example https://packagecontrol.io/packages/Codeium which do exactly that. There is not really the need for an LSP server in that case, and it might have other advantages like more control over when the completions are triggered, etc.

It would still have the same behavioral and presentation bugs, for example like in the screenshot from the comment above, where the cursor is accidentally placed to the right of the inline completion.

But to directly implement the inline completion from the LSP specs into this server and into the LSP package would have the advantage that the client part should be maintained in the long term, and potentially be improved if/when Sublime Text fixes its bugs. But it is still unclear whether this is the goal; I've asked in the Sublime forum at https://forum.sublimetext.com/t/how-to-implement-multiline-code-suggestions-in-sublime-text/68934/6 and in this issue whether it will or should be added to this server, but there is still no clear answer for that. So I think that my sentence from the comment above still stands - no server support, no merge. It's just too risky without any practical testing, and we don't want to have possibly very buggy behavior in the LSP package, if it doesn't work well.

The alternative with a custom LSP request would be much easier; we can just push the implementation to a new LSP-refact helper package, with a custom inline rendering for that request. This is how LSP-copilot works, which also uses a custom request for its completions (and also a custom listener implementation to control when they are triggered).

I could imagine that GitHub Copilot some time in the future will change its API to use the new textDocument/inlineCompletion request from the LSP specs, though. It seems to have some other advantages, like communicating the currently selected completion item from the regular completions popup to the server when requesting inline completions. See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#inlineCompletionContext. This probably can only work if implemented directly into the LSP client package (though I think this aspect is currently not possible with the Sublime API limitations).

digital-phoenix commented 9 months ago

I agree with what @jwortmann is saying. I believe the best step forward is to add spec conformant inline completion functionality to this server. That would provide a clear test case for this issue and would better future proof the project.

ogil7190 commented 9 months ago

We need to have a plugin like VScode

Right, let me create the repo:

https://github.com/smallcloudai/refact-sublime

To get a sense of what kind of behavior is needed, @ogil7190 you can try our VS Code plugin -- we have free tier cloud inference.

@olegklimov add me to some team or slack or collaboration group. email - ogil7190@gmail.com

olegklimov commented 9 months ago

I created an LSP channel on our slack, as a way to quickly ask questions. Anyone can join if you're interested, email needed for that. But I'll answer here just as well.

jwortmann commented 9 months ago

So was there any decision yet on how exactly this task should be implemented (referring to my previous comments)?

If it can be anything that looks somewhat like an inline completion, but doesn't have to be the exact description from the opening post (i.e. inline completions from the LSP specs for sublimelsp/LSP client, which is currently blocked by no server having support for that), it might just take an hour or two of work if you are familiar with the Sublime Text API and know what to do (like for example, just copy the phantom implementation from LSP-copilot...). But to get the best possible design for long term sustainability and to figure out the behavioral edge cases probably requires extensive testing and several iterations.

digital-phoenix commented 9 months ago

@olegklimov feel free to add my to the slack my email is jarobrownlee on gmail.

algora-pbc[bot] commented 8 months ago

@hnhparitosh: The Refact.ai team prefers to assign a single contributor to the issue rather than let anyone attempt it right away. We recommend waiting for a confirmation from a member before getting started.

VirtualDharm commented 8 months ago

/attempt https://github.com/smallcloudai/refact-lsp/issues/1

Options Cancel my attempt

algora-pbc[bot] commented 8 months ago

@VirtualDharm: The Refact.ai team prefers to assign a single contributor to the issue rather than let anyone attempt it right away. We recommend waiting for a confirmation from a member before getting started.

VirtualDharm commented 8 months ago

hey everyone I am new to open source, can i start working on this? or guide me to something where I can contribute to!!!!

olegklimov commented 8 months ago

Hey @ogil7190 can you show your prototype, for partial bounty or something? We want this plugin to actually be completed. Any progress seems to be stopped at the moment.