Skip to content

Commit

Permalink
repl: Add ability to evaluate Markdown code blocks (#15100)
Browse files Browse the repository at this point in the history
This adds the ability to evaluate TypeScript and Python code blocks in
Markdown files.

cc @rgbkrk 

Demo:


https://github.com/user-attachments/assets/55352de5-68f3-4aef-920a-78ca205651ba



Release Notes:

- N/A

---------

Co-authored-by: Nathan <[email protected]>
Co-authored-by: Antonio <[email protected]>
  • Loading branch information
3 people authored Jul 25, 2024
1 parent b56e4ff commit e2113e4
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 30 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions crates/language/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3058,6 +3058,43 @@ impl BufferSnapshot {
})
}

pub fn injections_intersecting_range<T: ToOffset>(
&self,
range: Range<T>,
) -> impl Iterator<Item = (Range<usize>, &Arc<Language>)> + '_ {
let offset_range = range.start.to_offset(self)..range.end.to_offset(self);

let mut syntax_matches = self.syntax.matches(offset_range, self, |grammar| {
grammar
.injection_config
.as_ref()
.map(|config| &config.query)
});

let configs = syntax_matches
.grammars()
.iter()
.map(|grammar| grammar.injection_config.as_ref())
.collect::<Vec<_>>();

iter::from_fn(move || {
let ranges = syntax_matches.peek().and_then(|mat| {
let config = &configs[mat.grammar_index]?;
let content_capture_range = mat.captures.iter().find_map(|capture| {
if capture.index == config.content_capture_ix {
Some(capture.node.byte_range())
} else {
None
}
})?;
let language = self.language_at(content_capture_range.start)?;
Some((content_capture_range, language))
});
syntax_matches.advance();
ranges
})
}

pub fn runnable_ranges(
&self,
range: Range<Anchor>,
Expand Down
4 changes: 4 additions & 0 deletions crates/repl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ gpui = { workspace = true, features = ["test-support"] }
http_client = { workspace = true, features = ["test-support"] }
indoc.workspace = true
language = { workspace = true, features = ["test-support"] }
languages = { workspace = true, features = ["test-support"] }
project = { workspace = true, features = ["test-support"] }
settings = { workspace = true, features = ["test-support"] }
theme = { workspace = true, features = ["test-support"] }
tree-sitter-md.workspace = true
tree-sitter-typescript.workspace = true
tree-sitter-python.workspace = true
util = { workspace = true, features = ["test-support"] }
Loading

0 comments on commit e2113e4

Please sign in to comment.