Sublime Forum

Is it possible auto complete brackets for functions and methods?

#1

I have been using Sublime Text for some time and really impressed its performance and ability to customise. However, the one feature which I feel missing in SLT is the auto inserting of brackets () when auto completing functions and methods. I am aware the same question is asked in this forum here Python autocomplete for methods (problems with parentheses) but found no answer. The problem I am having is the same like the user mentioned in the other post whenever I type print and enter with auto completer, it ony types print. Whereas the expected behaviour is print(). The same goes for any methods of classes as well.

I use primarily Python with Python-lsp for auto complete. Though it appears like a small request, it takes a lot of effort to take the hands-off to put only brackets which I feel that should be done by the editor. It is seamlessly integrated in PyCharm and possible to include in VS Code with addition of single line option.

Request if someone help me on this please.

0 Likes

#2

This is a feature of the LSP, not the Sublime Text LSP package, but the LSP itself. Clangd (the C++ LSP) and gopls (Go LSP) for example do that. So you have to open an issue at the actual LSP that you are using

0 Likes

#3

Sublime Text tries to guess trailing symbols such as ()in completions, if "auto_complete_trailing_symbols": true is set. It however works based on various heuristics and may not always provide expected results.

Static completions (via *.sublime-completions` files don’t provide information about which trailing symbols to optionally add.

Hence they either can always provide print(), which may result in print()("text") in your code, if argument list was already present.

    p|("printed text")
    # expands to
    print()("printed text")

To avoid that, static completions often only provide least viable content - the function name print.

Whether and which content to optionally append heavily depends on language and type of completion item, which probably makes it cumbersome/hard to implement a common algorithm, which works everywhere.

This behavior can however be achieved by auto-completion plugins. They can analyse text content and decide, whether to append content. CSS is doing that for instance.

But this requires someone to write such clever behaving completion plugin for a language.

Finally as demand of cleverness goes far beyond brackets after function names, the world is moving towards centralized code intelligence servers aka. language servers, which communicate via LSP.

0 Likes

#4

Thank you @Release-Candidate for your comment.

I agree that it should be provided by language server and right not the recommanded LSP for python is pyright which I have been using for quite some time. After you replied me, I have gone through closed issue of pyright GitHub repo and found that this request has been asked multiple times by multiple users. However, the issues were closed in all cases by denying that it won’t be implemented as it is exclusively earmarked for pylance. This makes me believe that ST team or community should make something from their end to make it work.

Thank you @deathaxe for your insight into how static completion works.

I have already enabled the option as suggested by you but it did not give me any intended result. Your statement:-

as demand of cleverness goes far beyond brackets after function names, the world is moving towards centralized code intelligence servers aka. language servers, which communicate via LSP.

I completely agree with your statement. For this, I recommand that ST team should try to implement the jedi-language-server which is exclusively implemented for autocompletion alone. This input or recommendation is based on my experience after I posted this message. I installed pylsp language server to check if the bracket completion works and to my surprise, it does. But the completion is inconsistent. Sometimes it puts the bracket, and most of the time it won’t. Moreover, it appears to be noticeably slower than pyright. Lastly, I tried with pyright lsp (https://github.com/sublimelsp/LSP-pyright) and jedi Python autocompletion plugin (https://packagecontrol.io/packages/Jedi%20-%20Python%20autocompletion). The last step (pyright + jedi) works finally. I don’t know how pyright and Jedi interacting in ST, but I have the result as per my expectation. This makes me believe that ST should have jedi-language-server as one of the choice for users.

If anyone facing similar issue, I would recommand that you install pyright LSP + ruff LSP + Jedi - Python autocompletion plugin to get better experience on Python development environment.

0 Likes

#5

I completely agree with your statement. For this, I recommand that ST team should try to implement the jedi-language-server

sublimhq focuses on creating a language agnostic editor, which works well for all sorts of syntaxes. As such it is not their goal to work on such language specific topics.

Jedi Python autocompletion is a package dating back to the time before LSP was invented. It works a propriate protocol to communicate with a jedi server. It works pretty much like a modern language server.

pylsp actually uses jedi as well as various other python plugins to provide its features. It is written in python and therefore much slower thatn pyright, which is implemented in JavaScript.

denying that it won’t be implemented as it is exclusively earmarked for pylance .

A common Microsoft strategy. Spoilering forks with cutted open-source stuff to promote comercial closed source tools.

Not checked your specific use case, but https://packagecontrol.io/packages/LSP-basedpyright claims to support many features, pyright team denied to implement.

0 Likes

#6

No adding of parens, unfortunately. But that is the LSP I use for Python, which adds e.g. semantic highlighting and inline hints https://packagecontrol.io/packages/LSP-basedpyright

0 Likes

#7

Hi,

Thank you all for your reply.

The frustration about this issue lead me to further investigate how LSP and jedi works in Sublime Text. I was looking for LSP-jedi auto-completion for Sublime Text. Since there was none, I created one. It is at the moment hosted in my repo at https://github.com/wxguy/LSP-jedi. Efforts are on to port it to Package Control. LSP-jedi works beautifully on my environment.

Dropped this message to help out the fellow users who are facing similar issue.

0 Likes

#8

I wonder what extra value is provided by Jedi here. Pyright itself provides autocompletion already and it’s even (much) faster than Jedi.

0 Likes

#9

There is nothing against pyright. It is just that won’t add brackets for functions and methods, it is really frustrated me a lot. In addition, it also provides extra arguments along with auto completion which improves the coding process. If LSP-jedi provides those additional functionality which suite your need, why not use it?

0 Likes

#10

I have a secret gift for you if you are able to contact me on Discord. :slight_smile:

0 Likes