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

Add LSP for Markdown #2182

Closed
wants to merge 2 commits into from
Closed

Conversation

slinlee
Copy link
Contributor

@slinlee slinlee commented Apr 19, 2022

Requires installing remark-language-server, adding a .remarkrc.yaml which defines the list of filters to enforce.

Example .remarkrc.yaml:

plugins:
  - remark-preset-lint-consistent
  - remark-preset-lint-recommended

These plugins need to be installed separately too:

pnpm i remark-language-server remark-preset-lint-consistent remark-lint-recommended remark

2022-04-19-120955_786x902_scrot

@slinlee slinlee marked this pull request as ready for review April 19, 2022 19:41
@David-Else
Copy link
Contributor

While this is cool, I think a superior default LSP for Markdown would be https://github.com/valentjn/ltex-ls , it has advanced grammar and spell checking from https://languagetool.org/ built in. It also works for text and latex docs. The problem is that ltex-ls doesn't fully work yet: #1942

Formatting Markdown documents often requires being able to also format all code blocks with different languages too, and only Prettier can do that at the moment. Prettier could take care of a lot of formatting needs in Helix and would make the best default when it can be integrated with LSP.

@the-mikedavis
Copy link
Member

Hmm this LS does seem a bit green (only a handful of stars so far and 3mo old) but I suppose I don't have an objection to it. Though if jalentjn/ltex-ls is more advanced we may want to plug that in instead. Even if the code actions aren't working it might be nice to have for formatting (if it provides that) and lints

@slinlee
Copy link
Contributor Author

slinlee commented Apr 20, 2022

Formatting Markdown documents often requires being able to also format all code blocks with different languages too, and only Prettier can do that at the moment. Prettier could take care of a lot of formatting needs in Helix and would make the best default when it can be integrated with LSP.

@David-Else Oh yeah I'd love prettier as the default formatter for anything supported.

@slinlee
Copy link
Contributor Author

slinlee commented Apr 20, 2022

While this is cool, I think a superior default LSP for Markdown would be https://github.com/valentjn/ltex-ls , it has advanced grammar and spell checking from https://languagetool.org/ built in. It also works for text and latex docs. The problem is that ltex-ls doesn't fully work yet: #1942

@David-Else @the-mikedavis - ltex-ls is just grammar and spell check right? That's pretty cool and I could see how it helps with markdown and text. Remark is more like a linter for things like using inconsistent bullet point styles etc.

I need to check if remark has plugins for suggesting links to internal headers or other files/github.

@the-mikedavis
Copy link
Member

Oh actually I haven't tried either yet, I'm not sure 😅

I would probably prefer the LS that does more syntactic than spelling/grammar checks. I think a spelling/grammar system would be nice to have outside of language-specific LSP stuff. Multiple LSs (#1396) might be a good way to tackle it but maybe an aspell-like plugin would be a better approach.

@David-Else
Copy link
Contributor

Stop press! Since updating Helix now ltex-ls does work. It still seems to need an extended config:
languages.toml

[[language]]
name = "markdown"
language-server = { command = "ltex-ls" }
file-types = ["md"]
scope = "source.markdown"
roots = []

Now you can use code actions and correct spelling/grammar. I don't think the 'add to dictionary' works yet, but in Neovim it needed a little extra code.

Prettier SO nearly works with the pipe command:

"=" = ":pipe prettier --parser json"

The problem is it needs to recognise the language, the above command only works for json.

I have been doing a lot of technical writing lately, and having ltex-ls has been amazing, it is by far the best free grammar and spell checker on the market. The only thing (slightly) better is Grammarly, and that is propriety closed source payware. Forget aspell and all that cruft! :)

My main problem with remark lint is I think the majority of rules are redundant when you are using a formatter like Prettier, and I think that in the near future everyone will be using Prettier, it has become the industry standard. I look at the rules:

https://github.com/remarkjs/remark-lint#rules

Many (most?) of the lints here are automatically corrected by Prettier. Also there are rules like warn when headings are too long, these seem cool, but there is a much better tool called vale https://github.com/errata-ai/vale that has the entire Microsoft technical writing style guide built in which will deal with this and many many more style guide violations.

Basically, to sum up, there is nothing wrong with using remark-language-server and the linting plugin, but it is inferior to using ltex-ls, prettier and vale for writing. I would personally not use it and wait until we have :pipe prettier working for formatting and multiple language servers for adding vale to ltex-ls. That is my setup in Neovim and I use it everyday.

@aral
Copy link
Contributor

aral commented Apr 20, 2022

I just had a quick look into ltex-ls and it apparently requires Java.

Isn’t that a huge dependency to require for default markdown language intelligence? (On Ubuntu, for example, the JRE is a 179MB download. And ltex-ls is another 200MB. I’m also worried about how taxing it will be on my system. I love Helix because it’s not Electron but I’m not sure if Java is going to be any better.)

About to try it out and document the required steps but just thought I’d document my initial misgivings before I forget them. It might be that the advantages outweigh the 400MB download + system resource usage but my gut feeling is that it won‘t.

@David-Else
Copy link
Contributor

@aral If you have the Java runtime installed then you can use the version that does not include it, https://github.com/valentjn/ltex-ls/releases/download/15.2.0/ltex-ls-15.2.0.tar.gz

I get what you are saying, but it really is the best grammar and spelling setup. You only need the Java runtime, not the entire language. Have a look at the language tool demo and see what it can do, it is very powerful https://languagetool.org/

@aral
Copy link
Contributor

aral commented Apr 20, 2022

@David-Else Just installed it; it definitely is very powerful. (And thank you for suggesting it and providing the configuration info.) :)

Running htop, while typing several of its processes did hit 100% CPU though and typing does seem laggy with it active.

Beyond that, it doesn’t seem to understand front-matter…

Screenshot from 2022-04-20 11-00-30@2x

… and HTML in Markdown.

Screenshot from 2022-04-20 11-01-18@2x

That said, it found that I had the wrong day for the date in my text. That’s quite cool.

Screenshot from 2022-04-20 11-02-20@2x

I still do believe that the default language server should be one that is less resource/dependency heavy but this is likely an issue that needs more of a general conversation.

Helix core might be light and performant but add a few heavy LSPs and it starts to slow down and require more resources. It will likely be a good idea to adopt a policy of trying to support lightweight LSPs whenever possible by default.

@aral
Copy link
Contributor

aral commented Apr 20, 2022

For anyone else who wants to install ltex-ls (and potentially for the LSP documentation page on the Helix Editor site):

  1. Make sure you have the Java Runtime Environment installed (e.g., on Ubuntu-based machines: sudo apt install default-jre)

  2. Download the latest release of ltex-ls: https://github.com/valentjn/ltex-ls/releases

  3. Extract it somewhere (e.g., ~/ltex-ls) then create a symbolic link to bin/ltex-ls from somewhere on your path (e.g., _/usr/local/bin/): sudo ln -s ~/ltex-ls/bin/ltex-ls /usr/local/bin/ltex-ls

  4. Copy @David-Else’s configuration from Add LSP for Markdown #2182 (comment) into your Helix Editor’s languages.toml file.

  5. Restart Helix Editor.

@aral
Copy link
Contributor

aral commented Apr 20, 2022

@aral If you have the Java runtime installed then you can use the version that does not include it, https://github.com/valentjn/ltex-ls/releases/download/15.2.0/ltex-ls-15.2.0.tar.gz

That’s the one I downloaded, yes. It’s ~200MB :)

@David-Else
Copy link
Contributor

David-Else commented Apr 20, 2022

@aral Thanks for checking it out!

Beyond that, it doesn’t seem to understand front-matter…

Currently it only understands some front matter, for example you can set the language in the front matter and it changes immediately. This is great for switching between US and UK English.

---
lang: en-GB
...

There are a lot of settings, in Helix we are of course currently using all the defaults. https://valentjn.github.io/ltex/settings.html

I found it only slows things down once the initial analysis is done, then it allows the editor to go back to full speed.

@the-mikedavis the-mikedavis added the S-needs-discussion Status: Needs discussion or design. label May 18, 2022
@lazytanuki
Copy link
Contributor

Currently it only understands some front matter, for example you can set the language in the front matter and it changes immediately. This is great for switching between US and UK English.

---
lang: en-GB
...

There are a lot of settings, in Helix we are of course currently using all the defaults. https://valentjn.github.io/ltex/settings.html

Just putting the information here since I was unable to find it anywhere else : in order for this to work, ltex-ls needs to understand that the document's language ID is "markdown". However, helix sends it the "md" language id, and you can see in the logs that it will only interpret the document as plaintext (WARNING: Unsupported code language ID 'md', treating text as plaintext)

Fortunately, this is configurable :

language-server = { command = "ltex-ls", language-id = "markdown" }

Hope this helps.

@slinlee
Copy link
Contributor Author

slinlee commented Jun 2, 2022

@David-Else @the-mikedavis @aral @lazytanuki - I agree that the Markdown server I hooked up here mostly focuses on things that a formatter like Prettier should cover.

Shall we close this in favor of something like ltex-ls?

@the-mikedavis
Copy link
Member

Yeah I'd say let's shoot for ltex-ls, it looks pretty sweet

@slinlee
Copy link
Contributor Author

slinlee commented Jun 2, 2022

Closing in favor of adding ltex-ls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-needs-discussion Status: Needs discussion or design.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants