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

Code before:
							header = syntaxFiles[i].Header
							signatureMatch = true
						}
					}
				}
			}
			if !signatureMatch {
				syntaxFile = syntaxFiles[0].FileName
				b.SyntaxDef = syntaxFiles[0].SyntaxDef
				header = syntaxFiles[0].Header
			}
		} else {
			if 0 < length {
				syntaxFile = syntaxFiles[0].FileName
				b.SyntaxDef = syntaxFiles[0].SyntaxDef
				header = syntaxFiles[0].Header
			}
		}
	}

	if syntaxFile != "" && !foundDef {
		// we found a syntax file using a syntax header file
		for _, f := range config.ListRuntimeFiles(config.RTSyntax) {
			if f.Name() == syntaxFile {
				data, err := f.Data()
				if err != nil {
					screen.TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
					continue
				}

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

				syndef, err := highlight.ParseDef(file, header)
				if err != nil {
					screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
					continue
				}
				b.SyntaxDef = syndef
				break
			}
		}
	}

	if b.SyntaxDef != nil && highlight.HasIncludes(b.SyntaxDef) {
		includes := highlight.GetIncludes(b.SyntaxDef)

		var files []*highlight.File

Review comment (suggestion):
How about reorganizing the code the following way (to avoid duplication of the above 3 lines):

```
length := len(syntaxFiles)
if length > 0 {
	signatureMatch := false
	if length > 1 {
		for i := 0; i < length && !signatureMatch; i++ {
			...
		}
	}
	if length == 1 || !signatureMatch {
		syntaxFile = syntaxFiles[0].FileName
		b.SyntaxDef = syntaxFiles[0].SyntaxDef
		header = syntaxFiles[0].Header
	}
}
```

Code after:
			} else if header.FileType == ft {
				syntaxFile = f.Name()
				break
			}
		}
	}

	if syntaxFile == "" {
		length := len(syntaxFiles)
		if length > 0 {
			signatureMatch := false
			if length > 1 {
				detectlimit := util.IntOpt(b.Settings["detectlimit"])
				lineCount := len(b.lines)
				limit := lineCount
				if detectlimit > 0 && lineCount > detectlimit {
					limit = detectlimit
				}
				for i := 0; i < length && !signatureMatch; i++ {
					if syntaxFiles[i].header.HasFileSignature() {
						for j := 0; j < limit && !signatureMatch; j++ {
							if syntaxFiles[i].header.MatchFileSignature(b.lines[j].data) {
								syntaxFile = syntaxFiles[i].fileName
								b.SyntaxDef = syntaxFiles[i].syntaxDef
								header = syntaxFiles[i].header
								signatureMatch = true
							}
						}
					}
				}
			}
			if length == 1 || !signatureMatch {
				syntaxFile = syntaxFiles[0].fileName
				b.SyntaxDef = syntaxFiles[0].syntaxDef
				header = syntaxFiles[0].header
			}
		}
	}

	if syntaxFile != "" && !foundDef {
		// we found a syntax file using a syntax header file
		for _, f := range config.ListRuntimeFiles(config.RTSyntax) {
			if f.Name() == syntaxFile {
				data, err := f.Data()
				if err != nil {
					screen.TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
					continue
				}

				file, err := highlight.ParseFile(data)
				if err != nil {