diff --git a/cmd/micro/buffer.go b/cmd/micro/buffer.go index a4ba7922..c505ca66 100644 --- a/cmd/micro/buffer.go +++ b/cmd/micro/buffer.go @@ -39,8 +39,6 @@ type Buffer struct { // Syntax highlighting rules rules []SyntaxRule - // The buffer's filetype - FileType string // Buffer local settings Settings map[string]interface{} @@ -78,6 +76,7 @@ func NewBuffer(txt []byte, path string) *Buffer { b.EventHandler = NewEventHandler(b) b.Update() + b.FindFileType() b.UpdateRules() if _, err := os.Stat(configDir + "/buffers/"); os.IsNotExist(err) { @@ -134,7 +133,17 @@ func NewBuffer(txt []byte, path string) *Buffer { // UpdateRules updates the syntax rules and filetype for this buffer // This is called when the colorscheme changes func (b *Buffer) UpdateRules() { - b.rules, b.FileType = GetRules(b) + b.rules = GetRules(b) +} + +// FindFileType identifies this buffer's filetype based on the extension or header +func (b *Buffer) FindFileType() { + b.Settings["filetype"] = FindFileType(b) +} + +// FileType returns the buffer's filetype +func (b *Buffer) FileType() string { + return b.Settings["filetype"].(string) } // CheckModTime makes sure that the file this buffer points to hasn't been updated diff --git a/cmd/micro/highlighter.go b/cmd/micro/highlighter.go index f636d87a..b59b654f 100644 --- a/cmd/micro/highlighter.go +++ b/cmd/micro/highlighter.go @@ -389,19 +389,29 @@ func LoadRulesFromFile(text, filename string) []SyntaxRule { return rules } -// GetRules finds the syntax rules that should be used for the buffer -// and returns them. It also returns the filetype of the file -func GetRules(buf *Buffer) ([]SyntaxRule, string) { +// FindFileType finds the filetype for the given buffer +func FindFileType(buf *Buffer) string { for r := range syntaxFiles { if r[0] != nil && r[0].MatchString(buf.Path) { - // Check if the syntax statement matches the extension - return LoadRulesFromFile(syntaxFiles[r].text, syntaxFiles[r].filename), syntaxFiles[r].filetype + // The syntax statement matches the extension + return syntaxFiles[r].filetype } else if r[1] != nil && r[1].MatchString(buf.Line(0)) { - // Check if the header statement matches the first line - return LoadRulesFromFile(syntaxFiles[r].text, syntaxFiles[r].filename), syntaxFiles[r].filetype + // The header statement matches the first line + return syntaxFiles[r].filetype } } - return nil, "Unknown" + return "Unknown" +} + +// GetRules finds the syntax rules that should be used for the buffer +// and returns them. It also returns the filetype of the file +func GetRules(buf *Buffer) []SyntaxRule { + for r := range syntaxFiles { + if syntaxFiles[r].filetype == buf.FileType() { + return LoadRulesFromFile(syntaxFiles[r].text, syntaxFiles[r].filename) + } + } + return nil } // SyntaxMatches is an alias to a map from character numbers to styles, @@ -425,7 +435,7 @@ func Match(v *View) SyntaxMatches { for i, line := range lines { matches[i] = make([]tcell.Style, len(line)+1) - for j, _ := range matches[i] { + for j := range matches[i] { matches[i][j] = defStyle } } diff --git a/cmd/micro/settings.go b/cmd/micro/settings.go index e08c49b9..a88f99be 100644 --- a/cmd/micro/settings.go +++ b/cmd/micro/settings.go @@ -86,6 +86,7 @@ func DefaultSettings() map[string]interface{} { "autoindent": true, "colorscheme": "monokai", "cursorline": false, + "filetype": "Unknown", "ignorecase": false, "indentchar": " ", "ruler": true, diff --git a/cmd/micro/statusline.go b/cmd/micro/statusline.go index 6ed86362..c5e7e406 100644 --- a/cmd/micro/statusline.go +++ b/cmd/micro/statusline.go @@ -34,7 +34,7 @@ func (sline *Statusline) Display() { file += " (" + lineNum + "," + columnNum + ")" // Add the filetype - file += " " + sline.view.Buf.FileType + file += " " + sline.view.Buf.FileType() rightText := helpBinding + " for help " if sline.view.Help {