-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: starting to implement pretty json output
- Loading branch information
Showing
8 changed files
with
220 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ pub mod command; | |
pub mod fs; | ||
pub mod net; | ||
pub mod schema; | ||
pub mod syntax; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod highlighter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use tree_sitter::{Parser, Query, QueryCursor}; | ||
|
||
pub struct Highlighter { | ||
parser: Parser, | ||
query: Query, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct ColorInfo { | ||
pub start: usize, | ||
pub end: usize, | ||
} | ||
|
||
impl Default for Highlighter { | ||
fn default() -> Self { | ||
let mut parser = Parser::new(); | ||
let json_language = include_str!("queries/json/highlights.scm"); | ||
let query = Query::new(&tree_sitter_json::language(), json_language) | ||
.expect("failed to load json query"); | ||
|
||
parser | ||
.set_language(&tree_sitter_json::language()) | ||
.expect("error loading json grammar"); | ||
|
||
Highlighter { parser, query } | ||
} | ||
} | ||
|
||
impl Highlighter { | ||
pub fn apply(&mut self, buffer: &str) -> Vec<ColorInfo> { | ||
let tree = self.parser.parse(buffer, None).unwrap(); | ||
|
||
let mut colors = Vec::new(); | ||
let mut cursor = QueryCursor::new(); | ||
let matches = cursor.matches(&self.query, tree.root_node(), buffer.as_bytes()); | ||
|
||
for m in matches { | ||
for cap in m.captures { | ||
let node = cap.node; | ||
let start = node.start_byte(); | ||
let end = node.end_byte(); | ||
let _capture_name = self.query.capture_names()[cap.index as usize]; | ||
colors.push(ColorInfo { start, end }); | ||
} | ||
} | ||
|
||
colors | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
[ | ||
(true) | ||
(false) | ||
] @boolean | ||
|
||
(null) @constant.builtin | ||
|
||
(number) @number | ||
|
||
(pair | ||
key: (string) @property) | ||
|
||
(pair | ||
value: (string) @string) | ||
|
||
(array | ||
(string) @string) | ||
|
||
[ | ||
"," | ||
":" | ||
] @punctuation.delimiter | ||
|
||
[ | ||
"[" | ||
"]" | ||
"{" | ||
"}" | ||
] @punctuation.bracket | ||
|
||
("\"" @conceal | ||
(#set! conceal "")) | ||
|
||
(escape_sequence) @string.escape | ||
|
||
((escape_sequence) @conceal | ||
(#eq? @conceal "\\\"") | ||
(#set! conceal "\"")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters