LuaLS Setup for CityRP Development
LuaLS (Lua Language Server) provides advanced IDE features for Lua development including syntax highlighting, error detection, auto-completion, and type checking. This guide covers setting up LuaLS for CityRP development.
Overview
LuaLS offers:
- Syntax Highlighting: Advanced Lua syntax highlighting
- Error Detection: Real-time error detection and warnings
- Auto-completion: Intelligent code completion
- Type Checking: Static type analysis with annotations
- Function Signatures: Parameter hints and documentation
- Go to Definition: Navigate to function/variable definitions
- Hover Information: Documentation on hover
Installation
Step 1: Install Visual Studio Code
- Download Visual Studio Code from https://code.visualstudio.com/
- Install VS Code following the standard installation process
- Launch VS Code
Step 2: Install LuaLS Extension
Open Extensions Panel:
Press
Ctrl+Shift+X(Windows/Linux) orCmd+Shift+X(Mac)Or click the Extensions icon in the sidebar
Search for LuaLS:
Search for "LuaLS" or "sumneko.lua"
Look for the extension by "sumneko"
Install the Extension:
Click "Install" on the LuaLS extension
Wait for installation to complete
Step 3: Install Garry's Mod Plugin
Open Command Palette:
Press
Ctrl+Shift+P(Windows/Linux) orCmd+Shift+P(Mac)Open Plugin Manager:
Type "LuaLS: Open Plugin Manager"
Select the command from the dropdown
Install Garry's Mod Plugin:
Search for "Garry's Mod" in the plugin manager
Click "Install" on the Garry's Mod plugin
Restart VS Code when prompted
Configuration
Workspace Setup
Open CityRP Workspace:
Open VS Code
File → Open Folder
Navigate to your CityRP gamemode directory
Select the folder containing
cityrp.txtCreate Workspace Settings: Create
.vscode/settings.jsonin your workspace root:
{
"Lua.runtime.version": "LuaJIT",
"Lua.runtime.special": {
"AddCSLuaFile": "dofile",
"cityrp.util.Include": "dofile",
"include": "dofile",
"IncludeCS": "dofile"
},
"Lua.typeFormat.config": {
"auto_complete_end": "true",
"auto_complete_table_sep": "true",
"format_line": "true"
},
"Lua.runtime.nonstandardSymbol": [
"!",
"!=",
"&&",
"||",
"//",
"/**/",
"continue"
],
"Lua.diagnostics.disable": [
"lowercase-global",
"inject-field",
"duplicate-set-field"
],
"Lua.completion.callSnippet": "Both",
"Lua.completion.keywordSnippet": "Both",
"Lua.hint.enable": true,
"Lua.hint.paramType": true,
"Lua.hint.setType": true,
"Lua.hint.await": false,
"Lua.hint.arrayIndex": "Enable",
"Lua.hint.enable": true,
"Lua.codeLens.enable": true,
"Lua.hint.setType": true,
"Lua.type.inferParamType": true,
"Lua.type.weakNilCheck": true,
"Lua.type.weakUnionCheck": true,
"Lua.workspace.preloadFileSize": 500000,
"Lua.completion.displayContext": 5,
"Lua.diagnostics.disableScheme": [
"git",
"vscode",
"github",
".vscode",
".github"
],
"Lua.diagnostics.groupSeverity": {
"type-check": "Information"
},
"Lua.diagnostics.libraryFiles": "Enable"
}
Advanced Configuration
See .luarc.json in your workspace root for team-wide configuration options, these settings should automatically apply to everyone using LuaLS.
Code Annotations and Documentation
LuaLS supports comprehensive type annotations that improve code intelligence and documentation. For a complete list of available annotations and detailed examples, refer to the official LuaLS Annotations Documentation.
Type Annotations
LuaLS supports type annotations that improve code intelligence:
--- @class Character --- @field id integer --- @field firstName string --- @field lastName string local character = {} --- Get character's full name --- @return string fullName function character:GetFullName() return self.firstName .. " " .. self.lastName end --- Set character data --- @param key string --- @param value any --- @param dataType? integer function character:SetData(key, value, dataType) -- Implementation end
Function Documentation
Document functions for better hover information:
--- Creates a new inventory --- @param owner Player|string The owner of the inventory --- @param size integer The size of the inventory --- @param invType string The type of inventory --- @param callback? function Callback when inventory is created --- @return boolean success Whether creation was initiated function cityrp.inventory.Create(owner, size, invType, callback) -- Implementation end
Class Definitions
Define classes for better type checking:
--- @class base_item --- @field name string --- @field description string --- @field cost integer --- @field size integer local ITEM = {} --- @class base_equip : base_item --- @field slot string --- @field dropOnDeath integer local EQUIP_ITEM = {}
Best Practices
File Organization
You should keep the standard gamemode structure within your Garry's Mod directory, for example:
cityrp/
├── .vscode/
│ └── settings.json
├── .luarc.json
├── gamemode/
│ ├── core/
│ │ ├── libraries/
│ │ ├── metatables/
│ │ └── items/
│ └── shared.lua
└── plugins/
└── myplugin/
├── sh_plugin.lua
└── items/
Annotation Guidelines
- Document Public APIs:
```lua --- @public --- @param client Player --- @return boolean success function cityrp.character.LoadAll(client) ```
- Mark Internal Functions:
```lua --- @private --- @param data table function cityrp.character.CreateInternal(data) ```
- Use Generic Types:
```lua
--- @generic T
--- @param base T
--- @return T
function ITEM:New(base)
```
Performance Tips
- Exclude Large Directories:
```json {
"Lua.workspace.ignoreDir": [ ".git", "node_modules", "cache" ]
} ```
- Limit File Scanning:
```json {
"Lua.workspace.maxPreload": 5000, "Lua.workspace.preloadFileSize": 500
} ```
Troubleshooting
Common Issues
LuaLS not recognizing Garry's Mod functions:
- Ensure Garry's Mod plugin is installed and enabled
- Check that globals are properly configured in settings
- Restart VS Code after plugin installation
Performance issues with large codebase:
- Increase
maxPreloadandpreloadFileSizelimits - Use
ignoreDirto exclude unnecessary directories - Consider splitting large files into smaller modules
Type checking errors:
- Add missing globals to configuration
- Use
---@diagnostic disable-next-linefor false positives - Check annotation syntax for typos
Debug Commands
Enable LuaLS logging for troubleshooting:
{
"Lua.misc.parameters": [
"--loglevel=trace",
"--logpath=/path/to/log/directory"
]
}
Reset Configuration
If LuaLS stops working properly:
Clear Cache:
Command Palette → "LuaLS: Clear Cache"
Restart Language Server:
Command Palette → "LuaLS: Restart Server"
Reload Window:
Command Palette → "Developer: Reload Window"
Integration with CityRP Development
Code Completion
LuaLS provides intelligent completion for:
- CityRP API functions (
cityrp.character.Create) - Garry's Mod functions (
hook.Add,timer.Simple) - Player methods (
client:GetCharacter()) - Item system (
ITEM:New(),:AddAction())
Error Detection
Common errors detected:
- Undefined variables and functions
- Type mismatches
- Missing parameters
- Syntax errors
Refactoring Support
LuaLS helps with:
- Renaming variables and functions
- Finding all references
- Go to definition/implementation
- Symbol search across workspace
Additional Resources
- LuaLS Documentation: https://luals.github.io/
- Garry's Mod Wiki: https://wiki.facepunch.com/gmod/
- CityRP Development Guide: See other documentation files in this directory
- GLua API Snippets Plugin Repo: https://github.com/luttje/glua-api-snippets/
Tips for Team Development
Shared Configuration
See .luarc.json within the repository, which should help make things such as the formatter consistent for us all.
Code Style
Use LuaLS formatting features:
- Enable format on save
- Configure indentation and spacing
- Use consistent annotation styles
Documentation Standards
Establish team standards for:
- Function documentation
- Type annotations
- Code organization
- Naming conventions