Advertisement
Guest User

TextAdept outline module

a guest
Aug 28th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.83 KB | None | 0 0
  1. -- File outline (functions, globals, sections...) for TextAdept
  2. --
  3. -- Copyright 2018 Google LLC.
  4. -- SPDX-License-Identifier: Apache 2.0
  5. --
  6. -- Usage: put the following somewhere in init.lua:
  7. --     keys[CURSES and 'mo' or 'ao'] = require('outline').show
  8. -- You can customize per-lexer patterns using outline.patters table
  9.  
  10. local textredux = require 'textredux'
  11. local outline = {}
  12. outline.patterns = {
  13.   lua      = {"^function", "^local", "^[%w%d%._]+%s*="},
  14.   go       = {"^import", "^func", "^type"},
  15.   python   = {"^%s*def%s", "^%s*class%s", "^%s*[A-Z_]+%s*=", "^[%w_]+%s*="},
  16.   protobuf = {"^%s*message", "^%s*enum", "^%s*service", "^%s*rpc"},
  17. }
  18. local function matches(str, patterns)
  19.   if type(patterns) ~= "table" then patterns = {patterns} end
  20.   local matched
  21.   for _,p in ipairs(patterns) do
  22.     matched = str:match(p); if matched then break end
  23.   end
  24.   if not matched then return false end
  25.   if patterns.blacklist then
  26.     for _,b in ipairs(patterns.blacklist) do
  27.       if matched:match(b) then return false end
  28.     end
  29.   end
  30.   return true
  31. end
  32. function outline.show()
  33.   if not buffer.filename then return end
  34.   local lines = {}
  35.   local patterns = outline.patterns[buffer:get_lexer()]
  36.   if patterns then
  37.     -- find matching lines
  38.     for i=0,buffer.line_count-1 do
  39.       local line = buffer:get_line(i):sub(1,-2)
  40.       if matches(line, patterns) then
  41.         lines[#lines + 1] = {line, i}
  42.       end
  43.     end
  44.   end
  45.   if #lines == 0 then return end
  46.   -- create the list
  47.   local list = textredux.core.list.new(
  48.     'Outline of ' .. buffer.filename:match('[^/]+$'),
  49.     lines, -- list items
  50.     function (list, item) -- on selection callback
  51.       list:close()
  52.       buffer:goto_line(item[2])
  53.       buffer:vertical_centre_caret()
  54.     end
  55.   )
  56.   list.keys.esc = function() list:close() end
  57.   list:show()
  58. end
  59. return outline
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement