Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Add syntax tree #402

Open
wants to merge 1 commit 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
3 changes: 3 additions & 0 deletions doc/rust-tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ Note: Requires rust-analyzer version after 2021-08-02. Shows the type in visual

`lua -- Command: -- RustJoinLines require'rust-tools'.join_lines.join_lines()`Structural Search Replace ~


`lua -- Command: -- RustSyntaxTree require'rust-tools'.syntax_tree.syntax_tree()`Show Syntax Tree ~

>lua
-- Command:
-- RustSSR [query]
Expand Down
3 changes: 3 additions & 0 deletions lua/rust-tools/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ function M.setup(opts)
local expand_macro = require("rust-tools.expand_macro")
M.expand_macro = expand_macro

local syntax_tree = require("rust-tools.syntax_tree")
M.syntax_tree = syntax_tree

local external_docs = require("rust-tools.external_docs")
M.external_docs = external_docs

Expand Down
1 change: 1 addition & 0 deletions lua/rust-tools/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ local function setup_commands()
rt.debuggables.debuggables,
},
RustExpandMacro = { rt.expand_macro.expand_macro },
RustSyntaxTree = { rt.syntax_tree.syntax_tree },
RustOpenExternalDocs = {
rt.external_docs.open_external_docs,
},
Expand Down
42 changes: 42 additions & 0 deletions lua/rust-tools/syntax_tree.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
local rt = require("rust-tools")

local M = {}

local function get_params()
return vim.lsp.util.make_range_params()
end

local latest_buf_id = nil

local function parse_lines(result)
local ret = {}

for line in string.gmatch(result, "([^\n]+)") do
table.insert(ret, line)
end

return ret
end

local function handler(_, result)
-- check if a buffer with the latest id is already open, if it is then
-- delete it and continue
rt.utils.delete_buf(latest_buf_id)

-- create a new buffer
latest_buf_id = vim.api.nvim_create_buf(false, true) -- not listed and scratch

-- split the window to create a new buffer and set it to our window
rt.utils.split(true, latest_buf_id)

vim.api.nvim_buf_set_name(latest_buf_id, "syntax.rust")
vim.api.nvim_buf_set_text(latest_buf_id, 0, 0, 0, 0, parse_lines(result))

rt.utils.resize(true, "-25")
end

function M.syntax_tree()
rt.utils.request(0, "rust-analyzer/syntaxTree", get_params(), handler)
end

return M