Skip to content

Commit 96f4f4d

Browse files
committed
refactor: move registry to pkg
1 parent 368f539 commit 96f4f4d

16 files changed

Lines changed: 126 additions & 113 deletions

File tree

internal/cmd/export.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import (
1313
tea "github.com/charmbracelet/bubbletea"
1414
"github.com/spf13/cobra"
1515
"github.com/spf13/viper"
16-
"github.com/xinnjie/onekeymap-cli/internal/plugins"
1716
"github.com/xinnjie/onekeymap-cli/internal/views"
1817
"github.com/xinnjie/onekeymap-cli/pkg/api/exporterapi"
1918
"github.com/xinnjie/onekeymap-cli/pkg/api/keymap"
2019
"github.com/xinnjie/onekeymap-cli/pkg/api/pluginapi"
20+
"github.com/xinnjie/onekeymap-cli/pkg/registry"
2121
)
2222

2323
type exportFlags struct {
@@ -34,7 +34,7 @@ func NewCmdExport() *cobra.Command {
3434
cmd := &cobra.Command{
3535
Use: "export",
3636
Short: "Export a universal keymap to an editor's format",
37-
RunE: exportRun(&f, func() (*slog.Logger, *plugins.Registry, exporterapi.Exporter) {
37+
RunE: exportRun(&f, func() (*slog.Logger, *registry.Registry, exporterapi.Exporter) {
3838
return cmdLogger, cmdPluginRegistry, cmdExportService
3939
}),
4040
Args: cobra.ExactArgs(0),
@@ -59,7 +59,7 @@ func NewCmdExport() *cobra.Command {
5959

6060
func exportRun(
6161
f *exportFlags,
62-
dependencies func() (*slog.Logger, *plugins.Registry, exporterapi.Exporter),
62+
dependencies func() (*slog.Logger, *registry.Registry, exporterapi.Exporter),
6363
) func(cmd *cobra.Command, _ []string) error {
6464
return func(cmd *cobra.Command, _ []string) error {
6565
logger, pluginRegistry, exportService := dependencies()
@@ -178,7 +178,7 @@ func handleInteractiveExportFlags(
178178
cmd *cobra.Command,
179179
f *exportFlags,
180180
onekeymapPlaceholder string,
181-
pluginRegistry *plugins.Registry,
181+
pluginRegistry *registry.Registry,
182182
) error {
183183
needSelectEditor := !cmd.Flags().Changed("to") || f.to == ""
184184
needInput := !cmd.Flags().Changed("input") || f.input == ""
@@ -210,7 +210,7 @@ func prepareExportInputFlags(
210210
cmd *cobra.Command,
211211
f *exportFlags,
212212
onekeymapPlaceholder string,
213-
pluginRegistry *plugins.Registry,
213+
pluginRegistry *registry.Registry,
214214
logger *slog.Logger,
215215
) error {
216216
if f.interactive {
@@ -250,7 +250,7 @@ func prepareExportInputFlags(
250250
return nil
251251
}
252252

253-
func runExportForm(pluginRegistry *plugins.Registry, to, input, output *string, onekeymapConfigPlaceHolder string,
253+
func runExportForm(pluginRegistry *registry.Registry, to, input, output *string, onekeymapConfigPlaceHolder string,
254254
needSelectEditor, needInput, needOutput bool) error {
255255
m, err := views.NewOutputFormModel(
256256
pluginRegistry,

internal/cmd/import.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import (
1212
tea "github.com/charmbracelet/bubbletea"
1313
"github.com/spf13/cobra"
1414
"github.com/spf13/viper"
15-
"github.com/xinnjie/onekeymap-cli/internal/plugins"
1615
"github.com/xinnjie/onekeymap-cli/internal/views"
1716
"github.com/xinnjie/onekeymap-cli/pkg/api/importerapi"
1817
"github.com/xinnjie/onekeymap-cli/pkg/api/keymap"
1918
"github.com/xinnjie/onekeymap-cli/pkg/api/platform"
2019
"github.com/xinnjie/onekeymap-cli/pkg/api/pluginapi"
2120
"github.com/xinnjie/onekeymap-cli/pkg/api/validateapi"
21+
"github.com/xinnjie/onekeymap-cli/pkg/registry"
2222
)
2323

2424
type importFlags struct {
@@ -35,7 +35,7 @@ func NewCmdImport() *cobra.Command {
3535
cmd := &cobra.Command{
3636
Use: "import",
3737
Short: "Import an editor's keymap to the universal format",
38-
RunE: importRun(&f, func() (*slog.Logger, *plugins.Registry, importerapi.Importer) {
38+
RunE: importRun(&f, func() (*slog.Logger, *registry.Registry, importerapi.Importer) {
3939
return cmdLogger, cmdPluginRegistry, cmdImportService
4040
}),
4141
Args: cobra.ExactArgs(0),
@@ -61,7 +61,7 @@ func NewCmdImport() *cobra.Command {
6161

6262
func importRun(
6363
f *importFlags,
64-
dependencies func() (*slog.Logger, *plugins.Registry, importerapi.Importer),
64+
dependencies func() (*slog.Logger, *registry.Registry, importerapi.Importer),
6565
) func(cmd *cobra.Command, _ []string) error {
6666
return func(cmd *cobra.Command, _ []string) error {
6767
logger, pluginRegistry, importService := dependencies()
@@ -79,7 +79,7 @@ func importRunInteractive(
7979
cmd *cobra.Command,
8080
f *importFlags,
8181
logger *slog.Logger,
82-
pluginRegistry *plugins.Registry,
82+
pluginRegistry *registry.Registry,
8383
importService importerapi.Importer,
8484
onekeymapConfig string,
8585
) error {
@@ -94,7 +94,7 @@ func importRunNonInteractive(
9494
cmd *cobra.Command,
9595
f *importFlags,
9696
logger *slog.Logger,
97-
pluginRegistry *plugins.Registry,
97+
pluginRegistry *registry.Registry,
9898
importService importerapi.Importer,
9999
onekeymapConfig string,
100100
) error {
@@ -282,7 +282,7 @@ func handleInteractiveImportFlags(
282282
cmd *cobra.Command,
283283
f *importFlags,
284284
onekeymapConfig string,
285-
pluginRegistry *plugins.Registry,
285+
pluginRegistry *registry.Registry,
286286
logger *slog.Logger,
287287
) error {
288288
needSelectEditor := !cmd.Flags().Changed("from") || f.from == ""
@@ -312,7 +312,7 @@ func prepareInteractiveImportFlags(
312312
cmd *cobra.Command,
313313
f *importFlags,
314314
onekeymapConfig string,
315-
pluginRegistry *plugins.Registry,
315+
pluginRegistry *registry.Registry,
316316
logger *slog.Logger,
317317
) error {
318318
if err := handleInteractiveImportFlags(cmd, f, onekeymapConfig, pluginRegistry, logger); err != nil {
@@ -345,7 +345,7 @@ func prepareInteractiveImportFlags(
345345
func prepareNonInteractiveImportFlags(
346346
f *importFlags,
347347
onekeymapConfig string,
348-
pluginRegistry *plugins.Registry,
348+
pluginRegistry *registry.Registry,
349349
logger *slog.Logger,
350350
) error {
351351
if f.from == "" {
@@ -388,7 +388,7 @@ func runImportChangesPreview(changes *importerapi.KeymapChanges) (bool, error) {
388388
// runImportForm runs the interactive import form and returns the selected values.
389389
// All TUI logic is encapsulated here to keep cmd/import.go simple.
390390
func runImportForm(
391-
pluginRegistry *plugins.Registry,
391+
pluginRegistry *registry.Registry,
392392
from, input, output *string,
393393
onekeymapConfigPlaceHolder string,
394394
needSelectEditor, needInput, needOutput bool,

internal/cmd/migrate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010

1111
tea "github.com/charmbracelet/bubbletea"
1212
"github.com/spf13/cobra"
13-
"github.com/xinnjie/onekeymap-cli/internal/plugins"
1413
"github.com/xinnjie/onekeymap-cli/internal/views"
1514
"github.com/xinnjie/onekeymap-cli/pkg/api/exporterapi"
1615
"github.com/xinnjie/onekeymap-cli/pkg/api/importerapi"
1716
"github.com/xinnjie/onekeymap-cli/pkg/api/pluginapi"
17+
"github.com/xinnjie/onekeymap-cli/pkg/registry"
1818
)
1919

2020
type migrateFlags struct {
@@ -31,7 +31,7 @@ func NewCmdMigrate() *cobra.Command {
3131
cmd := &cobra.Command{
3232
Use: "migrate",
3333
Short: "Migrate keymaps from one editor to another",
34-
RunE: migrateRun(&f, func() (*slog.Logger, importerapi.Importer, exporterapi.Exporter, *plugins.Registry) {
34+
RunE: migrateRun(&f, func() (*slog.Logger, importerapi.Importer, exporterapi.Exporter, *registry.Registry) {
3535
return cmdLogger, cmdImportService, cmdExportService, cmdPluginRegistry
3636
}),
3737
Args: cobra.ExactArgs(0),
@@ -49,7 +49,7 @@ func NewCmdMigrate() *cobra.Command {
4949

5050
func migrateRun(
5151
f *migrateFlags,
52-
dependencies func() (*slog.Logger, importerapi.Importer, exporterapi.Exporter, *plugins.Registry),
52+
dependencies func() (*slog.Logger, importerapi.Importer, exporterapi.Exporter, *registry.Registry),
5353
) func(cmd *cobra.Command, _ []string) error {
5454
return func(cmd *cobra.Command, _ []string) error {
5555
ctx := cmd.Context()

internal/cmd/root.go

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,14 @@ import (
1212
"github.com/spf13/viper"
1313
"github.com/xinnjie/onekeymap-cli/internal/cliconfig"
1414
"github.com/xinnjie/onekeymap-cli/internal/metrics"
15-
"github.com/xinnjie/onekeymap-cli/internal/plugins"
16-
"github.com/xinnjie/onekeymap-cli/internal/plugins/basekeymap"
17-
"github.com/xinnjie/onekeymap-cli/internal/plugins/helix"
18-
"github.com/xinnjie/onekeymap-cli/internal/plugins/intellij"
19-
"github.com/xinnjie/onekeymap-cli/internal/plugins/vscode"
20-
"github.com/xinnjie/onekeymap-cli/internal/plugins/xcode"
21-
"github.com/xinnjie/onekeymap-cli/internal/plugins/zed"
2215
"github.com/xinnjie/onekeymap-cli/internal/updatecheck"
2316
"github.com/xinnjie/onekeymap-cli/internal/views"
2417
"github.com/xinnjie/onekeymap-cli/pkg/api/exporterapi"
2518
"github.com/xinnjie/onekeymap-cli/pkg/api/importerapi"
2619
"github.com/xinnjie/onekeymap-cli/pkg/exporter"
2720
"github.com/xinnjie/onekeymap-cli/pkg/importer"
2821
"github.com/xinnjie/onekeymap-cli/pkg/mappings"
22+
"github.com/xinnjie/onekeymap-cli/pkg/registry"
2923
"golang.org/x/term"
3024
)
3125

@@ -41,7 +35,7 @@ var (
4135
//nolint:gochecknoglobals // TODO(xinnjie): Stop using these global variables. But for now I can not think of a better way.
4236
var (
4337
// Global shared state that needs to be accessed across commands
44-
cmdPluginRegistry *plugins.Registry
38+
cmdPluginRegistry *registry.Registry
4539
cmdImportService importerapi.Importer
4640
cmdExportService exporterapi.Exporter
4741
cmdLogger *slog.Logger
@@ -196,30 +190,7 @@ func rootPersistentPreRun(f *rootFlags) func(cmd *cobra.Command, _ []string) {
196190
}
197191
}
198192

199-
cmdPluginRegistry = plugins.NewRegistry()
200-
201-
// VSCode family
202-
cmdPluginRegistry.Register(vscode.New(cmdMappingConfig, cmdLogger, cmdRecorder))
203-
cmdPluginRegistry.Register(vscode.NewWindsurf(cmdMappingConfig, cmdLogger, cmdRecorder))
204-
cmdPluginRegistry.Register(vscode.NewWindsurfNext(cmdMappingConfig, cmdLogger, cmdRecorder))
205-
cmdPluginRegistry.Register(vscode.NewCursor(cmdMappingConfig, cmdLogger, cmdRecorder))
206-
207-
// IntelliJ family
208-
cmdPluginRegistry.Register(intellij.New(cmdMappingConfig, cmdLogger, cmdRecorder))
209-
cmdPluginRegistry.Register(intellij.NewPycharm(cmdMappingConfig, cmdLogger, cmdRecorder))
210-
cmdPluginRegistry.Register(intellij.NewIntelliJCommunity(cmdMappingConfig, cmdLogger, cmdRecorder))
211-
cmdPluginRegistry.Register(intellij.NewWebStorm(cmdMappingConfig, cmdLogger, cmdRecorder))
212-
cmdPluginRegistry.Register(intellij.NewClion(cmdMappingConfig, cmdLogger, cmdRecorder))
213-
cmdPluginRegistry.Register(intellij.NewPhpStorm(cmdMappingConfig, cmdLogger, cmdRecorder))
214-
cmdPluginRegistry.Register(intellij.NewRubyMine(cmdMappingConfig, cmdLogger, cmdRecorder))
215-
cmdPluginRegistry.Register(intellij.NewGoLand(cmdMappingConfig, cmdLogger, cmdRecorder))
216-
cmdPluginRegistry.Register(intellij.NewRustRover(cmdMappingConfig, cmdLogger, cmdRecorder))
217-
218-
cmdPluginRegistry.Register(helix.New(cmdMappingConfig, cmdLogger))
219-
cmdPluginRegistry.Register(zed.New(cmdMappingConfig, cmdLogger, cmdRecorder))
220-
cmdPluginRegistry.Register(xcode.New(cmdMappingConfig, cmdLogger, cmdRecorder))
221-
222-
cmdPluginRegistry.Register(basekeymap.New())
193+
cmdPluginRegistry = registry.NewRegistryWithPlugins(cmdMappingConfig, cmdLogger, cmdRecorder)
223194

224195
cmdImportService = importer.NewImporter(cmdPluginRegistry, cmdMappingConfig, cmdLogger, cmdRecorder)
225196
cmdExportService = exporter.NewExporter(cmdPluginRegistry, cmdMappingConfig, cmdLogger, cmdRecorder)

internal/cmd/serve.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ import (
1717
grpc_selector "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/selector"
1818
"github.com/spf13/cobra"
1919
"github.com/spf13/viper"
20-
"github.com/xinnjie/onekeymap-cli/internal/plugins"
2120
"github.com/xinnjie/onekeymap-cli/internal/service"
2221
"github.com/xinnjie/onekeymap-cli/pkg/api/exporterapi"
2322
"github.com/xinnjie/onekeymap-cli/pkg/api/importerapi"
2423
"github.com/xinnjie/onekeymap-cli/pkg/mappings"
24+
"github.com/xinnjie/onekeymap-cli/pkg/registry"
2525
keymapv1 "github.com/xinnjie/onekeymap-cli/protogen/keymap/v1"
2626
"google.golang.org/grpc"
2727
"google.golang.org/grpc/health"
@@ -43,7 +43,7 @@ func NewCmdServe(rootFlags *rootFlags) *cobra.Command {
4343
Run: serveRun(
4444
&f,
4545
rootFlags,
46-
func() (*slog.Logger, *plugins.Registry, importerapi.Importer, exporterapi.Exporter, *mappings.MappingConfig) {
46+
func() (*slog.Logger, *registry.Registry, importerapi.Importer, exporterapi.Exporter, *mappings.MappingConfig) {
4747
return cmdLogger, cmdPluginRegistry, cmdImportService, cmdExportService, cmdMappingConfig
4848
},
4949
),
@@ -63,7 +63,7 @@ func NewCmdServe(rootFlags *rootFlags) *cobra.Command {
6363
func serveRun(
6464
f *serveFlags,
6565
rootFlags *rootFlags,
66-
dependencies func() (*slog.Logger, *plugins.Registry, importerapi.Importer, exporterapi.Exporter, *mappings.MappingConfig),
66+
dependencies func() (*slog.Logger, *registry.Registry, importerapi.Importer, exporterapi.Exporter, *mappings.MappingConfig),
6767
) func(cmd *cobra.Command, _ []string) {
6868
return func(cmd *cobra.Command, _ []string) {
6969
logger, pluginRegistry, importService, exportService, mappingConfig := dependencies()

internal/plugins/registry.go

Lines changed: 0 additions & 36 deletions
This file was deleted.

internal/service/server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ package service
55
import (
66
"log/slog"
77

8-
"github.com/xinnjie/onekeymap-cli/internal/plugins"
98
"github.com/xinnjie/onekeymap-cli/pkg/api/exporterapi"
109
"github.com/xinnjie/onekeymap-cli/pkg/api/importerapi"
1110
"github.com/xinnjie/onekeymap-cli/pkg/mappings"
11+
"github.com/xinnjie/onekeymap-cli/pkg/registry"
1212
keymapv1 "github.com/xinnjie/onekeymap-cli/protogen/keymap/v1"
1313
)
1414

@@ -21,15 +21,15 @@ type Server struct {
2121

2222
importer importerapi.Importer
2323
exporter exporterapi.Exporter
24-
registry *plugins.Registry
24+
registry *registry.Registry
2525
mappingConfig *mappings.MappingConfig
2626
logger *slog.Logger
2727

2828
opt ServerOption
2929
}
3030

3131
func NewServer(
32-
registry *plugins.Registry,
32+
registry *registry.Registry,
3333
importer importerapi.Importer,
3434
exporter exporterapi.Exporter,
3535
mappingConfig *mappings.MappingConfig,

internal/views/export_form.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88

99
tea "github.com/charmbracelet/bubbletea"
1010
"github.com/charmbracelet/huh"
11-
"github.com/xinnjie/onekeymap-cli/internal/plugins"
1211
"github.com/xinnjie/onekeymap-cli/pkg/api/pluginapi"
12+
"github.com/xinnjie/onekeymap-cli/pkg/registry"
1313
)
1414

1515
var (
@@ -20,7 +20,7 @@ var (
2020
type ExportFormModel struct {
2121
form *huh.Form
2222

23-
pluginRegistry *plugins.Registry
23+
pluginRegistry *registry.Registry
2424

2525
needSelectEditor bool
2626
needInput bool
@@ -34,7 +34,7 @@ type ExportFormModel struct {
3434
}
3535

3636
func NewOutputFormModel(
37-
registry *plugins.Registry,
37+
registry *registry.Registry,
3838
needSelectEditor, needInput, needOutput bool,
3939
editor, onekeymapConfigInput, editorKeymapConfigOutput *string,
4040
onekeymapConfigPlaceHolder string,

internal/views/import_form.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88

99
tea "github.com/charmbracelet/bubbletea"
1010
"github.com/charmbracelet/huh"
11-
"github.com/xinnjie/onekeymap-cli/internal/plugins"
1211
"github.com/xinnjie/onekeymap-cli/pkg/api/pluginapi"
12+
"github.com/xinnjie/onekeymap-cli/pkg/registry"
1313
)
1414

1515
var (
@@ -22,7 +22,7 @@ const selectOtherOption = "Other (manual input)"
2222
type ImportFormModel struct {
2323
form *huh.Form
2424

25-
pluginRegistry *plugins.Registry
25+
pluginRegistry *registry.Registry
2626

2727
needSelectEditor bool
2828
needInput bool
@@ -34,7 +34,7 @@ type ImportFormModel struct {
3434
}
3535

3636
func NewImportFormModel(
37-
registry *plugins.Registry,
37+
registry *registry.Registry,
3838
needSelectEditor, needInput, needOutput bool,
3939
editor, editorKeymapConfigInput, onekeymapConfigOutput *string,
4040
onekeymapConfigPlaceHolder string,

0 commit comments

Comments
 (0)