mirror of
https://github.com/felixangell/phi.git
synced 2024-09-11 15:55:25 +03:00
dont seg fault when there is no highlighting info
This commit is contained in:
parent
1e6fc83b89
commit
264ec81fd6
@ -65,10 +65,11 @@ func (c CursorConfig) GetCaretWidth() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type RenderConfig struct {
|
type RenderConfig struct {
|
||||||
Aliased bool
|
Aliased bool
|
||||||
Accelerated bool
|
Accelerated bool
|
||||||
Throttle_Cpu_Usage bool
|
Throttle_Cpu_Usage bool
|
||||||
Always_Render bool
|
Always_Render bool
|
||||||
|
Syntax_Highlighting bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo make this more extendable...
|
// todo make this more extendable...
|
||||||
|
@ -18,6 +18,7 @@ aliased = true
|
|||||||
accelerated = true
|
accelerated = true
|
||||||
throttle_cpu_usage = true
|
throttle_cpu_usage = true
|
||||||
always_render = true
|
always_render = true
|
||||||
|
syntax_highlighting = true
|
||||||
|
|
||||||
[file_associations]
|
[file_associations]
|
||||||
[file_associations.c]
|
[file_associations.c]
|
||||||
|
@ -18,6 +18,7 @@ aliased = true
|
|||||||
accelerated = true
|
accelerated = true
|
||||||
throttle_cpu_usage = true
|
throttle_cpu_usage = true
|
||||||
always_render = true
|
always_render = true
|
||||||
|
syntax_highlighting = true
|
||||||
|
|
||||||
[file_associations]
|
[file_associations]
|
||||||
[file_associations.c]
|
[file_associations.c]
|
||||||
|
@ -18,6 +18,7 @@ aliased = true
|
|||||||
accelerated = true
|
accelerated = true
|
||||||
throttle_cpu_usage = true
|
throttle_cpu_usage = true
|
||||||
always_render = true
|
always_render = true
|
||||||
|
syntax_highlighting = true
|
||||||
|
|
||||||
[file_associations]
|
[file_associations]
|
||||||
[file_associations.c]
|
[file_associations.c]
|
||||||
|
141
gui/buffer.go
141
gui/buffer.go
@ -755,6 +755,78 @@ var ex, ey = 0, 0
|
|||||||
|
|
||||||
var compiledRegex = map[string]*regexp.Regexp{}
|
var compiledRegex = map[string]*regexp.Regexp{}
|
||||||
|
|
||||||
|
func (b *Buffer) syntaxHighlightLine(currLine string) map[int]syntaxRuneInfo {
|
||||||
|
matches := map[int]syntaxRuneInfo{}
|
||||||
|
|
||||||
|
subjects := make([]cfg.SyntaxCriteria, len(b.languageInfo.Syntax))
|
||||||
|
colours := make([]int, len(b.languageInfo.Syntax))
|
||||||
|
|
||||||
|
idx := 0
|
||||||
|
for _, criteria := range b.languageInfo.Syntax {
|
||||||
|
colours[idx] = criteria.Colour
|
||||||
|
subjects[idx] = criteria
|
||||||
|
idx++
|
||||||
|
}
|
||||||
|
|
||||||
|
// HOLY SLOW BATMAN
|
||||||
|
for charIndex := 0; charIndex < len(currLine); charIndex++ {
|
||||||
|
for syntaxIndex, syntax := range subjects {
|
||||||
|
|
||||||
|
if syntax.Pattern != "" {
|
||||||
|
// we have a regex pattern
|
||||||
|
|
||||||
|
// FIXME this is also very slow!
|
||||||
|
// we could easily compile all of these
|
||||||
|
// regular expressions when we load the
|
||||||
|
// syntax highlighter.
|
||||||
|
a := string(currLine[charIndex:])
|
||||||
|
|
||||||
|
// no need to compile the same regex
|
||||||
|
// pattern multiple times.
|
||||||
|
regex, ok := compiledRegex[syntax.Pattern]
|
||||||
|
if !ok {
|
||||||
|
var err error
|
||||||
|
regex, err = regexp.Compile(syntax.Pattern)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
matched := regex.FindStringIndex(a)
|
||||||
|
if matched != nil {
|
||||||
|
if _, ok := matches[charIndex]; !ok {
|
||||||
|
matchedStrLen := (matched[1] - matched[0])
|
||||||
|
matches[charIndex+matched[0]] = syntaxRuneInfo{colours[syntaxIndex], -1, matchedStrLen}
|
||||||
|
charIndex = charIndex + matchedStrLen
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
for _, subject := range syntax.Match {
|
||||||
|
if charIndex+len(subject)+1 > len(currLine) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
a := currLine[charIndex : charIndex+len(subject)+1]
|
||||||
|
|
||||||
|
// we only want to match words. so we check that it has a space
|
||||||
|
// before or after the subject word.
|
||||||
|
if strings.Compare(string(a), subject+" ") == 0 || strings.Compare(string(a), " "+subject) == 0 {
|
||||||
|
if _, ok := matches[charIndex]; !ok {
|
||||||
|
matches[charIndex] = syntaxRuneInfo{colours[syntaxIndex], -1, len(string(a))}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
charIndex += len(subject)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return matches
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Buffer) renderAt(ctx *strife.Renderer, rx int, ry int) {
|
func (b *Buffer) renderAt(ctx *strife.Renderer, rx int, ry int) {
|
||||||
// BACKGROUND
|
// BACKGROUND
|
||||||
ctx.SetColor(strife.HexRGB(b.cfg.Theme.Background))
|
ctx.SetColor(strife.HexRGB(b.cfg.Theme.Background))
|
||||||
@ -803,72 +875,9 @@ func (b *Buffer) renderAt(ctx *strife.Renderer, rx int, ry int) {
|
|||||||
currLine := []rune(rope.String())
|
currLine := []rune(rope.String())
|
||||||
|
|
||||||
// char index => colour
|
// char index => colour
|
||||||
matches := map[int]syntaxRuneInfo{}
|
var matches map[int]syntaxRuneInfo
|
||||||
|
if b.languageInfo != nil {
|
||||||
subjects := make([]cfg.SyntaxCriteria, len(b.languageInfo.Syntax))
|
matches = b.syntaxHighlightLine(string(currLine))
|
||||||
colours := make([]int, len(b.languageInfo.Syntax))
|
|
||||||
|
|
||||||
idx := 0
|
|
||||||
for _, criteria := range b.languageInfo.Syntax {
|
|
||||||
colours[idx] = criteria.Colour
|
|
||||||
subjects[idx] = criteria
|
|
||||||
idx++
|
|
||||||
}
|
|
||||||
|
|
||||||
// HOLY SLOW BATMAN
|
|
||||||
for charIndex := 0; charIndex < len(currLine); charIndex++ {
|
|
||||||
for syntaxIndex, syntax := range subjects {
|
|
||||||
|
|
||||||
if syntax.Pattern != "" {
|
|
||||||
// we have a regex pattern
|
|
||||||
|
|
||||||
// FIXME this is also very slow!
|
|
||||||
// we could easily compile all of these
|
|
||||||
// regular expressions when we load the
|
|
||||||
// syntax highlighter.
|
|
||||||
a := string(currLine[charIndex:])
|
|
||||||
|
|
||||||
// no need to compile the same regex
|
|
||||||
// pattern multiple times.
|
|
||||||
regex, ok := compiledRegex[syntax.Pattern]
|
|
||||||
if !ok {
|
|
||||||
var err error
|
|
||||||
regex, err = regexp.Compile(syntax.Pattern)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
matched := regex.FindStringIndex(a)
|
|
||||||
if matched != nil {
|
|
||||||
if _, ok := matches[charIndex]; !ok {
|
|
||||||
matchedStrLen := (matched[1] - matched[0])
|
|
||||||
matches[charIndex+matched[0]] = syntaxRuneInfo{colours[syntaxIndex], -1, matchedStrLen}
|
|
||||||
charIndex = charIndex + matchedStrLen
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
for _, subject := range syntax.Match {
|
|
||||||
if charIndex+len(subject)+1 > len(currLine) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
a := currLine[charIndex : charIndex+len(subject)+1]
|
|
||||||
|
|
||||||
// we only want to match words. so we check that it has a space
|
|
||||||
// before or after the subject word.
|
|
||||||
if strings.Compare(string(a), subject+" ") == 0 || strings.Compare(string(a), " "+subject) == 0 {
|
|
||||||
if _, ok := matches[charIndex]; !ok {
|
|
||||||
matches[charIndex] = syntaxRuneInfo{colours[syntaxIndex], -1, len(string(a))}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
charIndex += len(subject)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
colorStack := []int{}
|
colorStack := []int{}
|
||||||
|
Loading…
Reference in New Issue
Block a user