Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(type): add type annotation for defaults.lua #1333

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
014f98a
refactor(type): add type annotation for defaults.lua
pysan3 Feb 2, 2024
efcefd7
ci(lua_ls): add lua_ls actions to check type annotation
pysan3 Feb 2, 2024
fde92b3
chore: autoformat with stylua
pysan3 Feb 2, 2024
0c048b4
ci(lua_ls): force run type check on all branches for now
pysan3 Feb 2, 2024
5b8f58c
Merge branch 'add-type-annotations' of github.com:pysan3/neo-tree.nvi…
pysan3 Feb 2, 2024
f5d2207
fix(ci): fix typo
pysan3 Feb 2, 2024
913d998
ci(lua_ls): refer to types defined in the repo
pysan3 Feb 2, 2024
adee9b9
fix(type): better organized type names
pysan3 Feb 2, 2024
72776bc
refactor(type): move config types to another file
pysan3 Feb 3, 2024
ead7513
refactor(type): add types to log.lua
pysan3 Feb 3, 2024
e6236b9
refactor(type): add type for merge_config
pysan3 Feb 5, 2024
c331b7b
ci(lua_ls): add more files to check types
pysan3 Feb 5, 2024
4cfcbe2
refactor(defaults): move event_handler examples to wiki
pysan3 Feb 5, 2024
4b68620
refactor(types): add type files to library
pysan3 Feb 5, 2024
7744d57
refactor(types): ignore wrong or deprecated types
pysan3 Feb 5, 2024
e85fdcc
ci(lua_ls): remove unnecessary types to check
pysan3 Feb 5, 2024
a28b905
refactor(types): remove collections.lua from type check
pysan3 Feb 5, 2024
c4ab2c3
ci(lua_ls): make lua_ls typecheck run on PRs
pysan3 Feb 5, 2024
9ce9563
fix(type): add lacking field
pysan3 Feb 6, 2024
b42cefe
refactor(type): delete types to prevent lua_ls performance issue
pysan3 Feb 9, 2024
12a67ae
refactor(type): move component types to separate file
pysan3 Feb 9, 2024
710ddaf
refactor(type): add types to functions in common/components
pysan3 Feb 9, 2024
c1eaadb
refactor(type): capitalize all enum keys
pysan3 Feb 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/.luarc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
"runtime.version": "LuaJIT",
"runtime.path": [
"lua/?.lua",
"lua/?/init.lua"
],
"workspace.library": [
"/github/workspace/lua/neo-tree/types",
"/github/workspace/deps/neodev.nvim/types/stable",
"/github/workspace/deps/plenary.nvim/lua",
"/github/workspace/deps/nui.nvim/lua",
"/github/workspace/deps/nvim-web-devicons/lua",
"${3rd}/busted/library"
],
"diagnostics.libraryFiles": "Disable",
"workspace.checkThirdParty": "Disable"
}
53 changes: 53 additions & 0 deletions .github/workflows/lua_ls-typecheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: lua_ls-typecheck

on:
push:
branches:
- '*'
pull_request:

jobs:
build:
name: Type Check Code Base
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: Checkout dependency neodev
uses: actions/checkout@v3
with:
repository: "folke/neodev.nvim"
path: "deps/neodev.nvim"

- name: Checkout dependency plenary
uses: actions/checkout@v3
with:
repository: "nvim-lua/plenary.nvim"
path: "deps/plenary.nvim"

- name: Checkout dependency nvim-web-devicons
uses: actions/checkout@v3
with:
repository: "nvim-tree/nvim-web-devicons" # not strictly required, but recommended
path: "deps/nvim-web-devicons"

- name: Checkout dependency nui
uses: actions/checkout@v3
with:
repository: "MunifTanjim/nui.nvim"
path: "deps/nui.nvim"

- name: Print Filetree
run: ls -la && ls -la deps

- name: Type Check Code Base
uses: mrcjkb/lua-typecheck-action@v0.1.2
with:
configpath: .github/workflows/.luarc.json
directories: |
lua/neo-tree/defaults.lua
lua/neo-tree/log.lua
lua/neo-tree/setup/deprecations.lua
lua/neo-tree/setup/init.lua
49 changes: 49 additions & 0 deletions .github/workflows/stylua.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: stylua

on:
push:
branches:
- '*'
paths-ignore:
- ".github/**"
- "**.md"
- "**.norg"

jobs:
format-with-stylua:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Cache cargo modules
id: cache-cargo
uses: actions/cache@v2
env:
cache-name: cache-node-modules
with:
path: ~/.cargo
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/Cargo.toml') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-

- name: Install cargo
run: curl https://sh.rustup.rs -sSf | sh -s -- -y

- name: Install stylua
run: cargo install stylua --features lua52

- name: Run formatting
run: stylua -v --verify .

- uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: "chore: autoformat with stylua"
branch: ${{ github.ref }}

- name: Push changes
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.ref }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Compiled Lua sources
luac.out
!.luarc.json

# luarocks build files
*.src.rock
Expand Down
1 change: 0 additions & 1 deletion .styluaignore

This file was deleted.

3 changes: 3 additions & 0 deletions lua/neo-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ M.set_log_level = function(level)
log.set_level(level)
end

---Entry point for Neotree. Welcome to Neotree!
---@param config NeotreeConfig
---@param is_auto_config boolean|nil # When true, this function is called for testing. Skip changes to vim state.
M.setup = function(config, is_auto_config)
M.config = require("neo-tree.setup").merge_config(config, is_auto_config)
local netrw = require("neo-tree.setup.netrw")
Expand Down
108 changes: 60 additions & 48 deletions lua/neo-tree/collections.lua
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
local log = require("neo-tree.log")
---@class NeotreeCollections
local M = {}

local Node = {}
function Node:new(value)
---@class NeotreeListNode
M.Node = {}

---Create new NeotreeListNode of type <T>
function M.Node:new(value)
local props = { prev = nil, next = nil, value = value }
setmetatable(props, self)
self.__index = self
return props
end

local LinkedList = {}
function LinkedList:new()
---@class NeotreeLinkedList
M.LinkedList = {}
function M.LinkedList:new()
local props = { head = nil, tail = nil, size = 0 }
setmetatable(props, self)
self.__index = self
return props
end

function LinkedList:add_node(node)
---Add NeotreeListNode into linked list
function M.LinkedList:add_node(node)
if self.head == nil then
self.head = node
self.tail = node
Expand All @@ -29,7 +36,8 @@ function LinkedList:add_node(node)
return node
end

function LinkedList:remove_node(node)
---Remove NeotreeListNode from a linked list
function M.LinkedList:remove_node(node)
if node.prev ~= nil then
node.prev.next = node.next
end
Expand All @@ -48,70 +56,73 @@ function LinkedList:remove_node(node)
node.value = nil
end

-- First in Last Out
local Queue = {}
function Queue:new()
local props = { _list = LinkedList:new() }
---Clear all nodes in the list
function M.LinkedList:clear()
local current = self.head
while current ~= nil do
local next = current.next
self:remove_node(current)
current = next
end
end

---@class NeotreeQueue
---@field _list NeotreeLinkedList
M.Queue = {}

function M.Queue:new()
local props = {}
props._list = M.LinkedList:new()
setmetatable(props, self)
self.__index = self
self.__index = self ---@diagnostic disable-line
return props
end

---Add an element to the end of the queue.
---@param value any The value to add.
function Queue:add(value)
self._list:add_node(Node:new(value))
function M.Queue:add(value)
self._list:add_node(M.Node:new(value))
end

---Iterates over the entire list, running func(value) on each element.
---If func returns true, the element is removed from the list.
---@param func function The function to run on each element.
function Queue:for_each(func)
function M.Queue:for_each(func)
local node = self._list.head
while node ~= nil do
local result = func(node.value)
local node_is_next = false
if result then
if type(result) == "boolean" then
local node_to_remove = node
node = node.next
node_is_next = true
self._list:remove_node(node_to_remove)
elseif type(result) == "table" then
if type(result.handled) == "boolean" and result.handled == true then
log.trace(
"Handler ",
node.value.id,
" for "
.. node.value.event
.. " returned handled = true, skipping the rest of the queue."
if type(result) == "table" then
if result.handled == true then
local id = node.value.id or nil ---@diagnostic disable-line
local event = node.value.event or nil ---@diagnostic disable-line
log.trace(
string.format(
"Handler %s for %s returned handled = true, skipping the rest of the queue.",
id,
event
)
return result
end
)
return result
end
end
if not node_is_next then
if result == true then
local next = node.next
self._list:remove_node(node)
node = next
else
node = node.next
end
end
end

function Queue:is_empty()
function M.Queue:is_empty()
return self._list.size == 0
end

function Queue:remove_by_id(id)
function M.Queue:remove_by_id(id)
local current = self._list.head
while current ~= nil do
local is_match = false
local item = current.value
if item ~= nil then
local item_id = item.id or item
if item_id == id then
is_match = true
end
end
if is_match then
local item_id = type(item) == "table" and item.id or item
if item_id == id then
local next = current.next
self._list:remove_node(current)
current = next
Expand All @@ -121,7 +132,8 @@ function Queue:remove_by_id(id)
end
end

return {
Queue = Queue,
LinkedList = LinkedList,
}
function M.Queue:clear()
self._list:clear()
end

return M