How to force settings plugin save outside of settings itself? #1623
-
How to force settings plugin save outside of settings itself? Any idea? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
--
-- If has settings of plugins
if settings.config["plugins"] then
--
-- Create for ths plugin
settings.config["plugins"]["amxx"] = {compiler_path = path}
end |
Beta Was this translation helpful? Give feedback.
-
For the sake of completeness, there are many ways to write to a file on config change: 1. write to a file when plugin loadscore.add_thread(function()
...
end) This function will execute once the editor is set up, but will not be run again when user edits the init file. 2. Make the user write to the configYou can make the user call a function in the user init to write your config files. This is how its done some editors like Neovim, but some might not prefer this approach. 3. Use settings GUI callbackFor installs with the settings GUI, you can specify |
Beta Was this translation helpful? Give feedback.
-
I guess i not explained well, i need that settings plugin force save the own settings. I mean, i making a compiler script for pawn language compiler at CTRL+B key. But user most define where compiler is first. If user do not do that, the lite-xl will ask for compiler path, and when user put that setting the path should save on user_settings.lua. The code-- mod-version:3
--
-- Require
local core = require("core")
local common = require("core.common")
local config = require("core.config")
local command = require("core.command")
local doc = require("core.doc")
local view = require("core.view")
local keymap = require("core.keymap")
local rootView = require("core.rootview")
local console = require("plugins.console")
local settings = require("plugins.settings")
--
-- Plugin Settings
config.plugins.amxx = common.merge
({
--
-- Compiler Path
compiler_path = "",
--
-- Compiler include path
include = "include",
--
-- Compiler executable name
compiler = "amxxpc.exe",
--
-- Plugin output extension
extension = "amxx",
--
-- Plugin extra output folder
output = "",
--
-- Save current document on compile command
save = true,
--
-- Clear console output before each execution
clear = true,
--
-- The config specification used by the settings gui
config_spec =
{
--
-- Setting Label
name = "AMX Mod X Compiler",
--
-- Compiler Path
{
label = "AMX Mod X compiler folder",
description = "Folder of AMX Mod X compiler",
path = "compiler_path",
type = "directory",
default = ""
},
--
-- Compiler include path name
{
label = "AMX Mod X name of 'include' folder",
description = "Name of 'include' folder",
path = "include",
type = "string",
default = "include"
},
--
-- Compiler executable name
{
label = "AMX Mod X compiler executable",
description = "Name of compiler executable",
path = "compiler",
type = "string",
default = "amxxpc.exe"
},
--
-- Plugin output extension
{
label = "AMX Mod X output extension",
description = "Extension of output file (plugin)",
path = "extension",
type = "string",
default = "amxx"
},
--
-- Plugin extra output folder
{
label = "Copy plugin to specific path",
description = "Path to copy result plugin",
path = "output",
type = "directory",
exists = true,
default = ""
},
--
-- Save current document on compile command
{
label = "Save current document on compile",
description = "Save current document before compile command",
path = "save",
type = "boolean",
default = true
},
--
-- Clear console output before each execution
{
label = "Clear output before each execution",
description = "Clear output before each execution",
path = "clear",
type = "boolean",
default = true
},
}
}, config.plugins.amxx)
--
-- On view mouse pressed
local amxx_on_mouse_pressed = view.on_mouse_pressed
--
-- On view mouse pressed callback
function view:on_mouse_pressed(button, x, y, clicks)
--
-- Store result of original function
local result = amxx_on_mouse_pressed(self, button, x, y, clicks)
--
-- If current mouse click is not in console
if self:get_name() ~= "Console" and console.isVisible() then
--
-- Close console
console.close()
end
--
-- Return result
return result
end
--
-- Ask for compiler path
local amxx_ask_compiler_path = function()
--
-- Show nagview message
core.nag_view:show("AMX Mod X compiler not found", "AMX Mod X compiler not found, did you want to choose path?",
{
{text = "Yes", default_yes = true},
{text = "No", default_no = false}
}, function(item)
--
-- If yes
if item.text == "Yes" then
--
-- Create thread, the nagview is designed that
core.add_thread(function()
--
-- Try to get that path and set
core.command_view:enter("Specify the AMX Mod X Compiler path",
{
--
-- On submit
submit = function(text)
--
-- Get path
local path = system.absolute_path(text)
--
-- If path is not found, log error
if system.get_file_info(path) == nil then
--
-- Error message
core.error(string.format("Specified path not found: %s.",path))
else
--
-- Set path on current plugin
config.plugins.amxx.compiler_path = path
--
-- If has settings of plugins
if settings.config["plugins"] then
--
-- Create for ths plugin
settings.config["plugins"]["amxx"] = {compiler_path = path}
end
--
-- Reload settings plugin to get new config loaded and saved
settings.ui:new()
--
-- Try to build
command.perform("amxx:build-plugin")
end
end,
--
-- Suggest directory for amxx compiler finder
suggest = function(text)
-- Expand home directory from text
text = common.home_expand(text)
--
-- Get base dir from project directory
local basedir = common.dirname(core.project_dir)
--
-- Return encoded list with directory fround and recent suggestions
return common.home_encode_list((basedir and text == basedir .. PATHSEP or text == "") and core.recent_projects or common.dir_path_suggest(text))
end
})
end)
end
end)
end
--
-- Build plugin function
local amxx_build_plugin_func = function()
--
-- If is an .sma file
if core.active_view.doc.filename and string.match(core.active_view.doc.filename, "%.sma$") then
--
-- AMX Mod X compiler executable
local executablePath = system.absolute_path(config.plugins.amxx.compiler_path .. PATHSEP .. config.plugins.amxx.compiler)
---
-- Check executable before try to compile
if system.get_file_info(executablePath) == nil then
--
-- Show error message
core.error("AMX Mod X Compiler: Compiler not found: " .. executablePath)
--
-- Call for ask for compiler path
amxx_ask_compiler_path()
else
--
-- AMX Mod X binary default output file path (Convert old .sma extension to .amxx)
local outputPath = string.format("%s.%s", core.active_view.doc.abs_filename:match("(.+)%..+"), config.plugins.amxx.extension)
--
-- If output path setting is not empty
if config.plugins.amxx.output and config.plugins.amxx.output ~= "" then
--
-- Construct output path based on path specified
outputPath = config.plugins.amxx.output .. PATHSEP .. string.format("%s.%s", common.basename(core.active_view.doc.filename):match("(.+)%..+"), config.plugins.amxx.extension)
end
--
-- AMX Mod X compiler include
local includePath = config.plugins.amxx.path .. PATHSEP .. config.plugins.amxx.include
--
-- AMX Mod X compiler command line
local compileCommand = string.format("%s -i%s -o%s %s", executablePath, includePath, outputPath, core.active_view.doc.abs_filename)
--
-- If compilercommand is null
if compileCommand == nil then
--
-- Send error message
core.error("AMX Mod X Compiler: Command is empty.")
else
--
-- Save current file each run
if config.plugins.amxx.save == true then
--
-- Save
core.active_view.doc:save()
end
--
-- Clear console on each run
if config.plugins.amxx.clear == true then
--
-- Clear console
console.clear();
end
--
-- Run console command
console.run
{
--
-- Command
command = compileCommand,
--
-- Output warning / errors pattern
file_pattern = "(.*)%((.*)%) : (.*)",
--
-- File prefix separator
file_prefix = ".",
--
-- Current directory of command
cwd = ".",
--
-- Error pattern
error_pattern = "error",
--
-- Warning pattern
warning_pattern = "warning",
--
-- On command end
on_complete = function()
core.log("AMX Mod X Compiler: Compilation Complete")
end
}
end
end
else
--
-- Error message if is not an valid file
core.error("AMX Mod X Compiler: " .. common.basename(core.active_view.doc.filename) .. " is not a AMX Mod X source file")
end
end
--
-- Add commands on document view
command.add("core.docview",
{
--
-- AMXX build command
["amxx:build-plugin"] = amxx_build_plugin_func
})
--
-- Add CTRL + B
keymap.add({["ctrl+b"] = "amxx:build-plugin"}) That solves it for me: The code--
-- Set path on current plugin
config.plugins.amxx.compiler_path = path
--
-- If has settings of plugins
if settings.config["plugins"] then
--
-- Create for ths plugin
settings.config["plugins"]["amxx"] = {compiler_path = path}
end
--
-- Reload settings plugin to get new config loaded and saved
settings.ui:new() Ps.That will set the setting on current plugin, and in settings plugin too. And write on user settings correctly. |
Beta Was this translation helpful? Give feedback.
For the sake of completeness, there are many ways to write to a file on config change:
1. write to a file when plugin loads
This function will execute once the editor is set up, but will not be run again when user edits the init file.
2. Make the user write to the config
You can make the user call a function in the user init to write your config files. This is how its done some editors like Neovim, but some might not prefer this approach.
3. Use settings GUI callback
For installs with the settings GUI, you can specify
on_apply
callback to run something when a setting is changed via the GUI. This of course will not apply to settings changed outside of …