Repository: zyedidia/micro
File: internal/buffer/buffer.go (Go)

Code before:
				break
			} else {
				syntaxFiles = append(syntaxFiles, SyntaxFileBuffer{header, f.Name(), syndef})
			}
		}
	}

	if !foundDef {
		// search in the default syntax files
		for _, f := range config.ListRuntimeFiles(config.RTSyntaxHeader) {
			data, err := f.Data()
			if err != nil {
				screen.TermMessage("Error loading syntax header file " + f.Name() + ": " + err.Error())
				continue
			}

			header, err = highlight.MakeHeader(data)
			if err != nil {
				screen.TermMessage("Error reading syntax header file", f.Name(), err)
				continue
			}

			if ft == "unknown" || ft == "" {
				if highlight.MatchFileType(header.FtDetect, b.Path) {
					syntaxFiles = append(syntaxFiles, SyntaxFileBuffer{header, f.Name(), nil})
				}
			} else if header.FileType == ft {
				syntaxFile = f.Name()
				break
			}
		}
	}

	if syntaxFile == "" {
		length := len(syntaxFiles)
		if 1 < length {
			signatureMatch := false
			for i := 0; i < length && signatureMatch == false; i++ {
				if highlight.HasFileSignature(syntaxFiles[i].Header.FtDetect) {
					lineCount := len(b.lines)
					for j := 0; j < lineCount && j < 100 && signatureMatch == false; j++ {
						if highlight.MatchFileSignature(syntaxFiles[i].Header.FtDetect, b.lines[j].data) {
							syntaxFile = syntaxFiles[i].FileName
							b.SyntaxDef = syntaxFiles[i].SyntaxDef
							header = syntaxFiles[i].Header
							signatureMatch = true
						}
					}
				}
			}
			if !signatureMatch {

Review comment (suggestion):
IIUC adding this `if` is a separate logical change, in particular it makes user's custom syntax files take precedence over built-in syntax files (so this change is useful even regardless of the rest of the PR). Am I correct?

I'd suggest to add this `if` in a separate commit (prior to this commit), to make the PR easier to review.

Code after:
		}
		file, err := highlight.ParseFile(data)
		if err != nil {
			screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
			continue
		}

		if ((ft == "unknown" || ft == "") && header.MatchFileName(b.Path)) || header.FileType == ft {
			syndef, err := highlight.ParseDef(file, header)
			if err != nil {
				screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
				continue
			}
			foundDef = true

			if header.FileType == ft {
				b.SyntaxDef = syndef
				syntaxFile = f.Name()
				break
			} else {
				syntaxFiles = append(syntaxFiles, syntaxFileBuffer{header, f.Name(), syndef})
			}
		}
	}

	if !foundDef {
		// search in the default syntax files
		for _, f := range config.ListRuntimeFiles(config.RTSyntaxHeader) {
			data, err := f.Data()
			if err != nil {
				screen.TermMessage("Error loading syntax header file " + f.Name() + ": " + err.Error())
				continue
			}

			header, err = highlight.MakeHeader(data)
			if err != nil {
				screen.TermMessage("Error reading syntax header file", f.Name(), err)
				continue
			}

			if ft == "unknown" || ft == "" {
				if header.MatchFileName(b.Path) {
					syntaxFiles = append(syntaxFiles, syntaxFileBuffer{header, f.Name(), nil})
				}
			} else if header.FileType == ft {
				syntaxFile = f.Name()
				break
			}
		}
	}