Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Adding support for Sublime Text 3 #214

Closed
urschrei opened this issue Mar 15, 2017 · 47 comments
Closed

Adding support for Sublime Text 3 #214

urschrei opened this issue Mar 15, 2017 · 47 comments
Labels

Comments

@urschrei
Copy link

Sourcegraph have a beta version of an lsp client, here. I've set it up and configured it, and I've got rls (e24fc84, on Nightly 6f10e2f63) running too. Predictably, it's not working yet, but it looks like:

  • sublime-lsp is sending data to rls
  • it's getting responses

What are some next steps to figure out where the current incompatibilities are?

Sample request:
Action: hovering over .to_vec(), in a file at /Users/sth/dev/rust-geo/src/algorithm/simplifyvw.rs

This sends (visible from the sublime-lsp debug log in the console):

[
    'Content-Length: 259',
    '',
    '{',
    '  "method": "textDocument/hover", ',
    '  "params": {',
    '    "position": {',
    '      "character": 23, ',
    '      "line": 53',
    '    }, ',
    '    "textDocument": {',
    '      "uri": "file:///Users/sth/dev/rust-geo/src/algorithm/simplifyvw.rs"',
    '    }',
    '  }, ',
    '  "jsonrpc": "2.0", ',
    '  "id": 27',
    '}'
]

And receives the response:

{
    'result': {
        'range': None,
        'contents': []
    },
    'jsonrpc': '2.0',
    'id': 27
}

I've configured the User settings for Sublime-lsp as follows:

{
    "clients": [
        {
            "binary": "rls",
            "file_exts": ["rs"],
            "path_additions": [
                "/users/sth/dev/rls/target/release/",
                "/Users/sth/.rustup/toolchains/nightly-x86_64-apple-darwin/lib"
            ],
            "env": {
                "DYLD_LIBRARY_PATH": "/Users/sth/.rustup/toolchains/nightly-x86_64-apple-darwin/lib",
                "RLS_ROOT": "/users/sth/dev/rls/target/release/rls"
            }
        }
    ]
}

(I've also set up DYLD_LIBRARY_PATH and RLS_ROOT in my .bashrc)

@sophiajt
Copy link

@urschrei - great that you're digging into this.

A couple things -

Can you confirm that the test you're doing of hovering .to_vec works in VSCode with your configuration? I want to isolate that it's specific to the sublime plugin.

I'm a little confused by:

[
    'Content-Length: 259',
    '',
    '{',
    '  "method": "textDocument/hover", ',
    '  "params": {',
    '    "position": {',
    '      "character": 23, ',
    '      "line": 53',
    '    }, ',
    '    "textDocument": {',
    '      "uri": "file:///Users/sth/dev/rust-geo/src/algorithm/simplifyvw.rs"',
    '    }',
    '  }, ',
    '  "jsonrpc": "2.0", ',
    '  "id": 27',
    '}'
]

Should this be read as separate lines? Seems so, but I'm not sure how the line separator works here. I don't think we use one generally since it would (I think) be added to the character count in Content-Length: 259

@urschrei
Copy link
Author

Sorry, I hadn't seen your comments in sourcegraph/sublime-lsp#12 (comment)!

I think the separators I pasted above are inserted by the debug output in the Sublime console; if they weren't, wouldn't I be seeing errors from the RLS binary, as opposed to empty responses?

In any case:

  • I've set up VS Code for testing, and it's working as expected (When I launch projects from the rls-enabled instance they're analysed, and I get a squiggle for unclosed delimiters etc)
  • I've re-configured my sublime-lsp settings to match those you used in your comment (using the helper script etc.)
  • I've disabled definitions and index files, as per the Sublime-lsp docs.

After a Sublime restart, I can see the debug telemetry being sent and received again, and I get squiggles and errors as I type. (🎉):

screenshot 2017-03-16 16 12 04

Some questions (these aren't necessarily all for you. @alexsaveliev probably has some of them covered)

  • I'm not sure what langserver functionality should be enabled in Sublime (Local definitions on hover? Stdlib and import definitions on hover? Local references? Autocomplete?
  • How can we test whether that functionality might override what's provided by the Rust-enhanced and Rust-autocomplete packages (my autocomplete seems to have stopped working), or vice-versa?
  • Should we settle on a simple set of test files or a project to use for testing all this?
  • Can we figure out what a user can test, and what functionality she should see as a result?

I'm happy to turn the results into some docs and screenshots, and then open a PR.

@sophiajt
Copy link

sophiajt commented Mar 19, 2017

Awesome! I'm glad we see some signs of life. Would love to follow along with progress here

@sophiajt
Copy link

Circling back to this. I'm trying out https://github.com/tomv564/LSP with a recent rls, and everything seems to work now. Closing, but feel free to reopen if I'm missing something.

@nrc
Copy link
Member

nrc commented Sep 13, 2017

I expect we'd need to implement our custom parts of the protocol to have a complete experience here.

@nrc nrc reopened this Sep 13, 2017
@sophiajt
Copy link

@nrc - seems like this bug is fixed, but we could open another for the extras you mention

@nrc
Copy link
Member

nrc commented Sep 13, 2017

They're not really extras - its required to offer the complete RLS experience. But I plan to write some docs about this and organise the issues a bit today.

@kamek-pf
Copy link

After a quick test, this still looks too buggy to be usable.
If I make a typo, I'm getting an error, as I should :
1

But then if I fix it and save, the error doesn't go away.

@sophiajt
Copy link

@kamek-pf - I saw some of that, but it took a few seconds for the error to go away. You're seeing that sometimes the error doesn't go away at all?

@kamek-pf
Copy link

From what I see, it looks like it never goes away unless you break something else.
If you simply fix the error without breaking anything else, the error stays.

@sophiajt
Copy link

@kamek-pf I played with it for a few minutes but couldn't get it to break once. That said, I don't doubt you're seeing an issue.

Just to double check, how recent is your rls?

@kamek-pf
Copy link

I just installed it through rustup on my machine at work :

$HOME/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rls --version
rls-preview 0.122.0-nightly (303671e 2017-08-31)

@nrc nrc added the clients label Sep 13, 2017
@urschrei
Copy link
Author

This is also going to cause problems for people who use Rust Enhanced (which is where all the Sublime Rust development is happening now), because it's not clear how to point LSP at its definitions (although that's more of an issue on their side)

@ehuss
Copy link
Contributor

ehuss commented Sep 13, 2017

it's not clear how to point LSP at its definitions

You can change the config to point to the different syntax file like this:

    "clients": {
      "rls": {
        "command": ["rustup", "run", "nightly", "rls"],
        "scopes": ["source.rust"],
        "syntaxes": ["Packages/Rust Enhanced/RustEnhanced.sublime-syntax"],
        "languageId": "rust"
      }

However, there currently isn't a way to merge/update the existing clients dictionary (see sublimelsp/LSP#76), so you have to copy all clients to your custom config.

@urschrei
Copy link
Author

After ~3 days of use, this seems quite stable. I know there's some functionality missing, but aside from occasionally needing a server restart (which can be triggered from the command palette), it's working well enough to use as part of my setup.

@golddranks
Copy link
Contributor

So, there seems to have been a blur of activity, and encouraged, I installed the LSP plugin on my Sublime 3, and tried editing Rust code. It says "LSP initialized" in the status bar, but that disappears after a short while. Besides that, I don't notice anything to be different from the normal editing experience. There is no autocomplete. Goto definition from the menu bar causes an error "Unable to find ..." to show in the status bar. I've tried the "LPS: show diagnostics", but it just shows a separate console-like window... that doesn't print anything at all.

My RLS version, rls-preview 0.122.0-nightly (8dd7094 2017-09-12), doesn't seem to work with the LPS plugin as it is. Is there something I am missing?

@urschrei
Copy link
Author

@golddranks Are you using Rust Enhanced, or the bundled Sublime Text Rust plugin?

@golddranks
Copy link
Contributor

I haven't installed Rust Enhanced, but it recognizes that my code is Rust and syntax-hilights it, so I guess I'm using the bundled one...?

@urschrei
Copy link
Author

@golddranks And you can rustup run nightly rls --version from a console and get output?

@golddranks
Copy link
Contributor

Like I said, it's rls-preview 0.122.0-nightly (8dd7094 2017-09-12):

$ rustup run nightly rls --version
rls-preview 0.122.0-nightly (8dd7094 2017-09-12)

@urschrei
Copy link
Author

@golddranks Hm. The only other thing I can suggest is copying the prefs explicitly into your LSP user prefs: Here's what I'm using, adapted for the bundled Rust package: https://gist.github.com/urschrei/5f526b1a00378bfc049b23323b0002fb

@golddranks
Copy link
Contributor

Okay, I tried that. (I earlier had only the "auto_complete_triggers": [ {"selector": "source.rust", "characters": ".:"} ],) line, which was suggested by the LSP docs.

Still no effect – nothing changes. I'd like to help with debugging this, but I don't have a clue where to start. Is there a some way I can log the traffic between RLS and the editor?

@kamek-pf
Copy link

@golddranks Did you install LSP from the package manager ? I couldn't get it to work this way, I had to clone the repo.

Not sure if anyone else is seeing this btw.

@golddranks
Copy link
Contributor

@kamek-pf I installed it using Sublime's package control. I'll try installing from the repo!

@golddranks
Copy link
Contributor

Tried to install it from the repo. Still no signs of life. Btw. now using rls-preview 0.122.0-nightly (7221e38 2017-09-14) and Sublime Text 3.0, Build 3143.

I checked the console; it shows, among others:

reloading plugin LSP.main
plugins loaded
LSP: Config added: rls (enabled)

(Used the user config shared by @urschrei)

Still no signs of life in the editor, though.

Trying with this simple program; creating a new project with cargo, and writing:

struct Foo {
	bar: f32,
}

impl Foo {
	fn hoge() -> String {
		"hoge?".to_owned()
	}
}


fn main() {
    println!("Hello, world!");

    let fuga = Foo { bar: 1.0 };

    fuga
}

It recognises in the right bottom bar that the code is Rust. I think that it should be simple enough for RLS to able to provide completion? When I write a dot after fuga nothing happens. Also, when I do obvious syntax mistakes, nothing happens. Is there any way to see what gets sent to the server, if anything?

@urschrei
Copy link
Author

Can you confirm the Rust scope is active by going to Tools -> Developer -> Show scope name
Then, confirm the active syntax by pasting sublime.active_window().active_view().settings().get("syntax") in the Sublime Text console

@golddranks
Copy link
Contributor

Ah, okay, I managed to enable debug logging in console:

LSP: no clients found for window 5
LSP: no clients found for window 5
LSP: Couldn't determine project directory
LSP: client registered for window 5 {'rls': None}
LSP: Couldn't determine project directory
LSP: Couldn't determine project directory

@golddranks
Copy link
Contributor

Scope:

source.rust
meta.function.rust
meta.block.rust

Active syntax:

>>> sublime.active_window().active_view().settings().get("syntax")
'Packages/Rust/Rust.sublime-syntax'

@golddranks
Copy link
Contributor

sublime.active_window().folders() returns [], which is strange. The file is saved, and belongs to a normal cargo project.

@ehuss
Copy link
Contributor

ehuss commented Sep 18, 2017

@golddranks you need to ensure your project folder is part of a Sublime project (Project > Add Folder to Project...)

@golddranks
Copy link
Contributor

golddranks commented Sep 18, 2017

Okay, got some signs of life. (...so, it doesn't support single files at the moment?).

plugins loaded
LSP: Config added: rls (enabled)
LSP: no clients found for window 2
LSP: no clients found for window 2
LSP: starting in /Users/um003415/repos/hello
LSP: starting ['rustup', 'run', 'nightly', 'rls']
LSP: request 1: initialize 
LSP: client registered for window 2 {'rls': <LSP.main.Client object at 0x111b104d0>}
environment variables loaded using: /bin/bash -l
LSP: got json:  {"jsonrpc":"2.0","id":1,"result":{"capabilities":{"textDocumentSync":2,"hoverProvider":true,"completionProvider":{"resolveProvider":true,"triggerCharacters":[".",":"]},"definitionProvider":true,"refer
LSP: notify: initialized
LSP: notify: textDocument/didOpen
LSP: got json:  {"jsonrpc":"2.0","method":"rustDocument/beginBuild","params":null}
LSP: Unhandled notification: rustDocument/beginBuild
LSP: got json:  {"jsonrpc":"2.0","error":{"code":-32602,"message":"invalid type: unit value, expected struct NoParams"},"id":null}
LSP: got error:  {'message': 'invalid type: unit value, expected struct NoParams', 'code': -32602}
LSP: LSP stderr process ended.
LSP: LSP stdout process ended.
Package Control: Skipping automatic upgrade, last run at 2017-09-18 23:57:59, next run at 2017-09-19 00:57:59 or after

Next, it dies here, every time I save: (edit, or more like, the connection has died on the startup.)

LSP: notify: textDocument/didSave
LSP: client unexpectedly died: [Errno 32] Broken pipe

Still no activity in editor itself, only in the debug log.

@kamek-pf
Copy link

kamek-pf commented Sep 18, 2017

I think we're confusing two different things :

  • sublime-lsp (from sourcegraph), this one isn't available from package control
  • LSP, available from package control as LSP

The first one seems a bit buggy for me, the second one appears to work fine.

Also, they both seem to have trouble dealing with multiple folders (representing different projects) in the same window.
Creating different Sublime projects and switching between them using ctrl+alt+p works well though.

@golddranks
Copy link
Contributor

The issues I'm reporting in this threads are with LSP.

@kamek-pf
Copy link

Hmm, I got LSP and RLS to work on my laptop at work, but on my desktop at home I get the same error @golddranks has : LSP: client unexpectedly died: [Errno 32] Broken pipe.

Not the slightest idea what's different between the 2 setups.
I tried to move my .sublime-text-3 folder to start from scratch, doesn't help.

@golddranks
Copy link
Contributor

golddranks commented Sep 19, 2017

Setting RUST_LOG=rls=trace helps a lot seeing what's going on! Let's see if we can get this working smoothly!

@golddranks
Copy link
Contributor

golddranks commented Sep 19, 2017

So, the problem seems to be that the JSON message received is structured like {"jsonrpc": "2.0", "method": "initialized", "params": null}. There is a slight problem in the message itself, in params: null; according to JSON-RPC, params should be a Structured value (Object or Array) or may be omitted, but it mustn't be Null. (If I understand correctly, Null isn't a Structured value, although JavaScript accepts it as Object... Edit: Okay, it says pretty clearly in the spec that it MUST be a Structured value, or be omitted.)

This leads it to parse the value as Some(Value::Null), which might be an unexpected state; I think the valid, or expected ones are None, Some(Value::Object) and Some(Value::Array).

Initialized is a notification, and it dispatches the message to the parse_as_notification method of RawMessage. However, there onwards it gets murky: the as_params method fails if params is None, although this should be one of the expected cases! Also, it fails to parse the case Some(Value::Null) to the zero-sized struct NoParams, which is the expected outcome.

So, the parsing logic needs a bit more robustness and fixing. Sending a PR!

@tomv564
Copy link

tomv564 commented Sep 19, 2017

Wow, great support and troubleshooting in this issue!

Some updates:

  • initialized sending params: null has now been fixed in master.
  • The Readme now links to the troubleshooting guide
  • The troubleshooting guide now mentions the requirement for documents to be under a folder added to your Sublime workspace.
  • An issue was created to add some UI around these "LSP does nothing, what now?" scenarios.

A question:

  • LSP supports both initializationOptions (for omitInitBuild: true) and settings (for eg. rust.build_lib and rust.build_bin) (example here). Are these working for you? I'd like to document them if they are essential.

@kamek-pf
Copy link

Awesome !

@golddranks I compiled RLS from your branch, I can confirm it fixes it for me too.

@golddranks
Copy link
Contributor

@tomv564 Can the requirement of the workspace be relaxed some day? It would be useful to be able to use RLS even when editing some single file "ad hoc".

After implementing the fix, RLS+LSP now works for me. It shows warnings and errors, and hovering for types works. Go to symbol definition works. Code completion doesn't work for some reason, gonna look at it later.

I haven't tried per-project settings yet.

@tomv564
Copy link

tomv564 commented Sep 22, 2017

@golddranks I believe ad-hoc editing fits with the spirit of Sublime Text and LSP should support it in the future. I'll create an issue to track it now, thanks for your input!

@golddranks
Copy link
Contributor

golddranks commented Sep 23, 2017

Code completion not working was a configuration issue (and my fault, because I was skipping the explanation :P) – I sent a PR to LSP that makes the docs hopefully clearer!

Warnings, errors and code completion for fields and methods works now; for some reason, code completion with use std:: doesn't work.

For some reason, sometimes hovering brings up "No description available", and sometimes the correct types.

I'll keep investigating!

@golddranks
Copy link
Contributor

Also, when starting writing a new line, like printl, a code completion/suggestion menu flashes quickly and disappears.

@golddranks
Copy link
Contributor

golddranks commented Sep 23, 2017

The use std:: problem was because of Racer crashing – it couldn't find the RUST_SRC_PATH env var, which I thought it wouldn't need anymore! (This confused me: The Racer binary doesn't need it, it knows how to use rustup to get the path, so it works without. However, Racer as a library doesn't, so it crashes.) Sent a PR to Racer to fix this: racer-rust/racer#799

@golddranks
Copy link
Contributor

golddranks commented Sep 24, 2017

Hooray, most of the initial bugs I stumbled upon are either fixed or waiting for merge! (I'm gonna start using Sublime as my daily Rust editor and try to find some more.)

@nrc
Copy link
Member

nrc commented Nov 26, 2017

Some instructions for using rust-enhanced: https://github.com/rust-lang/rust-enhanced#rls

@nrc
Copy link
Member

nrc commented Jan 10, 2018

And just to note that Rust Enhanced has been doing really well recently - 50k+ downloads and in the top 15 trending packages - rust-lang/rust-enhanced#224

@Xanewok
Copy link
Member

Xanewok commented Mar 1, 2019

Triage: It looks like there are no outstanding bugs and Sublime is supported. Thanks to everyone involved! 🎉 I'll close this but feel free to reopen if there's anything crucial or Sublime-specific that needs fixing.

@Xanewok Xanewok closed this as completed Mar 1, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

8 participants