Skip to content

Commit

Permalink
Add tree-sitter-go
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchengxu committed Nov 15, 2023
1 parent c3dfb81 commit c180005
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 92 deletions.
36 changes: 12 additions & 24 deletions Cargo.lock

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

20 changes: 3 additions & 17 deletions crates/maple_core/src/stdio_server/plugin/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ops::Range;

use crate::stdio_server::input::{AutocmdEvent, AutocmdEventType};
use crate::stdio_server::plugin::{ActionRequest, ClapPlugin, PluginError, Toggle};
use crate::stdio_server::vim::{Vim, VimResult};
use crate::stdio_server::vim::Vim;
use itertools::Itertools;
use once_cell::sync::Lazy;
use sublime_syntax::{SyntaxReference, TokenHighlight};
Expand Down Expand Up @@ -164,14 +164,7 @@ impl Syntax {

// TODO: efficient SyntaxHighlighter
let mut tree_sitter_highlighter = tree_sitter::SyntaxHighlighter::new();
let buffer_highlights = tree_sitter_highlighter.highlight(
language,
&source_code,
&Language::HIGHLIGHT_NAMES
.iter()
.map(|(h, _)| *h)
.collect::<Vec<_>>(),
)?;
let buffer_highlights = tree_sitter_highlighter.highlight(language, &source_code)?;

let (_winid, line_start, line_end) = self.vim.get_screen_lines_range().await?;
self.apply_ts_highlights(
Expand Down Expand Up @@ -243,14 +236,7 @@ impl Syntax {
let source_code = std::fs::read(&source_file)?;

let mut tree_sitter_highlighter = tree_sitter::SyntaxHighlighter::new();
let new_highlights = tree_sitter_highlighter.highlight(
language,
&source_code,
&Language::HIGHLIGHT_NAMES
.iter()
.map(|(h, _)| *h)
.collect::<Vec<_>>(),
)?;
let new_highlights = tree_sitter_highlighter.highlight(language, &source_code)?;

let (_winid, line_start, line_end) = self.vim.get_screen_lines_range().await?;

Expand Down
11 changes: 6 additions & 5 deletions crates/tree_sitter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ name = "tree_sitter"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
cc = "*"

[dependencies]
tree-sitter-core = { package = "tree-sitter", version = "0.20" }
tree-sitter-highlight = "0.20"
# tree-sitter-tags = "0.20"
# tree-sitter-traversal = "0.1"

# Languages
tree-sitter-go = "0.20"
tree-sitter-rust = "0.20"
tree-sitter-tags = "0.20"
tree-sitter-toml = "0.20"
tree-sitter-vim = { git = "https://github.com/liuchengxu/tree-sitter-vim" }
tree-sitter-traversal = "0.1"
tree-sitter-vim = { git = "https://github.com/liuchengxu/tree-sitter-vim", rev = "a001587c17ab24fdade5403eb6c4763460d6814c" }

[dev-dependencies]
criterion = "0.3"
Expand Down
102 changes: 61 additions & 41 deletions crates/tree_sitter/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,27 @@ use tree_sitter_highlight::{Highlight, HighlightConfiguration};

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum Language {
Go,
Rust,
Toml,
Viml,
}

/// Small macro to declare the list of highlight name in tree_sitter_highlight
/// and associated vim highlight group name.
macro_rules! highlight_names {
($( ($name:expr, $group:expr) ),* $(,)?) => {
const HIGHLIGHT_NAMES: &'static [&'static str] = &[
$( $name ),*
];
const HIGHLIGHT_GROUPS: &'static [(&'static str, &'static str)] = &[
$( ($name, $group) ),*
];
};
}

impl Language {
// TODO: configurable per language
pub const HIGHLIGHT_NAMES: [(&'static str, &'static str); 23] = [
highlight_names![
("comment", "Comment"),
("constant", "Constant"),
("constant.builtin", "Constant"),
Expand Down Expand Up @@ -39,6 +52,7 @@ impl Language {
/// Constructs a new instance of [`Language`] from the file extension if any.
pub fn try_from_extension(extension: &str) -> Option<Self> {
let language = match extension {
"go" => Self::Go,
"rs" => Self::Rust,
"toml" => Self::Toml,
"vim" => Self::Viml,
Expand All @@ -51,6 +65,7 @@ impl Language {
/// Constructs a new instance of [`Language`] from the filetype if any.
pub fn try_from_filetype(filetype: &str) -> Option<Self> {
let language = match filetype {
"go" => Self::Go,
"rust" => Self::Rust,
"toml" => Self::Toml,
"vim" => Self::Viml,
Expand All @@ -60,60 +75,65 @@ impl Language {
Some(language)
}

pub fn highlight_names(&self) -> &[&str] {
Self::HIGHLIGHT_NAMES
}

pub fn highlight_name(&self, highlight: Highlight) -> &'static str {
Self::HIGHLIGHT_NAMES[highlight.0].0
Self::HIGHLIGHT_NAMES[highlight.0]
}

pub fn highlight_group(&self, highlight: Highlight) -> &'static str {
Self::HIGHLIGHT_NAMES[highlight.0].1
Self::HIGHLIGHT_GROUPS[highlight.0].1
}

fn create_new_highlight_config(&self) -> HighlightConfiguration {
let create_config_result = match self {
Language::Go => HighlightConfiguration::new(
tree_sitter_go::language(),
tree_sitter_go::HIGHLIGHT_QUERY,
"",
"",
),

Language::Rust => HighlightConfiguration::new(
tree_sitter_rust::language(),
tree_sitter_rust::HIGHLIGHT_QUERY,
"",
"",
),
Language::Toml => HighlightConfiguration::new(
tree_sitter_toml::language(),
tree_sitter_toml::HIGHLIGHT_QUERY,
"",
"",
),
Language::Viml => HighlightConfiguration::new(
tree_sitter_vim::language(),
tree_sitter_vim::HIGHLIGHT_QUERY,
"",
"",
),
};

let mut config = create_config_result.expect("Query creation must be succeed");

config.configure(self.highlight_names());

config
}
}

thread_local! {
static HIGHLIGHT_CONFIGS: RefCell<HashMap<Language, Arc<HighlightConfiguration>>> = Default::default();
}

pub fn get_highlight_config(
language: Language,
highlight_names: &[&str],
) -> Arc<HighlightConfiguration> {
pub fn get_highlight_config(language: Language) -> Arc<HighlightConfiguration> {
HIGHLIGHT_CONFIGS.with(|configs| {
let mut configs = configs.borrow_mut();
let config = configs
.entry(language)
.or_insert_with(|| Arc::new(create_new_highlight_config(language, highlight_names)));
.or_insert_with(|| Arc::new(language.create_new_highlight_config()));
config.clone()
})
}

fn create_new_highlight_config(
language: Language,
highlight_names: &[&str],
) -> HighlightConfiguration {
let create_config_result = match language {
Language::Rust => HighlightConfiguration::new(
tree_sitter_rust::language(),
tree_sitter_rust::HIGHLIGHT_QUERY,
"",
"",
),
Language::Toml => HighlightConfiguration::new(
tree_sitter_toml::language(),
tree_sitter_toml::HIGHLIGHT_QUERY,
"",
"",
),
Language::Viml => HighlightConfiguration::new(
tree_sitter_vim::language(),
tree_sitter_vim::HIGHLIGHT_QUERY,
"",
"",
),
};

let mut config = create_config_result.expect("Query creation must be succeed");

config.configure(highlight_names);

config
}
9 changes: 4 additions & 5 deletions crates/tree_sitter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ mod language;
mod utf8_char_indices;

use std::collections::{BTreeMap, HashSet};
use tree_sitter_core::{Node, Parser, Point, TreeCursor};
use tree_sitter_core::{Node, Point, TreeCursor};
use tree_sitter_highlight::{Highlight, HighlightConfiguration, HighlightEvent, Highlighter};
use tree_sitter_tags::{TagsConfiguration, TagsContext};
use tree_sitter_traversal::{traverse, traverse_tree, Order};

pub use self::language::Language;
pub use self::utf8_char_indices::{UncheckedUtf8CharIndices, Utf8CharIndices};
Expand Down Expand Up @@ -71,9 +69,8 @@ impl SyntaxHighlighter {
&mut self,
language: Language,
source: &[u8],
highlight_names: &[&str],
) -> Result<BTreeMap<usize, Vec<HighlightItem>>, tree_sitter_highlight::Error> {
let config = language::get_highlight_config(language, highlight_names);
let config = language::get_highlight_config(language);

highlight_inner(&mut self.highlighter, &config, source)
}
Expand Down Expand Up @@ -202,6 +199,7 @@ fn pretty_print_tree_impl<W: std::fmt::Write>(
mod tests {
use super::*;

/*
fn tags() {
let source_file = std::path::Path::new(
"/home/xlc/.vim/plugged/vim-clap/crates/maple_core/src/stdio_server/plugin/system.rs",
Expand Down Expand Up @@ -230,6 +228,7 @@ mod tests {
);
}
}
*/

#[test]
fn it_works() {
Expand Down

0 comments on commit c180005

Please sign in to comment.