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

fix: multiline emmy #15

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "tree-sitter-lua"
description = "lua grammar for the tree-sitter parsing library"
version = "0.0.1"
keywords = ["incremental", "parsing", "lua"]
categories = ["parsing", "text-editors"]
repository = "https://github.com/tree-sitter/tree-sitter-javascript"
edition = "2018"

build = "bindings/rust/build.rs"
include = [
"bindings/rust/*",
"grammar.js",
"queries/*",
"src/*",
]

[lib]
path = "bindings/rust/lib.rs"

[dependencies]
tree-sitter = "0.17"

[build-dependencies]
cc = "1.0"
7 changes: 2 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
ts := $(shell which tree-sitter 2> /dev/null)
ifeq (, ${ts})
ts := $(shell which tree-sitter-cli 2> /dev/null)
endif
ts := ./node_modules/tree-sitter-cli/tree-sitter

generate:
${ts} generate
Expand All @@ -19,7 +16,7 @@ test_docgen: generate
--headless \
--noplugin \
-u tests/minimal_init.vim \
-c "PlenaryBustedDirectory lua/tests/ {minimal_init = 'tests/minimal_init.vim'}"
-c "PlenaryBustedDirectory lua/tests/docgen {minimal_init = 'tests/minimal_init.vim'}"

build_parser: generate
mkdir -p build
Expand Down
2 changes: 1 addition & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
],
"sources": [
"src/parser.c",
"src/binding.cc"
"bindings/node/binding.cc"
],
"cflags_c": [
"-std=c99",
Expand Down
File renamed without changes.
19 changes: 19 additions & 0 deletions bindings/node/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
try {
module.exports = require("../../build/Release/tree_sitter_lua_binding");
} catch (error1) {
if (error1.code !== 'MODULE_NOT_FOUND') {
throw error1;
}
try {
module.exports = require("../../build/Debug/tree_sitter_lua_binding");
} catch (error2) {
if (error2.code !== 'MODULE_NOT_FOUND') {
throw error2;
}
throw error1
}
}

try {
module.exports.nodeTypeInfo = require("../../src/node-types.json");
} catch (_) {}
40 changes: 40 additions & 0 deletions bindings/rust/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
fn main() {
let src_dir = std::path::Path::new("src");

let mut c_config = cc::Build::new();
c_config.include(&src_dir);
c_config
.flag_if_supported("-Wno-unused-parameter")
.flag_if_supported("-Wno-unused-but-set-variable")
.flag_if_supported("-Wno-trigraphs");
let parser_path = src_dir.join("parser.c");
c_config.file(&parser_path);

// If your language uses an external scanner written in C,
// then include this block of code:

/*
let scanner_path = src_dir.join("scanner.c");
c_config.file(&scanner_path);
println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap());
*/

println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap());
c_config.compile("parser");

// If your language uses an external scanner written in C++,
// then include this block of code:

/*
let mut cpp_config = cc::Build::new();
cpp_config.cpp(true);
cpp_config.include(&src_dir);
cpp_config
.flag_if_supported("-Wno-unused-parameter")
.flag_if_supported("-Wno-unused-but-set-variable");
let scanner_path = src_dir.join("scanner.cc");
cpp_config.file(&scanner_path);
println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap());
cpp_config.compile("scanner");
*/
}
52 changes: 52 additions & 0 deletions bindings/rust/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//! This crate provides lua language support for the [tree-sitter][] parsing library.
//!
//! Typically, you will use the [language][language func] function to add this language to a
//! tree-sitter [Parser][], and then use the parser to parse some code:
//!
//! ```
//! let code = "";
//! let mut parser = tree_sitter::Parser::new();
//! parser.set_language(tree_sitter_javascript::language()).expect("Error loading lua grammar");
//! let tree = parser.parse(code, None).unwrap();
//! ```
//!
//! [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
//! [language func]: fn.language.html
//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
//! [tree-sitter]: https://tree-sitter.github.io/

use tree_sitter::Language;

extern "C" {
fn tree_sitter_lua() -> Language;
}

/// Get the tree-sitter [Language][] for this grammar.
///
/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
pub fn language() -> Language {
unsafe { tree_sitter_lua() }
}

/// The content of the [`node-types.json`][] file for this grammar.
///
/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
pub const NODE_TYPES: &'static str = include_str!("../../src/node-types.json");

// Uncomment these to include any queries that this grammar contains

// pub const HIGHLIGHTS_QUERY: &'static str = include_str!("../../queries/highlights.scm");
// pub const INJECTIONS_QUERY: &'static str = include_str!("../../queries/injections.scm");
// pub const LOCALS_QUERY: &'static str = include_str!("../../queries/locals.scm");
// pub const TAGS_QUERY: &'static str = include_str!("../../queries/tags.scm");

#[cfg(test)]
mod tests {
#[test]
fn test_can_load_grammar() {
let mut parser = tree_sitter::Parser::new();
parser
.set_language(super::language())
.expect("Error loading lua language");
}
}
17 changes: 17 additions & 0 deletions corpus/comments.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,20 @@ more
---

(program (comment))

==================
Can do comments with strings inside
==================

-- local description = table.concat(
-- map(function(val)
-- if val == '' then return '\n' end
-- return val
-- end, function_metadata.description or {}),
-- ' '
-- )
-- print(vim.inspect(function_metadata.description))

---

(program (comment) (comment) (comment) (comment) (comment) (comment) (comment) (comment))
153 changes: 147 additions & 6 deletions corpus/documentation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ Real life example from neovim

--- Store Diagnostic[] by line
---@param diagnostics Diagnostic[]: hello
---@return table<number, Diagnostic[]>

---@return my_type
local _diagnostic_lines = function(diagnostics)
end

Expand Down Expand Up @@ -495,13 +496,78 @@ end
)

==================
Using classes as types
Only a class
==================

---@class Map @table lie

---

(program (documentation_class (emmy_class (emmy_type (identifier)) (class_description))))

==================
Class with variable
==================

local Job = {}

---@class Map @table lie

---

(program
(variable_declaration (local) (variable_declarator (identifier)) (tableconstructor))
(documentation_class (emmy_class (emmy_type (identifier)) (class_description))))

==================
Class as part of header
==================

local Job = {}

--- HEADER
---@class Job @desc
---@field cmd string: comamnd
---@param o table: options
function Job:new(o)
return setmetatable(o, self)
end

---

(program
(variable_declaration
(local) (variable_declarator (identifier)) (tableconstructor))
(function_statement
(emmy_documentation
(emmy_header)
(emmy_class (emmy_type (identifier)) (class_description))
(emmy_field (identifier) (emmy_type (identifier)) (field_description))
(emmy_parameter (identifier) (emmy_type (identifier)) (parameter_description)))
(function_start)
(function_name (identifier) (table_colon) (identifier))
(function_body_paren)
(parameter_list (identifier))
(function_body_paren)
(return_statement
(function_call
(identifier)
(function_call_paren)
(function_arguments
(identifier)
(identifier))
(function_call_paren)))
(function_end)))

==================
Using classes as types
==================


---@class Map @table lie

local Job = {}

--- HEADER
---@class Job @desc
---@field cmd string: comamnd
Expand All @@ -513,8 +579,8 @@ end
---

(program
(variable_declaration (local) (variable_declarator (identifier)) (tableconstructor))
(documentation_class (emmy_class (emmy_type (identifier)) (class_description)))
(variable_declaration (local) (variable_declarator (identifier)) (tableconstructor))
(function_statement
(emmy_documentation
(emmy_header)
Expand All @@ -538,8 +604,83 @@ Multiline params
==================

--- Get the diagnostics by line
---@param opts table|nil: Configuration keys
---@param opts table: Configuration keys
--- - severity: (DiagnosticSeverity, default nil)
function M.get_line_diagnostics(bufnr, line_nr, opts, client_id)
--- - another line. please work.
---@param last number: Last param
function M.f()
end
)

---

(program
(function_statement
(emmy_documentation
(emmy_header)
(emmy_parameter (identifier) (emmy_type (identifier)) (parameter_description))
(emmy_parameter (identifier) (emmy_type (identifier)) (parameter_description)))
(function_start)
(function_name (identifier) (table_dot) (identifier))
(function_body_paren)
(function_body_paren)
(function_end)))

==================
Single line params from Conni
==================

local x = {}

--- This function has documentation
---@param abc string: Docs for abc
---@param def string: Other docs for def
---@param bxy string: Final docs
function x.hello(abc, def, bxy)
return abc .. def .. bxy
end

return x

---

(program
(variable_declaration
(local)
(variable_declarator
(identifier))
(tableconstructor))
(function_statement
(emmy_documentation
(emmy_header)
(emmy_parameter
(identifier)
(emmy_type (identifier))
(parameter_description))
(emmy_parameter
(identifier)
(emmy_type (identifier))
(parameter_description))
(emmy_parameter
(identifier)
(emmy_type (identifier))
(parameter_description)))
(function_start)
(function_name
(identifier)
(table_dot)
(identifier))
(function_body_paren)
(parameter_list
(identifier)
(identifier)
(identifier))
(function_body_paren)
(return_statement
(binary_operation
(identifier)
(binary_operation
(identifier)
(identifier))))
(function_end))
(module_return_statement
(identifier)))
Loading