sublimelsp / LSP

Client implementation of the Language Server Protocol for Sublime Text
https://lsp.sublimetext.io/
MIT License
1.64k stars 181 forks source link

${packages} in Development Version #1500

Closed jiehsheng closed 3 years ago

jiehsheng commented 3 years ago

I tried the Development Version and found ${pageckages} not defined. Is there any way to define it?

rchl commented 3 years ago

What are you trying to do exactly and where are you trying to use ${packages}?

Also, I'm not sure what you mean by "development version". I would just recommend installing it through Package Control.

jiehsheng commented 3 years ago

Appreciated your help. The following lists "Stable Version" and "Development Version."

https://github.com/sublimelsp/LSP

The reason why I need the Development Version is that: I'd using "autocomplete" to trigger GPT-2 at back-end pyls. For example:

PatentGPT demo

Now, I'd like to do autocomplete in ".patent" files instead of hacking ".py" files. It's where I bumped into the undefined "${packages}".

Thank you for your help.

rchl commented 3 years ago

Still have no idea what are you talking about.

The latest stable release is the same as the development version (at this moment).

And you still haven't explained in which place you are using the ${packages}.

And I don't know what .patent files are.

jiehsheng commented 3 years ago

Sorry about the confusion. When clicking "Preferences > Package Settings > LSP > Settings", it pops up the following error message:

The settings file "res://Packages/LSP/LSP.sublime-settings" coult not be opened.

I traced code and identified that the file "Packages\LSP-st4000-exploration\Main.sublime-menu" is the place to define the "LSP.sublime-settings" file. If I modify it to point to the full path of the actual "LSP.sublime-settings" file on my disk, it works. If not, the original ""base_file": "${packages}/LSP/LSP.sublime-settings"" does not work. It

As for the ".patent" files, I'm trying to define a language id with my own file extension to contain patent text. My plan is to trigger my language server only when editing a ".patent" text file. Hope this clarifies the background of my issue.

Thank you very much for your guidance.

rwols commented 3 years ago

The directory name must be LSP, not LSP-st4000-exploration.

jiehsheng commented 3 years ago

BTW, is there any "installation" step required for the Development Version? On the web page it says:

Development Version Clone this repository into your Packages directory. Open the command palette and run Package Control: Satisfy Dependencies.

The "Satisfy Dependencies" command works well, but I'm not sure that's all needed.

jiehsheng commented 3 years ago

The directory name must be LSP, not LSP-st4000-exploration.

Aha. It works like a charm. Really appreciated your kind help. đź‘Ť

rwols commented 3 years ago

Note that there are two branches: one for ST3 (master) and one for ST4 (st4000-exploration). By default you will clone the st4000-exploration branch. If you're not using ST4 then LSP won't work for you. Checkout the master branch if you're using ST3.

rwols commented 3 years ago

Also, you don't really need to have a development version of this package if all you want to do is work on a language server.

jiehsheng commented 3 years ago

Also, you don't really need to have a development version of this package if all you want to do is work on a language server.

But, I'd like to define my own languageId for the ".patent" file extension. Therefore, I'm trying to figure out how to add my languageId on this "LSP" side. In parallel, I'm working on the server side (based on palantir/python-language-server ) to catch the new languageId to trigger GPT-2 for autocomplete.

Is this the right way to add a new language id?

rwols commented 3 years ago

You should do the following (ST4 instructions):

  1. Write a sublime-syntax for .patent files. For the base scope of the syntax, choose source.patent (or maybe text.patent ?)
  2. Modify your pyls configuration so that it also starts for those kinds of scopes: "selector": "source.python | source.patent" (or, again, you may want to use text.patent)
  3. Modify pyls so that it understands those kinds of files (the language ID will be patent)
jiehsheng commented 3 years ago

Embarrassingly I've tried two hours and still failed. My users will use ST3. So I'd stick to ST3, if you don't mind. The document I'm following is:

  https://lsp.readthedocs.io/en/latest/

My "User/LSP.sublime-settings" contains:

"pyls": { "command": ["C:\lang\anaconda3\envs\py36\Scripts\pyls.exe"], "enabled": true, "languageId": "patent", "selector": "source.patent | source.python | text.patent | text.plain", "scopes": ["source.patent", "source.python", "text.patent", "text.plain"], }

I put the syntax file as "Packages/patent/patent.sublime-syntax" and its content is simple:

name: Patent file_extensions:

scope: text.patent

scope: source.patent

I tried both "text.patent" and "source.patent" to no avail. Is there any quick fix I can try? Or, is ST4 the only solution? It seems not easy to find where to download ST4. I can try that too if available.

Thank you for your kindness.

rchl commented 3 years ago

selector is not used in ST3. You also need:

syntaxes: ["Packages/patent/patent.sublime-syntax"],
rwols commented 3 years ago

Complete example for ST3 (forward-compatible with ST4):

{
    "clients":
    {
        "pyls":
        {
            "command": ["path/to/modified/pyls"],
            "languages": [
                {
                    "languageId": "patent",
                    "scopes": ["text.patent"],
                    "syntaxes": ["Packages/patent/patent.sublime-syntax"]
                },
                {
                    "languageId": "python",
                    "scopes": ["source.python"],
                    "syntaxes": ["Packages/Python/Python.sublime-syntax"]
                }
            ],
            "enabled": true
        }
    }
}

Complete example for ST4 (doesn't work for ST3):

{
    "clients":
    {
        "pyls":
        {
            "command": ["path/to/modified/pyls"],
            "selector": "source.python | text.patent",
            "enabled": true
        }
    }
}
jiehsheng commented 3 years ago

The ".py" file works well but the ".patent" file still failed. I check "Tools > Developer > Show Scope Name" for the "test.patent" file, and it shows "text.plain." Does this matter? It seems to me the languageId is not set to "patent" correctly. Two days ago I was tracing source code and it also seemed to me the "source.[languageId]" format was hard coded and such "text.plain" might not fit the format. Is this direction relevant? If yes, I can go back to those code again.

Many thanks for shedding light.

rwols commented 3 years ago

If the scope shows “text.plain”, then you have not assigned your patent.sublime-syntax to those kind of files. Look at the file_extensions key of the .sublime-syntax format to auto-assign the syntax to those type of files.

jiehsheng commented 3 years ago

Still fighting and sidetrack a question: I checked "Tools > Command Pallete > LSP: Enable Language Server Globally" and failed to find "patent" as a listed language there. Is this observation relevant? Where is the source of the language list?

rchl commented 3 years ago

Just create your own configuration for it as described here https://github.com/sublimelsp/LSP/issues/1500#issuecomment-736556219 and it will be enabled globally.

rwols commented 3 years ago

If you have this file:

%YAML 1.2
---
# http://www.sublimetext.com/docs/3/syntax.html
name: Patent
file_extensions: [patent]
scope: text.patent
contexts:
  main:
    - match: foo
      scope: keyword.other

and put it in Packages/patent/patent.sublime-syntax, then

1) Files with the extension .patent should have this syntax assigned. Make sure that's the case first :) 2) My example json from above, if put in Packages/User/LSP.sublime-settings, should assign patent files to pyls. 3) Make sure you restart ST3 after every change in LSP.sublime-settings (no longer needed in ST4 though).

jiehsheng commented 3 years ago

These details help a lot. Unfortunately, I'm still struggling at the first step (Files with the extension .patent should have this syntax assigned.).

The second step (Packages/User/LSP.sublime-settings) works because I added some debug code in start_window_config() (clients.py) to show the following:

args: ['C:\lang\anaconda3\envs\py36\Scripts\pyls.exe'] config.name: pyls config.binary_args: ['C:\lang\anaconda3\envs\py36\Scripts\pyls.exe'] config.enabled: True config.init_options: DottedDict({}) config.experimental_capabilities: {} config.additional_variables: {} config.languages: [<LSP.plugin.core.types.LanguageConfig object at 0x000001FAB00B9518>, <LSP.plugin.core.types.LanguageConfig object at 0x000001FAB00B9550>] [ 0 ] id: patent [ 0 ] scopes: ['text.patent'] [ 0 ] syntaxes: ['Packages/patent/patent.sublime-syntax'] [ 1 ] id: python [ 1 ] scopes: ['source.python'] [ 1 ] syntaxes: ['Packages/Python/Python.sublime-syntax']

Back to the first step. Is there anything else I should put in the same directory for "Packages/patent/patent.sublime-syntax"? Clicking "Tools > Developer > Show Scope Name" for the "test.patent" file always shows "text.plain". Or, how can I hack the source code to verify that the "patent.sublime-syntax" is actually consumed by ST3? No idea how this kind of syntax mechanism works.

Any advice?

rchl commented 3 years ago

Try closing and re-opening the file. If that doesn't help, you should be able to assign the syntax manually when clicking on the syntax name (probably Plain text right now) in the bottom-right corner of the window.

jiehsheng commented 3 years ago

The "bottom-right corner" works. Thanks a lot.

Now I'm fighting the "autocomplete" issue. For ".py" file, the character"." works well:

:: <- pyls textDocument/publishDiagnostics: {'diagnostics': [], 'uri': 'file:///C:/temp/patent1.py'} :: -> pyls textDocument/didChange :: --> pyls textDocument/completion(2): {'textDocument': {'uri': 'file:///C:/temp/patent1.py'}, 'position': {'character': 19, 'line': 0}}

But for ".patent" file, the character "." could not trigger "autocomplete":

:: -> pyls textDocument/didChange :: <- pyls textDocument/publishDiagnostics: {'diagnostics': [], 'uri': 'file:///C:/temp/tmp1.patent'}

No matter what I tried, it can not trigger ":: --> pyls textDocument/completion".

Is this caused by the content of the syntax file (patent.sublime-syntax)? Or something else? (I guess so. I'm tracing code.)

rchl commented 3 years ago

Server can define which characters trigger completion. Check CompletionOptions.triggerCharacters in the LSP spec.

Also, ST will trigger completion on certain scopes (like punctuation.accessor) but since your syntax file doesn't define proper scopes, it won't work for you.

jiehsheng commented 3 years ago

Thank you very much again. You saved me many hours.

jiehsheng commented 3 years ago

ST will trigger completion on certain scopes (like punctuation.accessor)

How does ST define the above scope (punctuation.accessor) to trigger the language server? Is it hard-coded in ST source code instead of LSP?

A strange thing is: even I copy all contents of Python syntax (Python.sublime-syntax) to my new patent syntax file (patent.sublime-syntax), ST still won't trigger the server side. After hours, I realized that the "scope:" in the syntax file must be "source.python" for ST to trigger. It won't work if I use "text.patent" as the scope.

With my new "scope", is it possible to define the symbol for ST to trigger?

rwols commented 3 years ago

Forget about LSP for a moment.

The triggering of the auto-complete is completely determined by selectors (or, less preferably, characters). Run Preferences: Settings from the Command Palette and look for the auto_complete_selector setting. This selector determines in which context auto-complete is triggered. The default selector is

meta.tag, source - comment - string.quoted.double.block - string.quoted.single.block - string.unquoted.heredoc

You should not touch/modify this setting because it is carefully chosen. Then there's also the auto_complete_triggers setting. This is a list of {"selector": ..., "characters": ...}, where each item in the list may define additional situations which trigger auto-complete.

You should not touch/modify that setting as well.

In LSP land, servers do not understand what selectors are. They only know what trigger characters are. This package reads the trigger characters advertised by the server in the initialize response, and puts them in the auto_complete_triggers setting of every view attached to the language server session.

You can look at what LSP is doing to your auto_complete_triggers by opening the ST console by clicking on View > Show Console, and running the following in the ST console:

view.settings().get("auto_complete_triggers")

For instance, I can see that pyright registers . and [ as a trigger characters:

[{'characters': '<', 'selector': 'text.html, text.xml'}, {'rhs_empty': True, 'selector': 'punctuation.accessor'}, {'characters': '.[', 'selector': 'meta.tag, source - comment - string.quoted.double.block - string.quoted.single.block - string.unquoted.heredoc', 'server': 'LSP-pyright'}]

By the way, I realize now that text. scopes don't exactly work correctly due to masking the registered trigger characters with meta.tag, source - comment - string.quoted.double.block - string.quoted.single.block - string.unquoted.heredoc. If you try putting source.patent as the base scope, perhaps things start to work. Sorry about that :)

jiehsheng commented 3 years ago

text. scopes don't exactly work correctly due to masking I found it true after trying several things. Even though, thank you indeed for your insightful advice.