-
Notifications
You must be signed in to change notification settings - Fork 9
Plugin. WIP 1 #75
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
Open
TIMONz1535
wants to merge
4
commits into
luttje:main
Choose a base branch
from
TIMONz1535:plugin-wip1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Plugin. WIP 1 #75
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
-- LuaLS plugin for Garry's Mod Lua API specific features | ||
|
||
local guide = require "parser.guide" | ||
local helper = require "plugins.astHelper" | ||
|
||
|
||
local scriptedScopes = { | ||
{ global = "ENT", folder = "entities" }, | ||
{ global = "SWEP", folder = "weapons" }, | ||
{ global = "EFFECT", folder = "effects" }, | ||
{ global = "TOOL", folder = "weapons/gmod_tool/stools" } | ||
} | ||
|
||
---@param uri string | ||
---@return string?, string? | ||
local function GetScopedClass(uri) | ||
for _, scope in ipairs(scriptedScopes) do | ||
-- gamemode based uri: | ||
-- .../gamemodes/my_gamemode/entities/entities/ent_class_name/any.lua | ||
-- .../gamemodes/my_gamemode/entities/entities/ent_class_name.lua | ||
local class = uri:match("/entities/" .. scope.folder .. "/([^/]+)/?[^/]*%.lua$") | ||
if not class then | ||
-- addon or root based uri: | ||
-- .../addons/my_addon/lua/entities/ent_class_name/any.lua | ||
-- .../addons/my_addon/lua/entities/ent_class_name.lua | ||
class = uri:match("/lua/" .. scope.folder .. "/([^/]+)/?[^/]*%.lua$") | ||
end | ||
if class then | ||
---@cast class string | ||
return scope.global, class | ||
end | ||
end | ||
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 | ||
---@field text string # What to replace | ||
|
||
---@param uri string # The uri of file | ||
---@param text string # The content of file | ||
---@return diff[]? | ||
function OnSetText(uri, text) | ||
---@type diff[] | ||
local diffs = {} | ||
|
||
-- This should be the first diff, so that when the start positions match, it doesn't break the entire file. | ||
-- TODO: Do real line insert if its possible, instead of text patching. | ||
-- Detect "scripted" scope by folder and localize its global table to @class annotation. | ||
local global = GetScopedClass(uri) | ||
if global then | ||
diffs[#diffs+1] = { | ||
start = 1, | ||
finish = 0, | ||
text = "local " .. global .. "\n\n" | ||
} | ||
end | ||
|
||
-- Replace preprocessor keyword DEFINE_BASECLASS. The preprocessor is not smart enough, so this is text patching. | ||
for start, finish in text:gmatch("()DEFINE_BASECLASS()") do | ||
diffs[#diffs+1] = { | ||
---@cast start integer | ||
start = start, | ||
finish = finish - 1, | ||
text = "local BaseClass = baseclass.Get", | ||
} | ||
end | ||
|
||
if #diffs == 0 then | ||
return nil | ||
end | ||
|
||
return diffs | ||
end | ||
|
||
|
||
local dtTypes = { | ||
String = "string", | ||
Bool = "boolean", | ||
Float = "number", | ||
Int = "integer", | ||
Vector = "Vector", | ||
Angle = "Angle", | ||
Entity = "Entity" | ||
} | ||
|
||
---@param ast parser.object | ||
---@param classNode parser.object | ||
---@param source parser.object | ||
---@param group table | ||
---@param isElement boolean | ||
---@return boolean? | ||
local function BindNetworkVar(ast, classNode, source, group, isElement) | ||
TIMONz1535 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
local args = guide.getParams(source) | ||
if not args or #args < (isElement and 4 or 3) then | ||
return | ||
end | ||
|
||
-- TODO: How to check argSelf "luadoc type" instead of double "node type"? | ||
local argSelf = args[1] | ||
local targetSelf = guide.getSelfNode(argSelf) | ||
if not targetSelf then | ||
return | ||
end | ||
if targetSelf.node ~= classNode then | ||
-- auto generated function self after colon | ||
targetSelf = guide.getSelfNode(targetSelf) | ||
if not targetSelf or targetSelf.node ~= classNode then | ||
return | ||
end | ||
end | ||
|
||
local argType = args[2] | ||
local argSlot = args[3] | ||
local argName = args[isElement and 5 or 4] | ||
---@cast argName +? | ||
|
||
-- TODO: How to check argName "luadoc type" instead of "node type"? | ||
if isElement then | ||
local argElement = args[4] | ||
if argSlot.type == "string" and argElement.type == "string" then | ||
argName = argElement | ||
end | ||
else | ||
if argSlot.type == "string" and (not argName or argName.type == "table") then | ||
argName = argSlot | ||
end | ||
end | ||
if not (argType.type == "string" and argName and argName.type == "string") then | ||
return | ||
end | ||
|
||
local dtType = isElement and "number" or dtTypes[argType[1]] | ||
local name = argName[1] ---@cast name string | ||
if not dtType then | ||
return | ||
end | ||
|
||
local ok = helper.addDoc(ast, classNode, "field", ("Set%s fun(self: Entity, value: %s)"):format(name, dtType), group) | ||
if not ok then | ||
return false | ||
end | ||
ok = helper.addDoc(ast, classNode, "field", ("Get%s fun(self: Entity): %s"):format(name, dtType), group) | ||
if not ok then | ||
return false | ||
end | ||
end | ||
|
||
---@param uri string # The uri of file | ||
---@param ast parser.object # The file ast | ||
---@return parser.object? ast | ||
function OnTransformAst(uri, ast) | ||
-- Detect "scripted" scope by folder and localize its global table to @class annotation. | ||
local global, class = GetScopedClass(uri) | ||
if not global then | ||
return | ||
end | ||
|
||
local classNode = ast[1] | ||
if not (classNode and classNode.type == "local" and guide.getKeyName(classNode) == global) then | ||
return | ||
end | ||
|
||
local group = {} | ||
local ok = helper.addClassDoc(ast, classNode, class .. ": " .. global, group) | ||
if not ok then | ||
return | ||
end | ||
|
||
ok = guide.eachSourceType(ast, "call", function (source) | ||
local targetMethod = source.node | ||
local targetName = guide.getKeyName(targetMethod) | ||
if targetName ~= "NetworkVar" and targetName ~= "NetworkVarElement" then | ||
return | ||
end | ||
if guide.getKeyName(source.parent.parent) ~= "SetupDataTables" then | ||
return | ||
end | ||
-- TODO: How to check call self "luadoc type" instead of "node type"? | ||
local targetSelf = targetMethod.node and guide.getSelfNode(targetMethod.node) | ||
if not targetSelf or targetSelf.node ~= classNode then | ||
return | ||
end | ||
return BindNetworkVar(ast, classNode, source, group, targetName == "NetworkVarElement") | ||
end) | ||
if ok == false then | ||
return | ||
end | ||
|
||
return ast | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.