LUA Scripting
Since CodeLite 18.3.0, CodeLite embeds a LUA engine to enable customization and extensibility. Custom actions can be added to CodeLite through menu integrations, allowing developers to extend the IDE's functionality. To facilitate interaction with CodeLite's internal components, a dedicated codelite namespace has been added to the LUA engine.
Complete Example: Selection Info
Objective
This example shows how to add a custom item to the editor context menu. The item shows information about the selected text.
User Workflow:
- User selects text in the editor
- Right-clicks and chooses Selection Info from the context menu
- A message box shows the file, the language, the number of lines and the number of characters of the selection
Implementation
function on_selection_info()
local selected_text = codelite.editor_selection()
-- Early return if no text is selected
if not selected_text or selected_text == "" then
codelite.message_box("Nothing is selected", MessageType.kWarn)
return
end
-- Count the lines of the selection
local _, newlines = string.gsub(selected_text, "\n", "")
local lines = newlines + 1
local info = string.format(
"File: %s\nLanguage: %s\nLines: %d\nCharacters: %d",
codelite.editor_filepath(),
codelite.editor_language(),
lines,
#selected_text
)
codelite.message_box(info, MessageType.kInfo)
end
-- Connect the new action "on_selection_info" to an existing menu
-- The menu ID can be found in the file `rc/menu.xrc` in CodeLite installation.
codelite.add_menu_item(
"editor_right_click", -- The menu ID
"Selection Info", -- The new item's label
on_selection_info -- The action
)
Code Explanation
| Component | Description |
|---|---|
codelite.editor_selection() |
Returns the currently selected text from the active editor |
codelite.editor_filepath() |
Returns the full path of the file in the active editor |
codelite.editor_language() |
Returns the programming language of the current editor context |
codelite.message_box() |
Shows a message box with the given text and type |
codelite.add_menu_item() |
Associates the custom action with an existing menu |
Key Steps:
- Retrieve selected text using
codelite.editor_selection() - Count the lines of the selection
- Build the message with the file path, the language, the lines and the characters
- Show it with
codelite.message_box() - Register the action with the
editor_right_clickmenu
Note: A complete reference to CodeLite's LUA API can be found here
Integration
To integrate this functionality into CodeLite:
- Navigate to Settings → Edit CodeLite's LUA script from the menu bar
- Place the above code in the
codelite.luafile - Save the file
The script is automatically compiled upon saving and becomes immediately available—no additional actions required.