Skip to content

feat: add Swift support via tree-sitter-swift - #8

Open
Kevinrob wants to merge 3 commits into
ThinkyMiner:mainfrom
qoqa:feat/swift-support
Open

Kevinrob wants to merge 3 commits into
ThinkyMiner:mainfrom
qoqa:feat/swift-support

Conversation

@Kevinrob

@Kevinrob Kevinrob commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds Swift (.swift) support, following the same model as the Kotlin plugin: a SwiftPlugin built on tree-sitter-swift (0.7.x, wheels for macOS arm64/x86_64, Linux and Windows).

In this grammar class, struct, enum, extension and actor are all class_declaration, told apart by declaration_kind; functions have no parameter-list node, so params are sliced between the declaration's own parens.

Changes

  • src/codetree/languages/swift.py — new SwiftPlugin:
    • Skeleton: class, actor (new kind actor), struct, enum + its cases (new kind case, with associated values as params), protocol (interface), typealias (type), func, init/deinit, subscript, computed properties (e.g. SwiftUI var body: some View). Nested types get the right parent. Like Rust impl Foo, an extension is not a symbol itself: its members keep the extended type as parent (last component for extension Outer.Inner), so references, call graph and blast radius attach them to the same type. Params keep external labels: (from url: URL, completion: @escaping (Result<Data, Error>) -> Void).
    • Docs: only /// and /** */ count, so // MARK: - comments are not reported as docstrings.
    • get_symbol: full source including attributes and modifiers (@MainActor, @available, private static, override…); a type's own declaration is preferred over its extensions.
    • Calls: functions, methods, self.foo(), foo?.bar(), Foo(...), Foo<T>(), trailing closures. init is not reported as a callee (super.init() / .init() would resolve to every init in the repo).
    • Complexity: if, guard, switch cases, for, while, repeat, catch, &&, ||, ??, ternary.
    • Variables: parameters, let/var (incl. tuple destructuring), if/guard/while let, loop vars, closure parameters, case/catch bindings.
    • Imports: import Foo, @testable import Foo, import struct Swift.Array.
    • Tests: test*() methods in classes inheriting a *Test* base (XCTestCase or a custom base class) and @Test functions (Swift Testing) carry an is_test flag.
  • src/codetree/server.py — the skeleton formatter renders the actor kind like the other type kinds (actor Worker → line 1, compact act Worker:1), and shows the parent of nested types the same way as for methods ( type ViewModel (in CardView) → line 3). Only Swift emits nested types with a parent, so other languages' output is unchanged.
  • src/codetree/indexer.py — find_tests honours the plugin-set is_test flag (one line), so XCTest testFoo() methods and @Test functions with free-form names are found.
  • src/codetree/registry.py, pyproject.toml — register .swift, add tree-sitter-swift>=0.7.0.
  • README.md, AGENTS.md, docs/LANDING_PAGE.md, .github/ISSUE_TEMPLATE/bug_report.yml — list Swift.
  • tests/languages/test_swift.py, tests/languages/test_swift_comprehensive.py — 45 tests with fixed fixtures (protocol + extension, generics with where, async/await/throws, @Published, SwiftUI view, enums with associated values, nested types, XCTest and Swift Testing through Indexer.find_tests, graph build with duplicate names).

Test plan

  • Ran pytest — all tests pass (1253 passed)
  • Added new tests for changed behavior — regressions proven red/green by breaking the implementation (guard in complexity, extensions not reported as a second type, actor and nested-type parents in the formatter, is_test in find_tests, MARK docs)
  • Tested on a real ~2 600-file Swift/SwiftUI repository: cold indexing ~3 s; every func declaration except local nested functions is in the skeleton; protocol/enum/extension counts match a grep; find_tests returns the Swift Testing tests of a view model. ~3% of files report syntax errors, almost all @available(...) on a #Preview macro, which the grammar does not parse — skeletons of those files stay complete.

Known limitations (not addressed here)

  • Call edges are resolved by name. Swift imports modules, not files, so almost no call is import-confirmed, and common names (load, body, a static object on every mock type) fan out to every definition — on that repository the graph build dominates startup and the graph is large. This is builder-level and affects every language; worth a separate PR (e.g. cap name-only matches).
  • No file-level IMPORTS edges for Swift, so get_dependency_graph is empty.
  • Operator functions (static func ==) are in the skeleton but get no references.

Related issues

None.

🤖 Generated with Claude Code

Add SwiftPlugin for .swift files, built on tree-sitter-swift (0.7.x,
alex-pinkus grammar). In this grammar class, struct, enum, extension and
actor are all class_declaration, told apart by declaration_kind.

- Skeleton: class/actor, struct, enum and its cases, protocol
  (interface), extension (named after the extended type, last component
  for Outer.Inner), typealias, func, init/deinit, subscript and computed
  properties (SwiftUI `var body: some View`), with the right parent for
  nested types. Params keep external labels. Doc comments come from ///
  and /** */ only, so `// MARK: -` is not reported as a docstring.
- get_symbol includes attributes and modifiers (@mainactor, private,
  static, override...).
- Calls: functions, methods, self.foo(), foo?.bar(), Foo(...),
  Foo<T>(), trailing closures. init is not reported as a callee, since
  super.init()/.init() would resolve to every init in the repo.
- Complexity: if, guard, switch cases, for, while, repeat, catch,
  &&, ||, ??, ternary.
- Variables: parameters, let/var (including tuples), if/guard/while
  let, for, closure parameters, case and catch bindings.
- Imports: import Foo, @testable import Foo.
- Tests: test*() methods of classes inheriting a *Test* base (XCTestCase
  or a custom one) and @test functions (Swift Testing) carry an is_test
  flag. Indexer.find_tests honours that flag, so tests that do not follow
  the test_*/Test* naming convention are found.

On a ~2 600-file Swift repository, indexing takes about 3 s, every
func declaration except local nested functions is in the skeleton, and
about 3% of files report syntax errors (mostly @available on #Preview,
which the grammar does not parse; skeletons stay complete).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Kevinrob
Kevinrob marked this pull request as ready for review November 18, 2026 10:34
Kevinrob and others added 2 commits November 18, 2026 12:47
…tor kind

An extension was reported as a second `class` with the extended type's
name. Like Rust `impl Foo`, it is no longer a skeleton item: its members
keep the extended type as parent, so references, call graph and blast
radius attach them to the same type, and the graph no longer merges a
duplicate symbol.

Actors are reported with a new `actor` kind, which the skeleton
formatter renders like the other type kinds (`actor Worker → line 1`,
compact `act Worker:1`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The full skeleton format printed type kinds (class, struct, enum, type,
actor...) without indentation or "(in Parent)", so a Swift typealias or
nested type declared in a type or an extension looked file-level. Type
kinds now use the same parent prefix as methods:

  struct CardView → line 2
    type ViewModel (in CardView) → line 3

Only Swift emits nested types with a parent today, so other languages'
output is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant