Skip to content

Commit

Permalink
chore: Add lint CI job (leoluz#25)
Browse files Browse the repository at this point in the history
* chore: Add lint CI job

* Fix folder name

* Add luacheckrc

* Fix gh workflow

* test super linter

* configure stylua

* Fix super linter

* Add luacheckrc link

* Fix lua format

* Fix Lua format

* Remove empty line

* Force lint error

* fix
  • Loading branch information
leoluz authored Dec 10, 2022
1 parent c759217 commit 4dd9c89
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 40 deletions.
1 change: 1 addition & 0 deletions .github/linters/.luacheckrc
30 changes: 30 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Lint
on:
pull_request: ~
push:
branches:
- master
jobs:
stylua:
name: Stylua
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- uses: JohnnyMorganz/stylua-action@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
# CLI arguments
args: --color always --check lua/

super-linter:
name: Super Linter
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v2
- name: Lint Code Base
uses: github/super-linter/slim@v4
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VALIDATE_JSCPD: false
VALIDATE_PYTHON_BLACK: false
10 changes: 10 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
globals = {
"vim",
}
read_globals = {
"describe",
"it",
"before_each",
"after_each",
"assert"
}
6 changes: 6 additions & 0 deletions .stylua.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
column_width = 120
line_endings = "Unix"
indent_type = "Spaces"
indent_width = 2
quote_style = "AutoPreferDouble"
call_parentheses = "Always"
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ An extension for [nvim-dap][1] providing configurations for launching go debugge
- [delve][2] >= 1.7.0

This plugin extension make usage of treesitter to find the nearest test to debug.
Make sure you have the Go treesitter parser installed.
Make sure you have the Go treesitter parser installed.
If using [nvim-treesitter][3] plugin you can install with `:TSInstall go`.

## Installation
Expand Down Expand Up @@ -46,10 +46,10 @@ lua require('dap-go').setup()


To debug the closest method above the cursor use you can run:
- `:lua require('dap-go').debug_test()`
- `:lua require('dap-go').debug_test()`

Once a test was run, you can simply run it again from anywhere:
- `:lua require('dap-go').debug_last_test()`
- `:lua require('dap-go').debug_last_test()`

It is better to define a mapping to invoke this command. See the mapping section bellow.

Expand All @@ -66,9 +66,9 @@ It is better to define a mapping to invoke this command. See the mapping section
### Debugging with dlv in headless mode

1. Start `dlv` in headless mode. You can specify subcommands and flags after `--`, e.g.,
```
dlv debug -l 127.0.0.1:38697 --headless ./main.go -- subcommand --myflag=xyz
```
```sh
dlv debug -l 127.0.0.1:38697 --headless ./main.go -- subcommand --myflag=xyz
```
1. Call `:lua require('dap').continue()` to start debugging.
1. Select the option `Attach remote`.

Expand Down
65 changes: 31 additions & 34 deletions lua/dap-go.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local query = require "vim.treesitter.query"
local query = require("vim.treesitter.query")

local M = {
last_testname = "",
last_testpath = "",
last_testname = "",
last_testpath = "",
}

local tests_query = [[
Expand All @@ -28,7 +28,7 @@ local subtests_query = [[

local function load_module(module_name)
local ok, module = pcall(require, module_name)
assert(ok, string.format('dap-go dependency error: %s not installed', module_name))
assert(ok, string.format("dap-go dependency error: %s not installed", module_name))
return module
end

Expand All @@ -37,14 +37,14 @@ local function get_arguments()
if co then
return coroutine.create(function()
local args = {}
vim.ui.input({ prompt = 'Args: ' }, function(input)
vim.ui.input({ prompt = "Args: " }, function(input)
args = vim.split(input or "", " ")
end)
coroutine.resume(co, args)
end)
else
local args = {}
vim.ui.input({ prompt = 'Args: ' }, function(input)
vim.ui.input({ prompt = "Args: " }, function(input)
args = vim.split(input or "", " ")
end)
return args
Expand All @@ -61,7 +61,7 @@ local function setup_go_adapter(dap)
local port = config.port or "38697"
local addr = string.format("%s:%s", host, port)

if (config.request == "attach" and config.mode == "remote") then
if config.request == "attach" and config.mode == "remote" then
-- Not starting delve server automatically in "Attach remote."
-- Will connect to delve server that is listening to [host]:[port] instead.
-- Users can use this with delve headless mode:
Expand All @@ -72,43 +72,41 @@ local function setup_go_adapter(dap)
print(msg)
else
local opts = {
stdio = {nil, stdout, stderr},
args = {"dap", "-l", addr},
detached = true
stdio = { nil, stdout, stderr },
args = { "dap", "-l", addr },
detached = true,
}
handle, pid_or_err = vim.loop.spawn("dlv", opts, function(code)
stdout:close()
stderr:close()
handle:close()
if code ~= 0 then
print('dlv exited with code', code)
print("dlv exited with code", code)
end
end)
assert(handle, 'Error running dlv: ' .. tostring(pid_or_err))
assert(handle, "Error running dlv: " .. tostring(pid_or_err))
stdout:read_start(function(err, chunk)
assert(not err, err)
if chunk then
vim.schedule(function()
require('dap.repl').append(chunk)
require("dap.repl").append(chunk)
end)
end
end)
stderr:read_start(function(err, chunk)
assert(not err, err)
if chunk then
vim.schedule(function()
require('dap.repl').append(chunk)
require("dap.repl").append(chunk)
end)
end
end)
end

-- Wait for delve to start
vim.defer_fn(
function()
callback({type = "server", host = host, port = port})
end,
100)
vim.defer_fn(function()
callback({ type = "server", host = host, port = port })
end, 100)
end
end

Expand Down Expand Up @@ -138,7 +136,7 @@ local function setup_go_configuration(dap)
name = "Attach",
mode = "local",
request = "attach",
processId = require('dap.utils').pick_process,
processId = require("dap.utils").pick_process,
},
{
type = "go",
Expand All @@ -159,7 +157,7 @@ local function setup_go_configuration(dap)
request = "launch",
mode = "test",
program = "./${relativeFileDirname}",
}
},
}
end

Expand All @@ -172,12 +170,12 @@ end
local function debug_test(testname, testpath)
local dap = load_module("dap")
dap.run({
type = "go",
name = testname,
request = "launch",
mode = "test",
program = testpath,
args = {"-test.run", testname},
type = "go",
name = testname,
request = "launch",
mode = "test",
program = testpath,
args = { "-test.run", testname },
})
end

Expand All @@ -203,7 +201,6 @@ local function get_closest_above_cursor(test_tree)
end
end


local function is_parent(dest, source)
if not (dest and source) then
return false
Expand All @@ -226,15 +223,15 @@ end

local function get_closest_test()
local stop_row = vim.api.nvim_win_get_cursor(0)[1]
local ft = vim.api.nvim_buf_get_option(0, 'filetype')
assert(ft == 'go', 'dap-go error: can only debug go files, not '..ft)
local ft = vim.api.nvim_buf_get_option(0, "filetype")
assert(ft == "go", "dap-go error: can only debug go files, not " .. ft)
local parser = vim.treesitter.get_parser(0)
local root = (parser:parse()[1]):root()

local test_tree = {}

local test_query = vim.treesitter.parse_query(ft, tests_query)
assert(test_query, 'dap-go error: could not parse test query')
assert(test_query, "dap-go error: could not parse test query")
for _, match, _ in test_query:iter_matches(root, 0, 0, stop_row) do
local test_match = {}
for id, node in pairs(match) do
Expand All @@ -251,14 +248,14 @@ local function get_closest_test()
end

local subtest_query = vim.treesitter.parse_query(ft, subtests_query)
assert(subtest_query, 'dap-go error: could not parse test query')
assert(subtest_query, "dap-go error: could not parse test query")
for _, match, _ in subtest_query:iter_matches(root, 0, 0, stop_row) do
local test_match = {}
for id, node in pairs(match) do
local capture = subtest_query.captures[id]
if capture == "testname" then
local name = query.get_node_text(node, 0)
test_match.name = string.gsub(string.gsub(name, ' ', '_'), '"', '')
test_match.name = string.gsub(string.gsub(name, " ", "_"), '"', "")
end
if capture == "parent" then
test_match.node = node
Expand Down Expand Up @@ -289,7 +286,7 @@ function M.debug_test()

if testname == "" then
vim.notify("no test found")
return false
return false
end

M.last_testname = testname
Expand Down

0 comments on commit 4dd9c89

Please sign in to comment.