Skip to content

Commit

Permalink
Add df-luacheck to Travis build.
Browse files Browse the repository at this point in the history
  • Loading branch information
BenLubar committed Jun 11, 2018
1 parent 02b7f21 commit 80ebc37
Show file tree
Hide file tree
Showing 20 changed files with 384 additions and 301 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@
[submodule "scripts2"]
path = scripts
url = ../../DFHack/scripts.git
[submodule "travis/df-luacheck"]
path = travis/df-luacheck
url = ../../mifki/df-luacheck.git
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ cache:
directories:
- $HOME/DF-travis
- $HOME/lua53
- $TRAVIS_BUILD_DIR/travis/df-luacheck/node_modules
addons:
apt:
packages: &default_packages
Expand All @@ -31,6 +32,7 @@ before_install:
- pip install --user "sphinx==1.4" "requests[security]"
- sh travis/build-lua.sh
- sh travis/download-df.sh
- cd travis/df-luacheck && npm install && cd ../..
- echo "export DFHACK_HEADLESS=1" >> "$HOME/.dfhackrc"
script:
- export PATH="$PATH:$HOME/lua53/bin"
Expand All @@ -51,6 +53,7 @@ script:
- cd ..
- cp travis/dfhack_travis.init "$DF_FOLDER"/
- python travis/run-tests.py "$DF_FOLDER"
- travis/df-luacheck/luacheck.sh --verbose .
notifications:
email: false
irc:
Expand Down
3 changes: 2 additions & 1 deletion library/lua/binpatch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ local function load_patch(name)
return nil, 'could not parse: '..line
end

offset, oldv, newv = tonumber(offset,16), tonumber(oldv,16), tonumber(newv,16)
offset, oldv, newv = tonumber(offset,16), tonumber(oldv,16), tonumber(newv,16) --luacheck: retype
if oldv > 255 or newv > 255 then
file:close()
return nil, 'invalid byte values: '..line
Expand Down Expand Up @@ -67,6 +67,7 @@ local function rebase_patch(patch)
return { name = patch.name, old_bytes = nold, new_bytes = nnew }
end

--luacheck: defclass={name:string,old_bytes:'number[]',new_bytes:'number[]'}
BinaryPatch = defclass(BinaryPatch)

BinaryPatch.ATTRS {
Expand Down
2 changes: 1 addition & 1 deletion library/lua/class.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class_obj = class_obj or {}
common_methods = common_methods or {}

-- Forbidden names for class fields and methods.
reserved_names = { super = true, ATTRS = true }
local reserved_names = { super = true, ATTRS = true }

-- Attribute table metatable
attrs_meta = attrs_meta or {}
Expand Down
15 changes: 10 additions & 5 deletions library/lua/dfhack.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ end

-- Error handling

--luacheck: global
safecall = dfhack.safecall
--luacheck: global
curry = dfhack.curry

function dfhack.pcall(f, ...)
Expand Down Expand Up @@ -87,6 +89,7 @@ dfhack.exception.__index = dfhack.exception

-- Module loading

--luacheck: skip
local function find_required_module_arg()
-- require -> module code -> mkmodule -> find_...
if debug.getinfo(4,'f').func == require then
Expand Down Expand Up @@ -243,7 +246,7 @@ function safe_index(obj,idx,...)
if type(idx) == 'number' and (idx < 0 or idx >= #obj) then
return nil
end
obj = obj[idx]
obj = obj[idx] --luacheck: retype
if select('#',...) > 0 then
return safe_index(obj,...)
else
Expand Down Expand Up @@ -486,6 +489,7 @@ end
function dfhack.reqscript(name)
return dfhack.script_environment(name, true)
end
--luacheck: global
reqscript = dfhack.reqscript

function dfhack.script_environment(name, strict)
Expand Down Expand Up @@ -622,7 +626,8 @@ function dfhack.script_help(script_name, extension)
end

local function _run_command(...)
args = {...}
local command = {} --as:__arg
local args = {...}
if type(args[1]) == 'table' then
command = args[1]
elseif #args > 1 and type(args[2]) == 'table' then
Expand Down Expand Up @@ -677,7 +682,7 @@ if dfhack.is_core_context then
if dfhack.filesystem.exists(name) then
dfhack.printerr(perr)
end
elseif safecall(f) then
elseif safecall(f) then --luacheck: skip
if not internal.save_init then
internal.save_init = {}
end
Expand All @@ -687,7 +692,7 @@ if dfhack.is_core_context then

dfhack.onStateChange.DFHACK_PER_SAVE = function(op)
if op == SC_WORLD_LOADED or op == SC_WORLD_UNLOADED then
if internal.save_init then
if internal.save_init then --luacheck: skip
for k,v in ipairs(internal.save_init) do
if v.onUnload then
safecall(v.onUnload)
Expand All @@ -713,7 +718,7 @@ if dfhack.is_core_context then
end
elseif internal.save_init then
for k,v in ipairs(internal.save_init) do
if v.onStateChange then
if v.onStateChange then --luacheck: skip
safecall(v.onStateChange, op)
end
end
Expand Down
49 changes: 37 additions & 12 deletions library/lua/gui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ local FAKE_INPUT_KEYS = {
}

function simulateInput(screen,...)
local keys = {}
local keys = {} --as:df.interface_key[]
local function push_key(arg)
local kv = arg
local kv = arg --as:df.interface_key
if type(arg) == 'string' then
local arg = arg --as:string
kv = df.interface_key[arg]
if kv == nil and not FAKE_INPUT_KEYS[arg] then
error('Invalid keycode: '..arg)
Expand All @@ -33,7 +34,7 @@ function simulateInput(screen,...)
end
end
for i = 1,select('#',...) do
local arg = select(i,...)
local arg = select(i,...) --as:df.interface_key
if arg ~= nil then
local t = type(arg)
if type(arg) == 'table' then
Expand Down Expand Up @@ -83,6 +84,8 @@ function compute_frame_rect(wavail,havail,spec,xgap,ygap)
return mkdims_wh(0,0,wavail,havail)
end

local spec = spec --as:{l:number,t:number}

local sw = wavail - (spec.l or 0) - (spec.r or 0)
local sh = havail - (spec.t or 0) - (spec.b or 0)
local rqw = math.min(sw, (spec.w or sw)+xgap)
Expand All @@ -98,12 +101,17 @@ end

local function parse_inset(inset)
local l,r,t,b

if type(inset) == 'table' then
l,r = inset.l or inset.x, inset.r or inset.x
t,b = inset.t or inset.y, inset.b or inset.y
l = inset.l or inset.x
r = inset.r or inset.x
t = inset.t or inset.y
b = inset.b or inset.y
else
l = inset or 0
t,r,b = l,l,l
t = l
r = l
b = l
end
return l,t,r,b
end
Expand Down Expand Up @@ -133,7 +141,7 @@ end

function getKeyDisplay(code)
if type(code) == 'string' then
code = df.interface_key[code]
code = df.interface_key[code] --luacheck: retype
end
return dscreen.getKeyDisplay(code)
end
Expand All @@ -142,9 +150,11 @@ end
-- Clipped view rectangle object --
-----------------------------------

--luacheck: defclass={x1:number,clip_x1:number,y1:number,clip_y1:number,x2:number,clip_x2:number,y2:number,clip_y2:number,width:number,height:number}
ViewRect = defclass(ViewRect, nil)

function ViewRect:init(args)
local args = args --as:{view_rect:{x1:number,clip_x1:number,y1:number,clip_y1:number,x2:number,clip_x2:number,y2:number,clip_y2:number},rect:{x1:number,y1:number,x2:number,y2:number},clip_rect:{x1:number,y1:number,x2:number,y2:number},clip_view:{clip_x1:number,clip_y1:number,clip_x2:number,clip_y2:number}}
if args.view_rect then
self:assign(args.view_rect)
else
Expand Down Expand Up @@ -194,11 +204,11 @@ end

function ViewRect:viewport(x,y,w,h)
if type(x) == 'table' then
x,y,w,h = x.x1, x.y1, x.width, x.height
x,y,w,h = x.x1, x.y1, x.width, x.height --luacheck: retype
end
local x1,y1 = self.x1+x, self.y1+y
local x2,y2 = x1+w-1, y1+h-1
local vp = {
return ViewRect{
-- Logical viewport
x1 = x1, y1 = y1, x2 = x2, y2 = y2,
width = w, height = h,
Expand All @@ -208,16 +218,17 @@ function ViewRect:viewport(x,y,w,h)
clip_x2 = math.min(self.clip_x2, x2),
clip_y2 = math.min(self.clip_y2, y2),
}
return mkinstance(ViewRect, vp)
end

----------------------------
-- Clipped painter object --
----------------------------

--luacheck: defclass={x:number,y:number,cur_pen:dfhack.pen,cur_key_pen:dfhack.pen,to_map:bool}
Painter = defclass(Painter, ViewRect)

function Painter:init(args)
local args = args --as:{x1:number,x2:number,pen:number,key_pen:number}
self.x = self.x1
self.y = self.y1
self.cur_pen = to_pen(args.pen or COLOR_GREY)
Expand Down Expand Up @@ -246,7 +257,7 @@ function Painter:isValidPos()
end

function Painter:viewport(x,y,w,h)
local vp = ViewRect.viewport(x,y,w,h)
local vp = ViewRect.viewport(x,y,w,h) --as:Painter
vp.cur_pen = self.cur_pen
vp.cur_key_pen = self.cur_key_pen
return mkinstance(Painter, vp):seek(0,0)
Expand Down Expand Up @@ -308,6 +319,14 @@ function Painter:clear()
end

function Painter:fill(x1,y1,x2,y2,pen,bg,bold)
local x1 = x1 --as:number
local y1 = y1 --as:number
local x2 = x2 --as:number
local y2 = y2 --as:number
local pen = pen --as:dfhack.pen
local bg = bg --as:number
local bold = bold --as:bool

if type(x1) == 'table' then
x1, y1, x2, y2, pen, bg, bold = x1.x1, x1.y1, x1.x2, x1.y2, y1, x2, y2
end
Expand Down Expand Up @@ -369,7 +388,7 @@ end
--------------------------
-- Abstract view object --
--------------------------

--luacheck: defclass={frame_rect:{_type:table,x1:number,y1:number,x2:number,y2:number,width:number,height:number},frame_parent_rect:ViewRect,frame_body:ViewRect,subviews:'View[]',view_id:string,render:'anyfunc:Painter'}
View = defclass(View)

View.ATTRS {
Expand Down Expand Up @@ -418,6 +437,7 @@ function View:getMousePos()
end
end

--luacheck: in=ViewRect
function View:computeFrame(parent_rect)
return mkdims_wh(0,0,parent_rect.width,parent_rect.height)
end
Expand All @@ -429,6 +449,7 @@ function View:updateSubviewLayout(frame_body)
end

function View:updateLayout(parent_rect)
local parent_rect = parent_rect --as:ViewRect
if not parent_rect then
parent_rect = self.frame_parent_rect
else
Expand Down Expand Up @@ -470,9 +491,11 @@ function View:render(dc)
self:renderSubviews(sub_dc)
end

--luacheck: in=Painter,View.frame_rect
function View:onRenderFrame(dc,rect)
end

--luacheck: in=Painter
function View:onRenderBody(dc)
end

Expand All @@ -497,6 +520,7 @@ end
-- Base screen object --
------------------------

--luacheck: defclass={_native:df.viewscreen,onShow:'anyfunc:none',onDismiss:'anyfunc:none',onDestroy:'anyfunc:none',onRender:'anyfunc:none',onIdle:'anyfunc:none',onHelp:'anyfunc:none',onGetSelectedUnit:'anyfunc:none',onGetSelectedItem:'anyfunc:none',onGetSelectedJob:'anyfunc:none',onGetSelectedBuilding:'anyfunc:none',onResize:'anyfunc:number,number',onInput:'anyfunc:__keyArray'}
Screen = defclass(Screen, View)

Screen.text_input_mode = false
Expand Down Expand Up @@ -623,6 +647,7 @@ function paint_frame(x1,y1,x2,y2,style,title)
end
end

--luacheck: defclass={frame_title:string,frame_width:number,frame_height:number}
FramedScreen = defclass(FramedScreen, Screen)

FramedScreen.ATTRS{
Expand Down
15 changes: 10 additions & 5 deletions library/lua/gui/dialogs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local utils = require('utils')

local dscreen = dfhack.screen

--luacheck: defclass={on_accept:'anyfunc:none',on_cancel:'anyfunc:none',on_close:'anyfunc:none'}
MessageBox = defclass(MessageBox, gui.FramedScreen)

MessageBox.focus_path = 'MessageBox'
Expand All @@ -34,7 +35,7 @@ function MessageBox:init(info)
end

function MessageBox:getWantedFrameSize()
local label = self.subviews.label
local label = self.subviews.label --as:widgets.Label
local width = math.max(self.frame_width or 0, 20, #(self.frame_title or '') + 4)
return math.max(width, label:getTextWidth()), label:getTextHeight()
end
Expand Down Expand Up @@ -87,6 +88,7 @@ function showYesNoPrompt(title, text, tcolor, on_accept, on_cancel)
}:show()
end

--luacheck: defclass={on_input:'anyfunc:string'}
InputBox = defclass(InputBox, MessageBox)

InputBox.focus_path = 'InputBox'
Expand All @@ -100,6 +102,7 @@ function InputBox:preinit(info)
end

function InputBox:init(info)
local info = info --as:{input:string,input_pen:dfhack.pen}
self:addviews{
widgets.EditField{
view_id = 'edit',
Expand All @@ -112,15 +115,15 @@ end

function InputBox:getWantedFrameSize()
local mw, mh = InputBox.super.getWantedFrameSize(self)
self.subviews.edit.frame.t = mh+1
self.subviews.edit.frame.t = mh + 1 --hint:widgets.EditField
return mw, mh+2
end

function InputBox:onInput(keys)
if keys.SELECT then
self:dismiss()
if self.on_input then
self.on_input(self.subviews.edit.text)
self.on_input(self.subviews.edit.text) --hint:widgets.EditField
end
elseif keys.LEAVESCREEN then
self:dismiss()
Expand All @@ -144,6 +147,7 @@ function showInputPrompt(title, text, tcolor, input, on_input, on_cancel, min_wi
}:show()
end

--luacheck: defclass={select_pen:dfhack.pen,cursor_pen:dfhack.pen,select2_hint:string,on_select:'anyfunc:none',on_select2:'anyfunc:none'}
ListBox = defclass(ListBox, MessageBox)

ListBox.focus_path = 'ListBox'
Expand All @@ -162,10 +166,11 @@ function ListBox:preinit(info)
end

function ListBox:init(info)
local info = info --as:{_type:table,selected:number}
local spen = dfhack.pen.parse(COLOR_CYAN, self.select_pen, nil, false)
local cpen = dfhack.pen.parse(COLOR_LIGHTCYAN, self.cursor_pen or self.select_pen, nil, true)

local list_widget = widgets.List
local list_widget = widgets.List --as:widgets.Widget
if self.with_filter then
list_widget = widgets.FilteredList
end
Expand Down Expand Up @@ -209,7 +214,7 @@ end

function ListBox:getWantedFrameSize()
local mw, mh = InputBox.super.getWantedFrameSize(self)
local list = self.subviews.list
local list = self.subviews.list --as:widgets.List
list.frame.t = mh+1
return math.max(mw, list:getContentWidth()), mh+1+math.min(20,list:getContentHeight())
end
Expand Down
Loading

0 comments on commit 80ebc37

Please sign in to comment.