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

plugin: Add new onBufferOptionChanged callback #2962

Merged
merged 5 commits into from
Jan 1, 2025
Merged
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
11 changes: 10 additions & 1 deletion internal/buffer/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"reflect"

"github.com/zyedidia/micro/v2/internal/config"
ulua "github.com/zyedidia/micro/v2/internal/lua"
"github.com/zyedidia/micro/v2/internal/screen"
luar "layeh.com/gopher-luar"
)

func (b *Buffer) ReloadSettings(reloadFiletype bool) {
Expand Down Expand Up @@ -46,7 +48,8 @@ func (b *Buffer) ReloadSettings(reloadFiletype bool) {
}

func (b *Buffer) DoSetOptionNative(option string, nativeValue interface{}) {
if reflect.DeepEqual(b.Settings[option], nativeValue) {
oldValue := b.Settings[option]
if reflect.DeepEqual(oldValue, nativeValue) {
return
}

Expand Down Expand Up @@ -117,6 +120,12 @@ func (b *Buffer) DoSetOptionNative(option string, nativeValue interface{}) {
if b.OptionCallback != nil {
b.OptionCallback(option, nativeValue)
}

if err := config.RunPluginFn("onBufferOptionChanged",
luar.New(ulua.L, b), luar.New(ulua.L, option),
luar.New(ulua.L, oldValue), luar.New(ulua.L, nativeValue)); err != nil {
screen.TermMessage(err)
}
}

func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error {
Expand Down
4 changes: 4 additions & 0 deletions runtime/help/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ that micro defines:
* `onBufferOpen(buf)`: runs when a buffer is opened. The input contains
the buffer object.

* `onBufferOptionChanged(buf, option, old, new)`: runs when an option of the
buffer has changed. The input contains the buffer object, the option name,
the old and the new value.

* `onBufPaneOpen(bufpane)`: runs when a bufpane is opened. The input
contains the bufpane object.

Expand Down
46 changes: 31 additions & 15 deletions runtime/plugins/linter/linter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function preinit()
end

makeLinter("gcc", "c", "gcc", {"-fsyntax-only", "-Wall", "-Wextra", "%f"}, "%f:%l:%c:.+: %m")
makeLinter("g++", "c++", "gcc", {"-fsyntax-only","-std=c++14", "-Wall", "-Wextra", "%f"}, "%f:%l:%c:.+: %m")
makeLinter("g++", "c++", "g++", {"-fsyntax-only","-Wall", "-Wextra", "%f"}, "%f:%l:%c:.+: %m")
makeLinter("dmd", "d", "dmd", {"-color=off", "-o-", "-w", "-wi", "-c", "%f"}, "%f%(%l%):.+: %m")
makeLinter("eslint", "javascript", "eslint", {"-f","compact","%f"}, "%f: line %l, col %c, %m")
makeLinter("gobuild", "go", "go", {"build", "-o", devnull, "%d"}, "%f:%l:%c:? %m")
Expand Down Expand Up @@ -107,26 +107,29 @@ function contains(list, element)
return false
end

function checkFtMatch(ft, v)
local ftmatch = ft == v.filetype
if v.domatch then
ftmatch = string.match(ft, v.filetype)
end

local hasOS = contains(v.os, runtime.GOOS)
if not hasOS and v.whitelist then
ftmatch = false
end
if hasOS and not v.whitelist then
ftmatch = false
end
return ftmatch
end

function runLinter(buf)
local ft = buf:FileType()
local file = buf.Path
local dir = "." .. util.RuneStr(os.PathSeparator) .. filepath.Dir(file)

for k, v in pairs(linters) do
local ftmatch = ft == v.filetype
if v.domatch then
ftmatch = string.match(ft, v.filetype)
end

local hasOS = contains(v.os, runtime.GOOS)
if not hasOS and v.whitelist then
ftmatch = false
end
if hasOS and not v.whitelist then
ftmatch = false
end

if ftmatch then
if checkFtMatch(ft, v) then
local args = {}
for k, arg in pairs(v.args) do
args[k] = arg:gsub("%%f", file):gsub("%%d", dir)
Expand All @@ -141,6 +144,19 @@ function onSave(bp)
return true
end

function onBufferOptionChanged(buf, option, old, new)
if option == "filetype" then
if old ~= new then
for k, v in pairs(linters) do
if checkFtMatch(old, v) then
buf:ClearMessages(k)
end
end
end
end
return true
end

function lint(buf, linter, cmd, args, errorformat, loff, coff, callback)
buf:ClearMessages(linter)

Expand Down
Loading