-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ex.location): the new component to show cursor location
.
- Loading branch information
1 parent
a1e2d7c
commit 35aad95
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
local log = require('plenary.log').new({ plugin = 'ex.location' }) | ||
|
||
local Location = require('lualine.ex.component'):extend({ | ||
pattern = '%2C:%-3L/%T', | ||
}) | ||
|
||
function Location:post_init() | ||
self.__substitutions = {} | ||
self.__pattern = string.gsub(self.options.pattern, '(%%%-?%d*[LCT]+)', function(template) | ||
return string.gsub(template, '([LCT])', function(value) | ||
table.insert(self.__substitutions, value) | ||
return 'd' | ||
end) | ||
end) | ||
log.debug(self.__substitutions) | ||
log.debug(self.__pattern) | ||
end | ||
|
||
function Location:update_status() | ||
local values = { | ||
L = vim.fn.line('.'), | ||
C = vim.fn.virtcol('.'), | ||
T = vim.fn.line('$'), | ||
} | ||
local substitutions = vim.tbl_map(function(key) | ||
return values[key] | ||
end, self.__substitutions) | ||
return string.format(self.__pattern, unpack(substitutions)) | ||
end | ||
|
||
return Location |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
local l = require('tests.ex.lualine') | ||
local t = require('tests.ex.busted') --:ignore_all_tests() | ||
|
||
local eq = assert.are.equal | ||
|
||
local orig = { | ||
line = vim.fn.line, | ||
virtcol = vim.fn.virtcol, | ||
} | ||
local mock = { | ||
line = {}, | ||
virtcol = {}, | ||
} | ||
|
||
local component_name = 'ex.location' | ||
describe(component_name, function() | ||
before_each(function() | ||
vim.fn.line = function(arg) | ||
return mock.line[arg] | ||
end | ||
vim.fn.virtcol = function(arg) | ||
return mock.virtcol[arg] | ||
end | ||
end) | ||
|
||
after_each(function() | ||
vim.fn.line = orig.line | ||
vim.fn.virtcol = orig.virtcol | ||
end) | ||
|
||
describe('default patter', function() | ||
it('should show {line}:{column}/{total}', function() | ||
mock.virtcol['.'] = 11 | ||
mock.line['.'] = 222 | ||
mock.line['$'] = 3333 | ||
local component = l.render_component(component_name) | ||
eq('11:222/3333', component) | ||
end) | ||
it('should fill numbers by space', function() | ||
mock.virtcol['.'] = 1 | ||
mock.line['.'] = 2 | ||
mock.line['$'] = 3 | ||
local component = l.render_component(component_name) | ||
eq(' 1:2 /3', component) | ||
end) | ||
end) | ||
end) |