helix icon indicating copy to clipboard operation
helix copied to clipboard

Add virtual text support

Open sudormrfbin opened this issue 4 years ago • 20 comments

Closes #411, called text annotations in code.

TODO

  • [x] End of line annotations
  • [x] Overwrite with virtual text

Future Works

  • Inline annotations
POC using diagnostics (click to expand patch):
diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs
index b04eef0..9018af7 100644
--- a/helix-term/src/application.rs
+++ b/helix-term/src/application.rs
@@ -422,8 +422,37 @@ impl Application {
                                         // source
                                     })
                                 })
-                                .collect();
-
+                                .collect::<Vec<_>>();
+
+                            doc.remove_text_annotations(|vt| vt.scope == "diagnostics");
+
+                            use helix_core::diagnostic::Severity;
+                            use helix_view::decorations::{TextAnnotation, TextAnnotationKind};
+                            use helix_view::graphics::{Color, Style};
+                            let style = Style::default().bg(Color::Gray);
+                            doc.extend_text_annotations(
+                                diagnostics
+                                    .iter()
+                                    .map(|d| TextAnnotation {
+                                        scope: "diagnostics".into(),
+                                        text: d.message.clone(),
+                                        style: d
+                                            .severity
+                                            .as_ref()
+                                            .map(|s| match s {
+                                                Severity::Error => style.clone().fg(Color::Red),
+                                                Severity::Warning => {
+                                                    style.clone().fg(Color::Yellow)
+                                                }
+                                                Severity::Info => style.clone().fg(Color::Blue),
+                                                Severity::Hint => style.clone().fg(Color::Green),
+                                            })
+                                            .unwrap_or(style.clone().fg(Color::Yellow)),
+                                        line: d.line,
+                                        kind: TextAnnotationKind::Eol,
+                                    })
+                                    .collect(),
+                            );
                             doc.set_diagnostics(diagnostics);
                         }
                     }

Screenshot_2021-07-05_20-56-30

sudormrfbin avatar Jul 05 '21 15:07 sudormrfbin

I think for an initial implementation, just end-of-line annotations are fine. As I mentioned in the linked issue, any rendering work you do here is likely to be redundant with planned future work. So keeping it to the simple case probably makes the most sense.

I'm also a little concerned that doing inline rendering right now will be a bit delicate, as the current rendering architecture isn't really set up to support it, nor is the cursor navigation system, etc. Inline annotations will need to touch a lot of systems to work properly.

cessen avatar Jul 05 '21 16:07 cessen

In that case I'll drop the inline rendering plan and keep it as is. Cursor and selection rendering for inline annotations will definitely look hacky with the current architecture.

sudormrfbin avatar Jul 05 '21 16:07 sudormrfbin

I'm also a little concerned that doing inline rendering right now will be a bit delicate, as the current rendering architecture isn't really set up to support it, nor is the cursor navigation system, etc. Inline annotations will need to touch a lot of systems to work properly.

Yeah, kak-lsp did the same and it is quite broken in kakoune too.

pickfire avatar Jul 06 '21 01:07 pickfire

As discussed on Matrix, this PR has been revived (yay !), and I have rebased the original changes on top of current master.

sudormrfbin avatar Nov 11 '21 18:11 sudormrfbin

Missing update in https://github.com/helix-editor/helix/blob/master/helix-core/src/position.rs I think, the visual_coords_at_pos should be updated.

pickfire avatar Nov 12 '21 23:11 pickfire

This is awesome, thanks for adding support! Is anyone using this and have made a snippet for enabling rust-analyzer type hints?

korken89 avatar Mar 27 '22 06:03 korken89

This PR as of now doesn't implement inlayed virtual text, i.e. virtual text in between normal text, which is what we would need to properly add support for rust-analyzer's type hints. Also the type hint API isn't part of the LSP spec yet, so we would have to probably wait on that too.

sudormrfbin avatar Mar 27 '22 09:03 sudormrfbin

Ah, I see - that's unfortunate. The type hint API seems to work in vim and vs-code, what is it that they do differently? Do you know?

korken89 avatar Mar 28 '22 11:03 korken89

They usually have dedicated plugins that do the setup for you, for example I use https://github.com/nvim-lua/lsp_extensions.nvim for Neovim which sets up inlay hints for rust. VSCode's rust extension also hooks into the decoration API provided by VSC and renders them.

sudormrfbin avatar Mar 28 '22 11:03 sudormrfbin

Also the type hint API isn't part of the LSP spec yet, so we would have to probably wait on that too.

The new spec seems final, rust-analyzer recently switched over: https://rust-analyzer.github.io/thisweek/2022/03/14/changelog-120.html#new-features

archseer avatar Mar 30 '22 01:03 archseer

This seems to now be part of the LSP spec: https://github.com/microsoft/language-server-protocol/issues/956

korken89 avatar Apr 19 '22 14:04 korken89

This PR as of now doesn't implement inlayed virtual text, i.e. virtual text in between normal text

@sudormrfbin I quite like the inlay hints from https://github.com/simrat39/rust-tools.nvim#inlay-hints

kellpossible avatar Jun 14 '22 10:06 kellpossible

Is it possible to make the hints show up below/above the line? If the hint is long it gets cut off when it is displayed beside the line.

2brownc avatar Jul 28 '22 07:07 2brownc

+1 Would love for this PR to land, even if behind an experimental config flag :) (it hasn't already landed experimentally right...?)

EriKWDev avatar Jul 28 '22 21:07 EriKWDev

Does this branch have working type hints?

dekuraan avatar Jul 29 '22 13:07 dekuraan

LSP 3.17 has support for inlay hints https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_inlayHint

[email protected](0.93.1) has support InlayHint

language support:

erasin avatar Sep 21 '22 10:09 erasin

LSP 3.17 has support for inlay hints https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_inlayHint

That should be done in another PR I think

poliorcetics avatar Sep 21 '22 15:09 poliorcetics

This could also be extended to debugging, so it displays the values of variable and objects, which would be of tremendous help.

filipdutescu avatar Oct 13 '22 11:10 filipdutescu

Update on this? Code folding and inlays are quite useful features currently blocked on this PR.

abseee avatar Oct 17 '22 13:10 abseee

An update for everyone interested in this: After discussion in the matrix chat, I will be taking over work on virtual text. However after discussion with @kirawi I plan to use a different approach than this PR that will build on his work in #3268 and #2184 so that the efforts on soft-wrapping and virtual text do not end up conflicting. This will involve a generic update to the rendering system that @kirawi and I are planning to develop in cooperation.

pascalkuthe avatar Nov 04 '22 17:11 pascalkuthe