Skip to content

Commit e822694

Browse files
Merge pull request #24 from JoosepAlviste/feature/ci
2 parents a2283cf + 08b208d commit e822694

File tree

6 files changed

+73
-48
lines changed

6 files changed

+73
-48
lines changed

.github/workflows/default.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: default
2+
3+
on: [pull_request]
4+
5+
jobs:
6+
lint:
7+
name: Lint Lua code
8+
runs-on: ubuntu-20.04
9+
steps:
10+
- name: Check out repo
11+
uses: actions/checkout@v2
12+
- name: Stylua check
13+
uses: JohnnyMorganz/[email protected]
14+
with:
15+
token: ${{ secrets.GITHUB_TOKEN }}
16+
args: --color always --check .

lua/ts_context_commentstring.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function M.init()
44
require('nvim-treesitter').define_modules {
55
context_commentstring = {
66
module_path = 'ts_context_commentstring.internal',
7-
}
7+
},
88
}
99
end
1010

lua/ts_context_commentstring/integrations/vim_commentary.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ local map = vim.api.nvim_buf_set_keymap
22

33
local M = {}
44

5-
-- Set up vim-commentary mappings to first update the commentstring, and then
5+
-- Set up vim-commentary mappings to first update the commentstring, and then
66
-- run vim-commentary
77
function M.set_up_maps()
8-
map(0, 'n', 'gc', [[v:lua.context_commentstring.update_commentstring_and_run('Commentary')]], {expr = true})
9-
map(0, 'x', 'gc', [[v:lua.context_commentstring.update_commentstring_and_run('Commentary')]], {expr = true})
10-
map(0, 'o', 'gc', [[v:lua.context_commentstring.update_commentstring_and_run('Commentary')]], {expr = true})
11-
map(0, 'n', 'gcc', [[v:lua.context_commentstring.update_commentstring_and_run('CommentaryLine')]], {expr = true})
12-
map(0, 'n', 'cgc', [[v:lua.context_commentstring.update_commentstring_and_run('ChangeCommentary')]], {expr = true})
8+
map(0, 'n', 'gc', [[v:lua.context_commentstring.update_commentstring_and_run('Commentary')]], { expr = true })
9+
map(0, 'x', 'gc', [[v:lua.context_commentstring.update_commentstring_and_run('Commentary')]], { expr = true })
10+
map(0, 'o', 'gc', [[v:lua.context_commentstring.update_commentstring_and_run('Commentary')]], { expr = true })
11+
map(0, 'n', 'gcc', [[v:lua.context_commentstring.update_commentstring_and_run('CommentaryLine')]], { expr = true })
12+
map(0, 'n', 'cgc', [[v:lua.context_commentstring.update_commentstring_and_run('ChangeCommentary')]], { expr = true })
1313
end
1414

1515
return M

lua/ts_context_commentstring/internal.lua

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
local api = vim.api
22

3-
local utils = require('ts_context_commentstring.utils')
4-
local configs = require('nvim-treesitter.configs')
3+
local utils = require 'ts_context_commentstring.utils'
4+
local configs = require 'nvim-treesitter.configs'
55

66
local M = {}
77

8-
-- The configuration object keys should be **treesitter** languages, NOT
8+
-- The configuration object keys should be **treesitter** languages, NOT
99
-- filetypes or file extensions.
1010
--
11-
-- You can get the treesitter language for the current file by running this
11+
-- You can get the treesitter language for the current file by running this
1212
-- command:
1313
-- `:lua print(require'nvim-treesitter.parsers'.get_buf_lang(0))`
1414
--
@@ -65,24 +65,22 @@ function M.setup_buffer()
6565
end
6666

6767
if enable_autocmd then
68-
utils.create_augroups({
68+
utils.create_augroups {
6969
context_commentstring_ft = {
70-
{'CursorHold', '<buffer>', [[lua require('ts_context_commentstring.internal').update_commentstring()]]},
70+
{ 'CursorHold', '<buffer>', [[lua require('ts_context_commentstring.internal').update_commentstring()]] },
7171
},
72-
})
72+
}
7373
end
7474
end
7575

7676
-- Calculate the commentstring based on the current location of the cursor.
7777
--
78-
-- **Note:** We should treat this function like a public API, try not to break
78+
-- **Note:** We should treat this function like a public API, try not to break
7979
-- it!
8080
--
8181
-- @returns the commentstring or nil if not found
8282
function M.calculate_commentstring()
83-
local node, language_tree = utils.get_node_at_cursor_start_of_line(
84-
vim.tbl_keys(M.config)
85-
)
83+
local node, language_tree = utils.get_node_at_cursor_start_of_line(vim.tbl_keys(M.config))
8684

8785
if not node and not language_tree then
8886
return nil
@@ -94,11 +92,11 @@ function M.calculate_commentstring()
9492
return M.check_node(node, language_config)
9593
end
9694

97-
-- Update the `commentstring` setting based on the current location of the
98-
-- cursor. If no `commentstring` can be calculated, will revert to the ofiginal
95+
-- Update the `commentstring` setting based on the current location of the
96+
-- cursor. If no `commentstring` can be calculated, will revert to the ofiginal
9997
-- `commentstring` for the current file.
10098
--
101-
-- **Note:** We should treat this function like a public API, try not to break
99+
-- **Note:** We should treat this function like a public API, try not to break
102100
-- it!
103101
function M.update_commentstring()
104102
local found_commentstring = M.calculate_commentstring()
@@ -117,11 +115,13 @@ end
117115
-- Check if the given node matches any of the given types. If not, recursively
118116
-- check its parent node.
119117
function M.check_node(node, language_config)
120-
-- There is no commentstring configuration for this language, use the
118+
-- There is no commentstring configuration for this language, use the
121119
-- `ts_original_commentstring`
122-
if not language_config then return nil end
120+
if not language_config then
121+
return nil
122+
end
123123

124-
-- There is no node, we have reached the top-most node, use the default
124+
-- There is no node, we have reached the top-most node, use the default
125125
-- commentstring from language config
126126
if not node then
127127
return language_config.__default or language_config
@@ -130,30 +130,31 @@ function M.check_node(node, language_config)
130130
local type = node:type()
131131
local match = language_config[type]
132132

133-
if match then return match end
133+
if match then
134+
return match
135+
end
134136

135137
-- Recursively check the parent node
136138
return M.check_node(node:parent(), language_config)
137139
end
138140

139141
-- Attach the module to the current buffer
140142
function M.attach()
141-
M.config = vim.tbl_deep_extend(
142-
'force', M.config,
143-
configs.get_module('context_commentstring').config or {}
144-
)
143+
M.config = vim.tbl_deep_extend('force', M.config, configs.get_module('context_commentstring').config or {})
145144

146145
return M.setup_buffer()
147146
end
148147

149-
function M.detach() return end
148+
function M.detach()
149+
return
150+
end
150151

151152
_G.context_commentstring = {}
152153

153-
-- Trigger re-calculation of the `commentstring` and trigger the given <Plug>
154+
-- Trigger re-calculation of the `commentstring` and trigger the given <Plug>
154155
-- mapping right after that.
155156
--
156-
-- This is in the global scope because
157+
-- This is in the global scope because
157158
-- `v:lua.require('ts_context_commentstring')` does not work for some reason.
158159
function _G.context_commentstring.update_commentstring_and_run(mapping)
159160
M.update_commentstring()

lua/ts_context_commentstring/utils.lua

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@ local api = vim.api
22
local cmd = vim.cmd
33
local fn = vim.fn
44

5-
local parsers = require('nvim-treesitter.parsers')
5+
local parsers = require 'nvim-treesitter.parsers'
66

77
local M = {}
88

99
function M.create_augroups(definitions)
1010
for group_name, definition in pairs(definitions) do
1111
cmd('augroup ' .. group_name)
12-
cmd('autocmd!')
12+
cmd 'autocmd!'
1313
for _, def in ipairs(definition) do
14-
local command = table.concat(vim.tbl_flatten{'autocmd', def}, ' ')
14+
local command = table.concat(vim.tbl_flatten { 'autocmd', def }, ' ')
1515
cmd(command)
1616
end
17-
cmd('augroup END')
17+
cmd 'augroup END'
1818
end
1919
end
2020

21-
-- Get the language tree from the given parser that is in the given range. Only
22-
-- accept the given languages. Ignores all language trees with a language not
21+
-- Get the language tree from the given parser that is in the given range. Only
22+
-- accept the given languages. Ignores all language trees with a language not
2323
-- included in `only_languages` parameter.
2424
--
25-
-- This function is pretty much copied from Neovim core
26-
-- (`LanguageTree:language_for_range`), but includes filtering of the injected
25+
-- This function is pretty much copied from Neovim core
26+
-- (`LanguageTree:language_for_range`), but includes filtering of the injected
2727
-- languages.
2828
local function language_for_range(parser, range, only_languages)
2929
for _, child in pairs(parser._children) do
@@ -55,23 +55,25 @@ local function tree_contains(tree, range)
5555
return false
5656
end
5757

58-
-- Get the node that is on the same line as the cursor, but on the first
59-
-- NON-WHITESPACE character. This also handles injected languages via language
58+
-- Get the node that is on the same line as the cursor, but on the first
59+
-- NON-WHITESPACE character. This also handles injected languages via language
6060
-- tree.
6161
--
6262
-- For example, if the cursor is at "|":
6363
-- | <div>
6464
--
6565
-- then will return the <div> node, even though it isn't at the cursor position
6666
--
67-
-- Returns the node at the cursor's line and the language tree for that
67+
-- Returns the node at the cursor's line and the language tree for that
6868
-- injection.
6969
function M.get_node_at_cursor_start_of_line(only_languages, winnr)
70-
if not parsers.has_parser() then return end
70+
if not parsers.has_parser() then
71+
return
72+
end
7173

7274
-- Get the position for the queried node
7375
local cursor = api.nvim_win_get_cursor(winnr or 0)
74-
local first_non_whitespace_col = fn.match(fn.getline('.'), '\\S')
76+
local first_non_whitespace_col = fn.match(fn.getline '.', '\\S')
7577
local range = {
7678
cursor[1] - 1,
7779
first_non_whitespace_col,
@@ -84,14 +86,16 @@ function M.get_node_at_cursor_start_of_line(only_languages, winnr)
8486
local language_tree = language_for_range(root, range, only_languages)
8587

8688
-- Get the sub-tree of the language tree that contains the given range.
87-
-- If there are multiple trees in the buffer for the same injected language,
89+
-- If there are multiple trees in the buffer for the same injected language,
8890
-- then we need to make sure that we are operating on the correct tree.
89-
local tree = vim.tbl_filter(function (tree)
91+
local tree = vim.tbl_filter(function(tree)
9092
return tree_contains(tree, range)
9193
end, language_tree:trees())[1]
9294

9395
-- avoid crash on empty files
94-
if not tree then return nil, language_tree end
96+
if not tree then
97+
return nil, language_tree
98+
end
9599

96100
-- Get the actual node on the location
97101
local injected_root = tree:root()

stylua.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
indent_type = "Spaces"
2+
indent_width = 2
3+
quote_style = "AutoPreferSingle"
4+
no_call_parentheses = true

0 commit comments

Comments
 (0)