Class: Aidp::Analyze::TreeSitterGrammarLoader

Inherits:
Object
  • Object
show all
Includes:
MessageDisplay
Defined in:
lib/aidp/analyze/tree_sitter_grammar_loader.rb

Constant Summary collapse

GRAMMAR_CONFIGS =

Default grammar configurations

{
  "ruby" => {
    name: "tree-sitter-ruby",
    version: "0.20.0",
    source: "https://github.com/tree-sitter/tree-sitter-ruby",
    file_patterns: ["**/*.rb"],
    node_types: {
      class: "class",
      module: "module",
      method: "method",
      call: "call",
      require: "call",
      require_relative: "call"
    }
  },
  "javascript" => {
    name: "tree-sitter-javascript",
    version: "0.20.0",
    source: "https://github.com/tree-sitter/tree-sitter-javascript",
    file_patterns: ["**/*.js", "**/*.jsx"],
    node_types: {
      class: "class_declaration",
      function: "function_declaration",
      call: "call_expression",
      import: "import_statement"
    }
  },
  "typescript" => {
    name: "tree-sitter-typescript",
    version: "0.20.0",
    source: "https://github.com/tree-sitter/tree-sitter-typescript",
    file_patterns: ["**/*.ts", "**/*.tsx"],
    node_types: {
      class: "class_declaration",
      function: "function_declaration",
      call: "call_expression",
      import: "import_statement"
    }
  },
  "python" => {
    name: "tree-sitter-python",
    version: "0.20.0",
    source: "https://github.com/tree-sitter/tree-sitter-python",
    file_patterns: ["**/*.py"],
    node_types: {
      class: "class_definition",
      function: "function_definition",
      call: "call",
      import: "import_statement"
    }
  }
}.freeze

Constants included from MessageDisplay

MessageDisplay::COLOR_MAP

Instance Method Summary collapse

Methods included from MessageDisplay

#display_message, #in_test_environment?, included, #message_display_prompt, #suppress_display_message?

Constructor Details

#initialize(project_dir = Dir.pwd, prompt: TTY::Prompt.new) ⇒ TreeSitterGrammarLoader

Returns a new instance of TreeSitterGrammarLoader.



67
68
69
70
71
72
# File 'lib/aidp/analyze/tree_sitter_grammar_loader.rb', line 67

def initialize(project_dir = Dir.pwd, prompt: TTY::Prompt.new)
  @project_dir = project_dir
  @grammars_dir = File.join(project_dir, ".aidp", "grammars")
  @loaded_grammars = {}
  @prompt = prompt
end

Instance Method Details

#file_patterns_for_language(language) ⇒ Object

Get file patterns for a language



86
87
88
89
90
91
# File 'lib/aidp/analyze/tree_sitter_grammar_loader.rb', line 86

def file_patterns_for_language(language)
  config = GRAMMAR_CONFIGS[language]
  return [] unless config

  config[:file_patterns]
end

#load_grammar(language) ⇒ Object

Load grammar for a specific language



75
76
77
78
79
80
81
82
83
# File 'lib/aidp/analyze/tree_sitter_grammar_loader.rb', line 75

def load_grammar(language)
  return @loaded_grammars[language] if @loaded_grammars[language]

  config = GRAMMAR_CONFIGS[language]
  raise "Unsupported language: #{language}" unless config

  ensure_grammar_available(language, config)
  @loaded_grammars[language] = create_parser(language, config)
end