Add filetype as an option

Still in progress
This commit is contained in:
Zachary Yedidia
2016-08-24 18:29:23 -07:00
parent 8803d93872
commit 348922e1f2
4 changed files with 33 additions and 13 deletions

View File

@@ -39,8 +39,6 @@ type Buffer struct {
// Syntax highlighting rules // Syntax highlighting rules
rules []SyntaxRule rules []SyntaxRule
// The buffer's filetype
FileType string
// Buffer local settings // Buffer local settings
Settings map[string]interface{} Settings map[string]interface{}
@@ -78,6 +76,7 @@ func NewBuffer(txt []byte, path string) *Buffer {
b.EventHandler = NewEventHandler(b) b.EventHandler = NewEventHandler(b)
b.Update() b.Update()
b.FindFileType()
b.UpdateRules() b.UpdateRules()
if _, err := os.Stat(configDir + "/buffers/"); os.IsNotExist(err) { 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 // UpdateRules updates the syntax rules and filetype for this buffer
// This is called when the colorscheme changes // This is called when the colorscheme changes
func (b *Buffer) UpdateRules() { 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 // CheckModTime makes sure that the file this buffer points to hasn't been updated

View File

@@ -389,19 +389,29 @@ func LoadRulesFromFile(text, filename string) []SyntaxRule {
return rules return rules
} }
// GetRules finds the syntax rules that should be used for the buffer // FindFileType finds the filetype for the given buffer
// and returns them. It also returns the filetype of the file func FindFileType(buf *Buffer) string {
func GetRules(buf *Buffer) ([]SyntaxRule, string) {
for r := range syntaxFiles { for r := range syntaxFiles {
if r[0] != nil && r[0].MatchString(buf.Path) { if r[0] != nil && r[0].MatchString(buf.Path) {
// Check if the syntax statement matches the extension // The syntax statement matches the extension
return LoadRulesFromFile(syntaxFiles[r].text, syntaxFiles[r].filename), syntaxFiles[r].filetype return syntaxFiles[r].filetype
} else if r[1] != nil && r[1].MatchString(buf.Line(0)) { } else if r[1] != nil && r[1].MatchString(buf.Line(0)) {
// Check if the header statement matches the first line // The header statement matches the first line
return LoadRulesFromFile(syntaxFiles[r].text, syntaxFiles[r].filename), syntaxFiles[r].filetype 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, // 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 { for i, line := range lines {
matches[i] = make([]tcell.Style, len(line)+1) matches[i] = make([]tcell.Style, len(line)+1)
for j, _ := range matches[i] { for j := range matches[i] {
matches[i][j] = defStyle matches[i][j] = defStyle
} }
} }

View File

@@ -86,6 +86,7 @@ func DefaultSettings() map[string]interface{} {
"autoindent": true, "autoindent": true,
"colorscheme": "monokai", "colorscheme": "monokai",
"cursorline": false, "cursorline": false,
"filetype": "Unknown",
"ignorecase": false, "ignorecase": false,
"indentchar": " ", "indentchar": " ",
"ruler": true, "ruler": true,

View File

@@ -34,7 +34,7 @@ func (sline *Statusline) Display() {
file += " (" + lineNum + "," + columnNum + ")" file += " (" + lineNum + "," + columnNum + ")"
// Add the filetype // Add the filetype
file += " " + sline.view.Buf.FileType file += " " + sline.view.Buf.FileType()
rightText := helpBinding + " for help " rightText := helpBinding + " for help "
if sline.view.Help { if sline.view.Help {