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

Improve intellisense plugin #6682

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 60 additions & 3 deletions .vscode/fa-plugin.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
local StringFind = string.find
local StringGmatch = string.gmatch
local StringMatch = string.match

local IoOpen = io.open

-- repository to use for hook file intellisense. Same as the one in the dev init file.
local initPath = 'C:/ProgramData/FAForever/bin/init_local_development.lua'

local initFile, err = IoOpen(initPath, 'r')
local locationOfRepository
if initFile then
for line in initFile:lines() do
local start, finish, repoPath = StringFind(line, 'locationOfRepository = [\'"]([%w:/]+)[\'"]')
if repoPath then
locationOfRepository = repoPath
print('[FA Plugin] Using repository path: ' .. repoPath)
break
end
end
if not locationOfRepository then
print('[FA Plugin] Could not find repository path in init file:' .. initPath)
end
else
print('[FA plugin] Could not open init file: ' .. tostring(initPath)
.. '\n[FA plugin] Error message: ' .. tostring(err)
)
end

---@class diff
---@field start integer # The number of bytes at the beginning of the replacement
---@field finish integer # The number of bytes at the end of the replacement
Expand All @@ -7,22 +36,50 @@
---@param text string # The content of file
---@return nil|diff[]
function OnSetText(uri, text)
---@type diff[]
local diffs = {}

local pos = text:match('^%s*()#')
-- Change `#` (valid Supcom Lua comment) to `--` (valid in language server's lua)
-- get first line
local pos = StringMatch(text, '^%s*()#')
if pos ~= nil then
diffs[#diffs + 1] = {
start = pos,
finish = pos,
text = '--'
}
end
for pos in text:gmatch '\r\n%s*()#' do
-- get any lines after that
for pos in StringGmatch(text, '\r\n%s*()#') do
diffs[#diffs + 1] = {
start = pos,
finish = pos,
text = '--'
}
end
-- Change `{&1 &1}` (valid Supcom Lua) to `{}` (valid in language server's lua)
-- It's changed inside comments too, but lua regex doesn't make that easy to fix.
for start, finish in StringGmatch(text, '(){&%d+ &%d+}()') do
diffs[#diffs + 1] = {
start = start,
finish = finish - 1,
text = '{}',
}
end

if locationOfRepository then
-- prepend the content of hooked files just like the game would
local first, last, hookDir = StringFind(uri, '/hook(/[%w/]+.lua)')
if hookDir then
local repoFile = IoOpen(locationOfRepository .. hookDir)
if repoFile then
diffs[#diffs + 1] = {
start = 1,
finish = 1,
text = repoFile:read("*a")
}
end
end
end

return diffs
end
1 change: 1 addition & 0 deletions changelog/snippets/fix.6682.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- (#6682) Fix pre-allocated table declaration causing syntax errors in intellisense.
3 changes: 3 additions & 0 deletions changelog/snippets/other.6682.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- (#6682) Add intellisense support for hooking files that are from the repository.

The simplest way to use it is to copy the setting `"Lua.runtime.plugin": ".vscode/fa-plugin.lua",` into your workspace settings and the file `fa-plugin.lua` into your `.vscode` folder.
1 change: 0 additions & 1 deletion lua/lazyvar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ function Create(initial)
end

---@type LazyVar
---@diagnostic disable-next-line:assign-type-mismatch,miss-symbol,exp-in-action,unknown-symbol
local result = {&1 &8} -- preallocate table with hashsize=1, arraysize=8
setmetatable(result, LazyVarMetaTable)
result[1] = initial
Expand Down
Loading