Skip to content

Commit 0806add

Browse files
authored
Merge pull request zyedidia#2933 from JoeKar/feature/default-syntax
syntax: Provide default.yaml as fallback definition
2 parents 8d8bc58 + 6cd39ef commit 0806add

File tree

4 files changed

+108
-39
lines changed

4 files changed

+108
-39
lines changed

cmd/micro/micro_test.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ func handleEvent() {
109109
if e != nil {
110110
screen.Events <- e
111111
}
112-
DoEvent()
112+
113+
for len(screen.DrawChan()) > 0 || len(screen.Events) > 0 {
114+
DoEvent()
115+
}
113116
}
114117

115118
func injectKey(key tcell.Key, r rune, mod tcell.ModMask) {
@@ -151,6 +154,16 @@ func openFile(file string) {
151154
injectKey(tcell.KeyEnter, rune(tcell.KeyEnter), tcell.ModNone)
152155
}
153156

157+
func findBuffer(file string) *buffer.Buffer {
158+
var buf *buffer.Buffer
159+
for _, b := range buffer.OpenBuffers {
160+
if b.Path == file {
161+
buf = b
162+
}
163+
}
164+
return buf
165+
}
166+
154167
func createTestFile(name string, content string) (string, error) {
155168
testf, err := ioutil.TempFile("", name)
156169
if err != nil {
@@ -190,14 +203,7 @@ func TestSimpleEdit(t *testing.T) {
190203

191204
openFile(file)
192205

193-
var buf *buffer.Buffer
194-
for _, b := range buffer.OpenBuffers {
195-
if b.Path == file {
196-
buf = b
197-
}
198-
}
199-
200-
if buf == nil {
206+
if findBuffer(file) == nil {
201207
t.Errorf("Could not find buffer %s", file)
202208
return
203209
}
@@ -236,6 +242,11 @@ func TestMouse(t *testing.T) {
236242

237243
openFile(file)
238244

245+
if findBuffer(file) == nil {
246+
t.Errorf("Could not find buffer %s", file)
247+
return
248+
}
249+
239250
// buffer:
240251
// base content
241252
// the selections need to happen at different locations to avoid a double click
@@ -299,6 +310,11 @@ func TestSearchAndReplace(t *testing.T) {
299310

300311
openFile(file)
301312

313+
if findBuffer(file) == nil {
314+
t.Errorf("Could not find buffer %s", file)
315+
return
316+
}
317+
302318
injectKey(tcell.KeyCtrlE, rune(tcell.KeyCtrlE), tcell.ModCtrl)
303319
injectString(fmt.Sprintf("replaceall %s %s", "foo", "test_string"))
304320
injectKey(tcell.KeyEnter, rune(tcell.KeyEnter), tcell.ModNone)

internal/buffer/buffer.go

Lines changed: 73 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,64 @@ func calcHash(b *Buffer, out *[md5.Size]byte) error {
682682
return nil
683683
}
684684

685+
func parseDefFromFile(f config.RuntimeFile, header *highlight.Header) *highlight.Def {
686+
data, err := f.Data()
687+
if err != nil {
688+
screen.TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
689+
return nil
690+
}
691+
692+
if header == nil {
693+
header, err = highlight.MakeHeaderYaml(data)
694+
if err != nil {
695+
screen.TermMessage("Error parsing header for syntax file " + f.Name() + ": " + err.Error())
696+
return nil
697+
}
698+
}
699+
700+
file, err := highlight.ParseFile(data)
701+
if err != nil {
702+
screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
703+
return nil
704+
}
705+
706+
syndef, err := highlight.ParseDef(file, header)
707+
if err != nil {
708+
screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
709+
return nil
710+
}
711+
712+
return syndef
713+
}
714+
715+
// findRealRuntimeSyntaxDef finds a specific syntax definition
716+
// in the user's custom syntax files
717+
func findRealRuntimeSyntaxDef(name string, header *highlight.Header) *highlight.Def {
718+
for _, f := range config.ListRealRuntimeFiles(config.RTSyntax) {
719+
if f.Name() == name {
720+
syndef := parseDefFromFile(f, header)
721+
if syndef != nil {
722+
return syndef
723+
}
724+
}
725+
}
726+
return nil
727+
}
728+
729+
// findRuntimeSyntaxDef finds a specific syntax definition
730+
// in the runtime files
731+
func findRuntimeSyntaxDef(name string, header *highlight.Header) *highlight.Def {
732+
for _, f := range config.ListRuntimeFiles(config.RTSyntax) {
733+
if f.Name() == name {
734+
syndef := parseDefFromFile(f, header)
735+
if syndef != nil {
736+
return syndef
737+
}
738+
}
739+
}
740+
return nil
741+
}
742+
685743
// UpdateRules updates the syntax rules and filetype for this buffer
686744
// This is called when the colorscheme changes
687745
func (b *Buffer) UpdateRules() {
@@ -710,6 +768,10 @@ func (b *Buffer) UpdateRules() {
710768
var header *highlight.Header
711769
// search for the syntax file in the user's custom syntax files
712770
for _, f := range config.ListRealRuntimeFiles(config.RTSyntax) {
771+
if f.Name() == "default" {
772+
continue
773+
}
774+
713775
data, err := f.Data()
714776
if err != nil {
715777
screen.TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
@@ -766,7 +828,7 @@ func (b *Buffer) UpdateRules() {
766828
}
767829

768830
if !foundDef {
769-
// search in the default syntax files
831+
// search for the syntax file in the runtime files
770832
for _, f := range config.ListRuntimeFiles(config.RTSyntaxHeader) {
771833
data, err := f.Data()
772834
if err != nil {
@@ -844,29 +906,7 @@ func (b *Buffer) UpdateRules() {
844906

845907
if syntaxFile != "" && !foundDef {
846908
// we found a syntax file using a syntax header file
847-
for _, f := range config.ListRuntimeFiles(config.RTSyntax) {
848-
if f.Name() == syntaxFile {
849-
data, err := f.Data()
850-
if err != nil {
851-
screen.TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
852-
continue
853-
}
854-
855-
file, err := highlight.ParseFile(data)
856-
if err != nil {
857-
screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
858-
continue
859-
}
860-
861-
syndef, err := highlight.ParseDef(file, header)
862-
if err != nil {
863-
screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
864-
continue
865-
}
866-
b.SyntaxDef = syndef
867-
break
868-
}
869-
}
909+
b.SyntaxDef = findRuntimeSyntaxDef(syntaxFile, header)
870910
}
871911

872912
if b.SyntaxDef != nil && highlight.HasIncludes(b.SyntaxDef) {
@@ -876,9 +916,10 @@ func (b *Buffer) UpdateRules() {
876916
for _, f := range config.ListRuntimeFiles(config.RTSyntax) {
877917
data, err := f.Data()
878918
if err != nil {
879-
screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
919+
screen.TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
880920
continue
881921
}
922+
882923
header, err := highlight.MakeHeaderYaml(data)
883924
if err != nil {
884925
screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
@@ -907,9 +948,14 @@ func (b *Buffer) UpdateRules() {
907948
if b.Highlighter == nil || syntaxFile != "" {
908949
if b.SyntaxDef != nil {
909950
b.Settings["filetype"] = b.SyntaxDef.FileType
951+
} else {
952+
// search for the default file in the user's custom syntax files
953+
b.SyntaxDef = findRealRuntimeSyntaxDef("default", nil)
954+
if b.SyntaxDef == nil {
955+
// search for the default file in the runtime files
956+
b.SyntaxDef = findRuntimeSyntaxDef("default", nil)
957+
}
910958
}
911-
} else {
912-
b.SyntaxDef = &highlight.EmptyDef
913959
}
914960

915961
if b.SyntaxDef != nil {

pkg/highlight/highlighter.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ func combineLineMatch(src, dst LineMatch) LineMatch {
6767
// A State represents the region at the end of a line
6868
type State *region
6969

70-
// EmptyDef is an empty definition.
71-
var EmptyDef = Def{nil, &rules{}}
72-
7370
// LineStates is an interface for a buffer-like object which can also store the states and matches for every line
7471
type LineStates interface {
7572
LineBytes(n int) []byte

runtime/syntax/default.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
filetype: unknown
2+
3+
detect:
4+
filename: ""
5+
6+
rules:
7+
# Mails
8+
- special: "[[:alnum:].%_+-]+@[[:alnum:].-]+"
9+
# URLs
10+
- identifier: "(https?|ftp|ssh)://\\S*[^])>\\s,.]"

0 commit comments

Comments
 (0)