Improve `lineguide` plugin (#1056)
* Add more options to lineguide * Allow lineguide plugin to load but remain disabled * Use config.line_limit for default ruler
This commit is contained in:
parent
ec0e3018a8
commit
1e91080680
|
@ -54,7 +54,6 @@ setmetatable(config.plugins, {
|
|||
|
||||
-- Disable these plugins by default.
|
||||
config.plugins.trimwhitespace = false
|
||||
config.plugins.lineguide = false
|
||||
config.plugins.drawwhitespace = false
|
||||
|
||||
return config
|
||||
|
|
|
@ -1,21 +1,59 @@
|
|||
-- mod-version:3
|
||||
local common = require "core.common"
|
||||
local command = require "core.command"
|
||||
local config = require "core.config"
|
||||
local style = require "core.style"
|
||||
local DocView = require "core.docview"
|
||||
local CommandView = require "core.commandview"
|
||||
|
||||
local draw_overlay = DocView.draw_overlay
|
||||
config.plugins.lineguide = common.merge({
|
||||
enabled = false,
|
||||
width = 2,
|
||||
rulers = {
|
||||
-- 80,
|
||||
-- 100,
|
||||
-- 120,
|
||||
config.line_limit
|
||||
}
|
||||
}, config.plugins.lineguide)
|
||||
|
||||
function DocView:draw_overlay(...)
|
||||
if not self:is(CommandView) then
|
||||
local offset = self:get_font():get_width("n") * config.line_limit
|
||||
local x = self:get_line_screen_position(1) + offset
|
||||
local y = self.position.y
|
||||
local w = math.ceil(SCALE * 1)
|
||||
local h = self.size.y
|
||||
|
||||
local color = style.guide or style.selection
|
||||
renderer.draw_rect(x, y, w, h, color)
|
||||
local function get_ruler(v)
|
||||
local result = nil
|
||||
if type(v) == 'number' then
|
||||
result = { columns = v }
|
||||
elseif type(v) == 'table' then
|
||||
result = v
|
||||
end
|
||||
draw_overlay(self, ...)
|
||||
return result
|
||||
end
|
||||
|
||||
local draw_overlay = DocView.draw_overlay
|
||||
function DocView:draw_overlay(...)
|
||||
draw_overlay(self, ...)
|
||||
|
||||
if config.plugins.lineguide.enabled and not self:is(CommandView) then
|
||||
local line_x = self:get_line_screen_position(1)
|
||||
local character_width = self:get_font():get_width("n")
|
||||
local ruler_width = config.plugins.lineguide.width
|
||||
local ruler_color = style.guide or style.selection
|
||||
|
||||
for k,v in ipairs(config.plugins.lineguide.rulers) do
|
||||
local ruler = get_ruler(v)
|
||||
|
||||
if ruler then
|
||||
local x = line_x + (character_width * ruler.columns)
|
||||
local y = self.position.y
|
||||
local w = ruler_width
|
||||
local h = self.size.y
|
||||
|
||||
renderer.draw_rect(x, y, w, h, ruler.color or ruler_color)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
command.add(nil, {
|
||||
["lineguide:toggle"] = function()
|
||||
config.plugins.lineguide.enabled = not config.plugins.lineguide.enabled
|
||||
end
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue