Skip to content

Commit fde00db

Browse files
Merge pull request #119 from CogentRedTester/type-annotations
Add type checking with Lua Language Server type annotations
2 parents b03e82f + dbad0f4 commit fde00db

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1427
-313
lines changed

.github/workflows/lint.yml

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# This is a basic workflow to help you get started with Actions
2+
3+
name: Lint
4+
5+
# Controls when the workflow will run
6+
on:
7+
# Triggers the workflow on push or pull request events but only for the "master" branch
8+
push:
9+
branches: [ "master" ]
10+
pull_request:
11+
branches: [ "master" ]
12+
13+
# Allows you to run this workflow manually from the Actions tab
14+
workflow_dispatch:
15+
16+
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
17+
jobs:
18+
# This workflow contains a single job called "build"
19+
Build_LuaLS:
20+
# The type of runner that the job will run on
21+
runs-on: ubuntu-latest
22+
23+
outputs:
24+
luals_version: ${{ steps.get-luaLS-version.outputs.version }}
25+
26+
# Steps represent a sequence of tasks that will be executed as part of the job
27+
steps:
28+
- name: Get LuaLS version
29+
id: get-luaLS-version
30+
run: |
31+
version=$(curl https://api.github.com/repos/LuaLS/lua-language-server/releases/latest | jq -r '.tag_name')
32+
echo $version
33+
echo "version=$version" >> $GITHUB_OUTPUT
34+
shell: bash
35+
36+
- name: Cache Lua Language Server
37+
uses: actions/cache@v4
38+
id: cache
39+
with:
40+
key: ${{ steps.get-luaLS-version.outputs.version }}
41+
path: ./luals
42+
43+
- uses: actions/checkout@v4
44+
if: steps.cache.outputs.cache-hit != 'true'
45+
with:
46+
repository: LuaLS/lua-language-server
47+
ref: ${{ steps.get-luaLS-version.outputs.version }}
48+
path: ./luals
49+
50+
# Runs a single command using the runners shell
51+
- name: Install Lua Language Server
52+
if: steps.cache.outputs.cache-hit != 'true'
53+
working-directory: ./luals
54+
run: |
55+
echo Running Lua Language Server build script
56+
sudo apt-get update
57+
sudo apt-get -y install ninja-build
58+
./make.sh
59+
60+
Lint_LuaJit:
61+
needs: Build_LuaLS
62+
runs-on: ubuntu-latest
63+
steps:
64+
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
65+
- uses: actions/checkout@v4
66+
with:
67+
path: ./file-browser
68+
- name: Cache Lua Language Server
69+
uses: actions/cache@v4
70+
id: cache
71+
with:
72+
key: ${{ needs.Build_LuaLS.outputs.luals_version }}
73+
path: ./luals
74+
- name: Run LuaLS
75+
run: |
76+
jq '."runtime.version" = "LuaJIT"' ./file-browser/.luarc.json > tmp.json
77+
mv tmp.json ./file-browser/.luarc.json
78+
./luals/bin/lua-language-server --check=./file-browser
79+
80+
Lint_Lua51:
81+
needs: Build_LuaLS
82+
runs-on: ubuntu-latest
83+
steps:
84+
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
85+
- uses: actions/checkout@v4
86+
with:
87+
path: ./file-browser
88+
- name: Cache Lua Language Server
89+
uses: actions/cache@v4
90+
id: cache
91+
with:
92+
key: ${{ needs.Build_LuaLS.outputs.luals_version }}
93+
path: ./luals
94+
- name: Run LuaLS
95+
run: |
96+
jq '."runtime.version" = "Lua 5.1"' ./file-browser/.luarc.json > tmp.json
97+
mv tmp.json ./file-browser/.luarc.json
98+
./luals/bin/lua-language-server --check=./file-browser
99+
100+
Lint_Lua52:
101+
needs: Build_LuaLS
102+
runs-on: ubuntu-latest
103+
steps:
104+
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
105+
- uses: actions/checkout@v4
106+
with:
107+
path: ./file-browser
108+
- name: Cache Lua Language Server
109+
uses: actions/cache@v4
110+
id: cache
111+
with:
112+
key: ${{ needs.Build_LuaLS.outputs.luals_version }}
113+
path: ./luals
114+
- name: Run LuaLS
115+
run: |
116+
jq '."runtime.version" = "Lua 5.2"' ./file-browser/.luarc.json > tmp.json
117+
mv tmp.json ./file-browser/.luarc.json
118+
./luals/bin/lua-language-server --check=./file-browser

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode

.luarc.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"runtime.version": "LuaJIT",
3+
"runtime.path": [
4+
"?.lua",
5+
"?/init.lua",
6+
"../../script-modules/?.lua"
7+
],
8+
"diagnostics.workspaceDelay": 1000,
9+
"diagnostics.groupFileStatus": {
10+
"await": "Any",
11+
"luadoc": "Any",
12+
"type-check": "Any",
13+
"unbalanced": "Any",
14+
"strong": "Any"
15+
},
16+
"diagnostics.groupSeverity": {
17+
"await": "Warning",
18+
"strong": "Warning"
19+
},
20+
"diagnostics.severity": {
21+
"missing-return": "Error!",
22+
"missing-return-value": "Error!",
23+
"cast-type-mismatch": "Error!",
24+
"assign-type-mismatch": "Error!",
25+
"global-element": "Warning!"
26+
}
27+
}

addons/apache-browser.lua

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ local fb = require "file-browser"
99

1010
--decodes a URL address
1111
--this piece of code was taken from: https://stackoverflow.com/questions/20405985/lua-decodeuri-luvit/20406960#20406960
12+
---@type fun(s: string): string
1213
local decodeURI
1314
do
1415
local char, gsub, tonumber = string.char, string.gsub, tonumber
@@ -20,22 +21,32 @@ do
2021
end
2122
end
2223

24+
---@type ParserConfig
2325
local apache = {
2426
priority = 80,
2527
api_version = "1.1.0"
2628
}
2729

30+
---@param name string
31+
---@return boolean
2832
function apache:can_parse(name)
29-
return name:find("^https?://")
33+
return name:find("^https?://") ~= nil
3034
end
3135

32-
--send curl errors through the browser empty_text
33-
function apache:send_error(str)
36+
---Send curl errors through the browser empty_text.
37+
---@param str string
38+
---@return List
39+
---@return Opts
40+
local function send_error(str)
3441
return {}, {empty_text = "curl error: "..str}
3542
end
3643

44+
---@async
45+
---@param args string[]
46+
---@return MPVSubprocessResult
3747
local function execute(args)
3848
msg.trace(utils.to_string(args))
49+
---@type boolean, MPVSubprocessResult
3950
local _, cmd = fb.get_parse_state():yield(
4051
mp.command_native_async({
4152
name = "subprocess",
@@ -48,21 +59,22 @@ local function execute(args)
4859
return cmd
4960
end
5061

62+
---@async
5163
function apache:parse(directory)
5264
msg.verbose(directory)
5365

5466
local test = execute({"curl", "-k", "-l", "-I", directory})
5567
local response = test.stdout:match("(%d%d%d [^\n\r]+)")
5668
if test.stdout:match("Content%-Type: ([^\r\n/]+)") ~= "text" then return nil end
57-
if response ~= "200 OK" then return self:send_error(response) end
69+
if response ~= "200 OK" then return send_error(response) end
5870

5971
local html = execute({"curl", "-k", "-l", directory})
60-
if html.status ~= 0 then return self:send_error(tostring(html.status))
72+
if html.status ~= 0 then return send_error(tostring(html.status))
6173
elseif not html.stdout:find("%[PARENTDIR%]") then return nil end
6274

63-
html = html.stdout
75+
local html_body = html.stdout
6476
local list = {}
65-
for str in string.gmatch(html, "[^\r\n]+") do
77+
for str in string.gmatch(html_body, "[^\r\n]+") do
6678
local valid = true
6779
if str:sub(1,4) ~= "<tr>" then valid = false end
6880

addons/favourites.lua

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
local mp = require "mp"
66
local msg = require "mp.msg"
7-
local utils = require "mp.utils"
8-
local save_path = mp.command_native({"expand-path", "~~/script-opts/file_browser_favourites.txt"})
7+
8+
local fb = require 'file-browser'
9+
local save_path = mp.command_native({"expand-path", "~~/script-opts/file_browser_favourites.txt"}) --[[@as string]]
910
do
1011
local file = io.open(save_path, "a+")
1112
if not file then
@@ -15,16 +16,24 @@ do
1516
file:close()
1617
end
1718

18-
local favourites = nil
19+
---@type Item[]
20+
local favourites = {}
21+
local favourites_loaded = false
22+
23+
---@type ParserConfig
1924
local favs = {
2025
api_version = "1.4.0",
2126
priority = 30,
2227
cursor = 1
2328
}
2429

2530
local use_virtual_directory = true
31+
32+
---@type table<string,string>
2633
local full_paths = {}
2734

35+
---@param str string
36+
---@return Item
2837
local function create_favourite_object(str)
2938
local item = {
3039
type = str:sub(-1) == "/" and "dir" or "file",
@@ -36,34 +45,38 @@ local function create_favourite_object(str)
3645
return item
3746
end
3847

48+
---@param self Parser
3949
function favs:setup()
4050
self:register_root_item('Favourites/')
4151
end
4252

4353
local function update_favourites()
44-
favourites = {}
45-
4654
local file = io.open(save_path, "r")
4755
if not file then return end
4856

4957
for str in file:lines() do
5058
table.insert(favourites, create_favourite_object(str))
5159
end
5260
file:close()
61+
favourites_loaded = true
5362
end
5463

5564
function favs:can_parse(directory)
5665
return directory:find("Favourites/") == 1
5766
end
5867

68+
---@async
69+
---@param self Parser
70+
---@param directory string
71+
---@return List?
72+
---@return Opts?
5973
function favs:parse(directory)
60-
if not favourites then update_favourites() end
74+
if not favourites_loaded then update_favourites() end
6175
if directory == "Favourites/" then
6276
local opts = {
6377
filtered = true,
6478
sorted = true
6579
}
66-
if self.cursor ~= 1 then opts.selected_index = self.cursor ; self.cursor = 1 end
6780
return favourites, opts
6881
end
6982

@@ -76,6 +89,7 @@ function favs:parse(directory)
7689
local list, opts = self:defer(full_path or "")
7790

7891
if not list then return nil end
92+
opts = opts or {}
7993
opts.id = self:get_id()
8094
if opts.directory_label then
8195
opts.directory_label = opts.directory_label:gsub(full_paths[name], "Favourites/"..name..'/')
@@ -94,10 +108,14 @@ function favs:parse(directory)
94108

95109
local list, opts = self:defer(path)
96110
if not list then return nil end
111+
opts = opts or {}
97112
opts.directory = opts.directory or path
98113
return list, opts
99114
end
100115

116+
---@param path string
117+
---@return integer?
118+
---@return Item?
101119
local function get_favourite(path)
102120
for index, value in ipairs(favourites) do
103121
if value.path == path then return index, value end
@@ -106,14 +124,14 @@ end
106124

107125
--update the browser with new contents of the file
108126
local function update_browser()
109-
if favs.get_directory():find("[fF]avourites/") then
110-
if favs.get_directory():find("[fF]avourites/$") then
111-
local cursor = favs.get_selected_index()
112-
favs.rescan()
113-
favs.set_selected_index(cursor)
114-
favs.redraw()
127+
if fb.get_directory():find("[fF]avourites/") then
128+
if fb.get_directory():find("[fF]avourites/$") then
129+
local cursor = fb.get_selected_index()
130+
fb.rescan()
131+
fb.set_selected_index(cursor)
132+
fb.redraw()
115133
else
116-
favs.clear_cache()
134+
fb.clear_cache()
117135
end
118136
end
119137
end
@@ -154,7 +172,7 @@ local function move_favourite(path, direction)
154172
end
155173

156174
local function toggle_favourite(cmd, state, co)
157-
local path = favs.get_full_path(state.list[state.selected], state.directory)
175+
local path = fb.get_full_path(state.list[state.selected], state.directory)
158176

159177
if state.directory:find("[fF]avourites/$") then remove_favourite(path)
160178
else add_favourite(path) end
@@ -163,15 +181,15 @@ end
163181

164182
local function move_key(cmd, state, co)
165183
if not state.directory:find("[fF]avourites/") then return false end
166-
local path = favs.get_full_path(state.list[state.selected], state.directory)
184+
local path = fb.get_full_path(state.list[state.selected], state.directory)
167185

168-
local cursor = favs.get_selected_index()
186+
local cursor = fb.get_selected_index()
169187
if cmd.name == favs:get_id().."/move_up" then
170188
move_favourite(path, -1)
171-
favs.set_selected_index(cursor-1)
189+
fb.set_selected_index(cursor-1)
172190
else
173191
move_favourite(path, 1)
174-
favs.set_selected_index(cursor+1)
192+
fb.set_selected_index(cursor+1)
175193
end
176194
update_browser()
177195
end

0 commit comments

Comments
 (0)