Skip to content

Commit

Permalink
Add XML support
Browse files Browse the repository at this point in the history
Fixes #10
  • Loading branch information
Wilfred committed Sep 9, 2023
1 parent 40e303e commit 9134593
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 0.52 (unreleased)

### Parsing

Added support for XML.

### Display

Improved syntax highlighting of constructors (i.e. type names when
Expand Down
5 changes: 5 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,11 @@ fn main() {
src_dir: "vendored_parsers/tree-sitter-typescript-src/typescript/src",
extra_files: vec!["scanner.c"],
},
TreeSitterParser {
name: "tree-sitter-xml",
src_dir: "vendored_parsers/tree-sitter-xml-src",
extra_files: vec!["scanner.c"],
},
TreeSitterParser {
name: "tree-sitter-yaml",
src_dir: "vendored_parsers/tree-sitter-yaml-src",
Expand Down
16 changes: 16 additions & 0 deletions src/parse/guess_language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub enum Language {
Toml,
TypeScript,
TypeScriptTsx,
Xml,
Yaml,
Zig,
}
Expand Down Expand Up @@ -153,6 +154,7 @@ pub fn language_name(language: Language) -> &'static str {
Toml => "TOML",
TypeScript => "TypeScript",
TypeScriptTsx => "TypeScript TSX",
Xml => "XML",
Yaml => "YAML",
Zig => "Zig",
}
Expand Down Expand Up @@ -340,6 +342,19 @@ pub fn language_globs(language: Language) -> Vec<glob::Pattern> {
],
TypeScript => &["*.ts"],
TypeScriptTsx => &["*.tsx"],
Xml => &[
"*.ant",
"*.csproj",
"*.plist",
"*.resx",
"*.svg",
"*.xml",
"App.config",
"nuget.config",
".classpath",
".cproject",
".project",
],
Yaml => &["*.yaml", "*.yml"],
Zig => &["*.zig"],
};
Expand Down Expand Up @@ -438,6 +453,7 @@ fn from_emacs_mode_header(src: &str) -> Option<Language> {
"java" => Some(Java),
"js" | "js2" => Some(JavaScript),
"lisp" => Some(CommonLisp),
"nxml" => Some(Xml),
"perl" => Some(Perl),
"python" => Some(Python),
"racket" => Some(Racket),
Expand Down
18 changes: 18 additions & 0 deletions src/parse/tree_sitter_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ extern "C" {
fn tree_sitter_toml() -> ts::Language;
fn tree_sitter_tsx() -> ts::Language;
fn tree_sitter_typescript() -> ts::Language;
fn tree_sitter_xml() -> ts::Language;
fn tree_sitter_yaml() -> ts::Language;
fn tree_sitter_zig() -> ts::Language;
}
Expand Down Expand Up @@ -997,6 +998,23 @@ pub fn from_language(language: guess::Language) -> TreeSitterConfig {
sub_languages: vec![],
}
}
Xml => {
let language = unsafe { tree_sitter_xml() };
TreeSitterConfig {
language,
// XMLDecl is the <?xml ...?> header, but the parser
// just treats it as a sequence of tokens rather than
// e.g. string subexpressions, so flatten.
atom_nodes: vec!["AttValue", "XMLDecl"].into_iter().collect(),
delimiter_tokens: (vec![("<", ">")]),
highlight_query: ts::Query::new(
language,
include_str!("../../vendored_parsers/highlights/xml.scm"),
)
.unwrap(),
sub_languages: vec![],
}
}
Yaml => {
let language = unsafe { tree_sitter_yaml() };
TreeSitterConfig {
Expand Down
1 change: 1 addition & 0 deletions vendored_parsers/highlights/xml.scm
1 change: 1 addition & 0 deletions vendored_parsers/tree-sitter-xml-src

0 comments on commit 9134593

Please sign in to comment.