Skip to content

Commit

Permalink
Added many more TreeSitter parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
elijah-potter committed Jan 25, 2024
1 parent 17353b2 commit cd7f116
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 9 deletions.
110 changes: 110 additions & 0 deletions Cargo.lock

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

10 changes: 10 additions & 0 deletions harper-ls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ once_cell = "1.19.0"
tree-sitter = "0.20.10"
tree-sitter-rust = "0.20.4"
tree-sitter-typescript = "0.20.3"
tree-sitter-python = "0.20.4"
tree-sitter-javascript = "0.20.1"
tree-sitter-go = "0.20.0"
tree-sitter-c = "0.20.7"
tree-sitter-cpp = "0.20.3"
tree-sitter-ruby = "0.20.0"
tree-sitter-swift = "0.4.0"
tree-sitter-c-sharp = "0.20.0"
tree-sitter-toml = "0.20.0"
tree-sitter-lua = "0.0.19"
8 changes: 4 additions & 4 deletions harper-ls/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use tower_lsp::{
use crate::{
diagnostics::{lint_to_code_actions, lints_to_diagnostics},
pos_conv::range_to_span,
rust_parser::RustParser,
tree_sitter_parser::TreeSitterParser,
};

pub struct Backend {
Expand All @@ -40,9 +40,9 @@ impl Backend {
let mut parser: Box<dyn Parser> = Box::new(MarkdownParser);

if let Some(extension) = url.to_file_path().unwrap().extension() {
if extension == "rs" {
parser = Box::new(RustParser)
}
parser = TreeSitterParser::new_from_extension(&extension.to_string_lossy())
.map::<Box<dyn Parser>, _>(|v| Box::new(v))
.unwrap_or(Box::new(MarkdownParser));
}

let doc = Document::new(text, parser);
Expand Down
2 changes: 1 addition & 1 deletion harper-ls/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use tokio::net::TcpListener;
mod backend;
mod diagnostics;
mod pos_conv;
mod rust_parser;
mod tree_sitter_parser;

use backend::Backend;
use clap::Parser;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,46 @@ use harper_core::{
parsers::{Parser, PlainEnglishParser},
Span,
};
use tree_sitter::TreeCursor;
use tree_sitter::{Language, TreeCursor};

pub struct RustParser;
pub struct TreeSitterParser {
language: Language,
}

impl TreeSitterParser {
pub fn new(language: Language) -> Self {
Self { language }
}

pub fn new_from_extension(file_extension: &str) -> Option<Self> {
let language = match file_extension {
"rs" => tree_sitter_rust::language(),
"tsx" => tree_sitter_typescript::language_tsx(),
"ts" => tree_sitter_typescript::language_typescript(),
"py" => tree_sitter_python::language(),
"js" => tree_sitter_javascript::language(),
"go" => tree_sitter_go::language(),
"c" => tree_sitter_c::language(),
"cpp" => tree_sitter_cpp::language(),
"rb" => tree_sitter_ruby::language(),
"swift" => tree_sitter_ruby::language(),
"cs" => tree_sitter_c_sharp::language(),
"toml" => tree_sitter_toml::language(),
"lua" => tree_sitter_lua::language(),
_ => return None,
};

Some(Self { language })
}
}

impl Parser for RustParser {
impl Parser for TreeSitterParser {
fn parse(&mut self, source: &[char]) -> Vec<harper_core::Token> {
let text: String = source.iter().collect();

let mut english_parser = PlainEnglishParser;
let mut parser = tree_sitter::Parser::new();
parser.set_language(tree_sitter_rust::language()).unwrap();
parser.set_language(self.language).unwrap();

// TODO: Use incremental parsing
let Some(root) = parser.parse(&text, None) else {
Expand Down

0 comments on commit cd7f116

Please sign in to comment.