@@ -1,4 +1,10 @@
1−use crate::{chars::char_is_line_ending, regex::Regex, Change, Rope, RopeSlice, Transaction};
1+use crate::{
2+ chars::char_is_line_ending,
3+ regex::Regex,
4+ transaction::{ChangeSet, Operation},
5+ Rope, RopeSlice, Tendril,
6+};
7+
28pub use helix_syntax::{get_language, get_language_name, Lang};
39
410use arc_swap::ArcSwap;
@@ -8,7 +14,7 @@ use std::{
814 cell::RefCell,
915 collections::{HashMap, HashSet},
1016 fmt,
11− path::{Path, PathBuf},
17+ path::Path,
1218 sync::Arc,
1319};
1420
@@ -473,12 +479,6 @@ pub struct LanguageLayer {
473479 pub(crate) tree: Option<Tree>,
474480}
475481
476−use crate::{
477− coords_at_pos,
478− transaction::{ChangeSet, Operation},
479− Tendril,
480−};
481−
482482impl LanguageLayer {
483483 // pub fn new() -> Self {
484484 // Self { tree: None }
@@ -1776,135 +1776,141 @@ impl<I: Iterator<Item = HighlightEvent>> Iterator for Merge<I> {
17761776 }
17771777}
17781778
1779−#[test]
1780−fn test_parser() {
1781− let highlight_names: Vec<String> = [
1782− "attribute",
1783− "constant",
1784− "function.builtin",
1785− "function",
1786− "keyword",
1787− "operator",
1788− "property",
1789− "punctuation",
1790− "punctuation.bracket",
1791− "punctuation.delimiter",
1792− "string",
1793− "string.special",
1794− "tag",
1795− "type",
1796− "type.builtin",
1797− "variable",
1798− "variable.builtin",
1799− "variable.parameter",
1800− ]
1801− .iter()
1802− .cloned()
1803− .map(String::from)
1804− .collect();
1805−
1806− let language = get_language(Lang::Rust);
1807− let mut config = HighlightConfiguration::new(
1808− language,
1809− &std::fs::read_to_string(
1810− "../helix-syntax/languages/tree-sitter-rust/queries/highlights.scm",
1811− )
1812− .unwrap(),
1813− &std::fs::read_to_string(
1814− "../helix-syntax/languages/tree-sitter-rust/queries/injections.scm",
1815− )
1816− .unwrap(),
1817− "", // locals.scm
1818− )
1819− .unwrap();
1820− config.configure(&highlight_names);
1821−
1822− let source = Rope::from_str(
1823− "
1824− struct Stuff {}
1825− fn main() {}
1826− ",
1827− );
1828− let syntax = Syntax::new(&source, Arc::new(config));
1829− let tree = syntax.tree();
1830− let root = tree.root_node();
1831− assert_eq!(root.kind(), "source_file");
1832−
1833− assert_eq!(
1834− root.to_sexp(),
1835− concat!(
1836− "(source_file ",
1837− "(struct_item name: (type_identifier) body: (field_declaration_list)) ",
1838− "(function_item name: (identifier) parameters: (parameters) body: (block)))"
1779+#[cfg(test)]
1780+mod test {
1781+ use super::*;
1782+ use crate::{Rope, Transaction};
1783+
1784+ #[test]
1785+ fn test_parser() {
1786+ let highlight_names: Vec<String> = [
1787+ "attribute",
1788+ "constant",
1789+ "function.builtin",
1790+ "function",
1791+ "keyword",
1792+ "operator",
1793+ "property",
1794+ "punctuation",
1795+ "punctuation.bracket",
1796+ "punctuation.delimiter",
1797+ "string",
1798+ "string.special",
1799+ "tag",
1800+ "type",
1801+ "type.builtin",
1802+ "variable",
1803+ "variable.builtin",
1804+ "variable.parameter",
1805+ ]
1806+ .iter()
1807+ .cloned()
1808+ .map(String::from)
1809+ .collect();
1810+
1811+ let language = get_language(Lang::Rust);
1812+ let mut config = HighlightConfiguration::new(
1813+ language,
1814+ &std::fs::read_to_string(
1815+ "../helix-syntax/languages/tree-sitter-rust/queries/highlights.scm",
1816+ )
1817+ .unwrap(),
1818+ &std::fs::read_to_string(
1819+ "../helix-syntax/languages/tree-sitter-rust/queries/injections.scm",
1820+ )
1821+ .unwrap(),
1822+ "", // locals.scm
18391823 )
1840− );
1824+ .unwrap();
1825+ config.configure(&highlight_names);
1826+
1827+ let source = Rope::from_str(
1828+ "
1829+ struct Stuff {}
1830+ fn main() {}
1831+ ",
1832+ );
1833+ let syntax = Syntax::new(&source, Arc::new(config));
1834+ let tree = syntax.tree();
1835+ let root = tree.root_node();
1836+ assert_eq!(root.kind(), "source_file");
1837+
1838+ assert_eq!(
1839+ root.to_sexp(),
1840+ concat!(
1841+ "(source_file ",
1842+ "(struct_item name: (type_identifier) body: (field_declaration_list)) ",
1843+ "(function_item name: (identifier) parameters: (parameters) body: (block)))"
1844+ )
1845+ );
18411846
1842− let struct_node = root.child(0).unwrap();
1843− assert_eq!(struct_node.kind(), "struct_item");
1844−}
1847+ let struct_node = root.child(0).unwrap();
1848+ assert_eq!(struct_node.kind(), "struct_item");
1849+ }
18451850
1846−#[test]
1847−fn test_input_edits() {
1848− use crate::State;
1849− use tree_sitter::InputEdit;
1850−
1851− let mut state = State::new("hello world!\ntest 123".into());
1852− let transaction = Transaction::change(
1853− &state.doc,
1854− vec![(6, 11, Some("test".into())), (12, 17, None)].into_iter(),
1855− );
1856− let edits = LanguageLayer::generate_edits(state.doc.slice(..), transaction.changes());
1857− // transaction.apply(&mut state);
1858−
1859− assert_eq!(
1860− edits,
1861− &[
1862− InputEdit {
1863− start_byte: 6,
1864− old_end_byte: 11,
1865− new_end_byte: 10,
1866− start_position: Point { row: 0, column: 6 },
1867− old_end_position: Point { row: 0, column: 11 },
1868− new_end_position: Point { row: 0, column: 10 }
1869− },
1870− InputEdit {
1871− start_byte: 12,
1872− old_end_byte: 17,
1873− new_end_byte: 12,
1874− start_position: Point { row: 0, column: 12 },
1875− old_end_position: Point { row: 1, column: 4 },
1876− new_end_position: Point { row: 0, column: 12 }
1877− }
1878− ]
1879− );
1880−
1881− // Testing with the official example from tree-sitter
1882− let mut state = State::new("fn test() {}".into());
1883− let transaction =
1884− Transaction::change(&state.doc, vec![(8, 8, Some("a: u32".into()))].into_iter());
1885− let edits = LanguageLayer::generate_edits(state.doc.slice(..), transaction.changes());
1886− transaction.apply(&mut state.doc);
1887−
1888− assert_eq!(state.doc, "fn test(a: u32) {}");
1889− assert_eq!(
1890− edits,
1891− &[InputEdit {
1892− start_byte: 8,
1893− old_end_byte: 8,
1894− new_end_byte: 14,
1895− start_position: Point { row: 0, column: 8 },
1896− old_end_position: Point { row: 0, column: 8 },
1897− new_end_position: Point { row: 0, column: 14 }
1898− }]
1899− );
1900−}
1851+ #[test]
1852+ fn test_input_edits() {
1853+ use crate::State;
1854+ use tree_sitter::InputEdit;
1855+
1856+ let mut state = State::new("hello world!\ntest 123".into());
1857+ let transaction = Transaction::change(
1858+ &state.doc,
1859+ vec![(6, 11, Some("test".into())), (12, 17, None)].into_iter(),
1860+ );
1861+ let edits = LanguageLayer::generate_edits(state.doc.slice(..), transaction.changes());
1862+ // transaction.apply(&mut state);
1863+
1864+ assert_eq!(
1865+ edits,
1866+ &[
1867+ InputEdit {
1868+ start_byte: 6,
1869+ old_end_byte: 11,
1870+ new_end_byte: 10,
1871+ start_position: Point { row: 0, column: 6 },
1872+ old_end_position: Point { row: 0, column: 11 },
1873+ new_end_position: Point { row: 0, column: 10 }
1874+ },
1875+ InputEdit {
1876+ start_byte: 12,
1877+ old_end_byte: 17,
1878+ new_end_byte: 12,
1879+ start_position: Point { row: 0, column: 12 },
1880+ old_end_position: Point { row: 1, column: 4 },
1881+ new_end_position: Point { row: 0, column: 12 }
1882+ }
1883+ ]
1884+ );
1885+
1886+ // Testing with the official example from tree-sitter
1887+ let mut state = State::new("fn test() {}".into());
1888+ let transaction =
1889+ Transaction::change(&state.doc, vec![(8, 8, Some("a: u32".into()))].into_iter());
1890+ let edits = LanguageLayer::generate_edits(state.doc.slice(..), transaction.changes());
1891+ transaction.apply(&mut state.doc);
1892+
1893+ assert_eq!(state.doc, "fn test(a: u32) {}");
1894+ assert_eq!(
1895+ edits,
1896+ &[InputEdit {
1897+ start_byte: 8,
1898+ old_end_byte: 8,
1899+ new_end_byte: 14,
1900+ start_position: Point { row: 0, column: 8 },
1901+ old_end_position: Point { row: 0, column: 8 },
1902+ new_end_position: Point { row: 0, column: 14 }
1903+ }]
1904+ );
1905+ }
19011906
1902−#[test]
1903−fn test_load_runtime_file() {
1904− // Test to make sure we can load some data from the runtime directory.
1905− let contents = load_runtime_file("rust", "indents.toml").unwrap();
1906− assert!(!contents.is_empty());
1907+ #[test]
1908+ fn test_load_runtime_file() {
1909+ // Test to make sure we can load some data from the runtime directory.
1910+ let contents = load_runtime_file("rust", "indents.toml").unwrap();
1911+ assert!(!contents.is_empty());
19071912
1908− let results = load_runtime_file("rust", "does-not-exist");
1909− assert!(results.is_err());
1913+ let results = load_runtime_file("rust", "does-not-exist");
1914+ assert!(results.is_err());
1915+ }
19101916}