refactor: improve codebase@@ -5,11 +5,14 @@ require "./extract_symbols.cr"
class GitFOSS::Code::Indexer
VERSION = "0.1.0"
+ LANGUAGE = "c"
+
@parser = TreeSitter::Parser.new
@files = [] of String
@symbols = {} of String => Array(CodeSymbol)
def initialize
+ TreeSitter::Repository.preload_all
end
def files=(values : Array(String))
@@ -29,18 +32,24 @@ class GitFOSS::Code::Indexer
symbols = [] of CodeSymbol
path = Path[file_path]
- symbols = File.open(path) do |f|
+ File.open(path) do |f|
file = RepositoryFile.new(
name: path.basename,
content: f.gets_to_end,
)
- @parser = TreeSitter::Parser.new ("crystal")
- tree = @parser.parse(nil, file.content)
- extract_symbols(
+ @parser = TreeSitter::Parser.new (LANGUAGE)
+
+ io = IO::Memory.new(file.content)
+ tree = @parser.parse(nil, io)
+
+ symbols = extract_symbols(
@parser,
tree.root_node,
file,
)
+
+ pp tree.root_node
+ pp symbols
end
symbols
@@ -49,9 +58,14 @@ end
code_indexer = GitFOSS::Code::Indexer.new
+# pp TreeSitter::Repository.language_names
+
code_indexer.files = [
- "/home/xana/Dev/gitfoss-code-indexer/src/extract_symbols.cr",
- "/home/xana/Dev/gitfoss-code-indexer/shard.yml",
- "/home/xana/Dev/gitfoss/app/islands/RepositoriesList.tsx"
+ # "/home/xana/Dev/gitfoss-code-indexer/src/extract_symbols.cr",
+ # "/home/xana/Dev/gitfoss-code-indexer/shard.yml",
+ # "/home/xana/Dev/gitfoss/app/islands/RepositoriesList.tsx",
+ "/home/xana/Dev/crystal-os/src/kernel/archive/c/main.c"
]
-code_indexer.get_files_symbols
+
+symbols = code_indexer.get_files_symbols
+pp symbols
@@ -214,22 +214,3 @@ def extract_symbols(parser : TreeSitter::Parser, node : TreeSitter::Node, file :
symbols
end
-
-# Example indexer that uses extract_symbols
-class CodeIndexer
- # store multiple symbols per path -> change value type to Array(CodeSymbol)
- property symbols : Hash(String, Array(CodeSymbol)) = {} of String => Array(CodeSymbol)
-
- def initialize
- @symbols = {} of String => Array(CodeSymbol)
- end
-
- def get_symbols(path : String) : Array(CodeSymbol)
- # placeholder: parse file, build tree, call extract_symbols
- file = RepositoryFile.new(path, File.read(path))
- parser = TreeSitter::Parser.new
- # (setup parser with language omitted for brevity)
- root = parser.parse_string(file.content).root_node
- extract_symbols(parser, root, file)
- end
-end