Skip to content

Commit

Permalink
Merge pull request #2962 from JoeKar/fix/linter-ft-relation
Browse files Browse the repository at this point in the history
plugin: Add new `onBufferOptionChanged` callback
  • Loading branch information
JoeKar authored Jan 1, 2025
2 parents 58d38af + d1f54ea commit 99a27db
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
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

0 comments on commit 99a27db

Please sign in to comment.