Skip to content

Commit 99a27db

Browse files
authored
Merge pull request zyedidia#2962 from JoeKar/fix/linter-ft-relation
plugin: Add new `onBufferOptionChanged` callback
2 parents 58d38af + d1f54ea commit 99a27db

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

internal/buffer/settings.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"reflect"
66

77
"github.com/zyedidia/micro/v2/internal/config"
8+
ulua "github.com/zyedidia/micro/v2/internal/lua"
89
"github.com/zyedidia/micro/v2/internal/screen"
10+
luar "layeh.com/gopher-luar"
911
)
1012

1113
func (b *Buffer) ReloadSettings(reloadFiletype bool) {
@@ -46,7 +48,8 @@ func (b *Buffer) ReloadSettings(reloadFiletype bool) {
4648
}
4749

4850
func (b *Buffer) DoSetOptionNative(option string, nativeValue interface{}) {
49-
if reflect.DeepEqual(b.Settings[option], nativeValue) {
51+
oldValue := b.Settings[option]
52+
if reflect.DeepEqual(oldValue, nativeValue) {
5053
return
5154
}
5255

@@ -117,6 +120,12 @@ func (b *Buffer) DoSetOptionNative(option string, nativeValue interface{}) {
117120
if b.OptionCallback != nil {
118121
b.OptionCallback(option, nativeValue)
119122
}
123+
124+
if err := config.RunPluginFn("onBufferOptionChanged",
125+
luar.New(ulua.L, b), luar.New(ulua.L, option),
126+
luar.New(ulua.L, oldValue), luar.New(ulua.L, nativeValue)); err != nil {
127+
screen.TermMessage(err)
128+
}
120129
}
121130

122131
func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error {

runtime/help/plugins.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ that micro defines:
6262
* `onBufferOpen(buf)`: runs when a buffer is opened. The input contains
6363
the buffer object.
6464

65+
* `onBufferOptionChanged(buf, option, old, new)`: runs when an option of the
66+
buffer has changed. The input contains the buffer object, the option name,
67+
the old and the new value.
68+
6569
* `onBufPaneOpen(bufpane)`: runs when a bufpane is opened. The input
6670
contains the bufpane object.
6771

runtime/plugins/linter/linter.lua

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function preinit()
6666
end
6767

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

110+
function checkFtMatch(ft, v)
111+
local ftmatch = ft == v.filetype
112+
if v.domatch then
113+
ftmatch = string.match(ft, v.filetype)
114+
end
115+
116+
local hasOS = contains(v.os, runtime.GOOS)
117+
if not hasOS and v.whitelist then
118+
ftmatch = false
119+
end
120+
if hasOS and not v.whitelist then
121+
ftmatch = false
122+
end
123+
return ftmatch
124+
end
125+
110126
function runLinter(buf)
111127
local ft = buf:FileType()
112128
local file = buf.Path
113129
local dir = "." .. util.RuneStr(os.PathSeparator) .. filepath.Dir(file)
114130

115131
for k, v in pairs(linters) do
116-
local ftmatch = ft == v.filetype
117-
if v.domatch then
118-
ftmatch = string.match(ft, v.filetype)
119-
end
120-
121-
local hasOS = contains(v.os, runtime.GOOS)
122-
if not hasOS and v.whitelist then
123-
ftmatch = false
124-
end
125-
if hasOS and not v.whitelist then
126-
ftmatch = false
127-
end
128-
129-
if ftmatch then
132+
if checkFtMatch(ft, v) then
130133
local args = {}
131134
for k, arg in pairs(v.args) do
132135
args[k] = arg:gsub("%%f", file):gsub("%%d", dir)
@@ -141,6 +144,19 @@ function onSave(bp)
141144
return true
142145
end
143146

147+
function onBufferOptionChanged(buf, option, old, new)
148+
if option == "filetype" then
149+
if old ~= new then
150+
for k, v in pairs(linters) do
151+
if checkFtMatch(old, v) then
152+
buf:ClearMessages(k)
153+
end
154+
end
155+
end
156+
end
157+
return true
158+
end
159+
144160
function lint(buf, linter, cmd, args, errorformat, loff, coff, callback)
145161
buf:ClearMessages(linter)
146162

0 commit comments

Comments
 (0)