Initial markdown support

Markdown doesnt have a language server, so do this manually
Closes #43
This commit is contained in:
simrat39
2021-08-01 12:21:49 -07:00
parent 0371627d01
commit 1941d9edf9
4 changed files with 65 additions and 11 deletions

View File

@@ -5,6 +5,7 @@ local ui = require('symbols-outline.ui')
local writer = require('symbols-outline.writer')
local config = require('symbols-outline.config')
local utils = require('symbols-outline.utils.lsp_utils')
local markdown = require('symbols-outline.markdown')
local M = {}
@@ -58,12 +59,14 @@ end
function M._refresh()
if M.state.outline_buf ~= nil then
vim.lsp.buf_request_all(0, "textDocument/documentSymbol", getParams(),
function(response)
local function refresh_handler(response)
if response == nil or type(response) ~= 'table' then
return
end
if not utils.is_buf_attached_to_lsp(vim.api.nvim_get_current_buf()) then
local current_buf = vim.api.nvim_get_current_buf()
if (not utils.is_buf_markdown(current_buf)) and
(not utils.is_buf_attached_to_lsp(current_buf)) then
return
end
@@ -75,7 +78,13 @@ function M._refresh()
writer.parse_and_write(M.state.outline_buf,
M.state.flattened_outline_items)
end)
end
if vim.api.nvim_buf_get_option(0, 'ft') == 'markdown' then
refresh_handler(markdown.handle_markdown())
end
vim.lsp.buf_request_all(0, "textDocument/documentSymbol", getParams(),
refresh_handler)
end
end
@@ -96,7 +105,10 @@ function M._highlight_current_item(winnr)
local doesnt_have_outline_buf = not M.state.outline_buf
local should_exit = doesnt_have_lsp or doesnt_have_outline_buf or
local is_not_markdown = not utils.is_buf_markdown(0)
local should_exit = (doesnt_have_lsp and is_not_markdown) or
doesnt_have_outline_buf or
is_current_buffer_the_outline
-- Make a special case if we have a window number
@@ -250,15 +262,17 @@ end
function M.toggle_outline()
if M.state.outline_buf == nil then
vim.lsp.buf_request_all(0, "textDocument/documentSymbol", getParams(),
handler)
M.open_outline()
else
vim.api.nvim_win_close(M.state.outline_win, true)
M.close_outline()
end
end
function M.open_outline()
if M.state.outline_buf == nil then
if vim.api.nvim_buf_get_option(0, 'ft') == 'markdown' then
handler(markdown.handle_markdown())
end
vim.lsp.buf_request_all(0, "textDocument/documentSymbol", getParams(),
handler)
end

View File

@@ -65,9 +65,7 @@ function M.get_position_navigation_direction()
end
end
function M.get_width_percentage()
return M.options.width / 100
end
function M.get_width_percentage() return M.options.width / 100 end
function M.get_split_command()
if M.options.position == 'left' then
@@ -90,6 +88,7 @@ end
function M.is_client_blacklisted(client_id)
local client = vim.lsp.get_client_by_id(client_id)
if not client then return false end
return has_value(M.options.lsp_blacklist, client.name)
end

View File

@@ -0,0 +1,37 @@
local M = {}
---Parses markdown files and returns a table of SymbolInformation[] which is
-- used by the plugin to show the outline.
-- We do this because markdown does not have a LSP.
-- Note that the headings won't have any heirarchy (as of now).
---@return table
function M.handle_markdown()
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
local results = {}
for line, value in ipairs(lines) do
if string.find(value, "^#+") then
if #results > 0 then
results[#results].selectionRange["end"].line = line - 1
results[#results].range["end"].line = line - 1
end
table.insert(results, {
kind = 13,
name = value,
selectionRange = {
start = {character = 1, line = line - 1},
["end"] = {character = 1, line = line - 1}
},
range = {
start = {character = 1, line = line - 1},
["end"] = {character = 1, line = line - 1}
}
})
end
end
return {[1000000]={result=results}}
end
return M

View File

@@ -7,4 +7,8 @@ function M.is_buf_attached_to_lsp(bufnr)
return clients ~= nil and #clients > 0
end
function M.is_buf_markdown(bufnr)
return vim.api.nvim_buf_get_option(bufnr, 'ft') == 'markdown'
end
return M