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

How to correctly use WidgetLSPAdapter to enable code completion in the code editor of custom extension #1109

Open
boogie-ben opened this issue Dec 27, 2024 · 3 comments

Comments

@boogie-ben
Copy link

boogie-ben commented Dec 27, 2024

I have written a custom extension which supports files with the .cpw suffix. After opening a .cpw file, there will be 0 to N code editors (created by newInlineEditor of the CodeMirrorEditorFactory instance).

I expect to get the code completion feature in these code editors.

I wrote a CPWAdapter by imitating NotebookAdapter and FileEditorAdapter. It can successfully start the LSP connection and communicate for .cpw files, but I haven't found the correct implementation to enable the code completion feature for the code editors. I'm asking for help.

I have created a minimum reproducible code repository. Please evaluate my code and let me know if there are any problems with my implementation process.

minimum reproducible

Current progress:

  1. In the activate function in index.ts, I use WidgetTracker and new CPWAdapter in widgetAdded.
  2. In CPWWidget, code editors are created in the content area. (In the actual business process, code editors are generated dynamically and there may be 0 or more of them.)
  3. After a certain code editor gets focused, it is designated as the activeEditor.
  4. In CPWAdapter, when the activeEditor is switched or its content changes, the VirtualDocument is updated.
@boogie-ben
Copy link
Author

3demo.mp4

This is the current effect. I expect the code completion of each code editor to be independent. Therefore, the content of the VirtualDocument should only contain the content of the activeEditor, rather than the content of all the code editors.

@krassowski
Copy link
Member

You will need to register a custom completion provider which sends textDocument/completion requests. This happens in:

export const COMPLETION_PLUGIN: JupyterFrontEndPlugin<ICompletionFeature | null> =

And the provider is implemented in:

export class CompletionProvider implements ICompletionProvider<CompletionItem> {

I imagine that you could inherit from the provider class of jupyterlab-lsp.

@boogie-ben
Copy link
Author

@krassowski
Thank you for your reminder. I have used the CompletionProvider and imitated the code of notebook-extension

my code

Now the code completion has taken effect!

However, I found that some features are still missing in my editor instance, such as the hover feature, the highlighting of error, and so on. That is to say, the HoverFeature and other features in the LSP are not working in my editor instance. What should I do next?

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

No branches or pull requests

2 participants