Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- File outline (functions, globals, sections...) for TextAdept
- --
- -- Copyright 2018 Google LLC.
- -- SPDX-License-Identifier: Apache 2.0
- --
- -- Usage: put the following somewhere in init.lua:
- -- keys[CURSES and 'mo' or 'ao'] = require('outline').show
- -- You can customize per-lexer patterns using outline.patters table
- local textredux = require 'textredux'
- local outline = {}
- outline.patterns = {
- lua = {"^function", "^local", "^[%w%d%._]+%s*="},
- go = {"^import", "^func", "^type"},
- python = {"^%s*def%s", "^%s*class%s", "^%s*[A-Z_]+%s*=", "^[%w_]+%s*="},
- protobuf = {"^%s*message", "^%s*enum", "^%s*service", "^%s*rpc"},
- }
- local function matches(str, patterns)
- if type(patterns) ~= "table" then patterns = {patterns} end
- local matched
- for _,p in ipairs(patterns) do
- matched = str:match(p); if matched then break end
- end
- if not matched then return false end
- if patterns.blacklist then
- for _,b in ipairs(patterns.blacklist) do
- if matched:match(b) then return false end
- end
- end
- return true
- end
- function outline.show()
- if not buffer.filename then return end
- local lines = {}
- local patterns = outline.patterns[buffer:get_lexer()]
- if patterns then
- -- find matching lines
- for i=0,buffer.line_count-1 do
- local line = buffer:get_line(i):sub(1,-2)
- if matches(line, patterns) then
- lines[#lines + 1] = {line, i}
- end
- end
- end
- if #lines == 0 then return end
- -- create the list
- local list = textredux.core.list.new(
- 'Outline of ' .. buffer.filename:match('[^/]+$'),
- lines, -- list items
- function (list, item) -- on selection callback
- list:close()
- buffer:goto_line(item[2])
- buffer:vertical_centre_caret()
- end
- )
- list.keys.esc = function() list:close() end
- list:show()
- end
- return outline
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement