Skip to content

Commit

Permalink
feat(ui): lsp context
Browse files Browse the repository at this point in the history
  • Loading branch information
matoous committed Sep 22, 2022
1 parent 43b31f7 commit 88a32c5
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 5 deletions.
11 changes: 6 additions & 5 deletions book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,12 @@ The following elements can be configured:

### `[editor.lsp]` Section

| Key | Description | Default |
| --- | ----------- | ------- |
| `display-messages` | Display LSP progress messages below statusline[^1] | `false` |
| `auto-signature-help` | Enable automatic popup of signature help (parameter hints) | `true` |
| `display-signature-help-docs` | Display docs under signature help popup | `true` |
| Key | Description | Default |
| --- | ----------- | ------- |
| `display-messages` | Display LSP progress messages below statusline[^1] | `false` |
| `auto-signature-help` | Enable automatic popup of signature help (parameter hints) | `true` |
| `display-signature-help-docs` | Display docs under signature help popup | `true` |
| `context` | Display context of current line if outside the view | `false` |

[^1]: By default, a progress spinner is shown in the statusline beside the file path.

Expand Down
80 changes: 80 additions & 0 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ impl EditorView {
highlights,
&editor.config(),
);

if self.config.load().editor.lsp.context {
Self::render_context(editor, doc, view, surface, theme);
}

Self::render_gutter(editor, doc, view, view.area, surface, theme, is_focused);
Self::render_rulers(editor, doc, view, inner, surface, theme);

Expand Down Expand Up @@ -380,6 +385,81 @@ impl EditorView {
spans
}

pub fn render_context(
editor: &Editor,
doc: &Document,
view: &View,
surface: &mut Surface,
theme: &Theme,
) {
// TODO: don't compute context on every render?
// TODO: correct line numbers
if let Some(syntax) = doc.syntax() {
let text = doc.text().slice(..);
let tree = syntax.tree();
let byte = doc.selection(view.id).primary().cursor(text);

let mut parent = tree
.root_node()
.descendant_for_byte_range(byte, byte)
.and_then(|n| n.parent());
// context is list of numbers of lines that should be rendered in the LSP context
let mut context: Vec<usize> = Vec::new();
while let Some(curr) = parent {
let line = text.byte_to_line(curr.start_byte());

// if parent of previous node is still on the same line, use the parent node
if let Some(&prev_line) = context.last() {
if prev_line == line {
context.pop();
}
}

context.push(line);
parent = curr.parent();
}

// we render from top most (last in the list)
context.reverse();

// TODO: this probably needs it's own style, although it seems to work well even with cursorline
let context_style = theme.get("ui.cursorline.primary");
let mut context_area = view.inner_area();
context_area.height = 1;

let mut last_line = 0;
for line_num in context {
surface.clear_with(context_area, context_style);

let offset = Position::new(line_num, 0);
let highlights = Self::doc_syntax_highlights(doc, offset, 1, theme);
Self::render_text_highlights(
doc,
offset,
context_area,
surface,
theme,
highlights,
&editor.config(),
);

context_area.y += 1;
last_line = line_num;
}

surface.clear_with(context_area, context_style);
let text_style = theme.get("ui.text");
let last_line = text.get_line(last_line).unwrap();
let padding = last_line.chars().take_while(|&c| c == ' ').count();
surface.set_string(
context_area.x,
context_area.y,
format!("{}--- context", " ".repeat(padding)),
text_style,
);
}
}

pub fn render_text_highlights<H: Iterator<Item = HighlightEvent>>(
doc: &Document,
offset: Position,
Expand Down
3 changes: 3 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ pub struct LspConfig {
pub auto_signature_help: bool,
/// Display docs under signature help popup
pub display_signature_help_docs: bool,
/// Display context of current cursor line if it is outside the view.
pub context: bool,
}

impl Default for LspConfig {
Expand All @@ -240,6 +242,7 @@ impl Default for LspConfig {
display_messages: false,
auto_signature_help: true,
display_signature_help_docs: true,
context: false,
}
}
}
Expand Down

0 comments on commit 88a32c5

Please sign in to comment.