Skip to content

Latest commit

 

History

History
171 lines (123 loc) · 5.25 KB

File metadata and controls

171 lines (123 loc) · 5.25 KB

自定义文件类型支持(扩展ripgrep)

问题描述

在使用search_file_content工具(基于ripgrep)搜索某些现代开发文件类型时,会遇到错误:

Error: Ripgrep failed with code 2: unrecognized file type: tsx/vue/svelte/...

根本原因

ripgrep本身只支持有限的内置文件类型(如js, ts, py, md等),不包括许多现代开发生态中的文件类型,如tsx、jsx、vue、svelte、dart等。

解决方案

packages/core/src/tools/grep.tsbuildRipgrepArgs方法中,添加了自定义文件类型到glob模式的转换逻辑,支持100+常见代码文件扩展名

核心实现

if (params.type) {
  // Handle custom file types that ripgrep doesn't recognize natively
  const customTypeToGlob: Record<string, string> = {
    'tsx': '*.tsx',
    'jsx': '*.jsx',
    'vue': '*.vue',
    'svelte': '*.svelte',
    'dart': '*.dart',
    // ... 100+ more types
  };

  if (customTypeToGlob[params.type]) {
    // Convert custom types to glob patterns
    args.push('--glob', customTypeToGlob[params.type]);
  } else {
    // Use native ripgrep type for standard types (js, ts, py, etc.)
    args.push('--type', params.type);
  }
}

实现细节

  1. 自动转换:当用户指定type: 'tsx'type: 'jsx'时,工具会自动将其转换为等效的glob模式(*.tsx*.jsx
  2. 向后兼容:对于ripgrep原生支持的文件类型,仍然使用--type参数以获得最佳性能
  3. 可扩展:如果将来需要支持其他自定义类型,只需在customTypeToGlob映射中添加即可

测试验证

集成测试结果

🔍 Testing TSX file type search...
✅ TSX search successful!
TestComponent.tsx:3:  return <div>Hello TSX sparkles</div>;

🔍 Testing JSX file type search...
✅ JSX search successful!
OldComponent.jsx:3:  return <div>Hello JSX sparkles</div>;

支持的文件类型

🌐 Web 前端开发

  • React: tsx, jsx
  • Vue.js: vue
  • Svelte: svelte
  • Angular: ng
  • 样式: sass, scss, less, styl, stylus
  • 模板: ejs, pug, jade, handlebars, mustache, twig, jinja
  • 文档: mdx

📱 移动开发

  • Flutter/Dart: dart
  • iOS: swift
  • Android: kotlin, kt, ktm, kts

🔧 现代 JavaScript/TypeScript

  • 模块: mjs, cjs, mts, cts

💻 编程语言

  • JVM: groovy, scala, kotlin
  • 函数式: clojure, elixir, erlang, haskell, ocaml, fsharp
  • 系统级: nim, crystal, zig
  • 数据科学: ipynb, rmd, jl

🎮 游戏开发

  • Godot: gdscript
  • 着色器: shader, glsl, hlsl, vert, frag

🔨 Shell & 脚本

  • bash, zsh, fish, powershell, bat

📦 配置 & 数据

  • toml, yaml, yml, ini, env, dotenv

🌍 Web Assembly

  • wasm, wat

🔌 API & 协议

  • GraphQL: graphql, gql
  • Protocol Buffers: proto

🏗️ 基础设施即代码

  • Terraform: terraform
  • Docker: dockerfile, dockerignore
  • CI/CD: gitlab-ci, github-workflow

📄 文档格式

  • tex, rst, adoc, asciidoc

使用示例

// React/TypeScript
{ pattern: 'useState', type: 'tsx', path: 'src/components' }

// Vue.js
{ pattern: 'computed', type: 'vue', path: 'src/views' }

// Flutter/Dart
{ pattern: 'StatefulWidget', type: 'dart' }

// GraphQL
{ pattern: 'query', type: 'graphql' }

// Terraform
{ pattern: 'resource', type: 'terraform' }

// Shell脚本
{ pattern: 'function', type: 'bash' }

// 配置文件
{ pattern: 'database', type: 'yaml' }

相关文件

  • 修复文件packages/core/src/tools/grep.ts
  • 测试文件packages/core/src/tools/grep.test.ts
  • vitest配置修复packages/core/vitest.config.ts(修正了setupFiles路径)

其他改进

在修复过程中,还发现并修复了以下问题:

  1. vitest配置错误packages/core/vitest.config.ts中的setupFiles路径不正确,已修正为../../scripts/tests/test-setup.ts
  2. 测试用例更新:移除了对"文件不能作为搜索路径"的限制测试,因为grep现在支持单文件搜索

影响范围

  • 100+ 常见代码文件扩展名完全支持
  • ✅ 覆盖 Web、移动、数据科学、游戏开发等各领域
  • ✅ 保持对原生ripgrep类型的高性能支持
  • ✅ 可轻松扩展支持其他自定义类型
  • ✅ 向后兼容,不影响现有功能
  • ✅ 修复了vitest配置问题

技术优势

  1. 智能回退:优先使用ripgrep原生类型(性能最佳),仅在需要时转换为glob
  2. 零配置:用户无需关心底层实现,直接使用文件类型名即可
  3. 易扩展:新增文件类型只需在映射表添加一行
  4. 生态覆盖:从传统语言到现代框架,从前端到后端,全面支持

备注

这不是工具的bug,而是ripgrep的设计限制。ripgrep只内置了有限的文件类型支持,但现代开发生态中有大量新兴的文件扩展名(tsx、jsx、vue、svelte、dart等)。

通过智能映射表 + glob模式转换的方式,我们不仅完美解决了这个问题,还一次性支持了100+种常见代码文件类型,极大提升了工具的实用性和开发者体验。🚀