Skip to content

Commit

Permalink
feat: treesitter support for latex (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Oct 29, 2022
1 parent 45de4de commit 1c666a6
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lua/aerial/backends/treesitter/extensions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,18 @@ M.typescript = {
end,
}

M.latex = {
postprocess = function(bufnr, item, match)
local type_node = (utils.get_at_path(match, "type") or {}).node
local base_type = type_node:type()
if base_type == "title_declaration" then
item.name = "Title: " .. item.name
elseif base_type == "author_declaration" then
item.name = "Authors: " .. item.name
end
end,
}

-- tsx needs the same transformations as typescript for now.
-- This may not always be the case.
M.tsx = M.typescript
Expand Down
9 changes: 9 additions & 0 deletions lua/aerial/backends/treesitter/language_kind_map.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ return {
module_definition = "Module",
struct_definition = "Class",
},
latex = {
generic_environment = "Class",
new_command_definition = "Function",
section = "Method",
subsection = "Method",
subsubsection = "Method",
title_declaration = "Field",
author_declaration = "Field",
},
lua = {
function_declaration = "Function",
function_definition = "Function",
Expand Down
24 changes: 24 additions & 0 deletions queries/latex/aerial.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(section
text: [(curly_group (text) @name) (_)] @name
) @type
(subsection
text: [(curly_group (text) @name) (_)] @name
) @type
(subsubsection
text: [(curly_group (text) @name) (_)] @name
) @type
(generic_environment
begin: (begin
name: [(curly_group_text text: (text) @name) (_)] @name
)) @type

(new_command_definition

This comment has been minimized.

Copy link
@mathjiajia

mathjiajia Oct 30, 2022

I don't think you should include this as one of the items:
for me, there are tons of \newcommand and DeclareMathOperator, etc.

This comment has been minimized.

Copy link
@stevearc

stevearc Oct 30, 2022

Author Owner

What do you think about changing its symbol kind to Operator, so it doesn't show up if you're using the default filter_kind?

Reminder that filter_kind can be a filetype map (:help aerial-filetype-map), so you can specify exactly which symbols you want to be visible for any given filetype.

This comment has been minimized.

Copy link
@mathjiajia

mathjiajia Oct 31, 2022

Thank you.
IMO, these symbols should be hidden by default (like the LSP backend).
Your suggestion sounds good: link to operator which is filtered out by default.

This comment has been minimized.

Copy link
@stevearc

stevearc Oct 31, 2022

Author Owner

Done!

declaration: [(curly_group_command_name command: (command_name) @name) (_)] @name
) @type

(title_declaration
text: [(curly_group (text) @name) (_)] @name
) @type
(author_declaration
authors: [(curly_group_author_list (author) @name) (_)] @name
) @type
86 changes: 86 additions & 0 deletions tests/treesitter/latex_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
local util = require("tests.test_util")

describe("treesitter latex", function()
it("parses all symbols correctly", function()
util.test_file_symbols("treesitter", "./tests/treesitter/latex_test.tex", {
{
kind = "Function",
name = "\\abs",
level = 0,
lnum = 3,
col = 0,
end_lnum = 3,
end_col = 38,
},
{
kind = "Field",
name = "Title: Lorem Ipsum",
level = 0,
lnum = 5,
col = 0,
end_lnum = 5,
end_col = 19,
},
{
kind = "Field",
name = "Authors: John Doe",
level = 0,
lnum = 6,
col = 0,
end_lnum = 6,
end_col = 17,
},
{
kind = "Class",
name = "document",
level = 0,
lnum = 8,
col = 0,
end_lnum = 24,
end_col = 14,
children = {
{
kind = "Method",
name = "First section",
level = 1,
lnum = 10,
col = 0,
end_lnum = 20,
end_col = 24,
children = {
{
kind = "Method",
name = "A subsection",
level = 2,
lnum = 14,
col = 0,
end_lnum = 20,
end_col = 24,
children = {
{
kind = "Method",
name = "A subsubsection",
level = 3,
lnum = 18,
col = 0,
end_lnum = 20,
end_col = 24,
},
},
},
},
},
{
kind = "Method",
name = "This is another subsection",
level = 1,
lnum = 22,
col = 0,
end_lnum = 22,
end_col = 36,
},
},
},
})
end)
end)
24 changes: 24 additions & 0 deletions tests/treesitter/latex_test.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
& tex

\newcommand*{\abs}[1]{\left|#1\right|}

\title{Lorem Ipsum}
\author{John Doe}

\begin{document}

\section{First section}

This is a section.

\subsection{A subsection}

This is a subsection.

\subsubsection{A subsubsection}

This is a subsubsection.

\section{This is another subsection}

\end{document}

0 comments on commit 1c666a6

Please sign in to comment.