♻️ Refactor search

This commit is contained in:
2025-12-08 23:20:37 +08:00
parent 281f53c049
commit 3660d13d7d
42 changed files with 1003 additions and 1953 deletions

View File

@@ -15,16 +15,19 @@ type ExtensionID string
const (
// 编辑增强扩展
ExtensionRainbowBrackets ExtensionID = "rainbowBrackets" // 彩虹括号
ExtensionHyperlink ExtensionID = "hyperlink" // 超链接
ExtensionColorSelector ExtensionID = "colorSelector" // 颜色选择器
ExtensionFold ExtensionID = "fold"
ExtensionTextHighlight ExtensionID = "textHighlight"
ExtensionCheckbox ExtensionID = "checkbox" // 选择框
ExtensionTranslator ExtensionID = "translator" // 划词翻译
ExtensionRainbowBrackets ExtensionID = "rainbowBrackets" // 彩虹括号
ExtensionHyperlink ExtensionID = "hyperlink" // 超链接
ExtensionColorSelector ExtensionID = "colorSelector" // 颜色选择器
ExtensionFold ExtensionID = "fold" // 代码折叠
ExtensionTranslator ExtensionID = "translator" // 划词翻译
ExtensionMarkdown ExtensionID = "markdown" // Markdown渲染
ExtensionHighlightWhitespace ExtensionID = "highlightWhitespace" // 显示空白字符
ExtensionHighlightTrailingWhitespace ExtensionID = "highlightTrailingWhitespace" // 高亮行尾空白
// UI增强扩展
ExtensionMinimap ExtensionID = "minimap" // 小地图
ExtensionMinimap ExtensionID = "minimap" // 小地图
ExtensionLineNumbers ExtensionID = "lineNumbers" // 行号显示
ExtensionContextMenu ExtensionID = "contextMenu" // 上下文菜单
// 工具扩展
ExtensionSearch ExtensionID = "search" // 搜索功能
@@ -87,21 +90,6 @@ func NewDefaultExtensions() []Extension {
IsDefault: true,
Config: ExtensionConfig{},
},
{
ID: ExtensionTextHighlight,
Enabled: true,
IsDefault: true,
Config: ExtensionConfig{
"backgroundColor": "#FFD700",
"opacity": 0.3,
},
},
{
ID: ExtensionCheckbox,
Enabled: true,
IsDefault: true,
Config: ExtensionConfig{},
},
{
ID: ExtensionTranslator,
Enabled: true,
@@ -112,6 +100,24 @@ func NewDefaultExtensions() []Extension {
"maxTranslationLength": 5000,
},
},
{
ID: ExtensionMarkdown,
Enabled: true,
IsDefault: true,
Config: ExtensionConfig{},
},
{
ID: ExtensionHighlightWhitespace,
Enabled: true,
IsDefault: true,
Config: ExtensionConfig{},
},
{
ID: ExtensionHighlightTrailingWhitespace,
Enabled: true,
IsDefault: true,
Config: ExtensionConfig{},
},
// UI增强扩展
{
@@ -124,6 +130,18 @@ func NewDefaultExtensions() []Extension {
"autohide": false,
},
},
{
ID: ExtensionLineNumbers,
Enabled: true,
IsDefault: true,
Config: ExtensionConfig{},
},
{
ID: ExtensionContextMenu,
Enabled: true,
IsDefault: true,
Config: ExtensionConfig{},
},
// 工具扩展
{

View File

@@ -16,13 +16,8 @@ type KeyBindingCommand string
const (
// 搜索扩展相关
ShowSearchCommand KeyBindingCommand = "showSearch" // 显示搜索
HideSearchCommand KeyBindingCommand = "hideSearch" // 隐藏搜索
SearchToggleCaseCommand KeyBindingCommand = "searchToggleCase" // 搜索切换大小写
SearchToggleWordCommand KeyBindingCommand = "searchToggleWord" // 搜索切换整词
SearchToggleRegexCommand KeyBindingCommand = "searchToggleRegex" // 搜索切换正则
SearchShowReplaceCommand KeyBindingCommand = "searchShowReplace" // 显示替换
SearchReplaceAllCommand KeyBindingCommand = "searchReplaceAll" // 替换全部
ShowSearchCommand KeyBindingCommand = "showSearch" // 显示搜索
HideSearchCommand KeyBindingCommand = "hideSearch" // 隐藏搜索
// 代码块扩展相关
BlockSelectAllCommand KeyBindingCommand = "blockSelectAll" // 块内选择全部
@@ -78,9 +73,6 @@ const (
HistoryRedoCommand KeyBindingCommand = "historyRedo" // 重做
HistoryUndoSelectionCommand KeyBindingCommand = "historyUndoSelection" // 撤销选择
HistoryRedoSelectionCommand KeyBindingCommand = "historyRedoSelection" // 重做选择
// 文本高亮扩展相关
TextHighlightToggleCommand KeyBindingCommand = "textHighlightToggle" // 切换文本高亮
)
// KeyBindingMetadata 快捷键配置元数据
@@ -124,41 +116,6 @@ func NewDefaultKeyBindings() []KeyBinding {
Enabled: true,
IsDefault: true,
},
{
Command: SearchToggleCaseCommand,
Extension: ExtensionSearch,
Key: "Alt-c",
Enabled: true,
IsDefault: true,
},
{
Command: SearchToggleWordCommand,
Extension: ExtensionSearch,
Key: "Alt-w",
Enabled: true,
IsDefault: true,
},
{
Command: SearchToggleRegexCommand,
Extension: ExtensionSearch,
Key: "Alt-r",
Enabled: true,
IsDefault: true,
},
{
Command: SearchShowReplaceCommand,
Extension: ExtensionSearch,
Key: "Mod-h",
Enabled: true,
IsDefault: true,
},
{
Command: SearchReplaceAllCommand,
Extension: ExtensionSearch,
Key: "Mod-Alt-Enter",
Enabled: true,
IsDefault: true,
},
// 代码块核心功能快捷键
{
@@ -496,15 +453,6 @@ func NewDefaultKeyBindings() []KeyBinding {
Enabled: true,
IsDefault: true,
},
// 文本高亮扩展快捷键
{
Command: TextHighlightToggleCommand,
Extension: ExtensionTextHighlight,
Key: "Mod-Shift-h",
Enabled: true,
IsDefault: true,
},
}
}

View File

@@ -121,6 +121,12 @@ func (es *ExtensionService) initDatabase() error {
es.logger.Error("Failed to insert default extensions", "error", err)
return err
}
} else {
// 检查并补充缺失的扩展
if err := es.syncExtensions(); err != nil {
es.logger.Error("Failed to ensure all extensions exist", "error", err)
return err
}
}
return nil
@@ -153,6 +159,80 @@ func (es *ExtensionService) insertDefaultExtensions() error {
return nil
}
// syncExtensions 确保数据库中的扩展与代码定义同步
func (es *ExtensionService) syncExtensions() error {
defaultSettings := models.NewDefaultExtensionSettings()
now := time.Now().Format("2006-01-02 15:04:05")
// 构建代码中定义的扩展ID集合
definedExtensions := make(map[string]bool)
for _, ext := range defaultSettings.Extensions {
definedExtensions[string(ext.ID)] = true
}
// 1. 添加缺失的扩展
for _, ext := range defaultSettings.Extensions {
var exists int
err := es.databaseService.db.QueryRow("SELECT COUNT(*) FROM extensions WHERE id = ?", string(ext.ID)).Scan(&exists)
if err != nil {
return &ExtensionError{"check_extension_exists", string(ext.ID), err}
}
if exists == 0 {
configJSON, err := json.Marshal(ext.Config)
if err != nil {
return &ExtensionError{"marshal_config", string(ext.ID), err}
}
_, err = es.databaseService.db.Exec(sqlInsertExtension,
string(ext.ID),
ext.Enabled,
ext.IsDefault,
string(configJSON),
now,
now,
)
if err != nil {
return &ExtensionError{"insert_missing_extension", string(ext.ID), err}
}
es.logger.Info("Added missing extension to database", "id", ext.ID)
}
}
// 2. 删除数据库中已不存在于代码定义的扩展
rows, err := es.databaseService.db.Query("SELECT id FROM extensions")
if err != nil {
return &ExtensionError{"query_all_extension_ids", "", err}
}
defer rows.Close()
var toDelete []string
for rows.Next() {
var id string
if err := rows.Scan(&id); err != nil {
return &ExtensionError{"scan_extension_id", "", err}
}
if !definedExtensions[id] {
toDelete = append(toDelete, id)
}
}
if err = rows.Err(); err != nil {
return &ExtensionError{"iterate_extension_ids", "", err}
}
// 删除不再定义的扩展
for _, id := range toDelete {
_, err := es.databaseService.db.Exec("DELETE FROM extensions WHERE id = ?", id)
if err != nil {
return &ExtensionError{"delete_obsolete_extension", id, err}
}
es.logger.Info("Removed obsolete extension from database", "id", id)
}
return nil
}
// ServiceStartup 启动时调用
func (es *ExtensionService) ServiceStartup(ctx context.Context, options application.ServiceOptions) error {
es.ctx = ctx