Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
.idea/
dist/
/test/
.gopad/

/dist/
/test/
/config/grammars/

go.work
go.work.sum
/debug.log
/lsp.log
/config/grammars/

debug.log
lsp.log
trace.log
panic.log
49 changes: 37 additions & 12 deletions cmd/gopad.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import (
"github.com/charmbracelet/bubbletea/v2"
"github.com/lrstanley/bubblezone"
"github.com/spf13/cobra"
"go.opentelemetry.io/otel/trace"
tracenoop "go.opentelemetry.io/otel/trace/noop"

"go.gopad.dev/gopad/gopad"
"go.gopad.dev/gopad/gopad/config"
"go.gopad.dev/gopad/gopad/editor/file"
"go.gopad.dev/gopad/gopad/editor/doc"
"go.gopad.dev/gopad/gopad/ls"
"go.gopad.dev/gopad/internal/xio"
)
Expand All @@ -33,6 +35,7 @@ func NewRootCmd(version string, defaultConfigs embed.FS) *cobra.Command {
workspace, _ := cmd.Flags().GetString("workspace")
debug, _ := cmd.Flags().GetString("debug")
debugLSP, _ := cmd.Flags().GetString("debug-lsp")
debugTrace, _ := cmd.Flags().GetString("debug-trace")
pprof, _ := cmd.Flags().GetString("pprof")
disableMouse, _ := cmd.Flags().GetBool("disable-mouse")

Expand Down Expand Up @@ -66,6 +69,29 @@ func NewRootCmd(version string, defaultConfigs embed.FS) *cobra.Command {
lspLogFile = xio.NopCloser(io.Discard)
}

loadConfig(configDir, defaultConfigs)
if err := doc.LoadLanguages(defaultConfigs); err != nil {
return err
}

var tracer trace.Tracer
if debugTrace != "" {
traceLogFile, err := os.OpenFile(debugTrace, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o600)
if err != nil {
log.Panicln("failed to open debug trace log file:", err)
}
defer func() {
_ = traceLogFile.Close()
}()

tracer = newTracer(traceLogFile, version)

log.Println("debug trace mode enabled")
} else {
tracer = tracenoop.NewTracerProvider().Tracer(Name)
}
config.Tracer = tracer

if pprof != "" {
go func() {
if err := http.ListenAndServe(pprof, nil); err != nil && !errors.Is(err, http.ErrServerClosed) {
Expand All @@ -75,13 +101,10 @@ func NewRootCmd(version string, defaultConfigs embed.FS) *cobra.Command {
log.Println("pprof enabled")
}

loadConfig(configDir, defaultConfigs)
if err := file.LoadLanguages(defaultConfigs); err != nil {
return err
}

lsClient := ls.New(version, config.LanguageServers, lspLogFile)
e := gopad.New(lsClient, version, getWorkspace(workspace, args), args)

workspace = parseWorkspace(workspace, args)
g := gopad.New(lsClient, version, workspace, args)

opts := []tea.ProgramOption{
tea.WithAltScreen(),
Expand All @@ -91,6 +114,7 @@ func NewRootCmd(version string, defaultConfigs embed.FS) *cobra.Command {
tea.WithKeyReleases,
tea.WithUniformKeyLayout,
),
tea.WithFerociousRenderer(),
}
zone.NewGlobal()
defer zone.Close()
Expand All @@ -99,7 +123,7 @@ func NewRootCmd(version string, defaultConfigs embed.FS) *cobra.Command {
} else {
zone.SetEnabled(false)
}
p := tea.NewProgram(e, opts...)
p := tea.NewProgram(g, opts...)
lsClient.SetProgram(p)
log.Println("running gopad")
if _, err := p.Run(); err != nil {
Expand All @@ -112,15 +136,16 @@ func NewRootCmd(version string, defaultConfigs embed.FS) *cobra.Command {

cmd.PersistentFlags().StringP("config-dir", "c", "", "set configuration directory (Default: ./.gopad, $XDG_CONFIG_HOME/gopad or $HOME/.config/gopad)")
cmd.Flags().StringP("workspace", "w", "", "set workspace directory (Default: first directory argument)")
cmd.Flags().StringP("debug", "d", "", "set debug log file")
cmd.Flags().StringP("debug-lsp", "l", "", "set debug lsp log file")
cmd.Flags().StringP("pprof", "p", "", "set pprof address:port")
cmd.Flags().StringP("debug", "", "", "set debug log file")
cmd.Flags().StringP("debug-lsp", "", "", "set debug lsp log file")
cmd.Flags().StringP("debug-trace", "", "", "set debug trace log file")
cmd.Flags().StringP("pprof", "", "", "set pprof address:port")
cmd.Flags().BoolP("disable-mouse", "", false, "disable mouse support (enabled by default)")

return cmd
}

func getWorkspace(workspace string, args []string) string {
func parseWorkspace(workspace string, args []string) string {
if workspace == "" {
for _, arg := range args {
stat, err := os.Stat(arg)
Expand Down
2 changes: 1 addition & 1 deletion cmd/grammar/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"
"sync"

"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/v2"
"github.com/spf13/cobra"

"go.gopad.dev/gopad/gopad/config"
Expand Down
2 changes: 1 addition & 1 deletion cmd/grammar/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os/exec"
"slices"

"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/v2"

"go.gopad.dev/gopad/gopad/config"
)
Expand Down
42 changes: 42 additions & 0 deletions cmd/otel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cmd

import (
"io"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.27.0"
"go.opentelemetry.io/otel/trace"

"go.gopad.dev/gopad/internal/outtrace"
)

const (
Name = "gopad"
Namespace = "go.gopad.dev/gopad"
)

func resources(instanceID string, version string) *resource.Resource {
return resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceName(Name),
semconv.ServiceNamespace(Namespace),
semconv.ServiceInstanceID(instanceID),
semconv.ServiceVersion(version),
)
}

func newTracer(w io.Writer, version string) trace.Tracer {
exp := outtrace.New(w)

tp := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(exp),
sdktrace.WithResource(resources("0", version)),
)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))

return otel.Tracer(Name)
}
2 changes: 1 addition & 1 deletion config/gopad.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# General configuration
theme = 'dark'
keymap = 'default'
keymap = 'linux'

# Editor configuration
[editor]
Expand Down
4 changes: 2 additions & 2 deletions config/keymaps/default.toml → config/keymaps/linux.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Key bindings configuration
name = 'default'
name = 'linux'

[keys]
quit = 'ctrl+q'
Expand All @@ -23,7 +23,7 @@ search = 'ctrl+f'
open_outline = 'alt+7'

refresh_syntax_highlight = 'f1'
toggle_tree_sitter_debug = 'f2'
toggle_debug = 'f2'
debug_tree_sitter_nodes = 'f3'

[keys.editor.file]
Expand Down
35 changes: 32 additions & 3 deletions config/language_servers.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

use_servers = { only = ['gopls'], except = [] }

[language_servers]

[language_servers.gopls]
command = 'gopls'
args = []
Expand All @@ -16,7 +14,15 @@ features = ['inlay_hints', 'diagnostics', 'completion', 'go_to_definition']
[language_servers.gopls.config]
'ui.completion.usePlaceholders' = true
'ui.diagnostic.staticcheck' = true
'ui.hints' = { assignVariableTypes = true, compositeLiteralFields = true, compositeLiteralTypes = true, constantValues = true, functionTypeParameters = true, parameterNames = true, rangeVariableTypes = true }

[language_servers.gopls.config.hints]
assignVariableTypes = true
compositeLiteralFields = true
compositeLiteralTypes = true
constantValues = true
functionTypeParameters = true
parameterNames = true
rangeVariableTypes = true

[language_servers.golangci-lint]
command = 'golangci-lint-langserver'
Expand All @@ -39,14 +45,37 @@ args = ['--stdio']
file_types = ['.json', '.jsonc']
files = []

[language_servers.json.config]
provideFormatter = true
json = { validate = { enable = true } }

[language_servers.html]
command = 'vscode-html-language-server'
args = ['--stdio']
file_types = ['.html', '.htm']
files = []

[language_servers.superhtml]
command = 'superhtml'
args = ['lsp']
file_types = ['.html', '.htm']
files = []

[language_servers.toml]
command = 'taplo'
args = ['language_servers', 'stdio']
file_types = ['.toml']
files = []

[language_servers.ts-query]
command = 'ts_query_ls'
args = []
file_types = ['.scm']
files = []
roots = []
features = ['diagnostics']

[language_servers.ts-query.config.settings]
'parser_install_directories' = ['config/grammars']
'parser_aliases' = {}
'language_retrieval_patterns' = [ 'config/queries/([^/]+)/[^/]+\\.scm$' ]
12 changes: 12 additions & 0 deletions config/languages.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ line_comment_tokens = ['//']
block_comment_tokens = [{ start = '/*', end = '*/' }]
auto_pairs = [{ open = '(', close = ')' }, { open = '{', close = '}' }, { open = '[', close = ']' }, { open = '"', close = '"' }, { open = "'", close = "'" }, { open = '`', close = '`' }]
grammar = { name = 'go', symbol_name = 'go', install = { git = 'https://github.com/tree-sitter/tree-sitter-go', rev = '7ee8d928db5202f6831a78f8112fd693bf69f98b', ref = 'master', ref_type = 'commit' } }
formatter = { command = 'gofmt', args = ['-s'] }

[languages.go-mod]
alt_names = ['go.mod']
Expand Down Expand Up @@ -97,6 +98,17 @@ block_comment_tokens = []
auto_pairs = [{ open = '"', close = '"' }, { open = '(', close = ')' }, { open = '[', close = ']' }]
grammar = { name = 'query', symbol_name = 'query', install = { git = 'https://github.com/tree-sitter-grammars/tree-sitter-query', rev = 'f767fb0ac5e711b6d44c5e0c8d1f349687a86ce0', ref = 'master', ref_type = 'commit' } }

[languages.sexp]
alt_names = ['S-expressions']
mime_types = []
file_types = ['.sexp']
files = []
line_comment_tokens = [';']
block_comment_tokens = []
auto_pairs = []
grammar = { name = 'sexp', symbol_name = 'sexp', install = { git = 'https://github.com/AbstractMachinesLab/tree-sitter-sexp', rev = 'b46b9f784b3e31bcbc0632f3294f1af6ba049241', ref = 'main', ref_type = 'commit' } }
formatter = { command = 'sexp-fmt', args = [] }

[languages.markdown]
alt_names = ['md']
mime_types = []
Expand Down
6 changes: 3 additions & 3 deletions config/queries/go/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@

"func" @keyword.function

"return" @keyword.return
"return" @keyword.control.return

[
"import"
Expand All @@ -183,9 +183,9 @@
"case"
"switch"
"if"
] @keyword.conditional
] @keyword.control.conditional

"for" @keyword.repeat
"for" @keyword.control.repeat

[
"var"
Expand Down
13 changes: 5 additions & 8 deletions config/queries/markdown/injections.scm
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
(fenced_code_block
(info_string
(language) @_lang)
(code_fence_content) @injection.content
(#set-lang-from-info-string! @_lang))
(language) @injection.language)
(code_fence_content) @injection.content)

([
(inline)
(pipe_table_cell)
] @injection.content
(#set! injection.language "markdown-inline"))
((pipe_table_cell) @injection.content (#set! injection.language "markdown-inline"))

((inline) @injection.content (#set! injection.language "markdown-inline"))
8 changes: 8 additions & 0 deletions config/queries/sexp/highlights.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(atom) @constant

;":" @punctuation.delimiter

[
"("
")"
] @punctuation.bracket
Loading