Skip to content

Commit

Permalink
add js & optimize syntax matches lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Jun 26, 2024
1 parent d303d9b commit 53e7972
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 34 deletions.
12 changes: 11 additions & 1 deletion config/languages.toml
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ files = ['**/hypr/*.conf']
line_comment_tokens = ['#']
block_comment_tokens = []
auto_pairs = []
grammar = { name = 'hyprlang', symbol_name = 'hyprlang', install = { git = 'https://github.com/tree-sitter-grammars/tree-sitter-hyprlang', rev = '088bcd4052de1b9b5d051dffc3d76e658f295a50', ref = 'master', ref_type = 'commit' } }
grammar = { name = 'hyprlang', symbol_name = 'hyprlang', install = { git = 'https://github.com/tree-sitter-grammars/tree-sitter-hyprlang', rev = 'c9012d6dcaaa939f17c21e1fdb17b013d139e6b9', ref = 'master', ref_type = 'commit' } }

[languages.bash]
alt_names = ['sh']
Expand Down Expand Up @@ -396,3 +396,13 @@ line_comment_tokens = ['#']
block_comment_tokens = []
auto_pairs = []
grammar = { name = 'requirements', symbol_name = 'requirements', install = { git = 'https://github.com/tree-sitter-grammars/tree-sitter-requirements', rev = '5ad9b7581b3334f6ad492847d007f2fac6e6e5f2', ref = 'master', ref_type = 'commit' } }

[languages.javascript]
alt_names = ['js']
mime_types = []
file_types = ['.js', '.jsx']
files = []
line_comment_tokens = ['//']
block_comment_tokens = [{ start = '/*', end = '*/' }]
auto_pairs = []
grammar = { name = 'javascript', symbol_name = 'javascript', install = { git = 'https://github.com/tree-sitter/tree-sitter-javascript', rev = '391a8fcc48a11f63bf18ec9885f6f069e760949a', ref = 'master', ref_type = 'commit' } }
2 changes: 2 additions & 0 deletions config/queries/javascript/injections.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
((comment) @injection.content
(#set! injection.language "comment"))
1 change: 1 addition & 0 deletions config/themes/dark.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ hint = 0xf06e8
"xml" = 0xf05c0
"python" = 0xf0320
"requirements" = 0xf0320
"javascript" = 0xf031e

[styles]
[styles.diagnostics]
Expand Down
1 change: 1 addition & 0 deletions config/themes/dark_simple.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ hint = 0xf06e8
"xml" = 0xf05c0
"python" = 0xf0320
"requirements" = 0xf0320
"javascript" = 0xf031e

[styles]
[styles.diagnostics]
Expand Down
23 changes: 23 additions & 0 deletions gopad/buffer/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ func (p Position) ToProtocol() protocol.Position {
}
}

func (p Position) Compare(start Position) int {
if p.Row < start.Row {
return -1
}
if p.Row > start.Row {
return 1
}
if p.Col < start.Col {
return -1
}
if p.Col > start.Col {
return 1
}
return 0
}

func ParseRange(r protocol.Range) Range {
return Range{
Start: ParsePosition(r.Start),
Expand Down Expand Up @@ -111,3 +127,10 @@ func (r Range) ToProtocol() protocol.Range {
func (r Range) Lines() int {
return r.End.Row - r.Start.Row + 1
}

func (r Range) Compare(start Range) int {
if c := r.Start.Compare(start.Start); c != 0 {
return c
}
return r.End.Compare(start.End)
}
6 changes: 6 additions & 0 deletions gopad/editor/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"slices"
"strings"
"time"

"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -862,6 +863,11 @@ func (e Editor) Update(msg tea.Msg) (Editor, tea.Cmd) {
}

func (e *Editor) View(width int, height int) string {
start := time.Now()
defer func() {
log.Printf("editor view took %s", time.Since(start))
}()

var fileTree string
if e.fileTree.Visible() {
fileTree = e.fileTree.View(height)
Expand Down
20 changes: 14 additions & 6 deletions gopad/editor/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package file

import (
"fmt"
"log"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"time"

"github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
Expand Down Expand Up @@ -85,13 +87,14 @@ type File struct {
language *Language
tree *Tree
autocomplete *Autocompleter
diagnosticVersions map[ls.DiagnosticType]int32
diagnostics []ls.Diagnostic
inlayHints []ls.InlayHint
matchesVersion int32
matches []Match
changes []Change
showCurrentDiagnostic bool

diagnosticVersions map[ls.DiagnosticType]int32
diagnostics []ls.Diagnostic
inlayHints []ls.InlayHint
matchesVersion int32
matches []Match
changes []Change
}

func (f *File) Name() string {
Expand Down Expand Up @@ -441,6 +444,11 @@ func (f *File) ToggleLineComment() tea.Cmd {
}

func (f *File) View(width int, height int, border bool, debug bool) string {
start := time.Now()
defer func() {
log.Printf("file view took %s", time.Since(start))
}()

styles := config.Theme.Editor
borderStyle := func(strs ...string) string { return strings.Join(strs, " ") }
if border {
Expand Down
47 changes: 20 additions & 27 deletions gopad/editor/file/syntax_highlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package file

import (
"fmt"
"log"
"strconv"
"strings"
"time"

"github.com/charmbracelet/lipgloss"
sitter "go.gopad.dev/go-tree-sitter"
"go.gopad.dev/go-tree-sitter"

"go.gopad.dev/gopad/gopad/buffer"
"go.gopad.dev/gopad/gopad/config"
Expand All @@ -25,10 +25,6 @@ func (f *File) SetMatches(version int32, matches []Match) {
f.matches = append(f.matches, matches...)
}

func (f *File) Matches() []Match {
return f.matches
}

func (f *File) MatchesForLineCol(row int, col int) []Match {
pos := buffer.Position{Row: row, Col: col}

Expand All @@ -38,6 +34,7 @@ func (f *File) MatchesForLineCol(row int, col int) []Match {
matches = append(matches, match)
}
}

return matches
}

Expand Down Expand Up @@ -116,17 +113,16 @@ type LocalScope struct {
}

func (f *File) HighlightTree() {
now := time.Now()
defer func() {
fmt.Println("highlighting took", time.Since(now))
}()
if f.tree == nil || f.tree.Tree == nil || f.tree.Language.Grammar == nil {
return
}
version := f.Version()

matches := highlightTree(f.tree.Copy())
// slices.SortFunc(matches, func(a, b Match) int {
// return b.Priority - a.Priority
// })

f.SetMatches(version, matches)
f.SetMatches(version, highlightTree(f.tree.Copy()))
}

func highlightTree(tree *Tree) []Match {
Expand Down Expand Up @@ -172,14 +168,12 @@ func highlightTree(tree *Tree) []Match {

if uint32(match.PatternIndex) < query.HighlightsPatternIndex {
if query.ScopeCaptureID != nil && capture.Index == *query.ScopeCaptureID {
log.Println("New scope")
scopes = append(scopes, &LocalScope{
Inherits: true,
Range: captureRange,
LocalDefs: nil,
})
} else if query.DefinitionCaptureID != nil && capture.Index == *query.DefinitionCaptureID {
log.Println("New definition:", capture.Node.Content())
if len(scopes) > 0 {
def := &LocalDef{
Name: capture.Node.Content(),
Expand All @@ -192,7 +186,6 @@ func highlightTree(tree *Tree) []Match {
scope.LocalDefs = append(scope.LocalDefs, def)
}
} else if query.ReferenceCaptureID != nil && capture.Index == *query.ReferenceCaptureID {
log.Println("Found reference:", capture.Node.Content())
for i := len(scopes) - 1; i >= 0; i-- {
for _, def := range scopes[i].LocalDefs {
if def.Name == capture.Node.Content() {
Expand All @@ -219,27 +212,27 @@ func highlightTree(tree *Tree) []Match {
lastDef.Type = query.Query.CaptureNameForID(capture.Index)
}

highlightMatch := Match{
var refType string
if lastRef != nil {
refType = lastRef.Type
}

matches = append(matches, Match{
Range: buffer.Range{
Start: buffer.Position{Row: int(capture.StartPoint().Row), Col: int(capture.StartPoint().Column)},
End: buffer.Position{Row: int(capture.EndPoint().Row), Col: max(0, int(capture.EndPoint().Column)-1)}, // -1 to exclude the last character idk why this is like this tbh
},
Type: query.Query.CaptureNameForID(capture.Index),
Priority: getPriority(match),
Source: tree.Language.Name,
}

if lastRef != nil {
highlightMatch.ReferenceType = lastRef.Type
}
Type: query.Query.CaptureNameForID(capture.Index),
ReferenceType: refType,
Priority: getPriority(match),
Source: tree.Language.Name,
})

matches = append(matches, highlightMatch)
lastCapture = &capture
}

for _, subTree := range tree.SubTrees {
subMatches := highlightTree(subTree)
matches = append(matches, subMatches...)
matches = append(matches, highlightTree(subTree)...)
}

return matches
Expand Down
6 changes: 6 additions & 0 deletions gopad/gopad.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -140,6 +141,11 @@ func (g Gopad) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (g Gopad) View() string {
start := time.Now()
defer func() {
log.Printf("gopad view took %s", time.Since(start))
}()

appBar := g.AppBar()
codeBar := g.CodeBar()

Expand Down

0 comments on commit 53e7972

Please sign in to comment.