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

Injection Query Support #430

Merged
merged 9 commits into from
Jul 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 31 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion helix-core/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct Configuration {
#[serde(rename_all = "kebab-case")]
pub struct LanguageConfiguration {
#[serde(rename = "name")]
pub(crate) language_id: Lang,
pub language_id: Lang,
pub scope: String, // source.rust
pub file_types: Vec<String>, // filename ends_with? <Gemfile, rb, etc>
pub roots: Vec<String>, // these indicate project roots <.git, Cargo.toml>
Expand Down
1 change: 1 addition & 0 deletions helix-syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ include = ["src/**/*", "languages/**/*", "build.rs", "!**/docs/**/*", "!**/test/
[dependencies]
tree-sitter = "0.19"
serde = { version = "1.0", features = ["derive"] }
strum = { version = "0.21", features = ["derive"] }

[build-dependencies]
cc = { version = "1", features = ["parallel"] }
Expand Down
4 changes: 3 additions & 1 deletion helix-syntax/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize};
use strum::EnumString;
use tree_sitter::Language;

#[macro_export]
Expand All @@ -13,7 +14,8 @@ macro_rules! mk_extern {
#[macro_export]
macro_rules! mk_enum {
( $( $camel:ident ),* ) => {
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize, EnumString)]
#[strum(ascii_case_insensitive)]
#[serde(rename_all = "lowercase")]
pub enum Lang {
$(
Expand Down
30 changes: 26 additions & 4 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use helix_view::{
keyboard::{KeyCode, KeyModifiers},
Document, Editor, Theme, View,
};
use std::borrow::Cow;
use std::{borrow::Cow, str::FromStr, sync::Arc};

use crossterm::event::Event;
use tui::buffer::Buffer as Surface;
Expand Down Expand Up @@ -64,6 +64,7 @@ impl EditorView {
surface: &mut Surface,
theme: &Theme,
is_focused: bool,
loader: Arc<syntax::Loader>,
) {
let area = Rect::new(
view.area.x + OFFSET,
Expand All @@ -72,7 +73,7 @@ impl EditorView {
view.area.height.saturating_sub(1),
); // - 1 for statusline

self.render_buffer(doc, view, area, surface, theme, is_focused);
self.render_buffer(doc, view, area, surface, theme, is_focused, loader);

// if we're not at the edge of the screen, draw a right border
if viewport.right() != view.area.right() {
Expand Down Expand Up @@ -106,6 +107,7 @@ impl EditorView {
surface: &mut Surface,
theme: &Theme,
is_focused: bool,
loader: Arc<syntax::Loader>,
) {
let text = doc.text().slice(..);

Expand All @@ -122,8 +124,19 @@ impl EditorView {
// TODO: range doesn't actually restrict source, just highlight range
let highlights: Vec<_> = match doc.syntax() {
Some(syntax) => {
let scopes = theme.scopes();
let configs: Vec<_> = loader
.language_configs_iter()
.map(|x| (x, x.highlight_config(scopes).unwrap()))
.collect();
syntax
.highlight_iter(text.slice(..), Some(range), None, |_| None)
.highlight_iter(text.slice(..), Some(range), None, |language| {
let language = syntax::Lang::from_str(language).unwrap();
let (.., highlight_config) = configs
.iter()
.find(|(config, ..)| config.language_id == language)?;
Some(highlight_config)
})
.collect() // TODO: we collect here to avoid holding the lock, fix later
}
None => vec![Ok(HighlightEvent::Source {
Expand Down Expand Up @@ -719,7 +732,16 @@ impl Component for EditorView {

for (view, is_focused) in cx.editor.tree.views() {
let doc = cx.editor.document(view.doc).unwrap();
self.render_view(doc, view, area, surface, &cx.editor.theme, is_focused);
let loader = cx.editor.syn_loader.clone();
self.render_view(
doc,
view,
area,
surface,
&cx.editor.theme,
is_focused,
loader,
);
}

if let Some(info) = std::mem::take(&mut cx.editor.autoinfo) {
Expand Down