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

Feat: Sticky Context #6118

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions book/src/editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- [`[editor.soft-wrap]` Section](#editorsoft-wrap-section)
- [`[editor.smart-tab]` Section](#editorsmart-tab-section)
- [`[editor.inline-diagnostics]` Section](#editorinline-diagnostics-section)
- [`[editor.sticky-context]` Section](#editorsticky-context-section)

### `[editor]` Section

Expand Down Expand Up @@ -433,3 +434,23 @@ end-of-line-diagnostics = "hint"
[editor.inline-diagnostics]
cursor-line = "warning" # show warnings and errors on the cursorline inline
```

### `[editor.sticky-context]` Section

Option for sticky context, which is a feature that puts bigger blocks of code
e.g. Functions to the top of the viewport

| Key | Description | Default |
| --- | --- | --- |
| `enable` | Display context of current line if outside the view | `false` |
| `indicator` | Display an additional line to indicate what part of the view is the sticky context | `false` |
| `max-lines` | The maximum number of lines to be shown as sticky context. 0 = 1/3 of the viewports height | `0` |

Example:

```toml
[editor.sticky-context]
enable = true
indicator = true
max-lines = 10
```
26 changes: 26 additions & 0 deletions book/src/guides/context.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Adding context queries

Helix uses tree-sitter to filter out specific scopes in which said scope may exceed the current
editor view, but which may be important for the developer to know.
These context require an accompanying tree-sitter grammar and a `context.scm` query file
to work properly.
Query files should be placed in `runtime/queries/{language}/context.scm`
when contributing to Helix. Note that to test the query files locally you should put
them under your local runtime directory (`~/.config/helix/runtime` on Linux for example).

The following [captures][tree-sitter-captures] are recognized:

| Capture Name |
| --- |
| `context` |
| `context.params` |

[Example query files][context-examples] can be found in the helix GitHub repository.

## Queries for the sticky-context feature

All nodes that have a scope, should be captured with `context`, as an example a basic class.
The `context.params` is a capture for all the function parameters.

[tree-sitter-captures]: https://tree-sitter.github.io/tree-sitter/using-parsers#capturing-nodes
[context-examples]: https://github.com/search?q=repo%3Ahelix-editor%2Fhelix+filename%3Acontext.scm&type=Code&ref=advsearch&l=&l=
18 changes: 18 additions & 0 deletions helix-core/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ pub struct LanguageConfiguration {
#[serde(skip_serializing_if = "Option::is_none")]
pub debugger: Option<DebugAdapterConfig>,

/// The Grammar query for Sticky Context
#[serde(skip)]
pub(crate) context_query: OnceCell<Option<ContextQuery>>,

/// Automatic insertion of pairs to parentheses, brackets,
/// etc. Defaults to true. Optionally, this can be a list of 2-tuples
/// to specify a list of characters to pair. This overrides the
Expand Down Expand Up @@ -609,6 +613,11 @@ pub struct TextObjectQuery {
pub query: Query,
}

#[derive(Debug)]
pub struct ContextQuery {
pub query: Query,
}

#[derive(Debug)]
pub enum CapturedNode<'a> {
Single(Node<'a>),
Expand Down Expand Up @@ -804,6 +813,15 @@ impl LanguageConfiguration {
.as_ref()
}

pub fn context_query(&self) -> Option<&ContextQuery> {
self.context_query
.get_or_init(|| {
self.load_query("context.scm")
.map(|query| ContextQuery { query })
})
.as_ref()
}

pub fn scope(&self) -> &str {
&self.scope
}
Expand Down
11 changes: 10 additions & 1 deletion helix-term/src/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,25 @@ pub enum TsFeature {
Highlight,
TextObject,
AutoIndent,
Context,
}

impl TsFeature {
pub fn all() -> &'static [Self] {
&[Self::Highlight, Self::TextObject, Self::AutoIndent]
&[
Self::Highlight,
Self::TextObject,
Self::AutoIndent,
Self::Context,
]
}

pub fn runtime_filename(&self) -> &'static str {
match *self {
Self::Highlight => "highlights.scm",
Self::TextObject => "textobjects.scm",
Self::AutoIndent => "indents.scm",
Self::Context => "context.scm",
}
}

Expand All @@ -32,6 +39,7 @@ impl TsFeature {
Self::Highlight => "Syntax Highlighting",
Self::TextObject => "Treesitter Textobjects",
Self::AutoIndent => "Auto Indent",
Self::Context => "Sticky Context",
}
}

Expand All @@ -40,6 +48,7 @@ impl TsFeature {
Self::Highlight => "Highlight",
Self::TextObject => "Textobject",
Self::AutoIndent => "Indent",
Self::Context => "Context",
}
}
}
Expand Down
Loading
Loading