Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for additionalTextEdits #61

Closed
2 tasks done
brennop opened this issue Apr 13, 2022 · 13 comments
Closed
2 tasks done

add support for additionalTextEdits #61

brennop opened this issue Apr 13, 2022 · 13 comments
Labels
feature-request Request for a feature to existing module mini.completion

Comments

@brennop
Copy link

brennop commented Apr 13, 2022

Contributing guidelines

Module(s)

mini.completion

Description

Some lsp provide a additionalTextEdits on completion items containing useful edits like auto-imports.

nvim-cmp has support for additionalTextEdits, it would be cool if mini.completion had too.

@brennop brennop added the feature-request Request for a feature to existing module label Apr 13, 2022
@echasnovski
Copy link
Owner

Thanks for suggestion! I am honestly quite surprised that any people decided to use 'mini.completion' instead of other solutions. Suppose, that is a pleasant surprise, after all :)

I am not sure, if this will be a small addition to codebase. The 'mini.completion' module is already too complicated and "big" for my taste. But we now have at least some testing coverage for that module, so I am more comfortable with experimenting with new features.

I'll look into it.

@echasnovski
Copy link
Owner

@brennop , I think I managed to come up with a reasonable solution, but not sure because I couldn't find any hands-on example of additionalTextEdits in LSP servers I use. Could you, please, test the completion-additionalTextEdits branch?

@brennop
Copy link
Author

brennop commented Apr 18, 2022

Thanks for trying to implement it. I tried with tsserver and clangd, but none of them worked.

I've tried with the snippet provided here but no luck still.

@echasnovski
Copy link
Owner

Ok, I finally got time to get back at this.

I found out that pyright has auto-import support and it works for me. Here is an example:
completion_pyright-auto-import

Could you, please, try again? This time:

  • Make sure that you are using completion-additionalTextEdits branch. The bulletproof method is to look at file 'lua/mini/completion.lua' (with root being wherever you installed 'mini.nvim' plugin) and it should have these lines:
    -- Try to apply additional text edits
    H.apply_additional_text_edits(vim.v.completed_item)
  • If no luck, could you provide a minimal example that you think should work but doesn't? I have installed 'tsserver' but have close to zero knowledge of Typescript.
  • If you have time and 'tsserver' still doesn't provide auto-import, could you set up pyright and try what I have tried in animaion? Create new file 'tmp.py', ensure running LSP server (can do :LspStart or :LspRestart if you are using 'nvim-lspconfig'), type accumulate or Counter, select completion item and hit <Space>.

@SebasAren
Copy link

@echasnovski I was looking for the same functionallity as @brennop but couldn't get the implementation to work with typescript. Using your above demonstration I got it to work with pyright, which is awesome, but typescript seems to ignore it.

I've created 2 files to setup a testing environment.
tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "baseUrl": "./"
  }
}

lib.ts

export function testFunction() {}

Now if you would add another file, eg: index.ts and start typing testFunction, it will show the autocomplete suggestion, but it won't auto-import.

Hope this helps.

@echasnovski
Copy link
Owner

That is certainly a great help: both 'pyright' confirmation and Typescript example. Thanks! I managed to reach the testFunction autocomplete suggestion and reproduce no auto-import. You would expect something like import { testFunction } from "./lib"; added as first line, right?

I am not sure how to deal with inconsistency between languages. Maybe typescript-language-server sends information about auto-completion in some other form, not sure. I'll try to dig a little further.

@echasnovski
Copy link
Owner

echasnovski commented May 2, 2022

So it seems like typescript-language-server returns additionalTextEdits not after 'textDocument/completion' request (as pyright, for example) but after 'completionItem/resolve'. I'll see what can be done here.

@echasnovski
Copy link
Owner

I just pushed in completion-additionalTextEdits branch what seems to be a working support of another way of supplying additionalTextEdits (in 'completionItem/resolve'). Here I made a what I think is a reasonable compromise: edits are applied if that request was already sent. That means edits will be applied if user selects completion item and waits some time (usually for information window to appear). Pretty sure that is the most support I think is reasonable to implement here.

@brennop, @SebasAren, could you, please, try this out? Especially with something other than typescript-language-server.

@SebasAren
Copy link

SebasAren commented May 2, 2022 via email

@SebasAren
Copy link

It now auto-imports from typescript! It also didn't break the pyright imports.

Seems to be working perfectly. Thank you!

@echasnovski
Copy link
Owner

That is great news! I'll wait a little bit in case other issues pop up (they shouldn't, but they always might). And also confirmation from some other LSP server (not already tested 'pyright' or 'typescript-language-server') would be nice (of course, if you already are using one).

@SebasAren
Copy link

Just tried it it out using volar, which is the Vue lsp. This worked as well. It uses typescript to resolve these imports I believe, so that was to be expected.

@echasnovski
Copy link
Owner

I decided to push this code to main. Hope it works with other LSP servers.

Thanks to both of you for suggestions and testing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request for a feature to existing module mini.completion
Projects
None yet
Development

No branches or pull requests

3 participants