Python Intellisense for Keywords

Hi, I was trying the Studio for LSP Demo runtime project, it seems that the python intellisense for keywords are not popping up. Do you support this on the current release version? I saw sample video of this that works but I cannot seem to make it work even I would just run the demo project. am I missing something in here?

Thank you,
Jena

Hi Jena,

We do not have much control over what the LSP server returns in response to the code completion request, and the current Python LPS server we use does not return keywords.

We’re now looking at changing the default LSP server to Pyright, one that drives default Python extension for Visual Studio Code.

This one is being actively developed, and it shows keywords in the code completion.

With the current implementation, it should be possible to extend code completion results with the list of keywords by handling SyntaxEdit.NeedCodeCompletion event, please take a look at CodeCompletion quick start project showing how to do that.

Kind regards,
Dmitry

Hello, Dmitry.

Oh… Okay. I got it. How about your existing parsers, like PythonParserEmbedded and PythonNETParser, does it support python intellisense for keywords?

I am currently trying to use a different LSP Server, not your built-in Palantir server and was able to override some of the implementations to make it work in AlterNETSoft CodeEditor. In addition, I know that the server I am currently using can return Python Intellisense Keywords because I am using that to an existing application where we have our own CodeEditor. So now, I am trying to migrate from our CodeEdtior to AlterNETSoft CodeEditor and would like to get your assistance on possible ways I could look into to get the return values of the server and have it displayed on the AlterNETSoft CodeEditor. I am trying to read AlterNETSoft libraries so I could extend or override, but if you could lead me to the right path then it would be a great help.

Let me know as well if this SyntaxEdit.NeedCodeCompletion is the same thing that I am asking above. I will review more of that.

Thank you,
Jena

Hi Jena,

We tried another Python LSP server, and it does not seem to return keywords either. Just wondering which LSP server you’re using - we can look at supporting it as well.

PythonParserEmbedded uses a Palantir LSP server. The only difference between this and a “regular” LSP Python Parser is that it contains LSP server installation in the form of the application resource and can be deployed on the user’s PC. Our LSP demo project uses PythonParserEmbedded, so it works on users’ computers that do not have Python or Python LSP server installed.

PythonNETParser is our implementation of Python parsing. It does not require Python to be installed, and we can change it as it’s our own source code.

It does display a list of keywords; however, there are some other limitations as to the code completion engine - for example, it does not support parsing packages with the complex structure (numpy is one of the examples).

NeedCodeCompletion allows you to extend the code completion list with custom items, but it’s definitely best if the parser can handle it.

Kind regards,
Dmitry

Hi Dmitry,

To answer your question, we are using the Python LSP, the same server that you latter mentioned. It is currently returning keywords and built-in functions on my end.

As for the NeedCodeCompletion, I was able to successfully sent a RequestCompletion from this event using the CodeCompletionArgs. I have a list of OmniSharp CompletionList model which contains the list of the completion words (both built-in python keywords and functions) that needs to be display on the TextEditor Completion Box. See below screenshot
intellisensewithbuiltinkeywordsfunctions

I can see that there’s TextEditor.ShowCodeCompletionBox to do this, however, I would need ICodeCompletionProvider, which I do not want to recreate from scratch and rather use the provider that TextEditor have. Is there an existing function I could use to fill the completion box with keywords and functions? that it would show like your native completion box without doing the provider and all the mapping of images?

I would definitely agree that it is best handled in the parser itself. I might do that later on. What I am trying to achieve now is to know the process and understand that this is a possible task using your TextEditor without doing too much work on the TextEditor implementation itself or even on the PythonParser because we don’t want to recreate something that you already have. Python built-in Keywords and Functions Intellisense are important features that we need to implement before we fully migrate to AlterNETSoft TextEditor.

Thanks,
Jena

Hi Jena,

You’re right, I should have double-checked it. This LSP server returns list of keywords - please refer to the attached image.

In the coming release, we will make the LSP server configurable (so you will be able to specify a different LSP server to be used).

Right now, if you execute a separate request to collect a list of keywords/functions/etc. in NeedCodeCompletion event, here’s the sample code that populates list members to be displayed in the code completion listbox:

    private void Edit_NeedCodeCompletion(object sender, CodeCompletionArgs e)
    {
        const int Keyword = 14;
        if (e.CompletionType == CodeCompletionType.ListMembers || e.CompletionType == CodeCompletionType.CompleteWord)
        {
            var members = new Alternet.Syntax.Parsers.Lsp.LspListMembers();

            members.Images = parser.CompletionRepository.Images;

            LspListMember member = new Alternet.Syntax.Parsers.Lsp.LspListMember
            {
                Name = "assert",
                ImageIndex = Keyword - 1,
            };
            members.Add(member);

            e.Provider = members;
            e.ToolTip = false;
        }
    }

If you already have CompletionItem list received via OmniSharp, you can instantiate fields of LspListMember like this:

member.Name = item.Value;
member.ImageIndex = (int)item.Kind - 1,
member.Selected = item.Preselect;
member.Description = item.Documentation != null ? item.Documentation : string.Empty,
member.InsertTet = item.InsertText != null ? item.InsertText : string.Empty;

Kind regards,
Dmitry

Hello Dmitry,

LSP server being configurable is a good news for us. And looking at your response, it seems that you made the other server work on your end. We will look forward on the updates.

Thank you for the sample code you have provided, I was able to test it out and made some things work. However, I have few more clarifications with regards to NeedCompletion event and how it works. Based on your sample code, it seems that it is strategical to only show the CompletionBox whenever the CompletionTypes are ListMembers and CompleteWord. This is because the NeedCompletion event is being triggered whenever I try to move the mouse, and that made sense. But it is unusual for me that I always get QuickInfo as a value of CompletionType in the CodeCompletionArgs whenever the NeedCompletion is hit. See below

Moreover, I was able to forcefully show the CodeCompletion just for testing by using ShowCodeCompletionBox. Is this the correct way? and what’s the difference of using editor. ShowCodeCompletionBox, editor.ListMembers, editor.CompleteWord or the like?

Regards,
Jena

Hi, do we have update on my query above? Thank you @dmitry.medvedev

Kind regards,
Jena

Hi Jena,

In the upcoming release (which at this point is expected mid-May), we’re going to support three Python LSP servers out of the box: pyls, pylsp ad pyright.

Both pylsp and pyright return keywords in the code completion; most likely we will use pyright by default as on our tests it works a lot faster.

Kind regards,
Dmitry

Hi Dmitry,

Thank you for your response. Supporting LSP servers out of the box is a good news for us. However, I’d like to ask if the mid-May release would also allow us to configure the Python version we would want to use in relation to LSP server? I want to raise this due to vulnerability issues we had experience using lower versions. Our release requirement also depends on the python version, relying solely to the deployed server from AlterNETSoft may limit us to use specific/newer version because the LSP server and its support could have been tied up in the backend. @dmitry.medvedev

Kindly advise.

Regards,
Jena

Hi Jena,

It’s already possible to configure LSP server to use a specific Python version instead of relying on embedded Python server we deploy in our examples by specifying PythonPath like this:

        var pythonParser = new Alternet.Syntax.Parsers.Lsp.Python.PythonParser();
        pythonParser.Workspace.ServerDeploymentOptions.Mode = Alternet.Syntax.Parsers.Lsp.ServerDeploymentMode.ApplicationDefined;
        pythonParser.Workspace.ServerDeploymentOptions.ApplicationDefinedServerDirectory = "C:\\Users\\dmitr\\AppData\\Local\\Programs\\Python\\Python39\\";

Please let me know if this is what you need.

Regards,
Dmitry

Hi Dmitry, I’d like to have a follow up inquiry regarding this. Do you now support Python Intellisense for keywords on the recent release version 9.0? I have tried both pyright and pyslp on your current release but I am not sure if keywords are being supported. Do we have sample demo project to utilize this?

Thanks,
Jena

Hi Jena,

Sorry for the late reply. Yes, we’ve updated our default LSP server to Pyright, which displays keywords in the code completion. Pylsp LSP server is supported as well and it shows keywords in the code completion too.

Kind regards,
Dmitry