Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: svelte support #60

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
28B3F05D290C3709000CD04D /* TreeSitterSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 28B3F05C290C3709000CD04D /* TreeSitterSwift */; };
28B3F063290C372D000CD04D /* TreeSitterZig in Frameworks */ = {isa = PBXBuildFile; productRef = 28B3F062290C372D000CD04D /* TreeSitterZig */; };
28B9F7AA290DDAC900245748 /* TreeSitterBash in Frameworks */ = {isa = PBXBuildFile; productRef = 28B9F7A9290DDAC900245748 /* TreeSitterBash */; };
5BDD4DB82ACA198D00DAF047 /* TreeSitterSvelte in Frameworks */ = {isa = PBXBuildFile; productRef = 5BDD4DB72ACA198D00DAF047 /* TreeSitterSvelte */; };
6CEC70FE29C3A85000B61C7A /* TreeSitterRegex in Frameworks */ = {isa = PBXBuildFile; productRef = 6CEC70FD29C3A85000B61C7A /* TreeSitterRegex */; };
8E74B41D2A4C88ED003A9550 /* TreeSitterHTML in Frameworks */ = {isa = PBXBuildFile; productRef = 8E74B41C2A4C88ED003A9550 /* TreeSitterHTML */; };
8E7B0EA92A5D9BB60040DDAB /* TreeSitterTOML in Frameworks */ = {isa = PBXBuildFile; productRef = 8E7B0EA82A5D9BB60040DDAB /* TreeSitterTOML */; };
Expand Down Expand Up @@ -78,6 +79,7 @@
28B3F030290C35F9000CD04D /* TreeSitterCPP in Frameworks */,
28B3F04E290C3698000CD04D /* TreeSitterJSON in Frameworks */,
28B3F036290C361D000CD04D /* TreeSitterCSS in Frameworks */,
5BDD4DB82ACA198D00DAF047 /* TreeSitterSvelte in Frameworks */,
28B3F033290C3608000CD04D /* TreeSitterCSharp in Frameworks */,
28B3F03F290C364D000CD04D /* TreeSitterGoMod in Frameworks */,
28B3F063290C372D000CD04D /* TreeSitterZig in Frameworks */,
Expand All @@ -90,7 +92,6 @@
28B3F048290C367C000CD04D /* TreeSitterJava in Frameworks */,
6CEC70FE29C3A85000B61C7A /* TreeSitterRegex in Frameworks */,
282E5977298051980064B34A /* TreeSitterYAML in Frameworks */,
6CA62EA929F9D36700785B11 /* TreeSitterTS in Frameworks */,
9D7399242A5A245000CEF6E8 /* TreeSitterAgda in Frameworks */,
8E7B8ABD2A5D916F00CFC1A7 /* TreeSitterPython in Frameworks */,
2886C788298135540023E016 /* TreeSitterKotlin in Frameworks */,
Expand Down Expand Up @@ -209,6 +210,7 @@
8E7B8ABC2A5D916F00CFC1A7 /* TreeSitterPython */,
8E7B0EA82A5D9BB60040DDAB /* TreeSitterTOML */,
9D920B972A93B248004E4451 /* TreeSitterJSDoc */,
5BDD4DB72ACA198D00DAF047 /* TreeSitterSvelte */,
);
productName = "CodeLanguages-Container";
productReference = 28B3F00C290C207D000CD04D /* CodeLanguages_Container.framework */;
Expand Down Expand Up @@ -276,6 +278,7 @@
8E7B8ABB2A5D916F00CFC1A7 /* XCRemoteSwiftPackageReference "tree-sitter-python" */,
8E7B0EA72A5D9BB60040DDAB /* XCRemoteSwiftPackageReference "tree-sitter-toml" */,
9D920B962A93B248004E4451 /* XCRemoteSwiftPackageReference "tree-sitter-jsdoc" */,
5BDD4DB62ACA198D00DAF047 /* XCRemoteSwiftPackageReference "tree-sitter-svelte" */,
);
productRefGroup = 28B3F00D290C207D000CD04D /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -709,6 +712,14 @@
kind = branch;
};
};
5BDD4DB62ACA198D00DAF047 /* XCRemoteSwiftPackageReference "tree-sitter-svelte" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/EstebanBorai/tree-sitter-svelte.git";
requirement = {
kind = revision;
revision = c50a98f46beb2909db4f66f43570cceff3ef60eb;
};
};
6CEC70FC29C3A85000B61C7A /* XCRemoteSwiftPackageReference "tree-sitter-regex" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/thecoolwinter/tree-sitter-regex/";
Expand Down Expand Up @@ -939,6 +950,11 @@
package = 28B9F7A6290DDAB500245748 /* XCRemoteSwiftPackageReference "tree-sitter-bash" */;
productName = TreeSitterBash;
};
5BDD4DB72ACA198D00DAF047 /* TreeSitterSvelte */ = {
isa = XCSwiftPackageProductDependency;
package = 5BDD4DB62ACA198D00DAF047 /* XCRemoteSwiftPackageReference "tree-sitter-svelte" */;
productName = TreeSitterSvelte;
};
6CEC70FD29C3A85000B61C7A /* TreeSitterRegex */ = {
isa = XCSwiftPackageProductDependency;
package = 6CEC70FC29C3A85000B61C7A /* XCRemoteSwiftPackageReference "tree-sitter-regex" */;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"location" : "https://github.com/tree-sitter/tree-sitter-bash.git",
"state" : {
"branch" : "master",
"revision" : "422a07cb221b92c6b117e854efa8945a506b5214"
"revision" : "fd4e40dab883d6456da4d847de8321aee9c80805"
}
},
{
Expand Down Expand Up @@ -288,6 +288,14 @@
"revision" : "e08036ee4928b32fbebe55ac9336f81b7389e107"
}
},
{
"identity" : "tree-sitter-svelte",
"kind" : "remoteSourceControl",
"location" : "https://github.com/EstebanBorai/tree-sitter-svelte.git",
"state" : {
"revision" : "c50a98f46beb2909db4f66f43570cceff3ef60eb"
}
},
{
"identity" : "tree-sitter-swift",
"kind" : "remoteSourceControl",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ extern TSLanguage *tree_sitter_ruby();
extern TSLanguage *tree_sitter_rust();
extern TSLanguage *tree_sitter_scala();
extern TSLanguage *tree_sitter_sql();
extern TSLanguage *tree_sitter_svelte();
extern TSLanguage *tree_sitter_swift();
extern TSLanguage *tree_sitter_toml();
extern TSLanguage *tree_sitter_tsx();
Expand Down
Binary file modified CodeLanguagesContainer.xcframework.zip
Binary file not shown.
8 changes: 8 additions & 0 deletions Sources/CodeEditLanguages/CodeLanguage+Definitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public extension CodeLanguage {
.rust,
.scala,
.sql,
.svelte,
.swift,
.toml,
.tsx,
Expand Down Expand Up @@ -305,6 +306,13 @@ public extension CodeLanguage {
extensions: ["sql"]
)

/// A language structure for `Svelte`
static let svelte: CodeLanguage = .init(
id: .svelte,
tsName: "svelte",
extensions: ["svelte"]
)

/// A language structure for `Swift`
static let swift: CodeLanguage = .init(
id: .swift,
Expand Down
2 changes: 2 additions & 0 deletions Sources/CodeEditLanguages/CodeLanguage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ public struct CodeLanguage {
return tree_sitter_scala()
case .sql:
return tree_sitter_sql()
case .svelte:
return tree_sitter_svelte()
case .swift:
return tree_sitter_swift()
case .toml:
Expand Down
2 changes: 2 additions & 0 deletions Sources/CodeEditLanguages/Documentation.docc/Add-Languages.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ private var tsLanguage: UnsafeMutablePointer<TSLanguage>? {
}
```

### CodeLanguage+Definitions.swift

On the bottom of the file add a new `static` constant:

```swift
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
(style_element)
(script_element)
(element)
(if_statement)
(else_statement)
(each_statement)
(await_statement)
] @fold
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
; Special identifiers
;--------------------

; TODO:
((element (start_tag (tag_name) @_tag) (text) @text.title)
(#match? @_tag "^(h[0-9]|title)$"))

((element (start_tag (tag_name) @_tag) (text) @text.strong)
(#match? @_tag "^(strong|b)$"))

((element (start_tag (tag_name) @_tag) (text) @text.emphasis)
(#match? @_tag "^(em|i)$"))

((element (start_tag (tag_name) @_tag) (text) @text.strike)
(#match? @_tag "^(s|del)$"))

((element (start_tag (tag_name) @_tag) (text) @text.underline)
(#eq? @_tag "u"))

((element (start_tag (tag_name) @_tag) (text) @text.literal)
(#match? @_tag "^(code|kbd)$"))

((element (start_tag (tag_name) @_tag) (text) @text.uri)
(#eq? @_tag "a"))

((attribute
(attribute_name) @_attr
(quoted_attribute_value (attribute_value) @text.uri))
(#match? @_attr "^(href|src)$"))

(tag_name) @tag
(attribute_name) @property
(erroneous_end_tag_name) @error
(comment) @comment

[
(attribute_value)
(quoted_attribute_value)
] @string

[
(text)
(raw_text_expr)
] @none

[
(special_block_keyword)
(then)
(as)
] @keyword

[
"{"
"}"
] @punctuation.bracket

"=" @operator

[
"<"
">"
"</"
"/>"
"#"
":"
"/"
"@"
] @tag.delimiter
20 changes: 20 additions & 0 deletions Sources/CodeEditLanguages/Resources/tree-sitter-svelte/indents.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
(element)
(if_statement)
(each_statement)
(await_statement)
(script_element)
(style_element)
] @indent

[
(end_tag)
(else_statement)
(if_end_expr)
(each_end_expr)
(await_end_expr)
">"
"/>"
] @branch

(comment) @ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
; injections.scm
; --------------
((style_element
(raw_text) @css))

(
(style_element
(start_tag
(attribute
(quoted_attribute_value (attribute_value))))
(raw_text) @css)
)

((attribute
(attribute_name) @_attr
(quoted_attribute_value (attribute_value) @css))
(#eq? @_attr "style"))

((script_element
(raw_text) @javascript))

((raw_text_expr) @javascript)

(
(script_element
(start_tag
(attribute
(quoted_attribute_value (attribute_value) @_lang)))
(raw_text) @typescript)
(#match? @_lang "(ts|typescript)")
)

(comment) @comment
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
;; locals
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
;; tags
1 change: 1 addition & 0 deletions Sources/CodeEditLanguages/TreeSitterLanguage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public enum TreeSitterLanguage: String {
case rust
case scala
case sql
case svelte
case swift
case toml
case tsx
Expand Down
7 changes: 7 additions & 0 deletions Sources/CodeEditLanguages/TreeSitterModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public class TreeSitterModel {
return scalaQuery
case .sql:
return sqlQuery
case .svelte:
return svelteQuery
case .swift:
return swiftQuery
case .toml:
Expand Down Expand Up @@ -262,6 +264,11 @@ public class TreeSitterModel {
return queryFor(.sql)
}()

/// Query for `Svelte` files.
public private(set) lazy var svelteQuery: Query? = {
return queryFor(.svelte)
}()

/// Query for `Swift` files.
public private(set) lazy var swiftQuery: Query? = {
return queryFor(.swift)
Expand Down
19 changes: 19 additions & 0 deletions Tests/CodeEditLanguagesTests/CodeEditLanguagesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,25 @@ final class CodeEditLanguagesTests: XCTestCase {
XCTAssertNotNil(query)
XCTAssertNotEqual(query?.patternCount, 0)
}

// MARK: - Svelte

func test_CodeLanguageSvelte() throws {
let url = URL(fileURLWithPath: "~/path/to/file.svelte")
let language = CodeLanguage.detectLanguageFrom(url: url)

XCTAssertEqual(language.id, .svelte)
}

func test_FetchQuerySvelte() throws {
var language = CodeLanguage.svelte
language.resourceURL = bundleURL

let data = try Data(contentsOf: language.queryURL!)
let query = try? Query(language: language.language!, data: data)
XCTAssertNotNil(query)
XCTAssertNotEqual(query?.patternCount, 0)
}

// MARK: - Swift

Expand Down